Thursday, March 5, 2009

Nerd Kit Week 2

Well we did get the stoplight operational and we learned some things along the way. The final lesson we learned is that the connections on the solderless breadboard are unstable. More on that later. The first step was to verify that the clock interrupt was working correctly. The ATmega168 has a couple of built in timers that essentially operate independent of the controller CPU. It has lots of options and can be used to generate PCM patterns on the controllers output pins to create a clock signal. Very useful, but got in the way of understanding what I wanted to use it for. Eventually, we figured out the configuration to just use it to interrupt our program.
The oscillator runs at 14.7456 MHz. That means it generates a clock tick 14, 745, 600 times per second. The timer can be set up to increment an internal 8 bit counter every clock tick. When the counter wraps around (256 ticks), an interrupt occurs and the controller executes the interrupt routine specified by the timer overflow vector. That would mean 57,600 interrupts per second. That's exactly 57,599 more interrupts per second than we needed. Luckily the control register for the timer provides a setting such that the timing can be divided by some amount. By setting up the timer to only increment its counter every 256 clock ticks, we get our interrupts down to a managable 225 times per second. Using that method and a simple 8 bit integer, I can accurately determine when a second has passed.
The idea behind our timer was that we had a global value that determined how many seconds were left in whatever cycle we were waiting on. The interrupt would simply decrement that value every second until it reached zero. To prove to myself that the timer was functional, I added an LED to one of the output pins on the controller. If the global timer value was odd, I'd set the pin to high, thus lighting the LED. If the global timer value was even, I'd set the pin low, turning off the LED. After discovering one bad bit combination that I had put in the control register, I did indeed get the LED to flash every other second! Now on to the switches.
The switches turned out to be more confusing. We didn't actually have weight sensing switches, but we did have a 7 position dip switch. The NerdKit came with a project by which you could do binary arithmetic by inputting the number through the position of the switches. In their example, they hooked up the dip switch to ground. So, I hooked up my dip switch to ground. Much to my dismay, once I read the value of the pin, the value seemed to disappear. That made the program think that the switch had been flipped off almost as soon as it was flipped on. Eventually, I randomly tried connecting the switch to +5v instead and the value stayed. Another problem solved, but not really understood. When the switch was connected to ground, the program would light the appropriate green LED, but would almost immediately change to yellow and start the timer. At least the cycles were working right! Once the switch was connected to +5v, the program waited until I flipped the switch back before "pretending" that there was no longer traffic on the sensor.
The program basically worked right the first time. That is, I guess it did, because we saw it do the right things in all of the scenarios we through at it. But I can't get it to repeat. One of the things we did was to display what was going on, including the timer value, on the LCD display. Unfortunately, that doesn't seem to work consistently either. And I didn't write the code for that! What I've discovered is that the connections are "brittle" and the program flakes out when the connections aren't quite right. Especially the power connection itself. So that's led me to soldering. I've purchased a printed circuit board (PCB) that's especially designed for the ATmegax8 series. I'm still waiting on parts, but eventually, we are going to wire/solder our traffic example so that the connections are stable. I'll report back when we finish. Once that's done, we are going to create the LED marquee display that got me interested in this in the first place.

No comments: