Advertisement
Mary_99

interrupts copy pit

Jan 2nd, 2021 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.96 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include "include/AT91SAM9263.h"
  4.  
  5. #define MCK 100000000
  6. #define PIV (MCK / (16 * 1000)) - 1
  7. #define DELAY 1000
  8. #define DELAY_STEP DELAY / 10
  9.  
  10. #define B1 AT91C_PIO_PC5 //left  click
  11. #define B2 AT91C_PIO_PC4// right click
  12. #define PID4 1 << 4
  13.  
  14. unsigned int TimerCounter;
  15. bool delayFlag;
  16.  
  17. void pitInit (void)
  18. {
  19.   volatile unsigned int read;
  20.   AT91C_BASE_PITC->PITC_PIMR = PIV; //init PIV //disable timer //disable interupts
  21.   read = AT91C_BASE_PITC->PITC_PIVR ; //to read and clear
  22. }
  23.  
  24. void timerInterruptHandler()
  25. {
  26.   volatile unsigned int read;
  27.   if (AT91C_BASE_PITC->PITC_PIMR & AT91C_PITC_PITIEN)
  28.   {
  29.     if (AT91C_BASE_PITC->PITC_PISR ) // 1 ms
  30.     {
  31.       read = AT91C_BASE_PITC->PITC_PIVR ;  //clerars PITS in PIT_SR
  32.       TimerCounter++;
  33.       delayFlag = true;
  34.     }
  35.   }
  36.   AT91C_BASE_AIC->AIC_EOICR = 0;
  37. }
  38.  
  39. void pitInitInterrupts()
  40. {
  41.     AT91C_BASE_AIC->AIC_IDCR = (1 << AT91C_ID_SYS);
  42.     AT91C_BASE_AIC->AIC_SVR[AT91C_ID_SYS] = (unsigned int) timerInterruptHandler;
  43.     AT91C_BASE_AIC->AIC_SMR[AT91C_ID_SYS] = AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE | AT91C_AIC_PRIOR_HIGHEST;
  44.     AT91C_BASE_AIC->AIC_ICCR = (1 << AT91C_ID_SYS);
  45.     AT91C_BASE_AIC->AIC_IECR = (1 << AT91C_ID_SYS);
  46.     AT91C_BASE_PITC->PITC_PIMR |= AT91C_PITC_PITEN | AT91C_PITC_PITIEN;
  47.     int LocalCounter = 0;
  48. }
  49.  
  50.  
  51. void pitDelayMs (unsigned int delay)
  52. {
  53.   volatile unsigned int read;
  54.   int time = 0;
  55.   while(time < delay)
  56.   {
  57.     time++;
  58.     while (!delayFlag){}
  59.     delayFlag = false;
  60.   }
  61.  
  62. }
  63. void configureButtons(void)
  64. {
  65.     AT91C_BASE_PIOC->PIO_PER = B1| B2;
  66.     AT91C_BASE_PIOC->PIO_ODR = B1 | B2 ;
  67.     AT91C_BASE_PIOC->PIO_PPUER = B1| B2 ;
  68. }
  69.  
  70. void configureClock(void)
  71. {
  72.     AT91C_BASE_PMC->PMC_PCER = PID4;
  73. }
  74.  
  75. void configureLEDs(void)
  76. {
  77.   AT91C_BASE_PIOB->PIO_PER = AT91C_PIO_PB8; //LED1
  78.   AT91C_BASE_PIOB->PIO_OER = AT91C_PIO_PB8; //LED1
  79.   AT91C_BASE_PIOB->PIO_SODR = AT91C_PIO_PB8; //LED1
  80.  
  81.   AT91C_BASE_PIOC->PIO_PER = AT91C_PIO_PC29; //LED2
  82.   AT91C_BASE_PIOC->PIO_OER = AT91C_PIO_PC29;  //LED2
  83.   AT91C_BASE_PIOB->PIO_SODR = AT91C_PIO_PC29; //LED2
  84. }
  85.  
  86.  
  87. int buttonState(int state)
  88. {
  89.   return ((AT91C_BASE_PIOC->PIO_PDSR & state) == 0);
  90. }
  91.  
  92. int main(void)
  93. {
  94.   pitInit ();
  95.   pitInitInterrupts();
  96.   configureLEDs();
  97.   configureButtons();
  98.   configureClock();
  99.   AT91C_BASE_PIOB->PIO_CODR= AT91C_PIO_PB8;   // Turn on
  100.   AT91C_BASE_PIOB->PIO_SODR = AT91C_PIO_PB8; // Turn off
  101.  
  102.   while(1)
  103.   {
  104.     int iterrator;
  105.     for(iterrator = 0; iterrator < DELAY_STEP; iterrator++)
  106.     {
  107.       pitDelayMs(DELAY);
  108.       AT91C_BASE_PIOB->PIO_CODR = AT91C_PIO_PB8;     // Turn on //LED1
  109.  
  110.       pitDelayMs(DELAY);
  111.       AT91C_BASE_PIOB->PIO_SODR = AT91C_PIO_PB8; // Turn off //LED1
  112.  
  113.       if(buttonState(B1))
  114.       {
  115.         AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC29; //LED2
  116.       }
  117.  
  118.       if(buttonState(B2))
  119.       {
  120.         AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC29; //LED2
  121.       }
  122.     }
  123.   }
  124.   return 0;
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement