Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/delay.h>
- #include <avr/interrupt.h>
- volatile unsigned int one_second_ready;
- volatile unsigned int overflow_ticks;
- /*--------------------------INTERUPTS----------------------------*/
- // TIMER 0 OVERFLOW INTERRUPT
- ISR (TIMER0_OVF_vect) {
- overflow_ticks++;
- }
- // TIMER 1 COMPARE VALUE INTERRUPT
- ISR (TIMER1_COMPA_vect) {
- // T0 DISABLE
- TCCR0 &= ~(1 << CS02) & ~(1 << CS01) & ~(1 << CS00);
- TIMSK &= ~(1 << TOIE0);
- // T1 DISABLE
- TCCR1B &= ~(1 << WGM12) & ~(1 << CS12) & ~(1 << CS10);
- TIMSK &= ~(1 << OCIE1A);
- one_second_ready = 1; // Send Signal To sensorMeasurement() Function
- }
- /*---------------------------------------------------------------*/
- int sensorMeasurement() {
- one_second_ready = 0;
- overflow_ticks = 0;
- // T0 INITIALIZE
- TCCR0 |= (1 << CS02) | (1 << CS01) | (1 << CS00); // External Clock T0 Rising Edge
- TIMSK |= (1 << TOIE0); // Enable T0 Interrupt
- // T1 INITIALIZE
- TCCR1B |= (1 << WGM12) | (1 << CS12) | (1 << CS10); // CTC MODE
- OCR1A = F_CPU / 1024; // 1 Second Delay
- TIMSK |= (1 << OCIE1A); // Enable T1 Interrupt
- sei(); // Enable Interrupts
- while (one_second_ready == 0); // Wait until T1 is 1 second
- cli(); // Disable Interrupts
- return overflow_ticks * 256; // Signal Frequency
- }
- int main() {
- DDRB |= (1 << PB0);
- int measure = sensorMeasurement();
- for (int i = 0 ; i < measure / 1000 ; i++) {
- PORTB ^= (1 << PB0);
- _delay_ms(300);
- }
- while(1) {
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement