Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * RadekTimer.c
- *
- * Created: 7. 6. 2016 16:32:43
- * Author: MartinPC
- *
- * CPUCLK = 6,4MHz
- *
- * Fuses:
- * LOW: 0x43
- * HIGH: 0xDF
- * EXTENDED: 0xFF
- * LOCK: none
- *
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #define bit_is_set(sfr,bit) (_SFR_BYTE(sfr) & _BV(bit))
- static int counter = 0;
- unsigned long ADCResult = 0;
- typedef unsigned char u08;
- typedef unsigned short u16;
- typedef unsigned long u32;
- int main(void)
- {
- DDRB |= (1 << PB0); // LED on PB0
- MCUCR = (1 << ISC01) | (1 << ISC00); // ISC01 = 1, ISC00 = 1 => rissing edge, ISC01 = 1, ISC00 = 0 => falling edge
- GIMSK = (1 << INT0);
- // Timer 1ms (6 400 000 / 8 / 8 / 100 = 1 000Hz; 1/1000 = 1ms
- // CPU_Freq (6,4MHz) / CKDIV8 / TIMER_Prescaller (8) / Timer compare counter (100)
- OCR0A = 0x64; // number to count up to (0x64 = 100)
- TCCR0A = (1 << WGM01); // Clear Timer on Compare Match (CTC) mode
- TIFR = 0; // clear interrupt flag
- TIMSK = (1 << OCIE0A); // TC0 compare match A interrupt enable
- TCCR0B = (1 << CS01); // clock source CLK/8, start timer
- DIDR0 = (1<<ADC2D);
- ADMUX = (1<<MUX1) | (1<<ADLAR);
- ADCSRA = (1<<ADEN) | (1<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
- sei(); // global interrupt enable
- ADCSRA |= (1<<ADSC);
- while(1)
- {
- // do nothing
- }
- }
- ISR(INT0_vect)
- {
- TIFR &= ~(1 << OCF0A);
- ADCSRA &= ~(1 << ADIF);
- ADCSRA |= (1 << ADIE);
- TIMSK |= (1 << OCIE0A);
- TCNT0 = 0;
- if (bit_is_set(PORTB,PB3))
- {
- PORTB |= (1 << PB0);
- } else {
- PORTB &= ~(1 << PB0);
- }
- }
- ISR(TIMER0_COMPA_vect)
- {
- if (bit_is_set(PORTB, PB1))
- {
- counter ++;
- if (counter >= (100 * ADCResult))
- {
- ADCSRA &= ~(1 << ADIE);
- TIMSK &= ~(1 << OCIE0A);
- counter = 0;
- if (bit_is_set(PORTB,PB3))
- {
- PORTB &= ~(1 << PB0);
- } else {
- PORTB |= (1 << PB0);
- }
- }
- }
- }
- ISR(ADC_vect)
- {
- ADCResult = ((u32)(((u32)((u32)ADCH*(u32)2700))/((u32)256/*(u32)10*/))); //ADCResult = 2700/256 = 0-2700 => 0 -> 270 000ms
- ADCSRA |= (1<<ADSC);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement