Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************************************
- * timi_cd.c
- * ATmega16A C only timer handling
- * by tovis + cd334 2015
- * Really simple code for ATmega16A timer0
- * Good: OC0 pin (DIP40/4) gives nice 1KHz square wave signal.
- * Bad: PORTA bit 0 (DIP40/40) shows that every fourth interrupt
- * too sort (bit after toggle quickly toggle back).
- * No square wave, every fourth toggle is too sort.
- ***********************************************************************/
- #include <avr/io.h>
- #include <avr/interrupt.h>
- //----------------------------------------------------------------------
- #define F_CPU 16000000ul // ATmega16A external crystal 16MHz
- #define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
- #define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
- #define FLIPBIT(ADDRESS,BIT) (ADDRESS ^= (1<<BIT))
- //----------------------------------------------------------------------
- volatile uint8_t int_source = 0x00;
- ISR(BADISR_vect)
- {
- PORTA = 0xFF;
- for(;;);
- }
- // Interrupt source debug on PORTA Pin 1 to 5
- // All ISR vector is defined
- ISR(INT0_vect)
- {
- int_source = 0x01;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(INT1_vect)
- {
- int_source = 0x02;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(TIMER2_COMP_vect)
- {
- int_source = 0x03;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(TIMER2_OVF_vect)
- {
- int_source = 0x04;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(TIMER1_CAPT_vect)
- {
- int_source = 0x05;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(TIMER1_COMPA_vect)
- {
- int_source = 0x06;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(TIMER1_COMPB_vect)
- {
- int_source = 0x07;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(TIMER1_OVF_vect)
- {
- int_source = 0x08;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(TIMER0_OVF_vect)
- {
- int_source = 0x09;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(SPI_STC_vect)
- {
- int_source = 0x0A;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(USART_RXC_vect)
- {
- int_source = 0x0B;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(USART_UDRE_vect)
- {
- int_source = 0x0C;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(USART_TXC_vect)
- {
- int_source = 0x0D;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(ADC_vect)
- {
- int_source = 0x0E;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(EE_RDY_vect)
- {
- int_source = 0x0F;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(ANA_COMP_vect)
- {
- int_source = 0x10;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(TWI_vect)
- {
- int_source = 0x11;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(INT2_vect)
- {
- int_source = 0x12;
- PORTA = int_source << 1;
- for(;;);
- }
- ISR(TIMER0_COMP_vect)
- {
- FLIPBIT(PORTA,0);
- }
- ISR(SPM_RDY_vect)
- {
- int_source = 0x13;
- PORTA = int_source << 1;
- for(;;);
- }
- // Interrupt source debug on PORTA Pin 1 to 5
- //----------------------------------------------------------------------
- int main(void)
- {
- DDRA = 0xFF; // whole PORTA set as output
- PORTA = 0x00;
- TCCR0 = 0x1B; // Timer0 CTC mode:
- // using internal clock,
- // prescaler 1/64, toggle OC0
- OCR0 = 250; // Set compare register for 1ms
- TIFR = 0xFF; // Clear pending interrupts
- TIMSK = 0x02; // Timer interrupt setting, only OCR0 enabled
- DDRB = 0x80; //OC0 pin to output
- PORTB = 0x00; //give initial port value;
- //---
- sei(); // Enbale all interrupts
- while ( 1 ) // Loop forever
- {
- FLIPBIT(PORTA,7); // Toggle port A pin 7
- }
- }
- /*** OLD MAIN function) ***/
- /*
- int main(void)
- {
- DDRA = 0xFF; // whole PORTA set as output
- TCCR0 = 0x1B; // Timer0 CTC mode:
- // using internal clock,
- // prescaler 1/64, toggle OC0
- OCR0 = 250; // Set compare register for 1ms
- TIFR = 0; // Clear pending interrupts
- TIMSK &= ~(1 << TOIE0); // Disable Timer0 overflow interrupt
- TIMSK |= (1 << OCIE0); // Enable timer 0 compare match interrupt
- DDRB = (1 << 3); // ??? set PORTB, OC0 pin to output
- //---
- sei(); // Enbale all interrupts
- while ( 1 ) // Loop forever
- {
- PORTA = PINA ^ 0x80; // Toggle port A pin 7
- }
- */
- /***********************************************************************
- * end timi_cd.c
- ***********************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement