0% found this document useful (0 votes)
154 views

Code Arduino

This document contains C code for an AVR microcontroller that implements a timer and sleep functions. It initializes timer 0 and timer 1, sets the timer compare values, and enables interrupts. The main loop puts the microcontroller to sleep, wakes it periodically based on the timer compare values, and toggles LEDs or timer settings depending on the interrupt pin state. It aims to implement low power sleep cycles with periodic wakeup using timers and interrupts.

Uploaded by

Suaib Danish
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views

Code Arduino

This document contains C code for an AVR microcontroller that implements a timer and sleep functions. It initializes timer 0 and timer 1, sets the timer compare values, and enables interrupts. The main loop puts the microcontroller to sleep, wakes it periodically based on the timer compare values, and toggles LEDs or timer settings depending on the interrupt pin state. It aims to implement low power sleep cycles with periodic wakeup using timers and interrupts.

Uploaded by

Suaib Danish
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <avr/sleep.

h>
#include <avr/power.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <math.h>
volatile int f_timer=0;
int pbIn =0;
int ICPPin =2;
volatile int count=0;
volatile int k=0;
void statechange1()
{
count = TCNT1;
TCCR1B=0;
//TCNT1=0;
f_timer=0;
//k=0;
}
void statechange2()
{
count = TCNT1;
TCCR1B=0;
//TCNT1=0;
f_timer=0;
}
ISR(TIMER0_COMPA_vect)
{
TCNT0 = 0;
k++;
if(k==250)
{
k=0;
// preload timer
if(f_timer == 1)
{
f_timer = 2;
}
else if (f_timer == 3)
{
TCCR0B = 0;
f_timer=4;
}
}
}
void enterSleep(void)
{
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
sleep_mode();
sleep_disable(); /* First thing to do is disable sleep. */
}
void setup()
// run once, when the sketch starts
{
DDRD |= B11111000; // sets Arduino pins 3 to 7 as outputs, pin 0 to 2 as inpu
t
Serial.begin(9600); // set up Serial library at 9600 bps
TCCR1A = 0;
TCCR1B = 0;
}

void start_timer0()
{
noInterrupts(); // disable all interrupts
TCCR0A = 0;
TCCR0B = 0;
// pwm generation
OCR0A = 249;// = (16*10^6) / (8*8000) - 1 (must be <65536)
TCCR0A |= (1 << WGM01);
//TCCR0B |= (1 << WGM01);
TIMSK0 |= (1 << OCIE0A);
TCCR0B |= (1 << CS00);
TCCR0B |= (1 << CS01);
interrupts();
}
void start_timer2()
{
TCNT1=0;
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS11);
}
void loop()
{
autozero();
while(1)
{
if((f_timer==0)|(f_timer==1))
//if(f_timer==0)
{
f_timer = 1;
PORTD=B00001000;
start_timer0();
Serial.println(count);
enterSleep();
}
if((f_timer==2)|(f_timer==3))
//if(f_timer==2)
{
f_timer=3;
PORTD=B00110000;
start_timer0();
enterSleep();
}
if(f_timer==4)
{
if (PIND & _BV(2))
{
attachInterrupt(pbIn, statechange1, FALLING);
PORTD=B11100000;
start_timer2();
enterSleep();
}
else
{
attachInterrupt(pbIn, statechange2, RISING);
PORTD=B11111000;
start_timer2();
enterSleep();
}
}
}
}

void autozero()
{
if (PIND & _BV(2))
{
do
{
PORTD=B11100000;
}while(PIND & _BV(2));
}
else
{
do
{
PORTD=B11111000;
}while (digitalRead(ICPPin)==LOW);
}
}

You might also like