Advertisement
Guest User

microcontroller_backup5

a guest
Oct 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. // PIO enable register
  2. #define PIOB_PER (volatile unsigned int *) 0xFFFFF400 // B - left LED
  3. #define PIOC_PER (volatile unsigned int *) 0xFFFFF600 // C - right LED
  4.  
  5. // output disable register
  6. #define PIOC_ODR (volatile unsigned int *) 0xFFFFF614 // for both buttons
  7.  
  8. // output data status register
  9. #define PIOB_ODSR (volatile unsigned int *) 0xFFFFF438 // for left LED
  10.  
  11. // output enable register
  12. #define PIOB_OER (volatile unsigned int *) 0xFFFFF410 // B - left LED
  13. #define PIOC_OER (volatile unsigned int *) 0xFFFFF610 // C - right LED
  14.  
  15. // set/clear for PB
  16. #define PIOB_SODR (volatile unsigned int *) 0xFFFFF430
  17. #define PIOB_CODR (volatile unsigned int *) 0xFFFFF434
  18.  
  19. // set/clear for PC
  20. #define PIOC_SODR (volatile unsigned int *) 0xFFFFF630
  21. #define PIOC_CODR (volatile unsigned int *) 0xFFFFF634
  22.  
  23. // pull-up enabled/disabled
  24. #define PIOC_PUER (volatile unsigned int *) 0xFFFFF664 // for buttons: BP1 and BP2
  25. #define PIOC_PUDR (volatile unsigned int *) 0xFFFFF660 // for buttons: BP1 and BP2
  26.  
  27. // pull-up status
  28. #define PIOC_PDSR (volatile unsigned int *) 0xFFFFF63C
  29.  
  30. // clock
  31. #define PMC_PCER (volatile unsigned int *) 0xFFFFF63C
  32.  
  33. #define LEFT_BUTTON (1 << 5)
  34. #define RIGHT_BUTTON (1 << 4)
  35.  
  36. #define LEFT_LED (1 << 8)
  37. #define RIGHT_LED (1 << 29)
  38.  
  39. #define CLOCK (1 << 4)
  40.  
  41. void dbgu_print_ascii(const char *a) {}
  42.  
  43.  
  44. void delay(int how_long) {
  45.     volatile unsigned int counter = how_long;
  46.     while(counter > 0)
  47.         --counter;
  48. }
  49.  
  50. void toggle() {
  51.     if (!(*PIOB_ODSR & LEFT_LED)) {
  52.         *PIOB_SODR = LEFT_LED;
  53.     } else {
  54.         *PIOB_CODR = LEFT_LED ;
  55.     }
  56. }
  57.  
  58. int isLeftButtonClicked() {
  59.     if (!(*PIOC_PDSR & LEFT_BUTTON))
  60.         return 1;
  61.     return 0;
  62. }
  63.  
  64. int isRightButtonClicked() {
  65.     if (!(*PIOC_PDSR & RIGHT_BUTTON))
  66.         return 1;
  67.     return 0;
  68. }
  69.  
  70. void setOutputLED1() {
  71.     *PIOB_PER = LEFT_LED;
  72.     *PIOB_OER = LEFT_LED;
  73.     *PIOB_SODR = LEFT_LED; // turn off the LED1
  74. }
  75.  
  76. void setOutputLED2() {
  77.     *PIOC_PER = RIGHT_LED;
  78.     *PIOC_OER = RIGHT_LED;
  79.     *PIOB_SODR = RIGHT_LED; // turn off the LED2
  80. }
  81.  
  82. void setInputBP1() {
  83.     *PIOC_PER = LEFT_BUTTON;
  84.     *PIOC_ODR = LEFT_BUTTON;
  85.     *PIOC_PUER = LEFT_BUTTON;
  86.     *PMC_PCER = CLOCK;
  87. }
  88.  
  89. void setInputBP2() {
  90.     *PIOC_PER = RIGHT_BUTTON;
  91.     *PIOC_ODR = RIGHT_BUTTON;
  92.     *PIOC_PUER = RIGHT_BUTTON;
  93.     *PMC_PCER = CLOCK;
  94. }
  95.  
  96. void configureIO() {
  97.     setOutputLED1();
  98.     setOutputLED2();
  99.     setInputBP1();
  100.     setInputBP2();
  101. }
  102.  
  103. int main() {
  104.     configureIO();
  105.  
  106.     int forCounter;
  107.  
  108.     while(1){
  109.         toggle();
  110.         for (forCounter = 0; forCounter < 100; ++forCounter) {
  111.             if (isLeftButtonClicked()) {
  112.                 *PIOC_CODR = RIGHT_LED;
  113.             } else if (isRightButtonClicked()) {
  114.                 *PIOC_SODR = RIGHT_LED;
  115.             }
  116.             delay(10000);
  117.         }
  118.     }
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement