Sunteți pe pagina 1din 43

THE UNIVERSITY OF LAHORE

Microprocessor based
EmbeddedSystem
Lab Manual
Revised: September 2012
CONTENTS
Sr.
no.
Name of Experiment
Page
no.
1.
Setup Development Environment for STM32
1
2.
Create a Bare-bone Project for STM32
3
3.
Configure a GPIO Pin as Output
10
4.
Create a Software Delay Function
12
5.
Configure a GPIO Pin as Input And Using Debugger
14
6.
De-bounce a Switch Input and Count its State Changes
17
7.
Implement Timed Cyclic Executiveusing Timer
19
8.
Start Development and Debugging of Course Project
22
9.
Implement a Finite State Machine
26
10.
Implement a Multi-States Machine
29
11.
Implement a Multi-Rate Cyclic Executive
31
12.
UsingLCD Library for HD44780 controller
33
13.
UsingUSART Library for Serial Communication
35
14.
Interface a 4x3 Keypad Matrix With STM32
37
15.
Configure and Use an ADC
39
16.
Demonstration of Course Project. Evaluation of Project.
41
Lab 1: Development Environment
OBJECTIVE:
To Setup Development Environment for STM32.
MATERIAL: uVision4 IDE, GCC compiler, ST-Link utility, STM32 board, USB cable.
STEP No. 1.Install the following tool chain for STM32 development board
1. Keil uVision IDE (Linker, Simulator)
Filename: mdk413a.exe
Default path: C:\Keil\UV4
2. Sourcery GCC C Compiler
Filename: arm-2010q1-188-arm-none-eabi.exe
Default path: C:\Program Files\CodeSourcery\Sourcery G++Lite
3. STM ST-Link utility
Filename: STM32 ST-LINK Utility_v2.3.0.exe
Default path: C:\Program Files\STMicroelectronics\STM32 ST-LINK Utility\
STEP No. 2.Connect STM32 development board to your PC via USB cable.
STEP No. 3.Install USB drivers for the STM32 development board if Windows do not auto detect the hardware.
STEP No. 4.Run ST-Link utility from its path and press the Connect button as shown below. If connection is made
successfully between the STM32 development board and ST-Link utility then you will see message Connected via SWD
as shown below.
Exercise:
1. What is an IDE? What is the name and version of the IDE used in this lab?
___________________________________________________________
___________________________________________________________
___________________________________________________________
2. What is a compiler? What are its input and output files? Which compiler is used in this lab?
___________________________________________________________
___________________________________________________________
___________________________________________________________
3. What is a linker? What are its input and output files? Which linker is used in this lab?
___________________________________________________________
___________________________________________________________
___________________________________________________________
4. What is a simulator? Which simulator is used in this lab?
___________________________________________________________
___________________________________________________________
___________________________________________________________
5. What is the purpose of ST-Link utility?
___________________________________________________________
___________________________________________________________
6. Which LED turns-ON on the STM32 board when you connect the board with PC via USB cable?
___________________________________________________________
7. Which LED blinks on the STM32 board when ST-Link utility makes successful connection with the STM32 board?
___________________________________________________________
Lab 2: Bare-bone Project
OBJECTIVE: To Create a Bare-bone Project for STM32.
MATERIAL: uVision4 IDE, GCC compiler, ST-Link utility, STM32 board, USB cable.
STEP No. 1. Create a new folder of name Lab2 and save following two files in it.
STM32F100RB_FLASH.ld startup_stm32f10x_md_vl.s
STEP No. 2. Run Keil uVision4 from C:\Keil\UV4\Uv4.exe.
STEP No. 3. Click on Project and select New uVision Project from the list. Select your folder Lab2 from the directory.
Write Lab2 in the File Name. This will create a new project Lab2 in your folder Lab2.
Caution:Do not create more than one project in a single folder.
STEP No. 4. Select Target device STM32F103RB under STMicroelectronics in the list.
STEP No. 5. Click on File and select New. Write the code as shown in the figure. Save as main.c in your folder Lab2.
STEP No. 6. Click on Manage Components button. Select Add Files and then your folder Lab2. Add files main.c and
startup_stm32f10x_md_vl.s in your project.
STEP No. 7. Select the tab Folders/Extentions and check the option GNU Compiler in it.
STEP No. 8. Click on Target options button and go through each of the tabs one by one. Make changes in the options
available under Output, CC and Linker tabs as shown below.
STEP No. 9. Click on Rebuild button and wait until your project is compiled. If your project is compiled successfully then
you will see message 0 Errors, 0 Warnings. Also a new file Lab2.hex will be created in your folder Lab2. Close Keil
uVision.
STEP No. 10. Open ST Link Utility as in Lab 1. Click on File and select Lab2.hex file from your folder Lab2. If your file is
programmed successfully then will see Verification .. OK message as shown below.
Exercise:
1. Open the folder Lab2 and find out how many files are contained in it after completing the ten steps?
___________________________________________________________
2. Write down the names of the files in folder Lab2 that contain software code in them for the target device.
___________________________________________________________
3. What is the name and extension of the uVision project file?
___________________________________________________________
4. What is the purpose of startup (.s) file?
___________________________________________________________
___________________________________________________________
___________________________________________________________
5. What is the purpose of linker script (.ld) file?
___________________________________________________________
___________________________________________________________
___________________________________________________________
6. What is the name and extension of file that is downloaded on the STM32 board?
___________________________________________________________
Lab 3: GPIO Output
OBJECTIVE:
To Configure a GPIO Pin as Output.
MATERIAL:
uVision4 IDE, GCC compiler, ST-Link utility, STM32 board, USB cable, oscilloscope.
STEP No. 1.Create a new project Lab3 in folder Lab3. Write following code in main.c file.
#def i ne RCC_APB2ENR ( * ( ( vol at i l e unsi gned l ong*) 0x40021018) )
#def i ne GPI OC_CRH ( * ( ( vol at i l e unsi gned l ong*) 0x40011004) )
#def i ne GPI OC_BSRR ( * ( ( vol at i l e unsi gned l ong*) 0x40011010) )
voi d Del ay( voi d)
{
vol at i l e unsi gned shor t i ;
f or ( i = 0; i < 650000; i ++)
{
}
}
voi d Syst emI ni t ( voi d)
{
}
i nt mai n( voi d)
{
RCC_APB2ENR | = 0x00000010; / / enabl e cl ock t o GPI O por t C
GPI OC_CRH &= 0xFFFFFFF0; / / cl ear pr evi ous conf i g f or por t C pi n 8
GPI OC_CRH | = 0x00000001; / / conf i g por t C pi n 8 as 10Mhz push pul l
/ / gener al pur pose out put .
whi l e( 1)
{
GPI OC_BSRR = 0x00000100; / / set pi n hi gh
Del ay( ) ;
GPI OC_BSRR = 0x01000000; / / set pi n l ow
Del ay( ) ;
}
r et ur n( 1) ;
}
STEP No. 2.Build the project and download Lab3.hex file on STM32 board.
STEP No. 3.Change the value and data type of variable i in Delay() function until you obtain LED blinking at 1 Hz. You
can connect oscilloscope at Port C Pin 8 (PC8) to measure the blinking time period accurately.
LD3: Green LED LD3 is connected to the I/O PC9 of STM32F100RBT6B.
LD4: Blue LED LD4 is connected to the I/O PC8 of STM32F100RBT6B.
Exercise:
1. What is the value and data type of variable i in Delay() function that gives blinking frequency of 1 Hz?
___________________________________________________________
2. How can you modify the program to blink the LED such that it stays ON for 1 second and stays OFF for 2
seconds?
___________________________________________________________
___________________________________________________________
3. Why masking is used (instead of simple assignment) to change the contents of GPIOC_CRH register?
___________________________________________________________
___________________________________________________________
4. Why OR operation is used (instead of simple assignment) to change the contents of RCC_APB2ENR register?
___________________________________________________________
___________________________________________________________
5. How can you enable clock for GPIOA, GPIOB and GPIOC simultaneously?
___________________________________________________________
6. How can you disable clock for GPIOA, GPIOB and GPIOC simultaneously?
___________________________________________________________
7. What is the purpose of #define directives at the start of the file?
___________________________________________________________
Lab 4: Software Delay
OBJECTIVE:
To Create a Software Delay Function.
MATERIAL:
uVision4 IDE, GCC compiler, ST-Link utility, STM32 board, USB cable, oscilloscope.
STEP No. 1.Create a new project Lab4 in folder Lab4. Use the following code in main.c file.
voi d Del ay( voi d)
{
vol at i l e unsi gned i nt i ;
f or ( i = 0; i < 650000; i ++)
{
}
}
i nt mai n( voi d)
{
. . .
whi l e( 1)
{
GPI OC_BSRR = 0x00000100; / / set pi n hi gh
Del ay( ) ;
GPI OC_BSRR = 0x01000000; / / set pi n l ow
Del ay( ) ;
}
r et ur n( 1) ;
}
STEP No. 3.Complete the main.c file yourself. Change the value and data type of variable i in Delay() function until
you obtain LED blinking at 1 Hz. You can connect oscilloscope at Port C Pin 8 (PC8) to measure the blinking time period
accurately.
STEP No. 2.Build the project and download Lab4.hex file on STM32 board.
LD3: Green LED LD3 is connected to the I/O PC9 of STM32F100RBT6B.
LD4: Blue LED LD4 is connected to the I/O PC8 of STM32F100RBT6B.
Exercise:
1. What is the value and data type of variable i in Delay() function that gives blinking frequency of 1 Hz?
___________________________________________________________
2. How can you modify the program to blink the LED such that it stays ON for 1 second and stays OFF for 2
seconds?
___________________________________________________________
___________________________________________________________
3. Find values of delay variable i for 10 different blinking frequencies in the range 0.5 Hz to 5 Hz. Find out an
approximate linear relationship for the value of i and blinking frequency. Show your data and working below.
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
4. Modify the simple Delay() function to write three separate functions as shown below. Pass appropriate type of
variable to these functions to generate specific delay time. Use the method of Q. 3 to estimate the value of
delay variable i in each case. Give your working and code below.
a. Seconds_Delay();
b. Milliseconds_Delay();
c. Microseconds_Delay();
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
Lab 5: GPIO Input
OBJECTIVES:
To Configure a GPIO Pin as Input and Using Debugger.
MATERIAL:
uVision4 IDE, GCC compiler,ST-Link utility, STM32 board, USB cable, oscilloscope.
STEP No. 1.Create a new project Lab5 in folder Lab5. Use the following block of code in your main.c file.
i nt mai n( voi d)
{
unsi gned shor t Por t _dat a;
RCC_APB2ENR | = 0x00000014; / / enabl e cl ock t o GPI O por t A and C
GPI OA_CRH = 0x44444444; / / al l pi ns t o f l oat i ng i nput s
GPI OA_CRL = 0x44444444;
GPI OC_CRH = 0x11111111; / / al l pi ns t o 10MHz push- pul l out put s
GPI OC_CRL = 0x11111111;
whi l e( 1)
{
Por t _dat a = GPI OA_I DR;
i f ( ( Por t _dat a & 0x00000001) ==1)
GPI OC_ODR = 0x00000300;
el se
GPI OC_ODR = 0;
}
r et ur n( 1) ;
}
STEP No. 2.Complete the main.c file yourself. Add #define directives at the beginning of main.c file for all the
register names that are used in the main() function.
STEP No. 3.You can use Debugger to test your code. Press Debug button or CTRL+F5 to start the debug session as
shown below.
STEP No. 4.Click on Run or F5 button to start code execution. Then click on Peripherals button and select GPIOA and
GPIOC from the drop down menu successively. You will see two dialog boxes will appear as shown below. Click on Pin 0
in the GPIOA dialog box as shown below. This will simulate push-button press action. Click on it again to simulate the
push-button un-press action. Repeat this press-and-un-press action three times. You will see state of the LED pin will
toggle in the GPIOC dialog box.
STEP No. 5.Build the project and download Lab5.hex file on STM32 board.
STEP No. 6.Modify the above code such that if button is pressed then blue LED turns ON and if the button is not pressed
then green LED turns ON.
B1: Push button labeled USER is connectedto the GPIOA pin 0 (PA0) of STM32F100RBT6B. It is active high, normally low.
B2: Push button labeled RST is dedicated to the RESET pin of STM32F100RBT6B.
Exercise:
1. Calculate all register addresses which are used in this lab using the base addresses of peripherals and offset
addresses of registers. Show your working below.
___________________________________________________________
___________________________________________________________
___________________________________________________________
2. Why bit-wise AND (&) operation is used while reading the value of GPIOA Pin 0?
___________________________________________________________
___________________________________________________________
3. Modify above code and remove the bit-wise AND (&) operation while reading the button input. Now check your
results both in debugger and on hardware. Give your comments on both results that you get.
___________________________________________________________
___________________________________________________________
___________________________________________________________
4. Write the code for STEP No. 3 below.
___________________________________________________________
___________________________________________________________
___________________________________________________________
5. Why the data type of variable Port_data is unsigned short?
___________________________________________________________
6. Merge the code of Lab3 in the above code such that LED blinks at 1 Hz when button is pressed while it blinks at
2 Hz if the button is not pressed. Give the new code below.
___________________________________________________________
___________________________________________________________
___________________________________________________________
Lab 6: Input State Changes
OBJECTIVES:
To De-bounce a Switch Input and Count its State Changes.
MATERIAL:
uVision4 IDE, GCC compiler,Debugger, ST-Link utility, STM32 board, USB cable.
STEP No. 1.Create a new project Lab6 in folder Lab6. Use the following blocks of code in your main.c file.
/ / Swi t ch connect ed t o GPI O A pi n 0 and i s act i ve hi gh
unsi gned char Swi t ch_count = 0;
. . .
voi d Toggl e_Led( )
{
st at i c unsi gned char Led_st at us=0;
i f ( Led_st at us==0)
{
GPI OC_ODR=0x100;
Led_st at us=1;
}
el se
{
GPI OC_ODR=0;
Led_st at us=0;
}
}
. . .
/ / Bl ock of code i nsi de mai n( ) t o count but t on pr esses
i f ( ( GPI OA_I DR & 0x00000001) ! = 0)
{
Debounce_Del ay( ) ; / / sof t war e del ay equal t o t he debounce per i od
i f ( ( GPI OA_I DR & 0x00000001) ! = 0)
{
Swi t ch_count ++;
whi l e ( ( GPI OA_I DR & 0x00000001) ! = 0)
{
}
Debounce_Del ay( ) ;
}
}
STEP No. 2.Complete the main.c file yourself. Inside the main() function use the block of code given above to count
the number of times a switch is pressed and released. When this count reaches value of 3 then call the Toggle_Led()
function that will invert the state of the LED. You should include the Toggle_Led() function in your file. It is important
that this function precedes the main() function in the file. The Debounce_Delay() function is same as the Delay() function
in Lab3 except that the delay time is 20 msec now.
STEP No. 3.Build the project and download Lab6.hex file on STM32 board.
Exercise:
1. What is the value of i in the Debounce_Delay() function for 20msec?
___________________________________________________________
2. Draw timeline waveform for following signals:
a. Actual push-button input (3 button presses with 20 msec bouncing time)
b. Input after de-bouncing
c. Output (LED toggle)
___________________________________________________________
___________________________________________________________
___________________________________________________________
3. Why is the variable Led_status in the Toggle_Led() function qualified as static data type? Test your code result
after removing this qualifier.
___________________________________________________________
___________________________________________________________
4. How can you estimate the value of Debounce_Delay() variable i for 20 msec using the Debugger?
___________________________________________________________
___________________________________________________________
Lab 7: Timed Cyclic Executive
OBJECTIVE:
To Implement Timed Cyclic Executive Using a Hardware Timer.
MATERIAL:
uVision4 IDE, GCC compiler, Debugger, ST-Link utility, STM32 board, USB cable.
STEP No. 1.Create a new project Lab7 in folder Lab7. Use the following blocks of code in your main.c file.
/ / Conf i gur e and st ar t t he t i mer
voi d Del ay_St ar t ( unsi gned i nt Ti mer _del ay_ms)
{
TI M2_CR1 = 0; / / St op t i mer
TI M2_CNT = 0; / / r eset t he t i mer count er
TI M2_PSC = 7999; / / count er i s i ncr ement ed ever y ms
TI M2_ARR = Ti mer _del ay_ms; / / set up t o count t i l l 65535
TI M2_CR1 = 1; / / st ar t t i mer i n one pul se mode
}
/ / Wai t f or t he set t i me t o pass
voi d Del ay_Wai t ( )
{
whi l e ( ( TI M2_SR & 0x00000001) ==0) ; / / wai t f or updat e i nt er r upt f l ag
TI M2_SR = 0; / / cl ear t he updat e i nt er r upt f l ag
}
/ / Bl ock of code f or usi ng i nsi de mai n( ) f unct i on
Count ++;
i f ( ( GPI OA_I DR & 0x1) ! =0)
{
i f ( Count >=100)
{
Toggl e_Led( ) ;
Count =0;
}
}
el se
{
i f ( Count >=200)
{
Toggl e_Led( ) ;
Count =0;
}
}
STEP No. 2.Complete the main.c fileyourself. Inside the main() function use the above block of code such that if
button is pressed then LED flashes at 1 Hz, and if it is not pressed then LED flashes at 2 Hz.
STEP No. 3.Build the project and download Lab7.hex file on STM32 board.
STEP No. 4.Rewrite your program such that main() function looks like as shown below. Add de-bouncing feature into
the button read function.
Del ay_St ar t ( 10) ;
whi l e( 1)
{
count ++;
t ask_swi t ch( ) ;
t ask_l ed( ) ;
Del ay_Wai t ( ) ;
}
STEP No. 5.Implement a periodic task for reading button input as a state machine using switch-case construct. Do not
use f or ( ) or whi l e( ) loops inside tasks to avoid blocking code. Sample code for Swi t ch_Task( ) is given
below.
#def i ne NOT_ PRESSED 1
#def i ne PRESSED 2
#def i ne BOUNCI NG 3
#def i ne ACTI VE_HI GH 1
#def i ne ACTI VE_LOW 0
unsi gned char But t on_ Updat e( )
{
i f ( ( GPI OA_I DR & 0x1) ==ACTI VE_HI GH)
{
r et ur n PRESSED;
}
el se
{
r et ur n NOT_PRESSED;
}
}
void Switch_task(void)
{
...
New_but t on_st at e = But t on_ Updat e( ) ;
swi t ch( But t on_st at e_machi ne)
{
case NOT_PRESSED:
but t on_st at e_G = NOT_PRESSED;
i f ( New_but t on_ st at e == PRESSED)
{
But t on_ st at e_machi ne = BOUNCI NG;
Debounce_count =0;
}
br eak;
case BOUNCING:
Debounce_count ++;
i f ( Debounce_ count == 3)
{
i f ( New_but t on_ st at e == NOT_PRESSED)
{
But t on_st at e_machi ne = NOT_PRESSED;
Debounce_count = 0;
}
el se
{
But t on_st at e_machi ne = PRESSED;
Debounce_count = 0;
}
}
br eak;
case PRESSED:
but t on_st at e_G = PRESSED;
i f ( New_but t on_ st at e == NOT_PRESSED)
{
But t on_ st at e_machi ne = BOUNCI NG;
Debounce_count = 0;
}
br eak;
default:
But t on_st at e_machi ne = BOUNCI NG;
Debounce_count = 0;
br eak;
}
}
Exercise:
1. Show the calculation of TI M2_PSC = 7999; such that counter is incremented after every millisecond.
___________________________________________________________
___________________________________________________________
2. How are Del ay_St ar t ( ) and Del ay_Wai t ( ) functions used to implement a sandwich timer?
___________________________________________________________
___________________________________________________________
3. What are the three states of a push button?
___________________________________________________________
___________________________________________________________
4. Draw state diagram for the push button as shown in Switch_Task().
___________________________________________________________
___________________________________________________________
5. Write three benefits of Timed Cyclic Executive software architecture over a simple super-loop?
___________________________________________________________
___________________________________________________________
___________________________________________________________
6. What are hazards of using f or ( ) loop or whi l e( ) loop inside a periodic task()?
___________________________________________________________
___________________________________________________________
___________________________________________________________
Lab 8: Course Project
OBJECTIVE:
To Start Development and Debugging of Course Project.
MATERIAL:
uVision4 IDE, GCC compiler, Debugger, ST-Link utility, Trainer board, STM32 board, USB cable, project items.
STEP No. 1.Make groups for your course project.
STEP No. 2.One of the following four projects will be assigned to each group. The details of all projects are given below.
STEP No. 3.Submit initial project report that would contain hardware and software design of your project.
Project 1: Buzzer Game
Description:
The main objective of the system is to determine which of four players is the first to press a button in response to a go
signal. The LED for the first player to make a valid button press should light up, indicating the winner.
Hardware Connections:
5 x LEDs: 1 for the go signal and 1 each to indicate the winning player.
5 x push buttons: 1 for the go signal generation and 1 each for the four players.
1 x analog audio output (PA4).
1 x analog input (PA0) to control the duration of the response window.
USART 2 and LCD on the trainer.
Operating procedure:
After power up, the system should wait for the go signal button to be pressed.
In response to a press of the go signal button, the system should turn on the Go LED after a random period between 5 to 10
seconds (should change every time the system runs).
In response to the go signal, the system should expect a button press on one or more of the user buttons and should turn on
the LED corresponding to the first person to make a valid button press.
For a button press to be valid, it should not be pressed when the go signal is generated.
The duration of the response window should be set between 10 and 60 seconds depending on an analog value read using
the ADC.
After the duration for the response (response window) has passed, the system should generate a detailed report in which
the times at which: the duration of the response window is given, the times at which the buttons were pressed are given,
along with indications of invalid and multiple button presses and the winner.
The LCD (optional) should be used to provide information on the current operating mode of the system, the duration of the
response window as well as the remaining time and the winning player.
A unique analog audio signal should be generated to indicate which of the four players won.
The system should be configurable to work with both the rising as well as falling edge of the player switches (optional).
Project 2: Tick Tack Toe Game
Description:
The main objective of the system is to provide a 1 / 2 player tick tack toe game based on a microcontroller.
Hardware Connections:
9 x LED for player 1
9 x LED for player 2
Matrix keypad (PC4:11)
1 x analog input (PA0) to control the duration of the response window.
USART 2 and LCD on the trainer.
Operating Procedure:
The system should start in 1 player mode with the 2
nd
player provided by the software in the microcontroller.
The LCD should indicate the time the player has to make the next move. If this time reaches zero, the player loses the match.
The system should also maintain a total score indicating number of matches won as well as the number of matches played.
The time for each move should be configurable between 1 and 60 seconds and should be set by reading the analog value.
After each move, the system should analyze if the game has ended. If yes, indicate the winner and the match time.
A detailed serial report (optional) should be sent on the serial port to indicate the winner and other game stats.
A two player mode should also be included (optional). In this mode, there should be a 5 second switch over time allowing
the players to change places. In this time, no input should be accepted from the keypad.
Project 3: Vending Machine
Description
The vending machine acts as an automated store keeper. The purpose of this system is to receive payment, get the users
choice for item required, release the item in question if sufficient payment has been received and the required item is in
stock.
Hardware Connections
3 x General purpose outputs to simulate the vending mechanism
4 x Switches for denominations (Rs 5, Rs 10, Rs 20, Rs 50)
4 x LEDs for return change (optional)
Matrix keypad (PC4:11)
USART 2 and LCD on the trainer.
Operating procedure
The system should operate in the following sequence:
1. Wait for cash entry.
2. Wait for product selection (options 1 to 6).
3. If sufficient cash has been enteredand product is in stock, vend the item according to the table below.
Item 1 Item 4
Output Pulse start Pulse width Output Pulse start Pulse width
A 0 1 A 0 1
B 0 3.2 B 3 1
C 5 1 C 5 1
Item 2 Item 5
Output Pulse start Pulse width Output Pulse start Pulse width
A 0 1.5 A 0 1.5
B 0 3.2 B 3 2
C 5 1 C 5 1
Item 3 Item 6
Output Pulse start Pulse width Output Pulse start Pulse width
A 0 2 A 0 2
B 0 3.2 B 3 3
C 5 1 C 5 1
4. Return the remaining cash or wait for next product selection.
5. There should also be a mechanism to check the status of the machine and restock it (preferably through serial).
For cash input, count the logic high pulses on the denomination inputs. Only pulses with a period more than 50 ms should be
counted.
For cash return (optional) generate square pulses of 500 ms high duration and a wait of at least 500ms between pulses.
The status check should also include a log of operations which should indicate time at which different transactions and
operations were carried out.
The system should include an error recovery mechanism (optional) (e.g. what if the vending mechanism gets stuck or the
system is unable to get more cash or has insufficient change. For the purpose of the project, you can use separate switches
to indicate separate problems).
Project 4: Adaptive Traffic Light
Description
The adaptive traffic light is a smart system that should vary the traffic signal timings in an attempt to reduce the congestion
on different approaches to the traffic intersection.
Hardware connections
4 x analog inputs (PA4:7) for traffic levels.
4 x switches to indicate a waiting vehicle.
12 x LEDs for all the traffic signals.
Matrix keypad (PC4:11).
USART 2 and LCD on the trainer.
Operating procedure
The system should control the traffic lights for a 4 way intersection. No two signals should be green or yellow at the same
time.
The timing of the signals should be varied on the basis of traffic density indicated by four analog voltage signals. The timings
can be varied on the basis of preset threshold levels or using some equations (optional). In the case of thresholds, the
timings will shift between predetermined combinations, where as in the case of equations, the timing will vary smoothly
within a range of possibilities.
Serial link (optional) should provide a detailed status of the setup.
Keypad and LCD should be used to configure the following system parameters:
1. Minimum and maximum traffic light green times.
2. Priority of each route (all equal or higher priority to one of the four approaches)
When system configuration is not being done, the LCD should show which light is green, which will be the next light to be
green and the traffic densities on the different paths.
Exercise:
1. Submit Initial Design Report that would contain:
a. Overall Design
b. Hardware Details of Motors, Sensors and Circuit Diagrams
c. Software Details using State Diagrams.
Lab 9: Finite State Machine
OBJECTIVE:
To Implement a Finite State Machine for Switch De-bouncing.
MATERIAL:
uVision4 IDE, GCC compiler, Debugger, ST-Link utility, STM32 board, USB cable.
STEP No. 1.Create a new project Lab9 in folder Lab9. Use the following blocks of code in your main.c file.
/ / Conf i gur e and st ar t t he t i mer
voi d Del ay_St ar t ( unsi gned i nt Ti mer _del ay_ms)
{
TI M2_CR1 = 0; / / St op t i mer
TI M2_CNT = 0; / / r eset t he t i mer count er
TI M2_PSC = 7999; / / count er i s i ncr ement ed ever y ms
TI M2_ARR = Ti mer _del ay_ms; / / set up t o count t i l l 65535
TI M2_CR1 = 1; / / st ar t t i mer i n one pul se mode
}
/ / Wai t f or t he set t i me t o pass
voi d Del ay_Wai t ( )
{
whi l e ( ( TI M2_SR & 0x00000001) ==0) ; / / wai t f or updat e i nt er r upt f l ag
TI M2_SR = 0; / / cl ear t he updat e i nt er r upt f l ag
}
/ / Bl ock of code f or usi ng i nsi de mai n( ) f unct i on
Count ++;
i f ( ( GPI OA_I DR & 0x1) ! =0)
{
i f ( Count >=100)
{
Toggl e_Led( ) ;
Count =0;
}
}
el se
{
i f ( Count >=200)
{
Toggl e_Led( ) ;
Count =0;
}
}
STEP No. 2.Complete the main.c fileyourself. Inside the main() function use the above block of code such that if button is
pressed then LED flashes at 1 Hz, and if it is not pressed then LED flashes at 2 Hz.
STEP No. 3.Build the project and download Lab9.hex file on STM32 board.
STEP No. 4.Rewrite your program such that main() function looks like as shown below. Add de-bouncing feature into the
button read function.
Del ay_St ar t ( 10) ;
whi l e( 1)
{
count ++;
t ask_swi t ch( ) ;
t ask_l ed( ) ;
Del ay_Wai t ( ) ;
}
STEP No. 5.Implement a periodic task for reading button input as a state machine using switch-case construct. Do not use
f or ( ) or whi l e( ) loops inside tasks to avoid blocking code. Sample code for Swi t ch_Task( ) is given below.
#def i ne NOT_ PRESSED 1
#def i ne PRESSED 2
#def i ne BOUNCI NG 3
#def i ne ACTI VE_HI GH 1
#def i ne ACTI VE_LOW 0
unsi gned char But t on_ Updat e( )
{
i f ( ( GPI OA_I DR & 0x1) ==ACTI VE_HI GH)
{
r et ur n PRESSED;
}
el se
{
r et ur n NOT_PRESSED;
}
}
void Switch_task(void)
{
...
New_but t on_st at e = But t on_ Updat e( ) ;
swi t ch( But t on_st at e_machi ne)
{
case NOT_PRESSED:
but t on_st at e_G = NOT_PRESSED;
i f ( New_but t on_ st at e == PRESSED)
{
But t on_ st at e_machi ne = BOUNCI NG;
Debounce_count =0;
}
br eak;
case BOUNCING:
Debounce_count ++;
i f ( Debounce_ count == 3)
{
i f ( New_but t on_ st at e == NOT_PRESSED)
{
But t on_st at e_machi ne = NOT_PRESSED;
Debounce_count = 0;
}
el se
{
But t on_st at e_machi ne = PRESSED;
Debounce_count = 0;
}
}
br eak;
case PRESSED:
but t on_st at e_G = PRESSED;
i f ( New_but t on_ st at e == NOT_PRESSED)
{
But t on_ st at e_machi ne = BOUNCI NG;
Debounce_count = 0;
}
br eak;
default:
But t on_st at e_machi ne = BOUNCI NG;
Debounce_count = 0;
br eak;
}
}
Exercise:
1. Show the calculation of TI M2_PSC = 7999; such that counter is incremented after every millisecond.
___________________________________________________________
___________________________________________________________
2. How are Del ay_St ar t ( ) and Del ay_Wai t ( ) functions used to implement a sandwich timer?
___________________________________________________________
___________________________________________________________
3. What are the three states of a push button?
___________________________________________________________
___________________________________________________________
4. Draw state diagram for the push button as shown in Switch_Task().
___________________________________________________________
___________________________________________________________
5. Write three benefits of Timed Cyclic Executive software architecture over a simple super-loop?
___________________________________________________________
___________________________________________________________
___________________________________________________________
6. What are hazards of using f or ( ) loop or whi l e( ) loop inside a periodic task()?
___________________________________________________________
___________________________________________________________
___________________________________________________________
Lab 10: Multi-States Machine
OBJECTIVE:
To Implement a Multi-States Machine Using Timed Cyclic Executive.
MATERIAL:
uVision4 IDE, GCC compiler, Debugger, ST-Link utility, STM32 board, USB cable.
STEP No. 1.Create a new project Lab10 in folder Lab10. Use the following blocks of code in your main.c file.
voi d t ask_l ed( )
{
. . .
i f ( ol d_but t on_st at e == NOT_PRESSED && but t on_st at e_G == PRESSED)
{
but t on_pr ess_count ++;
count =0;
}
ol d_but t on_st at e = but t on_st at e_G;
i f ( ++count >= 500)
{
count = 500;
but t on_pr ess_count = 0;
}
i f ( but t on_pr ess_count == 3)
{
t oggl e_l ed( ) ;
but t on_pr ess_count =0;
count =0;
}
}
STEP No. 2.Complete the main.c file yourself. Your main() function and task_switch() will be same as in Lab No. 9. Use the
above block of code for task_led() such that if button is pressed three times then the LED toggles its state with timeout of 5
seconds between two successive presses.
STEP No. 3.Build the project and download Lab10.hex file on STM32 board.
Exercise:
1. Which peripherals are connected on APB1 and APB2?
___________________________________________________________
___________________________________________________________
2. Draw state diagram of task_led() of your program.
___________________________________________________________
___________________________________________________________
3. What is a multi-state machine?
___________________________________________________________
___________________________________________________________
4. Explain how does following if() condition works in task_led()?
i f ( ol d_but t on_st at e == NOT_PRESSED && but t on_st at e_G == PRESSED)
___________________________________________________________
___________________________________________________________
___________________________________________________________
5. What are the correct definitions and initializations of following variables in task_led()? Explain with reason.
ol d_but t on_st at e, but t on_pr ess_count .
___________________________________________________________
___________________________________________________________
6. Draw timing diagram to show button presses, LED toggle and timeouts.
___________________________________________________________
___________________________________________________________
___________________________________________________________
Lab 11: Multi-Rate Cyclic Executive
OBJECTIVE:
To implement a Multirate Cyclic Executive.
MATERIAL:
uVision4 IDE, GCC compiler, Debugger, ST-Link utility, STM32 board, USB cable.
STEP No. 1.Create a new project Lab11 in folder Lab11. Use the following blocks of code in your main.c file.
STEP No. 2.Complete the main.c file yourself assuming that a switch is connected to port A pin 0 (active high) and four
LEDs (active high) are connected on port C pins 3 to 0.
STEP No. 3.Build the project and download Lab11.hex file on STM32 board.
i nt mai n ( voi d)
{
# decl ar e var i abl es.
# Enabl e Ti mer 2 and GPI O A and C per i pher al s i n RCC.
# Conf i gur e Por t C pi ns 0 t o 3 as gener al pur pose push- pul l out put s.
# Conf i gur e Por t A pi n 0 as f l oat i ng i nput .
# Conf i gur e t he t i mer t o gener at e an updat e event ever y 10 ms.
whi l e ( 1)
{
Swi t ch_Updat e( ) ; / / Task A
i f ( Swi t ch _st at us_1_G == 1) / / Task B st ar t
{
i f ( Count _l i mi t ! = 100)
{
Count _l i mi t = 100;
}
el se
{
Count _l i mi t = 200;
}
} / / Task B end
i f ( ++Count >= Count _l i mi t )
{
Count = 0;
GPI OC_ODR &= 0xFFF0; / / Task C st ar t
GPI OC_ODR | = ( Di spl ay_sequence & 0x0F) ;
Di spl ay_sequence = ( Di spl ay_sequence + 1) & 0x0F; / / Task C end
}
# Wai t f or t i mer updat e event .
}
}
voi d Swi t ch_ Updat e( voi d)
{
st at i c char Swi t ch_st at e = SW_STATE_ BOUNCI NG;
st at i c char Debounce_ count = 0;
char Swi t ch_ cur r ent _val ue = GPI OA_I DR & 0x0001;
swi t ch( Swi t ch_st at e)
{
case SW_STATE_PRESSED:
i f ( Swi t ch_st at us_ 2_G ! = 1)
{
Swi t ch_st at us_1_G = 1;
}
el se
{
Swi t ch_st at us_1_G = 0;
}
Swi t ch_st at us_2_G =1;
i f ( Swi t ch_cur r ent _val ue == 0)
{
Swi t ch_st at e = SW_STATE_BOUNCI NG;
Debounce_count = 0;
}
br eak;
case SW_STATE_NOT_PRESSED:
Swi t ch_st at us_1_G = 0;
Swi t ch_st at us_2_G = 0;
i f ( Swi t ch_cur r ent _val ue ! = 0)
{
Swi t ch_st at e = SW_STATE_BOUNCI NG;
Debounce_count = 0;
}
Br eak;
case SW_STATE_BOUNCING:
i f ( ++Debounce_count >= 3)
{
i f ( Swi t ch_cur r ent _val ue == 0)
{
Swi t ch_ st at e =
SW_STATE_NOT_PRESSED;
}
el se
Swi t ch_ st at e =
SW_STATE_PRESSED;
}
}
Br eak;
default:
Swi t ch_ st at e = SW_STATE_BOUNCI NG;
Br eak;
}
}
Exercise:
1. What is the behavior of Swi t ch_st at e_1_G and Swi t ch_st at e_2_G ? Explain with timeline diagram.
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
2. What is thedisplay sequence on the four LEDs on port C pins 3 to 0? Does this sequence repeat?
___________________________________________________________
___________________________________________________________
___________________________________________________________
3. What is the effect of switch presses on the display sequence?
___________________________________________________________
___________________________________________________________
Lab 12: LCD Library
OBJECTIVE:
To use LCD Library for HD44780 controller.
MATERIAL:
uVision4 IDE, GCC compiler, Debugger, ST-Link utility, Trainer board, STM32 board, USB cable.
STEP No. 1.Create a new project Lab12 in folder Lab12. Insert the LCD library files (LCD.c, LCD.h), LED library files (LED.c,
LED.h) and main.h file in the folder Lab11 along with the startup.s and linker_script.ld files.
STEP No. 2.Add LCD.c and LED.c files in your Lab12 project along with the main.c and startup.s files.
STEP No. 3.In the main.c file use following block of code to blink an LED at 1 Hz and display text messages on the LCD.
i nt mai n( voi d)
{
ui nt 16_t Count = 0, Count _2 = 0;
LED_I ni t ( ) ;
LCD_I ni t ( ) ;
Cycl i c_St ar t ( 10) ;
whi l e( 1)
{
i f ( ++Count >= 100)
{
Count = 0;
LED_Toggl e( ) ; / / Task A
LCD_Wr i t e_Char _To_Buf f ( ' a' +Count _2, Count _2) ;
i f ( ++Count _2 >= 10)
{
Count _2 = 0;
LCD_Wr i t e_St r _To_Buf f ( " Hel l o : ) " , 16, 8) ;
LCD_Wr i t e_St r _To_Buf f ( " Hel l o embedded wor l d" , 14, 15) ;
}
}
LCD_Updat e( ) ; / / Task B
Cycl i c_Wai t ( ) ;
}
r et ur n( 1) ;
}
STEP No. 4.Build the project and download Lab12.hex file on STM32 board.
STEP No. 5.Insert the STM32 board in the slots on trainer board and close the LCD jumper. Run your program and see its
output on the LCD.
Exercise:
1. Write down the names and brief description of all LCD functions?
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
2. Modify the given code to scroll a message of 20 characters on Line 1 only or Line 2 only.
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
3. Modify the given code to scroll two messages, each 20 characters long, onLine 1 and Line 2 separately.
___________________________________________________________
___________________________________________________________
___________________________________________________________
4. Change the value of LCD_UPDATE_BYTES_PER_UPDATE' from 0 to 32 successively and describe how does it affect
the program execution and display on the LCD.
___________________________________________________________
___________________________________________________________
___________________________________________________________
5. Integrate the code of Lab7 with this lab to display the button press count and button state on LCD.
___________________________________________________________
___________________________________________________________
___________________________________________________________
Lab 13: USART Library
OBJECTIVE:
To Use USART Library for Serial Communication With PC Terminal.
MATERIAL:
uVision4 IDE, GCC compiler, Debugger, ST-Link utility, Trainer board, STM32 board, USB cable, USB-to-serial converter.
STEP No. 1.Create a new project Lab13 in folder Lab13. Insert the USART library files (Serial_lib.c, Serial_lib.h), LED library
files (LED.c, LED.h) and main.h filein the folder Lab13 along with the startup.s and linkerscript.ld files.
STEP No. 2.Add Serial_lib.c and LED.c files in your Lab13 project along with the main.c and startup.s files.
STEP No. 3.In the main.c file use following block of code to perform serial communication with PC terminal.
i nt mai n( voi d)
{
ui nt 16_t Count = 0, Count _2 = 0;
LED_I ni t ( ) ;
Ser i al _I ni t ( 9600) ;
Cycl i c_St ar t ( 1) ;
whi l e( 1)
{
Ser i al _Updat e( ) ;
i f ( Ser i al _Get _Buf f er _St at us( ) > 0)
{
Ser i al _Send_Byt e( Ser i al _Get _Byt e( ) +1) ;
}
i f ( ++Count >= 1000)
{
Count = 0;
LED_Toggl e( ) ;
Ser i al _Send_Byt e( ' a' +Count _2) ;
i f ( ++Count _2 >= 10)
{
Count _2 = 0;
Ser i al _Send_St r i ng( " \ r \ nHel l o : ) " , 16) ;
}
}
Cycl i c_Wai t ( ) ;
}
r et ur n( 1) ;
}
STEP No. 4.Build the project and download Lab13.hex file on STM32 board.
STEP No. 5.Insert the STM32 board in the slots on trainer board. Use USB-to-serial converter cable to connect the trainer
board to PC terminal. Run Terminal software on PC.
STEP No. 6.Run your program on STM board and see it sends and receives text data over serial port with PC terminal.
Exercise:
1. Write down the names and brief description of all Serial Library functions?
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
2. What is baud rate and how is it calculated?
___________________________________________________________
___________________________________________________________
___________________________________________________________
3. Modify the given code to send out time every second in HH:MM format to PC terminal.
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
4. Add time setting feature in your code with following command set. Your STM32 board will receive these commands
from PC terminal.
H : Hour i ncr ement h : Hour decr ement
M : Mi nut e i ncr ement m : Mi nut e decr ement
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
Lab 14: Keypad Matrix
OBJECTIVE:
To Interface a 4x3 Keypad Matrix With STM32.
MATERIAL:
uVision4 IDE, GCC compiler, Debugger, ST-Link utility, Trainer board, STM32 board, USB cable, Keypad matrix 4x3.
STEP No. 1.Create a new project Lab14 in folder Lab14.
STEP No. 2.Use the following algorithm to implement a task for scanning a 4x3 keypad matrix.
Al gor i t hm:
For dr i ven col umns and i nput r ows wi t h
act i ve hi gh l ogi c.
Conf i gur e t he r ow pi ns ( R1, R2, R3 and R4)
and col umn pi ns ( C1, C2 and C3)
Out put pi ns: Push- pul l mode
I nput pi ns: f l oat i ng i nput mode
C1 = 1 ( act i ve) , C2 = C3 = 0
i f ( R1 ! = 0)
But t on 1 i s pr essed
i f ( R2 ! = 0)
But t on 4 i s pr essed
i f ( R3 ! = 0)
But t on 7 i s pr essed
i f ( R4 ! = 0)
But t on 10 i s pr essed
C2 = 1 ( act i ve) , C1 = C3 = 0
i f ( R1 ! = 0)
But t on 2 i s pr essed
i f ( R2 ! = 0)
But t on 5 i s pr essed
i f ( R3 ! = 0)
But t on 8 i s pr essed
i f ( R4 ! = 0)
But t on 11 i s pr essed
C3 = 1 ( act i ve) , C1 = C2 = 0
i f ( R1 ! = 0)
But t on 3 i s pr essed
i f ( R2 ! = 0)
But t on 6 i s pr essed
i f ( R3 ! = 0)
But t on 9 i s pr essed
i f ( R4 ! = 0)
But t on 12 i s pr essed
}
STEP No. 3.Complete the main.c file to read single digit keypad inputs.
STEP No. 4.Blink an LED for as many times as the numeric input received from keypad.
STEP No. 5.Build the project and download Lab14.hex file on STM32 board.
Exercise:
1. How will the pin configuration and the algorithm be changed so that active low signals are applied to the output
group?
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
2. How will the algorithm be changed if rows are the output group and columns are the input group?
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
3. Do you need to de-bounce the keypad input? Give reason for your answer.
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
Lab 15: ADC
OBJECTIVE:
To Configure and Use an ADC.
MATERIAL:
uVision4 IDE, GCC compiler, Debugger, ST-Link utility, Trainer board, STM32 board, USB cable, variable analog source.
STEP No. 1.Create a new project Lab15 in folder Lab15.
STEP No. 2.Use the following configuration to run ADC peripheral.
ui nt 32_t ADC_t i meout = 0;
ui nt 16_t ADC_dat a = 0;
RCC_APB2ENR | = 0x00000200;
ADC_CR2 = 0x00CE6800;
ADC_CR2 | = 0x00000001;
ADC_SMPR1 = 0x00D80000;
ADC_SMPR2 = 0x00000007;
ADC_SQR3 = 0x00000000; / / ch 0
/ / ADC_SQR3 = 0x00000010; / / ch 16 conver si on
ADC_CR2 | = 0x00000001;
whi l e( ( ++ADC_t i meout ) && ( ( ADC_SR & 0x0002) == 0) ) ; / / wai t f or end of conver si on
ADC_dat a = ADC_DR;
STEP No. 3.Complete the main.c file to read analog input signal. Use polling based scanning algorithm to read ADC values.
STEP No. 4.Blink an LED in 1 Hz-to-10 Hz range mapped onto the 0-to-full-scale reading of ADC input channel.
STEP No. 5.Build the project and download Lab15.hex file on STM32 board.
Exercise:
1. What is the resolution of ADC used in this lab? Explain in detail.
___________________________________________________________
___________________________________________________________
___________________________________________________________
2. What is the sampling frequency of ADC used in this lab? Explain in detail.
___________________________________________________________
___________________________________________________________
___________________________________________________________
3. What is the conversion time of ADC used in this lab? Explain in detail.
___________________________________________________________
___________________________________________________________
___________________________________________________________
4. In which operating mode ADC is used in this lab?
___________________________________________________________
5. Describe in steps the polling based scanning algorithm for ADC.
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
Lab 16: Project Demo & Evaluation
OBJECTIVE:
To Demonstrate Your Course Project. Evaluation of Project.
MATERIAL:
uVision4 IDE, GCC compiler, Debugger, ST-Link utility, Trainer board, STM32 board, USB cable, project items.
STEP No. 1.Run your project and demonstrate its required features.
STEP No. 2.Show your algorithms and simulation results in detail.
STEP No. 3.Project viva.

S-ar putea să vă placă și