/* timer2_init() * Timer 2 is an 8-bit timer that can be operated asynchronously. I'll * use it clocked by the 32.768 kHz crystal on the butterfly. * In CTC mode, the OC2A pin will toggle when the timer reaches the * compare register value. The pin has to toggle twice to reach * its original state, so the square wave frequency at OC2A will be * 32.768kHz / (2 * OCR2A) with no prescaler. */ void timer2_init(void) { /* The recommended procedure for switching to an asychronous clock * source: * 1. Disable interrupts from timer 2 compare match and overflow */ TIMSK2 = 0; /* 2. Set timer 2 to be clocked from the TOSC1 pin */ ASSR = (1<<AS2); /* 3a. Set timer 2 prescaler. No prescaler is 001. */ TCCR2A = (0<<CS22) | (0<<CS21) | (1<<CS20); /* 3b. Set Clear on Timer match mode */ TCCR2A |= (1<<WGM21) | (0<<WGM20); /* 3c. Set the OC2A pin to toggle on compare match */ TCCR2A |= (0<<COM2A1) | (1<<COM2A0); /* 4. Set timer 2's output compare register */ OCR2A = 33; /* 5. Set the initial counter register value */ TCNT2 = 0; /* 6. Look at the asynchronous status register to figure out if * timer 2 has settled in to normal operation. This means that * the counter value is being automatically updated (TCN2UB = 0), * the configuration register is ready to take new values, * (TCR2UB = 0), and that the output compare register is ready * to take new values (OCR2UB = 0). */ while((ASSR & 1<<TCN2UB) | (ASSR & 1<<TCR2UB) | (ASSR & 1<<OCR2UB)); /* 7. Clear timer 2 interrupt flags */ TIFR2 = 0; /* 8. Enable output compare match interrupt if necessary using: * TIMSK2 = 1 << OCIE2A */ timer2_stop(); // Stop the counter TCNT2 = 0; // Reset the counter }
/// Blocks for a specified number of milliseconds void wait_ms(unsigned int time_val) { //Seting OC value for time requested OCR2=250; //Clock is 16 MHz. At a prescaler of 64, 250 timer ticks = 1ms. timer2_tick=0; timer2_start(0); //Waiting for time while(timer2_tick < time_val); timer2_stop(); }
__interrupt void timer2_overflow() { TIM2_SR1&=~0X01;//clear UIF bit if(cnt_dir) { if(timecnt2++ == 2) uart_state=UART_OVER; else return; } else if(--timeout2) return; timer2_stop(); }
inline void sendData(bool askforStatus,RF24 & radio){ reportBuffer.reportid=askforStatus?1:0; bool ok = radio.write( &reportBuffer, sizeof(report_t) ); if (ok){ //status led off PORTD &= ~(1<<LED1); } else{ //status led on PORTD |= (1<<LED1); #ifdef DEBUG print_string("pa error\n"); #endif radio.startListening(); radio.stopListening(); return; } radio.startListening(); if (askforStatus){ // Wait here until we get a response, or timeout (250ms) bool timeout = false; timer2_start(); while ( ! radio.available() && ! timeout ){ if (timer2_gettick() > 200 ) timeout = true; } // Describe the results if ( timeout ) { //status led on PORTD |= (1<<LED1); #ifdef DEBUG print_string("Failed, response timed out.\n\r"); #endif } else { uint8_t response; radio.read( &response, sizeof(uint8_t) ); //status led off PORTD &= ~(1<<LED1); if (response){ //battery led on #ifdef DEBUG print_string("led on\n"); #endif PORTD |= (1<<LED2); }else{ //battery led off PORTD &= ~(1<<LED2); #ifdef DEBUG print_string("led off\n"); #endif } } timer2_stop(); } radio.stopListening(); }