Advertisement
Monika__

LED with PIT and INTERRUPT

Jan 2nd, 2021
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include "include/AT91SAM9263.h"
  4.  
  5.  
  6. #define DELAY_TIME 100
  7. #define LED1 (1 << 8) //PB8
  8. #define LED2 (1 << 29) //PC29
  9. #define RIGHTCLIC (1 << 4) //PC4
  10. #define LEFTCLIC (1 << 5) //PC5
  11. #define ENABLECLOCK (1 << 4) //change name!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  12. #define DELAY_IN_SEC (0.001)
  13. #define PIV ( (DELAY_IN_SEC / (160*0.000000001) ) - 1)
  14.  
  15. bool flag = false;
  16.  
  17. void TIMER_INT_handler (void){
  18.    
  19.     if (AT91C_BASE_PITC->PITC_PIMR & AT91C_PITC_PITIEN ){
  20.         flag = true;
  21.         volatile unsigned int clear = AT91C_BASE_PITC->PITC_PIVR;
  22.     }
  23.    
  24. }
  25.  
  26. void ConfigureLEDs (void)
  27. {
  28.     *AT91C_PIOB_PER = LED1;
  29.     *AT91C_PIOB_OER = LED1;
  30.    
  31.     *AT91C_PIOC_PER = LED2;
  32.     *AT91C_PIOC_OER = LED2;
  33.    
  34.     *AT91C_PIOB_SODR = LED1;
  35.     *AT91C_PIOC_SODR = LED2;
  36.    
  37. }
  38.  
  39. void ConfigureButtons (void) {
  40.     //enable peripherial control of both pins
  41.     *AT91C_PIOC_PER = RIGHTCLIC;
  42.     *AT91C_PIOC_PER = LEFTCLIC;
  43.    
  44.     //disable output, which means that the port works as input
  45.     *AT91C_PIOC_ODR = RIGHTCLIC;
  46.     *AT91C_PIOC_ODR = LEFTCLIC;
  47.    
  48.     *AT91C_PIOB_PPUER = RIGHTCLIC;
  49.     *AT91C_PIOB_PPUER = LEFTCLIC;
  50.    
  51.     //enable clock
  52.     *AT91C_PMC_PCER = ENABLECLOCK;
  53.    
  54. }
  55.  
  56. void PIT_Init (void) {
  57.     AT91C_BASE_PITC->PITC_PIMR = PIV; // in mode register er set periodic interval value
  58.    
  59.     AT91C_BASE_AIC->AIC_IDCR = 1 << AT91C_ID_SYS; //disable PIT Timer interrupts
  60.     AT91C_BASE_AIC->AIC_SMR[AT91C_ID_SYS] = (AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE | AT91C_AIC_PRIOR_LOWEST); //configure method of interrupt triggering: level, edge
  61.     AT91C_BASE_AIC->AIC_SVR[AT91C_ID_SYS] = (unsigned int)TIMER_INT_handler;// name for interrupt handler
  62.     AT91C_BASE_AIC->AIC_ICCR =  1 << AT91C_ID_SYS;
  63.     AT91C_BASE_AIC->AIC_IECR =  1 << AT91C_ID_SYS;
  64.    
  65.     AT91C_BASE_PITC->PITC_PIMR |= AT91C_PITC_PITIEN; // enable PIT interrupt
  66.     volatile unsigned int clear = AT91C_BASE_PITC->PITC_PIVR; //reading the register resets it 
  67. }
  68.  
  69. void PIT_delay_ms (int delay_in_ms ) {
  70.     int counter = 0;
  71.     for(counter; counter < delay_in_ms; counter++) {
  72.         AT91C_BASE_PITC->PITC_PIMR |= AT91C_PITC_PITEN;
  73.         while(!flag){}
  74.         flag = false;
  75.     }
  76. }
  77.  
  78. void CheckButton (void) {
  79.         if ( *AT91C_PIOC_PDSR == (1 << 4 ) ) {
  80.             *AT91C_PIOC_CODR = LED2;
  81.         }  
  82.  
  83.         if ( *AT91C_PIOC_PDSR == (1 << 5 ) ) {
  84.             *AT91C_PIOC_SODR = LED2;
  85.         }
  86. }
  87.  
  88.  
  89.  
  90. int main(void) {
  91.  
  92.     ConfigureLEDs();
  93.     ConfigureButtons();
  94.     PIT_Init();
  95.     while(1){
  96.        
  97.         int counter;
  98.         *AT91C_PIOB_CODR = LED1; //turn on LED1
  99.        
  100.         for(counter = 0; counter < 100; counter++){
  101.             PIT_delay_ms(10);
  102.             CheckButton();
  103.         }
  104.        
  105.         *AT91C_PIOB_SODR = LED1; //turn off LED2
  106.        
  107.         for(counter = 0; counter < 100; counter++){
  108.             PIT_delay_ms(10);
  109.             CheckButton();
  110.         }  
  111.     }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement