Tuesday, June 23, 2009

8-bit microcontoller programming

I'm once again learning how to do simple embedded programming. I'm using the DEMO9S08AW60E board from Freescale. Unfortunately I'm solving the same problems I ran into last summer I worked on this.

So just in case someone (like me) runs into these problems, I'll blog about them so that the solution can be found via Google.

1. CodeWarrior for Microcontrollers doesn't work with 64-bit Microsoft Vista. It won't even install. Its been that way for several years, no fix coming. Even more annoying, the installer just fails with a cryptic message. Nothing that would indicate that the problem is 64-bit Vista incompatibility.

2. Any time my program tries to run an analog to digital conversion (ADC), the program hangs. I'm not sure why, but I traced the issue to this line in the ADC bean's initialization function:

setReg8(ADC1CFG, 0x7A); /* Set prescaler bits */

Once that runs, I can't do a conversion. The line is auto-generated by the 'processor expert' that comes with CodeWarrior. Every time the processor expert generates the code, I have to comment out this line. I'm not sure yet whatthe 'prescalar bits' are supposed to be set to. I figure when I take the time to understand that, I can code around the issue.

3. Since doing a ADC uses an interrupt, it doesn't work to place the code inside a different interrupt. Measurements have to be done in the main loop.

Now I can blink LEDs and read a 2-axis accelerometer. My not-very-exciting program can change the rate the lights blink depending on how the board is tilted. Next step is to see if the custom printed board a student built last year can be programmed. That board is set up for interfacing robotic servos.

Saturday, June 13, 2009

Python and scope

I think this rule with scope in Python is not intuitive. Take a look at the following program:

1 x = 1
3 def main():
4 print x
6 main()

x is in scope within main(), and the program runs correctly. Now look at this program:

1 x = 1
3 def main():
4 print x
5 x = 1
7 main()

This program fails, it can't print x because the value for x has not been set before printing it in line 4.

Why? x is set in line 1, correct? True. But that x is not in scope.

By setting x=1 in the main function, we get a brand new variable called x, which is only in the main function. There are 2 variables, both named x. One for the main file script, and one for the main function. And since x has not been set in the main function, print x does not work.