Advertisement
Guest User

cd334

a guest
May 20th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.80 KB | None | 0 0
  1. /***********************************************************************
  2.  *  timi_cd.c
  3.  *  ATmega16A C only timer handling
  4.  *  by tovis + cd334 2015
  5.  *  Really simple code for ATmega16A timer0
  6.  *  Good: OC0 pin (DIP40/4) gives nice 1KHz square wave signal.
  7.  *  Bad:  PORTA bit 0 (DIP40/40) shows that every fourth interrupt
  8.  *        too sort (bit after toggle quickly toggle back).
  9.  *        No square wave, every fourth toggle is too sort.
  10. ***********************************************************************/
  11. #include  <avr/io.h>
  12. #include  <avr/interrupt.h>
  13.  
  14.  
  15.  
  16. //----------------------------------------------------------------------
  17. #define F_CPU           16000000ul      // ATmega16A external crystal 16MHz
  18.  
  19. #define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
  20. #define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
  21. #define FLIPBIT(ADDRESS,BIT) (ADDRESS ^= (1<<BIT))
  22.  
  23. //----------------------------------------------------------------------
  24.  
  25. volatile uint8_t int_source = 0x00;
  26.  
  27. ISR(BADISR_vect)
  28. {
  29.     PORTA = 0xFF;
  30.     for(;;);
  31. }
  32.  
  33. // Interrupt source debug on PORTA Pin 1 to 5
  34. // All ISR vector is defined
  35.  
  36. ISR(INT0_vect)
  37. {
  38.     int_source = 0x01;
  39.     PORTA = int_source << 1;
  40.     for(;;);
  41. }
  42.  
  43. ISR(INT1_vect)
  44. {
  45.     int_source = 0x02;
  46.     PORTA = int_source << 1;
  47.     for(;;);
  48. }
  49.  
  50. ISR(TIMER2_COMP_vect)
  51. {
  52.     int_source = 0x03;
  53.     PORTA = int_source << 1;
  54.     for(;;);
  55. }
  56.  
  57. ISR(TIMER2_OVF_vect)
  58. {
  59.     int_source = 0x04;
  60.     PORTA = int_source << 1;
  61.     for(;;);
  62. }
  63.  
  64. ISR(TIMER1_CAPT_vect)
  65. {
  66.     int_source = 0x05;
  67.     PORTA = int_source << 1;
  68.     for(;;);
  69. }
  70.  
  71. ISR(TIMER1_COMPA_vect)
  72. {
  73.     int_source = 0x06;
  74.     PORTA = int_source << 1;
  75.     for(;;);
  76. }
  77. ISR(TIMER1_COMPB_vect)
  78. {
  79.     int_source = 0x07;
  80.     PORTA = int_source << 1;
  81.     for(;;);
  82. }
  83.  
  84. ISR(TIMER1_OVF_vect)
  85. {
  86.     int_source = 0x08;
  87.     PORTA = int_source << 1;
  88.     for(;;);
  89. }
  90.  
  91. ISR(TIMER0_OVF_vect)
  92. {
  93.     int_source = 0x09;
  94.     PORTA = int_source << 1;
  95.     for(;;);
  96. }
  97.  
  98. ISR(SPI_STC_vect)
  99. {
  100.     int_source = 0x0A;
  101.     PORTA = int_source << 1;
  102.     for(;;);
  103. }
  104.  
  105. ISR(USART_RXC_vect)
  106. {
  107.     int_source = 0x0B;
  108.     PORTA = int_source << 1;
  109.     for(;;);
  110. }
  111.  
  112. ISR(USART_UDRE_vect)
  113. {
  114.     int_source = 0x0C;
  115.     PORTA = int_source << 1;
  116.     for(;;);
  117. }
  118.  
  119. ISR(USART_TXC_vect)
  120. {
  121.     int_source = 0x0D;
  122.     PORTA = int_source << 1;
  123.     for(;;);
  124. }
  125.  
  126. ISR(ADC_vect)
  127. {
  128.     int_source = 0x0E;
  129.     PORTA = int_source << 1;
  130.     for(;;);
  131. }
  132.  
  133. ISR(EE_RDY_vect)
  134. {
  135.     int_source = 0x0F;
  136.     PORTA = int_source << 1;
  137.     for(;;);
  138. }
  139.  
  140. ISR(ANA_COMP_vect)
  141. {
  142.     int_source = 0x10;
  143.     PORTA = int_source << 1;
  144.     for(;;);
  145. }
  146.  
  147. ISR(TWI_vect)
  148. {
  149.     int_source = 0x11;
  150.     PORTA = int_source << 1;
  151.     for(;;);
  152. }
  153.  
  154. ISR(INT2_vect)
  155. {
  156.     int_source = 0x12;
  157.     PORTA = int_source << 1;
  158.     for(;;);
  159. }
  160.  
  161. ISR(TIMER0_COMP_vect)
  162. {
  163.     FLIPBIT(PORTA,0);
  164. }
  165.  
  166. ISR(SPM_RDY_vect)
  167. {
  168.     int_source = 0x13;
  169.     PORTA = int_source << 1;
  170.     for(;;);
  171. }
  172.  
  173. // Interrupt source debug on PORTA Pin 1 to 5
  174.  
  175. //----------------------------------------------------------------------
  176. int             main(void)
  177. {
  178.  
  179.   DDRA   = 0xFF;                        // whole PORTA set as output
  180.   PORTA  = 0x00;
  181.   TCCR0  = 0x1B;                        // Timer0 CTC mode:
  182.                                         // using internal clock,
  183.                                         // prescaler 1/64, toggle OC0
  184.                                        
  185.   OCR0   = 250;                         // Set compare register for 1ms
  186.  
  187.   TIFR   = 0xFF;                        // Clear pending interrupts
  188.   TIMSK  = 0x02;                        // Timer interrupt setting, only OCR0 enabled
  189.  
  190.   DDRB = 0x80;                          //OC0 pin to output
  191.   PORTB = 0x00;                         //give initial port value;
  192.  
  193.   //---
  194.   sei();                                // Enbale all interrupts
  195.   while ( 1 )                           // Loop forever
  196.   {
  197.     FLIPBIT(PORTA,7);               // Toggle port A pin 7
  198.   }
  199. }
  200. /*** OLD MAIN function) ***/
  201. /*
  202. int             main(void)
  203. {
  204.   DDRA   = 0xFF;                        // whole PORTA set as output
  205.   TCCR0  = 0x1B;                        // Timer0 CTC mode:
  206.                                         // using internal clock,
  207.                                         // prescaler 1/64, toggle OC0
  208.   OCR0   = 250;                         // Set compare register for 1ms
  209.   TIFR   = 0;                           // Clear pending interrupts
  210.   TIMSK &= ~(1 << TOIE0);               // Disable Timer0 overflow interrupt
  211.   TIMSK |= (1 << OCIE0);                // Enable timer 0 compare match interrupt
  212.   DDRB   = (1 << 3);                    // ??? set PORTB, OC0 pin to output
  213.   //---
  214.   sei();                                // Enbale all interrupts
  215.   while ( 1 )                           // Loop forever
  216.   {
  217.     PORTA = PINA ^ 0x80;                // Toggle port A pin 7
  218.   }
  219. */
  220.  
  221. /***********************************************************************
  222.  *  end timi_cd.c
  223. ***********************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement