Led and button with Cortex M3, LM4F120 Microcontroller

This is simple program made on LM4f123 launch pad of texas instruments. One button and a led is interfaced with PORT E pin 0 and Port E pin 1 respectively. Led is initially on, when button is pressed, it will change its state or toggle with a delay of approximately 100ms.

Debugging:

Logic Analyzer: 


Register Window in KEIL




Following is algorithm of the program..

1) Initializes registers of ARM
2) LED is initially on
3) Check if button is on or not
    if button is on, toggle led with 100ms delay
   else nothing

Downloads


Delay function is a approximation and made by knowing the time a command takes to execute. So it is not precise.  It will take 13333 cycles of while loop for almost 1ms. This fact is used in this program.


Code:

  1. // ***** 0. Documentation Section *****
  2. // SwitchLEDInterface.c
  3. // Runs on LM4F120/TM4C123
  4. // Use simple programming structures in C to toggle an LED
  5. // while a button is pressed and turn the LED on when the
  6. //      Date:    Feb 11, 2016
  7. //      Student: Muhammad Awais 2012-EE-506
  8. //      Contact Info: Fb/awais12506
  9. //      Class:   UTAustinX: UT.6.03x Embedded Systems - Shape the World
  10. // ***** 1. Pre-processor Directives Section *****
  11. #include "tm4c123gh6pm.h"
  12. //Bit specfic adressing for PE0 and PE1
  13. //base adress of PORTE is 0x40024000
  14. #define SW_PE0   (*((volatile unsigned long *)0x40024004))
  15. #define LED_PE1   (*((volatile unsigned long *)0x40024008))
  16. // ***** 2. Global Declarations Section *****
  17. // FUNCTION PROTOTYPES: Each subroutine defined
  18. void PORTE_Init(void);
  19. void Delay1ms(unsigned long msec);
  20. // ***** 3. Subroutines Section *****
  21. //GPIO_PORTE_DATA_R
  22. // PE0, PB0, or PA2 connected to positive logic momentary switch using 10k ohm pull down resistor
  23. // PE1, PB1, or PA3 connected to positive logic LED through 470 ohm current limiting resistor
  24. int main(void){
  25. //**********************************************************************
  26. // The following version tests input on PE0 and output on PE1
  27. //**********************************************************************
  28.     PORTE_Init();      // Initlizes PORTE


  29.     LED_PE1=0x02;      //On LED connected at PE1
  30.     Delay1ms(100);     // Delay 100ms
  31.   while(1)           //Infinte loop
  32.         {    
  33.    
  34.         if(SW_PE0==0x01)  //If switch is presses
  35.         {
  36.         LED_PE1^=0x02;    //Toggle LED
  37.             Delay1ms(100);
  38.         }
  39.         else               //else Just on it
  40.             LED_PE1=0x02;
  41.        
  42.   }
  43. }
  44. /*
  45. Descrption: Initilizes PE0==>Input PE1==> Output
  46. Input: None
  47. Output: None
  48. */
  49. void PORTE_Init(void)
  50. {
  51.   volatile unsigned long delay;
  52.   SYSCTL_RCGC2_R |= 0x00000010;      // 1) E clock
  53.   delay = SYSCTL_RCGC2_R;            // delay to allow clock to stabilize    
  54.   GPIO_PORTE_AMSEL_R &= 0x00;        // 2) disable analog function
  55.   GPIO_PORTE_PCTL_R &= 0x00000000;   // 3) GPIO clear bit PCTL  
  56.   GPIO_PORTE_DIR_R &= ~0x01;         // 4.1) PE0 input,
  57.   GPIO_PORTE_DIR_R |= 0x02;          // 4.2) PE1 output  
  58.   GPIO_PORTE_AFSEL_R &= 0x00;        // 5) no alternate function      
  59.   GPIO_PORTE_DEN_R |= 0x03;          // 6) enable digital pins PE4-PE1
  60. }
  61. /*
  62. Descrption: Uses time of a instruction to delay time
  63. Almost 1ms for each cycle
  64. Input: Number of ms to delay
  65. Output: None
  66. */
  67. void Delay1ms(unsigned long msec){
  68. // write this function
  69.     unsigned long i;
  70.    
  71.   while(msec > 0){
  72.     i = 13333;  // this number means 1ms
  73.     while(> 0){
  74.       i = i - 1;
  75.     }
  76.     msec = msec - 1; // decrements every 1 ms
  77.   }
  78. }

Practical Implementation :

No comments:

Post a Comment

Thank you for your response.