Advertisement
Mary_99

PIT INTERRUPTS

Jan 3rd, 2021 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.58 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. bool delayFlag = false;
  15. unsigned int TimerCounter = 0;
  16. unsigned int elseInterrupt = 0;
  17.  
  18. void pitInit (void)
  19. {
  20.   volatile unsigned int read;
  21.   AT91C_BASE_PITC->PITC_PIMR = PIV; //init PIV //disable timer //disable interupts
  22.   read = AT91C_BASE_PITC->PITC_PIVR ; //to read and clear
  23. }
  24.  
  25. void timerInterruptHandler()
  26. {
  27.   volatile unsigned int read;
  28.   if (AT91C_BASE_PITC->PITC_PIMR & AT91C_PITC_PITIEN) // if flag interrupt is set/interrupt enabled
  29.   {
  30.     if (AT91C_BASE_PITC->PITC_PISR ) // 1 ms // if flag pits is set / time requested/check if pit intterrupt was triggereds
  31.     {
  32.       read = AT91C_BASE_PITC->PITC_PIVR ;  //clerars PITS in PIT_SR//and count once again
  33.       TimerCounter++;
  34.       delayFlag = true;
  35.     }
  36.     else
  37.     {
  38.       elseInterrupt++;
  39.     }
  40.   }
  41.   AT91C_BASE_AIC->AIC_EOICR = 0;
  42. }
  43.  
  44. void pitInitInterrupts()
  45. {
  46.     AT91C_BASE_AIC->AIC_IDCR = (1 << AT91C_ID_SYS); //disable PIT timer interrupts
  47.     AT91C_BASE_AIC->AIC_SVR[AT91C_ID_SYS] = (unsigned int) timerInterruptHandler; //configuration of the pointer for timer intterupt handler -handler for processor periherial device
  48.     AT91C_BASE_AIC->AIC_SMR[AT91C_ID_SYS] = AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE | AT91C_AIC_PRIOR_HIGHEST; //configure method of interrupt triggering
  49.     AT91C_BASE_AIC->AIC_ICCR = (1 << AT91C_ID_SYS); //clear an interrupt flag
  50.     AT91C_BASE_AIC->AIC_IECR = (1 << AT91C_ID_SYS); //turn on an interrupt
  51.     AT91C_BASE_PITC->PITC_PIMR |= AT91C_PITC_PITIEN;  //turn on PIT timer interrupt
  52.  
  53. }
  54.  
  55.  
  56. void pitDelayMs (unsigned int delay)
  57. {
  58.   volatile unsigned int read;
  59.   int time = 0;
  60.   read = AT91C_BASE_PITC->PITC_PIVR;//clear PITS
  61.   AT91C_BASE_PITC->PITC_PIMR |= AT91C_PITC_PITEN; //turn on timer
  62.   while(time < delay)
  63.   {
  64.     time++;
  65.     while (!delayFlag){}
  66.     delayFlag = false;
  67.   }
  68.   AT91C_BASE_PITC->PITC_PIMR &= ~ (AT91C_PITC_PITEN); //turn off timer
  69. }
  70. void configureButtons(void)
  71. {
  72.     AT91C_BASE_PIOC->PIO_PER = B1| B2;
  73.     AT91C_BASE_PIOC->PIO_ODR = B1 | B2 ;
  74.     AT91C_BASE_PIOC->PIO_PPUER = B1| B2 ;
  75. }
  76.  
  77. void configureClock(void)
  78. {
  79.     AT91C_BASE_PMC->PMC_PCER = PID4;
  80. }
  81.  
  82. void configureLEDs(void)
  83. {
  84.   AT91C_BASE_PIOB->PIO_PER = AT91C_PIO_PB8; //LED1
  85.   AT91C_BASE_PIOB->PIO_OER = AT91C_PIO_PB8; //LED1
  86.   AT91C_BASE_PIOB->PIO_SODR = AT91C_PIO_PB8; //LED1
  87.  
  88.   AT91C_BASE_PIOC->PIO_PER = AT91C_PIO_PC29; //LED2
  89.   AT91C_BASE_PIOC->PIO_OER = AT91C_PIO_PC29;  //LED2
  90.   AT91C_BASE_PIOB->PIO_SODR = AT91C_PIO_PC29; //LED2
  91. }
  92.  
  93.  
  94. int buttonState(int state)
  95. {
  96.   return ((AT91C_BASE_PIOC->PIO_PDSR & state) == 0);
  97. }
  98.  
  99. int main(void)
  100. {
  101.   pitInit ();
  102.   pitInitInterrupts();
  103.   configureLEDs();
  104.   configureButtons();
  105.   configureClock();
  106.   AT91C_BASE_PIOB->PIO_CODR= AT91C_PIO_PB8;   // Turn on
  107.   AT91C_BASE_PIOB->PIO_SODR = AT91C_PIO_PB8; // Turn off
  108.  
  109.   while(1)
  110.   {
  111.     int iterrator;
  112.     for(iterrator = 0; iterrator < DELAY_STEP; iterrator++)
  113.     {
  114.       pitDelayMs(DELAY);
  115.       AT91C_BASE_PIOB->PIO_CODR = AT91C_PIO_PB8;     // Turn on //LED1
  116.  
  117.       pitDelayMs(DELAY);
  118.       AT91C_BASE_PIOB->PIO_SODR = AT91C_PIO_PB8; // Turn off //LED1
  119.  
  120.       if(buttonState(B1))
  121.       {
  122.         AT91C_BASE_PIOC->PIO_CODR = AT91C_PIO_PC29; //LED2
  123.       }
  124.  
  125.       if(buttonState(B2))
  126.       {
  127.         AT91C_BASE_PIOC->PIO_SODR = AT91C_PIO_PC29; //LED2
  128.       }
  129.     }
  130.   }
  131.   return 0;
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement