Advertisement
tovis

AVR Timer0 CTC mode

May 19th, 2015
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.15 KB | None | 0 0
  1. /***********************************************************************
  2.  *  timi-main.c
  3.  *  ATmega16A timer handling C and assembly training
  4.  *  by tovis 2015
  5. ***********************************************************************/
  6. #include  <avr/io.h>
  7. #include  <avr/interrupt.h>
  8.  
  9. //----------------------------------------------------------------------
  10. #define F_CPU           16000000ul      // ATmega16A external crystal 16MHz
  11.  
  12. //----------------------------------------------------------------------
  13. #define PERIOD_10MS     10
  14. #define PERIOD_100MS    100
  15. #define PERIOD_1S       1000
  16.  
  17. //----------------------------------------------------------------------
  18. volatile uint16_t       tick_counter;
  19.  
  20. //----------------------------------------------------------------------
  21. int             main(void)
  22. {
  23.     uint8_t     aux;
  24.   uint16_t  tmr0;
  25.     //... code ...
  26.   DDRA   = 0xFF;                        // whole PORTA set as output
  27.   TCCR0  = 0x1B;                        // Timer0 CTC mode:
  28.                                         // using internal clock,
  29.                                         // prescaler 1/64, toggle OC0
  30.   OCR0   = 250;                         // Set compare register for 1ms
  31.   TIFR   = 0;                           // Clear pending interrupts
  32.   // Enable timer 0 compare match interrupt
  33.   TIMSK |= (1 << OCIE0);
  34.   TIMSK |= _BV(OCIE0);
  35.   /*
  36.     It's not so good. Why not using SBI?
  37.     TIMSK |= (1 << OCIE0);
  38.     ca: 89 b7           in  r24, 0x39   ; 57
  39.     cc: 82 60           ori r24, 0x02   ; 2
  40.     ce: 89 bf           out 0x39, r24   ; 57
  41.     TIMSK |= _BV(OCIE0);
  42.     d0: 89 b7           in  r24, 0x39   ; 57
  43.     d2: 82 60           ori r24, 0x02   ; 2
  44.     d4: 89 bf           out 0x39, r24   ; 57
  45.   */
  46.   DDRB  |= (1 << 3);                    // ??? set PORTB, OC0 pin to output
  47.   sei();                                // Enbale all interrupts
  48.   //---
  49.   tmr0 = 0;
  50.   while ( 1 )                           // Loop forever
  51.   {
  52.     toggle_A0();
  53.     if ( tick_counter >= tmr0 )
  54.     {
  55.       TIMSK = (1 << OCIE0);             // Enable timer 0 compare match interrupt
  56.       tmr0 = tick_counter + PERIOD_10MS;
  57.       // tmr0 = tick_counter + PERIOD_100MS;
  58.       // tmr0 = tick_counter + PERIOD_1S;
  59.       toggle_A2();
  60.     }
  61.   }
  62. } //end main
  63.  
  64. /***********************************************************************
  65.  *  end timi-main.c
  66. ***********************************************************************/
  67.  
  68. /***********************************************************************
  69.  *  timi.S
  70.  *  ATmega16A timer handling C and assembly training
  71.  *  by tovis 2015
  72. ***********************************************************************/
  73. #include  <avr/io.h>
  74.  
  75. .extern tick_counter
  76.  
  77. //----------------------------------------------------------------------
  78. .global TIMER0_COMP_vect
  79. TIMER0_COMP_vect:
  80.         RCALL   toggle_A1
  81.         PUSH    R16
  82.         IN      R16, _SFR_IO_ADDR(SREG)
  83.         PUSH    R16
  84. //------
  85.         LDS     R24, (tick_counter)
  86.         LDS     R25, (tick_counter) + 1
  87.         ADIW    R24, 0x0001
  88.         STS     tick_counter, R24
  89.         STS     tick_counter+1, R25
  90. //------
  91.         POP     R16
  92.         OUT     _SFR_IO_ADDR(SREG), R16
  93.         POP     R16
  94.         RETI
  95. //**********************************************************************
  96. .global toggle_A0
  97. toggle_A0:
  98.         SBIC    _SFR_IO_ADDR(PINA),  0
  99.         CBI     _SFR_IO_ADDR(PORTA), 0
  100.         SBIS    _SFR_IO_ADDR(PINA),  0
  101.         SBI     _SFR_IO_ADDR(PORTA), 0
  102.         RET
  103. //----------------------------------------------------------------------
  104. .global toggle_A1
  105. toggle_A1:
  106.         SBIC    _SFR_IO_ADDR(PINA),  1
  107.         CBI     _SFR_IO_ADDR(PORTA), 1
  108.         SBIS    _SFR_IO_ADDR(PINA),  1
  109.         SBI     _SFR_IO_ADDR(PORTA), 1
  110.         RET
  111. //----------------------------------------------------------------------
  112. .global toggle_A2
  113. toggle_A2:
  114.         SBIC    _SFR_IO_ADDR(PINA),  2
  115.         CBI     _SFR_IO_ADDR(PORTA), 2
  116.         SBIS    _SFR_IO_ADDR(PINA),  2
  117.         SBI     _SFR_IO_ADDR(PORTA), 2
  118.         RET
  119. /***********************************************************************
  120.  *  end timi.S
  121. ***********************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement