embedded system lab manual fall-2012

Upload: nabeelasim

Post on 13-Oct-2015

48 views

Category:

Documents


4 download

DESCRIPTION

this is embedded manual for free

TRANSCRIPT

  • THE UNIVERSITY OF LAHORE

    Microprocessor basedEmbedded System

    Lab ManualRevised: 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 Executive using Timer 198. 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. Using LCD Library for HD44780 controller 3313. Using USART 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.exeDefault path: C:\Keil\UV4

    2. Sourcery GCC C CompilerFilename: arm-2010q1-188-arm-none-eabi.exeDefault path: C:\Program Files\CodeSourcery\Sourcery G++ Lite

    3. STM ST-Link utility Filename: STM32 ST-LINK Utility_v2.3.0.exeDefault 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.

    #define RCC_APB2ENR ( * ((volatile unsigned long*) 0x40021018))#define GPIOC_CRH ( * ((volatile unsigned long*) 0x40011004))#define GPIOC_BSRR ( * ((volatile unsigned long*) 0x40011010))

    void Delay(void){

    volatile unsigned short i;for (i = 0; i < 650000; i++){}

    }

    void SystemInit(void){}

    int main(void){

    RCC_APB2ENR |= 0x00000010; //enable clock to GPIO port CGPIOC_CRH &= 0xFFFFFFF0; //clear previous config for port C pin 8GPIOC_CRH |= 0x00000001; //config port C pin 8 as 10Mhz push pull

    //general purpose output.while(1){

    GPIOC_BSRR = 0x00000100; //set pin highDelay();GPIOC_BSRR = 0x01000000; //set pin lowDelay();

    }return(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.

    void Delay(void){

    volatile unsigned int i;for (i = 0; i < 650000; i++)

    {}

    }

    int main(void){

    . . .

    while(1){

    GPIOC_BSRR = 0x00000100; //set pin highDelay();GPIOC_BSRR = 0x01000000; //set pin lowDelay();

    }return(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.

    int main(void){

    unsigned short Port_data;RCC_APB2ENR |= 0x00000014; //enable clock to GPIO port A and CGPIOA_CRH = 0x44444444; //all pins to floating inputsGPIOA_CRL = 0x44444444;GPIOC_CRH = 0x11111111; //all pins to 10MHz push-pull outputsGPIOC_CRL = 0x11111111;while(1){

    Port_data = GPIOA_IDR;if ((Port_data & 0x00000001)==1)

    GPIOC_ODR = 0x00000300;else

    GPIOC_ODR = 0;}return(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 connected to 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.

    //Switch connected to GPIO A pin 0 and is active highunsigned char Switch_count = 0;...

    void Toggle_Led(){static unsigned char Led_status=0;

    if (Led_status==0){GPIOC_ODR=0x100;Led_status=1;

    }else{GPIOC_ODR=0;Led_status=0;

    }}

    ...

    //Block of code inside main() to count button pressesif ((GPIOA_IDR & 0x00000001) != 0){

    Debounce_Delay( ); //software delay equal to the debounce periodif ((GPIOA_IDR & 0x00000001) != 0){

    Switch_count++;while ((GPIOA_IDR & 0x00000001) != 0){}Debounce_Delay( );

    }}

    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-bouncingc. 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.

    //Configure and start the timervoid Delay_Start(unsigned int Timer_delay_ms){

    TIM2_CR1 = 0; //Stop timer TIM2_CNT = 0; //reset the timer counterTIM2_PSC = 7999; //counter is incremented every msTIM2_ARR = Timer_delay_ms; //setup to count till 65535TIM2_CR1 = 1; //start timer in one pulse mode

    }

    //Wait for the set time to passvoid Delay_Wait(){

    while ((TIM2_SR & 0x00000001)==0); //wait for update interrupt flagTIM2_SR = 0; //clear the update interrupt flag

    }

    //Block of code for using inside main() functionCount++;if ((GPIOA_IDR & 0x1)!=0){

    if (Count>=100){

    Toggle_Led();Count=0;

    }}else{

    if (Count>=200){

    Toggle_Led();Count=0;

    }}

    STEP No. 2. Complete the main.c file yourself. 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.

  • Delay_Start(10);while(1){

    count++;task_switch();task_led();Delay_Wait();

    }

    STEP No. 5. Implement a periodic task for reading button input as a state machine using switch-case construct. Do not use for() or while() loops inside tasks to avoid blocking code. Sample code for Switch_Task() is given below.

    #define NOT_PRESSED 1#define PRESSED 2#define BOUNCING 3#define ACTIVE_HIGH 1#define ACTIVE_LOW 0

    unsigned char Button_Update(){if((GPIOA_IDR & 0x1)==ACTIVE_HIGH){return PRESSED;

    }else{return NOT_PRESSED;

    }}

    void Switch_task(void)

    {

    ...

    New_button_state = Button_Update();switch(Button_state_machine){case NOT_PRESSED:

    button_state_G = NOT_PRESSED;if (New_button_state == PRESSED){

    Button_state_machine = BOUNCING;Debounce_count=0;

    }break;

    case BOUNCING:Debounce_count++;if(Debounce_count == 3){

    if(New_button_state == NOT_PRESSED){Button_state_machine = NOT_PRESSED;Debounce_count = 0;}else{Button_state_machine = PRESSED;Debounce_count = 0;}

    }break;

    case PRESSED:button_state_G = PRESSED;if (New_button_state == NOT_PRESSED){

    Button_state_machine = BOUNCING;Debounce_count = 0;

    }break;

    default:Button_state_machine = BOUNCING;Debounce_count = 0;break;

    }}

  • Exercise:

    1. Show the calculation of TIM2_PSC = 7999; such that counter is incremented after every millisecond.

    ___________________________________________________________

    ___________________________________________________________

    2. How are Delay_Start() and Delay_Wait() 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 for() loop or while() 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 2nd 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

    DescriptionThe 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 Connections3 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 procedureThe 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 entered and product is in stock, vend the item according to the table below.

    Item 1 Item 4Output Pulse start Pulse width Output Pulse start Pulse widthA 0 1 A 0 1B 0 3.2 B 3 1C 5 1 C 5 1

    Item 2 Item 5Output Pulse start Pulse width Output Pulse start Pulse widthA 0 1.5 A 0 1.5B 0 3.2 B 3 2C 5 1 C 5 1

    Item 3 Item 6Output Pulse start Pulse width Output Pulse start Pulse widthA 0 2 A 0 2B 0 3.2 B 3 3C 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

    DescriptionThe 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 connections4 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 procedureThe 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 Designb. Hardware Details of Motors, Sensors and Circuit Diagramsc. 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.

    //Configure and start the timervoid Delay_Start(unsigned int Timer_delay_ms){

    TIM2_CR1 = 0; //Stop timer TIM2_CNT = 0; //reset the timer counterTIM2_PSC = 7999; //counter is incremented every msTIM2_ARR = Timer_delay_ms; //setup to count till 65535TIM2_CR1 = 1; //start timer in one pulse mode

    }

    //Wait for the set time to passvoid Delay_Wait(){

    while ((TIM2_SR & 0x00000001)==0); //wait for update interrupt flagTIM2_SR = 0; //clear the update interrupt flag

    }

    //Block of code for using inside main() functionCount++;if ((GPIOA_IDR & 0x1)!=0){

    if (Count>=100){

    Toggle_Led();Count=0;

    }}else{

    if (Count>=200){

    Toggle_Led();Count=0;

    }}

    STEP No. 2. Complete the main.c file yourself. 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.

  • Delay_Start(10);while(1){

    count++;task_switch();task_led();Delay_Wait();

    }

    STEP No. 5. Implement a periodic task for reading button input as a state machine using switch-case construct. Do not use for() or while() loops inside tasks to avoid blocking code. Sample code for Switch_Task() is given below.

    #define NOT_PRESSED 1#define PRESSED 2#define BOUNCING 3#define ACTIVE_HIGH 1#define ACTIVE_LOW 0

    unsigned char Button_Update(){if((GPIOA_IDR & 0x1)==ACTIVE_HIGH){return PRESSED;

    }else{return NOT_PRESSED;

    }}

    void Switch_task(void)

    {

    ...

    New_button_state = Button_Update();switch(Button_state_machine){case NOT_PRESSED:

    button_state_G = NOT_PRESSED;if (New_button_state == PRESSED){

    Button_state_machine = BOUNCING;Debounce_count=0;

    }break;

    case BOUNCING:Debounce_count++;if(Debounce_count == 3){

    if(New_button_state == NOT_PRESSED){Button_state_machine = NOT_PRESSED;Debounce_count = 0;}else{Button_state_machine = PRESSED;Debounce_count = 0;}

    }break;

    case PRESSED:button_state_G = PRESSED;if (New_button_state == NOT_PRESSED){

    Button_state_machine = BOUNCING;Debounce_count = 0;

    }break;

    default:Button_state_machine = BOUNCING;Debounce_count = 0;break;

    }}

  • Exercise:

    1. Show the calculation of TIM2_PSC = 7999; such that counter is incremented after every millisecond.

    ___________________________________________________________

    ___________________________________________________________

    2. How are Delay_Start() and Delay_Wait() 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 for() loop or while() 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.

    void task_led(){

    ...

    if (old_button_state == NOT_PRESSED && button_state_G == PRESSED){

    button_press_count++;count=0;

    }

    old_button_state = button_state_G;

    if (++count >= 500){

    count = 500;button_press_count = 0;

    }

    if (button_press_count == 3){

    toggle_led();button_press_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()?if (old_button_state == NOT_PRESSED && button_state_G == PRESSED)

    ___________________________________________________________

    ___________________________________________________________

    ___________________________________________________________

    5. What are the correct definitions and initializations of following variables in task_led()? Explain with reason.old_button_state, button_press_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.

    int main (void){

    # declare variables.# Enable Timer 2 and GPIO A and C peripherals in RCC.# Configure Port C pins 0 to 3 as general purpose push-pull outputs.# Configure Port A pin 0 as floating input.# Configure the timer to generate an update event every 10 ms.

    while (1){

    Switch_Update(); // Task A

    if (Switch _status_1_G == 1) // Task B start{

    if (Count_limit != 100){

    Count_limit = 100;}else{

    Count_limit = 200;}

    } // Task B end

    if (++Count >= Count_limit){

    Count = 0;

    GPIOC_ODR &= 0xFFF0; // Task C startGPIOC_ODR |= (Display_sequence & 0x0F);

    Display_sequence = (Display_sequence + 1) & 0x0F; // Task C end }

    # Wait for timer update event.}}

  • void Switch_Update(void){

    static char Switch_state = SW_STATE_BOUNCING;static char Debounce_count = 0;char Switch_current_value = GPIOA_IDR & 0x0001;

    switch(Switch_state){

    case SW_STATE_PRESSED:if (Switch_status_2_G != 1){

    Switch_status_1_G = 1;}else{

    Switch_status_1_G = 0;}Switch_status_2_G =1;if (Switch_current_value == 0){

    Switch_state = SW_STATE_BOUNCING;Debounce_count = 0;

    }break;

    case SW_STATE_NOT_PRESSED:Switch_status_1_G = 0;Switch_status_2_G = 0;

    if (Switch_current_value != 0){

    Switch_state = SW_STATE_BOUNCING;Debounce_count = 0;

    }Break;

    case SW_STATE_BOUNCING:if (++Debounce_count >= 3){

    if (Switch_current_value == 0){

    Switch_state = SW_STATE_NOT_PRESSED;

    }else

    Switch_state = SW_STATE_PRESSED;

    }}Break;

    default:Switch_state = SW_STATE_BOUNCING;Break;

    }}

    Exercise:

    1. What is the behavior of Switch_state_1_G and Switch_state_2_G? Explain with timeline diagram.___________________________________________________________

    ___________________________________________________________

    ___________________________________________________________

    ___________________________________________________________

    2. What is the display 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.

    int main(void){

    uint16_t Count = 0, Count_2 = 0;LED_Init();LCD_Init();Cyclic_Start(10);while(1){

    if(++Count >= 100){

    Count = 0;LED_Toggle(); //Task ALCD_Write_Char_To_Buff('a'+Count_2, Count_2);if(++Count_2 >= 10){

    Count_2 = 0;LCD_Write_Str_To_Buff("Hello :)", 16, 8);LCD_Write_Str_To_Buff("Hello embedded world", 14, 15);

    }}LCD_Update(); //Task BCyclic_Wait();

    }return(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, on Line 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 file in 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.

    int main(void){

    uint16_t Count = 0, Count_2 = 0;LED_Init();Serial_Init(9600);Cyclic_Start(1);while(1){

    Serial_Update();if(Serial_Get_Buffer_Status() > 0){

    Serial_Send_Byte(Serial_Get_Byte()+1);}

    if(++Count >= 1000){

    Count = 0;LED_Toggle();Serial_Send_Byte('a'+Count_2);if(++Count_2 >= 10){

    Count_2 = 0;Serial_Send_String("\r\nHello :)", 16);

    }}Cyclic_Wait();

    }return(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 increment h : Hour decrementM : Minute increment m : Minute decrement

    ___________________________________________________________

    ___________________________________________________________

    ___________________________________________________________

    ___________________________________________________________

  • 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.

    Algorithm:

    For driven columns and input rows with active high logic.

    Configure the row pins (R1, R2, R3 and R4) and column pins (C1, C2 and C3)

    Output pins: Push-pull modeInput pins: floating input mode

    C1 = 1 (active), C2 = C3 = 0if (R1 != 0)

    Button 1 is pressedif (R2 != 0)

    Button 4 is pressedif (R3 != 0)

    Button 7 is pressedif (R4 != 0)

    Button 10 is pressed

    C2 = 1 (active), C1 = C3 = 0if (R1 != 0)

    Button 2 is pressedif (R2 != 0)

    Button 5 is pressedif (R3 != 0)

    Button 8 is pressedif (R4 != 0)

    Button 11 is pressed

    C3 = 1 (active), C1 = C2 = 0if (R1 != 0)

    Button 3 is pressedif (R2 != 0)

    Button 6 is pressedif (R3 != 0)

    Button 9 is pressedif (R4 != 0)

    Button 12 is pressed

    }

    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.

    uint32_t ADC_timeout = 0;uint16_t ADC_data = 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 conversionADC_CR2 |= 0x00000001;while((++ADC_timeout) && ((ADC_SR & 0x0002) == 0)); //wait for end of conversionADC_data = 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.