|
|||||
Chapter 3 problems |
- D3.3 - |
Designing a LED rotator (μC - C) |
|||
|
1. Specifications
Our aim is to implement the LED rotator in Fig. 1 as an application of FSM adaptation to µC as represented in Fig. 1. Our target microcontroller is the PIC18F4520.
The same project designed using hardware is stated in D2.3.
The system operates by means of clicking several times the same push-button; an external interrupt is generated by such event. After turning on the circuit's power supply switch, the LED array is initially off.
- First click: all LED on.
- Second click: shift and rotate right at 2.5 Hz
- Third click: shift and rotate left at 5 Hz
- Fourth click: light intensity modulation and toggle flashing at 7.5 Hz four LED at a time: even LED at full intensity; odd LED at half light intensity.
- P is a switch to select between two rotation patterns. When '0' the pattern is a code one-hot as represented in Fig. 1. When '1' the pattern rotated is a code three-hot like "01110000", "00111000", ...
Fig. 1. LED_rotator symbol and idea. |
Solving this project requires several steps and design phases. Therefore, when planning we will pay attention on how to solve the basic circuit and then how to add a few new features at a time.
2. Planning
Design phase #1: basic features. FSM architecture, asynchronous reset (CD_L or MCLR_L), µC quartz crystal oscillator (OSC, 10 MHz), timing diagram, using interrupts, writing outputs, RAM variables, state diagram. Solve the circuit for the design step #1. This means covering P9 and P10 circuits, concepts and ideas. Study, copy and adapt from examples in LAB10.
Project location:
C:\CSD\P10\LED_rotator\(files)
- Design step #1. Consider an initial circuit to generate several binary codes to detect the sequence of PB clicks. The FSM will advance accordingly to the number of clicks detected using an external interrupt.
Fig. 2. Basic FSM symbol to detect button clicks. |
Adapt the state diagram from D2.3 to this problem. Deduce which signals will be read (in any in this step) and which will generate interrupts.
Fig. 3. State diagram adaptation. |
List the required RAM variables and their type. These variables are used as control signals and outputs in the state diagram in Fig. 3.
Fig. 4. RAM variables list. |
Draw an example of timing diagram.
Fig. 5. Timing diagram example. |
Draw the hardware schematic. Switch P and push-button PB and pull-up resistors, LED to represent the CNTL vector. Reset circuit MCLR_L and a 10 MHz quartz crystal oscillator. External CLK oscillator block. Which pins will interrupt? Which will be read? Assign inputs and outputs to μC port pins.
Fig. 6. Hardware used in this application. |
Draw the software flowchart organised as a FSM.
Fig. 7. Software organisation and the idea of FSM adaptation. |
Draw the hardware/software diagram explaining how the FSM is solved in software.
Fig. 8. Hardware-software diagram. |
Draw the main ideas of init_system(). Configure input and output pins. Consider as well interrupt configuration bits to enable external interrupts.
Fig. 9. TRIS configuration bits. |
Draw the flowchart, memory diagrams and C instructions of read_inputs() function to poll input voltages as in P9.
Fig. 10. How to read inputs. |
Draw the flowchart, memory diagrams and C instructions of write_outputs() function to write pin voltages as in P9. At this step the outputs are CNTL(2..0) bits.
Fig. 11. How to write outputs. |
Infer how to organise the interrupt service routine ISR() to handle push-button falling edge events.
Fig. 12. Interrupt service routine flowchart. |
And now, it is necessary to discuss how to transfer all the state transitions into a truth table.
Draw state_logic() truth table and its equivalent flowchart.
Fig. 13. Truth table for state_logic() function and its equivalent behavioural plan B flowchart. |
Draw output_logic() truth table and its equivalent flowchart.
Fig. 14. Truth table for output_logic() function and its equivalent behavioural plan B flowchart. |
3. Development & 4. Test
Develop and test (debugging) the project capturing the hardware circuit in Proteus and writing the C source code. Do not start your hardware circuit and software source file from scratch, but copy and adapt from examples in LAB10 or P10.
- Design step #2. Let us add a 5 Hz CLK to the system, so that we can generate LED sequences.
At this initial stage, taking advantage of our previous hardware design, we can imagine the application blocks, which forcibly has to be engineered in several steps and phases in software functions and using specialised peripherals. You can imagine how to divide the CLK frequency by 2 to drive the rotator function (CLK) at 2.5 Hz (right rotation) or 5 Hz (left rotation).
Fig. 15. The same blocks inferred in our hardware design, can be used here to imagine how the successive functions in C language will be conceived to generate LED rotations. |
- Design step #3. Modulating LED intensity.
Light intensity can be modulated applying less power to the LED. For instance, driving a LED with a '1' means maximum power and light intensity. In this way, if we apply 1 kHz squared signal, it will be integrated by the eye so that the user will see half light intensity. Adjusting pulse duration (PWM) allows light intensity modulation.
In this step, the external CLK will 1 kHz. Thus, the 5 Hz and 2.5 Hz signals will be attained using C functions for counting frequency dividers).
Fig. 16. The same blocks inferred in our hardware design, can be used here to imagine how to modulate the LED light intensiy by means of two internal CLK signals of 1 kHz and 7.5 Hz. |
Design phase #2: advanced features to interface an LCD display.
Only when design phase #1 is fully tested working correctly and reported, solve the design phase #2 as advancing to cover P11 materials.
Project location:
C:\CSD\P11\LED_rotator_LCD\(files)
Let us add an LCD display to this application. The idea is to use several characters to represent an equivalent sequence rotation.
Enhance the schematic from design phase #1 to include an LCD attached to port D as studied in tutorials.
Enhance the software and the source file to drive the LCD.
In this design phase also two design steps may be conceived, for instance:
- step #1: Print ASCII messages on the LCD.
- step #2: Print as well the number of rotations (dynamic numerical data).
Design phase #3: On the use of internal peripherals to reduce components and board size.
Only when design phase #2 is fully tested working correctly and reported, solve the design phase #3 as advancing to cover P12 materials.
Project location:
C:\CSD\P12\LED_rotator_LCD_TRM2\(files)
The external CLK is replaced by the internal 8-bit TMR2 peripheral to generate interrupts (TMR2IF). In this way, we will save power consumption, reduce board footprint, and also free an external INT for other future applications.
Calculate TMR2 parameters required to generate a CLK of 5 Hz (var_CLK_5Hz_flag, period = 200 ms) to run the machine.
Or calculate TMR2 parameters required to generate a CLK of 1 kHz (var_CLK_1kHz_flag, period = 1 ms) to run the machine if you have solved the design phase #1 step #3.