Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 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. // Global Variables
  29. unsigned long SW1;
  30.  
  31. // basic functions defined at end of startup.s
  32. void DisableInterrupts(void); // Disable interrupts
  33. void EnableInterrupts(void);  // Enable interrupts
  34. void Delay100ms(unsigned long time);
  35. void PortF_Init(void);
  36.  
  37. int main(void){ unsigned long volatile delay;
  38.   TExaS_Init(SW_PIN_PF4, LED_PIN_PF2);  // activate grader and set system clock to 80 MHz
  39.   // initialization goes here
  40.     PortF_Init();        // Call initialization of port PF2
  41.    
  42.   EnableInterrupts();           // enable interrupts for the grader
  43.     GPIO_PORTF_DATA_R = 0x04;
  44.   while(1){
  45.         SW1 = GPIO_PORTF_DATA_R&0x10;
  46.     // body goes
  47.         Delay100ms(1);
  48.         if(SW1)
  49.         {
  50.             GPIO_PORTF_DATA_R ^= 0x04;
  51.         }
  52.         else
  53.         {
  54.             GPIO_PORTF_DATA_R = 0x04;
  55.         }
  56.   }
  57. }
  58. void Delay100ms(unsigned long time){
  59.   unsigned long i;
  60.   while(time > 0){
  61.     i = 1333333;  // this number means 100ms
  62.     while(i > 0){
  63.       i = i - 1;
  64.     }
  65.     time = time - 1; // decrements every 100 ms
  66.   }
  67. }
  68. void PortF_Init(void){ volatile unsigned long delay;
  69.   SYSCTL_RCGC2_R |= 0x00000020;     // 1) F clock
  70.   delay = SYSCTL_RCGC2_R;           // delay    
  71.   GPIO_PORTF_AMSEL_R = 0x00;        // 3) disable analog function
  72.   GPIO_PORTF_PCTL_R = 0x00000000;   // 4) GPIO clear bit PCTL  
  73.   GPIO_PORTF_DIR_R = 0x0E;          // 5) PF4,PF0 input, PF3,PF2,PF1 output  
  74.   GPIO_PORTF_AFSEL_R = 0x00;        // 6) no alternate function
  75.   GPIO_PORTF_PUR_R = 0x11;          // enable pullup resistors on PF4,PF0      
  76.   GPIO_PORTF_DEN_R = 0x1F;          // 7) enable digital pins PF4-PF0        
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement