Yet another blinky project that is based on internal timer. In our circuit a LED is connected to PB0 and it is made to blink for roughly every second by using timer OVF (overflow). The code is on Github, click here.
Parts Required
- ATtiny13 – i.e. MBAVR-1 development board
- Resistor – 220Ω, see LED Resistor Calculator
- LED
Circuit Diagram
Firmware
This code is written in C and can be compiled using the avr-gcc. More details on how compile this project is here.
#include <avr/io.h> #include <avr/interrupt.h> #define LED_PIN PB0 ISR(TIM0_OVF_vect) { PORTB ^= _BV(LED_PIN); // toggle LED pin } int main(void) { /* setup */ DDRB = 0b00000001; // set LED pin as OUTPUT PORTB = 0b00000000; // set all pins to LOW TCCR0B |= _BV(CS02)|_BV(CS00); // set prescaler to 1024 (CLK=1200000Hz/1024/256=4Hz, 0.25s) TIMSK0 |= _BV(TOIE0); // enable Timer Overflow interrupt sei(); // enable global interrupts /* loop */ while (1); }
i followed your code to implement millis() function, but its not working
————————————————————————————————–
#include
#include
#define LED_PIN PB0
volatile unsigned long timer0_millis = 0;
ISR(TIM0_OVF_vect)
{
//PORTB ^= _BV(LED_PIN); // toggle LED pin
unsigned long m = timer0_millis;
m++;
timer0_millis = m;
}
unsigned long millis()
{
unsigned long m;
uint8_t oldSREG = SREG;
// disable interrupts while we read timer0_millis or we might get an
// inconsistent value (e.g. in the middle of a write to timer0_millis)
cli();
m = timer0_millis;
SREG = oldSREG;
return m;
}
int main(void)
{
/* setup */
DDRB = 0b00000001; // set LED pin as OUTPUT
PORTB = 0b00000000; // set all pins to LOW
TCCR0B |= _BV(CS02)|_BV(CS00); // set prescaler to 1024 (CLK=1200000Hz/1024/256=4Hz, 0.25s)
TIMSK0 |= _BV(TOIE0); // enable Timer Overflow interrupt
sei(); // enable global interrupts
unsigned long prev_millis; //the last time the led was toggled
prev_millis = millis();
/* loop */
while (1)
{
if (millis() – prev_millis > 5000)
{
PORTB ^= (1 << 0); //Turn on / Turn off the LED
prev_millis = millis();
}
}
}
————————————————————————————————
can you help please
Hi,
first of all thanks for above tutorials, i am currently working on toy based products, in which we are using IR based remote mechanism(NEC), we have used same controller at our side for remote design.but we are facing range problem; as we can talk to receiver module from 2-3 meters when we try to go above 3 meters receiver not responding.
How to resolve this issue
regards,
prashant