circuitcellar.com
Magazine Support   Digital Library   Products & Services   Suppliers Directory 
 
 





 

February 2005, Issue 175

A Look at the M16C Lineup


by Jeff Bachiochi

REACTION MEASUREMENT

It’s been proven that alcohol can slow your reaction time. Slowed reflexes increase the probability of causing (or not being able to avoid) an accident while driving. Sadly, drunk driving is the nation’s most frequently committed violent crime. Someone dies in a drunk driving-related accident every 30 minutes.[1] You can use a Breathalyzer to measure your alcohol level. However, reaction time also can be used in determining your level of inebriation. My project uses the SKP16C62P demo board as a reaction time measurement device (see Figure 4 on page 67).

(Click here to enlarge)

Figure 4—I modified the demo program for my application. I was able to make use of many of the prewritten routines, which really sped up the development process. 

The program allows you to indicate readiness by pressing any button. It will cease the LED chase pattern, wait a pseudorandom time, and light a pseudorandom LED. At that point, it will begin a timer and display the time in milliseconds after you’ve pressed the button associated with the illuminated LED. Pressing the wrong switch generates a “WRONG!” message. Pressing the correct button causes reaction time to be displayed. Pressing the correct button after 5 s displays “MAX” as the time. In addition to time, a short text message is displayed for various timings arbitrarily set at less than 500 ms (“Good”), less than 1,000 ms (“Poor”), less than 1,500 ms (“Pokey”), and 2,000 ms (“DWI”).

Most of the needed routines are present in the demo program, so I was able to modify sections of the Renesas code for this application. A/D conversions weren’t used, so out went support code for that. Timer0 incremented disp_count and was used to step the LEDs through a four-step, back-and-forth pattern. I reduced this pattern to a three-step chase pattern. Because I wanted to use this for random LED selection, each LED had to have equal time.

Timer1 handled periodic A/D conversions and button presses. I chose a new mode for Timer1 to produce 1-ms interrupts. The interrupt now increments time_count between 0 and 5,000, or 5 s, and then clears time_count. This is also used as a random time generator.

By far the most work was done to the main loop after the stock initialization and the displaying of the Renesas logo. If buttons aren’t initially pushed, Timer0 will continuously increment disp_count0. This count (1–3) is used to turn on and off the LEDs in a chase pattern (red-yellow-green-red-yellow-green…).

After you push a button, disp_count clears, thus turning off all of the LEDs. When the button is released, match_count is stored with whatever is presently in time_count (0–5000). match_count is the random time delay. (Because time_count can only count up to 5,000, 1,000 is subtracted from match_count if match_count is greater than 5,000.) Now time_count is cleared (also the overflow flag over5S) and a “GetReady” message is displayed on the second line of the LCD. At this point, you must wait until time_count equals match_count. This is the random time you must wait for the system to signal a reaction timer start by lighting a random LED.

The random LED choice, which comes from the recirculating disp_count (1–3), is saved in pick. Led_display(pick) turns on the LED, clears time_count, and begins counting milliseconds. After you see an illuminated LED, you must press the appropriate button as fast as possible. After you press a key, the key compared is to pick. If there’s a match, the get_time() routine is called. If there’s a mismatch, then the get_bad() routine is called. After returning from one of these routines and delay, it holds the results briefly before resetting the display and going back to the main loop.