Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1.  
  2.  
  3. #include <avr/io.h>
  4. #include <avr/interrupt.h>
  5.  
  6. volatile unsigned char TimerFlag = 0; // TimerISR() sers this to 1. C programmer should clear to 0;
  7.  
  8. // Internal variables for mapping AVR'S ISR to out cleaner TimerISR model.
  9. unsigned long _avr_timer_M = 1;// Start count from here, down to 0, Default 1 ms.
  10. unsigned long _avr_timer_cntcurr = 0; // Current internal count of 1ms ticks
  11. void TimerOn(){
  12. //AVR timer/counter controller register TCCR1
  13. TCCR1B = 0x0B; // bit3 = 0: CTC mode (clear timer on compare)
  14. // bit2bit1bit0=011: pre-scalar /64
  15. // 00001011: 0x0B
  16. // SO, 8MHz clock or 8,000,000 /64 = 125,000 ticks/sec
  17. // Thus, TCNT1 register will count at 125,000 tick/sec
  18.  
  19. // AVR output compare register OCR1A
  20. OCR1A = 125; // Timer interrupt will be generated when TCNT1 == OCR1A
  21. // We want a 1 ms tick. 0.001 s * 125,000 ticks/s = 125
  22. // So when TCNT1 register equals 125,
  23. // 1 ms has passed. Thus, we compare to 125.
  24. // AVR timer interrupt mask register
  25. TIMSK1 = 0x02; // bit1: OCIE1A -- enables compare match interrupt
  26.  
  27. // Initialize avr counter
  28. TCNT1 = 0;
  29.  
  30. _avr_timer_cntcurr = _avr_timer_M;
  31. // TimerISR will be called every _avr_timer_cntcurr milliseconds
  32.  
  33. //Enable global interrupts
  34. SREG |= 0x80; // 0x80: 1000000
  35. }
  36.  
  37. void TimerOff(){
  38. TCCR1B = 0x00; // bit3bit1bit0=000: timer off
  39. }
  40.  
  41. // In our approach, the C programmer does not touch this ISR, but rather TimerISR()
  42. void TimerISR(){
  43. TimerFlag = 1;
  44. };
  45. // CPU automatically calls when TCNT1 == OCR1 (every 1 ms per TimerOn settings)
  46. ISR(TIMER1_COMPA_vect){
  47. // CPU automatically calls when TCNT1 == 0CR1 (every 1 ms per TimerOn settings)
  48. _avr_timer_cntcurr--; // Count down to 0 rather than up to TOP
  49. if(_avr_timer_cntcurr ==0){ // results in a more efficient compare
  50. TimerISR(); // Call the ISR that the user uses.
  51. _avr_timer_cntcurr = _avr_timer_M;
  52. }
  53. }
  54. // Set TimerISR() to tick every M ms
  55. void TimerSet(unsigned long M){
  56. _avr_timer_M = M;
  57. _avr_timer_cntcurr = _avr_timer_M;
  58. }
  59. int main(void){
  60. /* Replace with your application code */
  61. DDRB = 0xFF; // Set port B to output
  62. PORTB =0x00; // Init port B to 0s
  63. TimerSet(10000);
  64. TimerOn();
  65. unsigned char tmpB = 0x00;
  66. while (1){
  67. // User code
  68. tmpB = ~tmpB;// Toggle PORTB; Temporary bad programming style
  69. PORTB = tmpB;
  70. while(!TimerFlag); // Wait 1 sec
  71. TimerFlag = 0;
  72. //Note: For the above a better stiel woulf use a synchSM with TickSM()
  73. // This example just illustrates the use of the ISR and flag
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement