Advertisement
Monika__

PIT timer

Nov 29th, 2020
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.34 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include "include/AT91SAM9263.h"
  4.  
  5. #define PIO_PER_B (volatile unsigned int *) AT91C_PIOB_PER //enables PIO control of corresponding pin
  6. #define PIO_OER_B (volatile unsigned int *) AT91C_PIOB_OER  //enables output
  7. #define PIO_SODR_B (volatile unsigned int *) AT91C_PIOB_SODR//sets data on the I/O line
  8. #define PIO_CODR_B (volatile unsigned int *) AT91C_PIOB_CODR //clears data on I/O line
  9.  
  10. #define PMC_PCER (volatile unsigned int *) AT91C_PMC_PCER
  11.  
  12. #define PIO_PER_C (volatile unsigned int *) AT91C_PIOC_PER
  13. #define PIO_OER_C (volatile unsigned int *) AT91C_PIOC_OER
  14. #define PIO_SODR_C (volatile unsigned int *) AT91C_PIOC_SODR
  15. #define PIO_CODR_C (volatile unsigned int *) AT91C_PIOC_CODR
  16. #define PIO_ODR_C (volatile unsigned int *) AT91C_PIOC_ODR //disable output (enables input) on I/O line
  17. #define PIO_PUER_C (volatile unsigned int *) AT91C_PIOB_PPUER   //pull-up
  18.  
  19. #define PIO_PDSR_C (volatile unsigned int *) AT91C_PIOC_PDSR //pin data status, when 0 on I/O line returns 0
  20. #define DELAY_TIME 100
  21. #define LED1 (1 << 8) //PB8
  22. #define LED2 (1 << 29) //PC29
  23. #define RIGHTCLIC (1 << 4) //PC4
  24. #define LEFTCLIC (1 << 5) //PC5
  25. #define PID4 (1 << 4) //enable clock input for port C
  26.  
  27.  
  28. #define PIT_MR (volatile unsigned int *) AT91C_PITC_PIMR //RW mode register
  29. #define PIT_PIVR (volatile unsigned int *) AT91C_PITC_PIVR //reading this register clears counter and flag
  30. #define PIT_SR  (volatile unsigned int *) AT91C_PITC_PISR //status register, when counting =0
  31. #define PITIEN (1<<25) //interapt register
  32. #define PITEN (1<<24) // PIT enable
  33. #define DELAY_IN_SEC (0.001)
  34. #define PIV ( (DELAY_IN_SEC / (160*0.000000001) ) - 1)
  35.  
  36. void delay_ms (int delay){
  37.     delay *=10;
  38.     unsigned int i;
  39.     for(i = 0; i < delay; i++){
  40.         volatile unsigned int j;
  41.         for(j=0; j<1275; j++);
  42.     }
  43. }
  44.  
  45. void ConfigureLEDs (void)
  46. {
  47.     *PIO_PER_B = LED1;
  48.     *PIO_OER_B = LED1;
  49.    
  50.     *PIO_PER_C = LED2;
  51.     *PIO_OER_C = LED2; 
  52.    
  53.     *PIO_SODR_B = LED1;
  54.     *PIO_SODR_C = LED2;
  55.    
  56. }
  57.  
  58. void ConfigureButtons (void) {
  59.     //enable peripherial control of both pins
  60.     *PIO_PER_C = RIGHTCLIC;
  61.     *PIO_PER_C = LEFTCLIC;
  62.    
  63.     //disable output, which means that the port works as input
  64.     *PIO_ODR_C = RIGHTCLIC;
  65.     *PIO_ODR_C = LEFTCLIC;
  66.    
  67.     *PIO_PUER_C = RIGHTCLIC;
  68.     *PIO_PUER_C = LEFTCLIC;
  69.    
  70.     //enable clock
  71.     *PMC_PCER = PID4;
  72.    
  73. }
  74.  
  75. void PIT_Init (void) {
  76.     *PIT_MR = PIV;
  77.     *PIT_MR &= ~(PITIEN | PITEN);
  78.     //1, program delay
  79.     volatile unsigned int clear = *PIT_PIVR; //reset counter and clear flag
  80. }
  81.  
  82. void PIT_delay_ms (int delay_in_ms ) {
  83.     volatile unsigned int clear;
  84.     int counter = 0;
  85.     for(counter; counter < delay_in_ms; counter++) {
  86.         *PIT_MR |= PITEN; // start timer
  87.         while(*PIT_SR == 0){} //wait for the flag
  88.         *PIT_MR &= ~(PITEN); //stop timer
  89.         clear = *PIT_PIVR; //clear clock
  90.     }
  91. }
  92.  
  93. void CheckButton (void) {
  94.         if ( *PIO_PDSR_C == (1 << 4 ) ) {
  95.             *PIO_CODR_C = LED2;
  96.         }  
  97.  
  98.         if ( *PIO_PDSR_C == (1 << 5 ) ) {
  99.             *PIO_SODR_C = LED2;
  100.         }
  101. }
  102.  
  103.  
  104. int main(void) {
  105.  
  106.  
  107.     ConfigureLEDs();
  108.     ConfigureButtons();
  109.     PIT_Init();
  110.     while(1){
  111.        
  112.         int counter;
  113.         *PIO_CODR_B = LED1; //turn on LED1
  114.        
  115.         for(counter = 0; counter < 100; counter++){
  116.             PIT_delay_ms(10);
  117.             CheckButton();
  118.         }
  119.        
  120.         *PIO_SODR_B = LED1; //turn off LED2
  121.        
  122.         for(counter = 0; counter < 100; counter++){
  123.             PIT_delay_ms(10);
  124.             CheckButton();
  125.         }  
  126.     }
  127. }
  128.  
  129.  
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement