ABSTRACT
In this lab, we implemented a reaction timer and a state machine that can vary the
timing of the traffic light sequence based on a sensor input like a walk button or under-the-street pressure sensor. The reaction timer used a linear feedback shift register to generate a random number and then used this number to light up an LED; as soon as the LED was lit up, the counter started counting (in 10s of ms) until a user reacted by activating a sensor.
Task 1 - The Reaction Timer
In order to build a reaction timer, we first created an LFSR (linear feedback shift register) that gave us random numbers. We had the circuit wait for some time depending on the random number generated before we lit up an LED. When the LED was on, the seven-segment display started counting in milliseconds until the user pressed the reaction button, when the reaction time was displayed in milliseconds. Displaying in milliseconds was tricky but not that hard. We used an internal variable and at every clock cycle the variable was incremented. Every time it reached 1/100 of the clock frequency, we incremented the time (in milliseconds). To extend this task, we displayed the numbers in milliseconds.
Fig 1. State diagram of the reaction timer.
|
adem |
karl |
mas |
bruce |
|
16 |
16 |
16 |
22 |
|
13 |
14 |
14 |
15 |
|
3 |
16 |
16 |
14 |
|
16 |
9 |
18 |
16 |
|
17 |
14 |
16 |
16 |
|
14 |
16 |
16 |
|
|
14 |
16 |
14 |
|
|
13 |
16 |
12 |
|
|
15 |
16 |
18 |
|
|
15 |
14 |
23 |
|
|
16 |
17 |
16 |
|
|
21 |
16 |
14 |
|
|
12 |
10 |
14 |
|
|
14 |
12 |
16 |
|
|
14 |
14 |
18 |
|
|
14 |
14 |
16 |
|
|
14 |
16 |
14 |
|
|
12 |
17 |
12 |
|
|
12 |
16 |
16 |
|
|
14 |
16 |
16 |
|
|
|
|
|
|
average |
13.95 |
14.75 |
15.75 |
16.6 |
mode |
14 |
16 |
16 |
16 |
median |
14 |
16 |
16 |
16 |
table 1. Some reactions... Adem was lucky!.. especially with that 3 millisecond reaction."No i wasn't!" - adem
Task 2 - Dynamic Traffic Light
The traffic light system operates as follows: when reset, it starts at a red/red state (R/R 1 in fig 2.), waits for two seconds and goes to the second state; the green/red state which lasts for L1 seconds (L1 is decided by the user, but we used 10 seconds). If the 'urgent' button is pressed (ie. there is a pedestrian) during this state, then the second green/red state is skipped and it goes to the yellow/red state. If the urgent button is not pressed, then the green/red state lasts for another L2- L1 seconds (we made L2 15 seconds). However pressing the urgent button after L1 seconds doesn't have any effect, namely the green/red state lasts for either L1 or L2 seconds. Pressing the urgent button has no effect outsite the first green/red state. The yellow/red state lasts for 4 seconds and the next state it the second red/red state. After this the system works symmetrically.
Fig 2. state diagram for the traffic lights
The reason for creating two states for green/red was to make our system more expandable. We could have squeezed the two states into one, but that would limit us in a couple of ways. For example, for one of the extensions, the second green/red state is skipped only if the urgent button is being pressed at the end of the first green/red state. By having two states, we can put in a quick check at the beginning of the second state and skip it if the urgent button is pressed; one line of code in VHDL.
We could also squeeze the two red/red states into one, in fact we could squeeze the yellow/red and red/yellow states into one state (and have a variable that specifies which side is yellow), but again, it makes more sense to keep them separate. For example if we had a crossroads where the traffic flows mainly in one direction then we'd want to keep one of the red red states shorter. Having two red/red states makes this very easy to implement.
We extended this lab in a couple of ways. First we set up the system so that if the pressure sensor is released before time L1 then the traffic light does not change early but continues as if nothing happened. This was easy to implement and we simply checked if the sensor was tripped as we entered the G/R L2 state.
Then we set up an additional input that, when set, put the traffic lights in a blinking configuration with yellow blinking N-S and red blinking E-W and only allowed this state to be entered from a red-red state. To imlement this, we had a variable in the yellow/red state which specified that it was a blinking state and which also specified that we shouldn't go to the next state. Indeed the only way to get out of this state was to reset the whole thing.
We used a six bit std_logic_vector to represent each state and used the state names to output the actual signals. It made life easier for us; we simply made the red/red state 011011, and named the other states appropriately so that we could directly use the state names as outputs.
Problems Encountered
Both of the tasks worked just as we expected them to, so we didn't have major problems but there were a few things that we had to be careful about. First, this was the first time we used the flex10 chip, so it took us a while to figure out what we had to customize on the Altera board (we actually thought we had problems with the code -- always hard to change the right code!).
The rest of the problems were mainly software problems; we had the LFSR and the counter as two different components (although both of them could be implemented inside the main program), so figuring out the way components work was a little problematic. Also, we had to be careful about resetting the local variables; obviously, leaving the variables as they were effected the next time we came back to the same state.
What We Learned
There were three main things we learned doing this lab:
1- State machines: this was the main idea behind the lab and we learned how to set-up, modify, optimize state machines.
2- Using components in VHDL: Our first thought was that components would work just like functions in other computer languages (first thoughts are always dumb and costly!), but we soon figured that wasn't the case. Since components are always running (not only when they are called; in fact they're always called, or never called depending on how you look at it) we had to create variables that would drive the components and had to be careful about setting and resetting these variables.
3- Setting up the FLEX10 chip: sounds easy, is easy, but not when you don't know about it! Now we know, and we never want to go back to the other chip :)
Extensions
We talked about the details of the extensions as we explained the tasks, but the main extensions were the following:
- displaying decimals, instead of hex values.
-
Setting up an additional input that, when set, puts the traffic lights in a blinking configuration with yellow blinking N-S and red blinking E-W. Tthe configuration is entered from a red-red state.
- Setting up the circuit so that if the pressure sensor is released before time L1 then the traffic light does not change early, but continues as if nothing happened.
|