Advertisement
Guest User

Untitled

a guest
Oct 17th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.04 KB | None | 0 0
  1. // BranchingFunctionsDelays.c Lab 6
  2. // Runs on LM4F120/TM4C123
  3. // Use simple programming structures in C to
  4. // toggle an LED while a button is pressed and
  5. // turn the LED on when the button is released.  
  6. // This lab will use the hardware already built into the LaunchPad.
  7. // Daniel Valvano, Jonathan Valvano
  8. // January 15, 2016
  9.  
  10. // built-in connection: PF0 connected to negative logic momentary switch, SW2
  11. // built-in connection: PF1 connected to red LED
  12. // built-in connection: PF2 connected to blue LED
  13. // built-in connection: PF3 connected to green LED
  14. // built-in connection: PF4 connected to negative logic momentary switch, SW1
  15.  
  16. #include "TExaS.h"
  17.  
  18. #define GPIO_PORTF_DATA_R       (*((volatile unsigned long *)0x400253FC))
  19. #define GPIO_PORTF_DIR_R        (*((volatile unsigned long *)0x40025400))
  20. #define GPIO_PORTF_AFSEL_R      (*((volatile unsigned long *)0x40025420))
  21. #define GPIO_PORTF_PUR_R        (*((volatile unsigned long *)0x40025510))
  22. #define GPIO_PORTF_DEN_R        (*((volatile unsigned long *)0x4002551C))
  23. #define GPIO_PORTF_AMSEL_R      (*((volatile unsigned long *)0x40025528))
  24. #define GPIO_PORTF_PCTL_R       (*((volatile unsigned long *)0x4002552C))
  25. #define SYSCTL_RCGC2_R          (*((volatile unsigned long *)0x400FE108))
  26. #define SYSCTL_RCGC2_GPIOF      0x00000020  // port F Clock Gating Control
  27.  
  28. // basic functions defined at end of startup.s
  29. void DisableInterrupts(void); // Disable interrupts
  30. void EnableInterrupts(void);  // Enable interrupts
  31. void portF_init(void);
  32. void delay100ms(unsigned long time);
  33.  
  34.  
  35. int main(void)
  36. {
  37.         unsigned long volatile delay;
  38.       // activate grader and set system clock to 80 MHz
  39.         TExaS_Init(SW_PIN_PF4, LED_PIN_PF2);  
  40.         portF_init();
  41.         EnableInterrupts();      
  42.         // set PF2
  43.         GPIO_PORTF_DATA_R |= 0x04;
  44.         while(1)
  45.         {
  46.                 delay100ms(1);
  47.               // if switch PF4 is pressed and LED is ON (00000101)
  48.             if( GPIO_PORTF_DATA_R == 0x05)
  49.                 {
  50.                       // turn LED OFF (clear bit)
  51.                       GPIO_PORTF_DATA_R &= ~0x04;
  52.                 }      
  53.                 // if switch PF4 is pressed and LED is OFF (00000001)
  54.                 else if (GPIO_PORTF_DATA_R == 0x01)
  55.                 {
  56.                         // set PF2 - turn LED ON
  57.                       GPIO_PORTF_DATA_R |= 0x04;
  58.                 }
  59.                 else
  60.                 {
  61.                         // set PF2
  62.                         GPIO_PORTF_DATA_R |= 0x04; 
  63.                 }
  64.         }
  65. }
  66.  
  67. void portF_init(void)
  68. {
  69.         volatile unsigned long delay;
  70.         SYSCTL_RCGC2_R |= 0x00000020;     // 1) F clock
  71.         delay = SYSCTL_RCGC2_R;           // delay  
  72.         GPIO_PORTF_AMSEL_R = 0x00;        // 3) disable analog function
  73.         GPIO_PORTF_PCTL_R = 0x00000000;   // 4) GPIO clear bit PCTL  
  74.         GPIO_PORTF_DIR_R = 0x04;          // 5) PF4 input, PF2 output  
  75.         GPIO_PORTF_AFSEL_R = 0x00;        // 6) no alternate function
  76.         GPIO_PORTF_PUR_R = 0x01;          // disble pull-up resistor
  77.         GPIO_PORTF_DEN_R = 0x14;          // 7) enable digital pins PF4 & PF2        
  78. }
  79.  
  80. void delay100ms(unsigned long time)
  81. {
  82.         unsigned long i;
  83.         while(time > 0)
  84.         {
  85.                 i = 1333333;  // this number means 100ms
  86.                 while(i > 0)
  87.                 {
  88.                         i = i - 1;
  89.                 }
  90.                 time = time - 1; // decrements every 100 ms
  91.       }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement