Created
February 13, 2015 17:29
-
-
Save aniline/214733edc7ec215439f6 to your computer and use it in GitHub Desktop.
Some Datasheet fu - (hw) hello world using timer in attiny.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/interrupt.h> | |
#include <avr/io.h> | |
#include <util/atomic.h> | |
#define F_CPU 8000000 | |
#include <util/delay.h> | |
volatile uint8_t ctr = 0; | |
volatile uint16_t ms = 0; | |
volatile uint16_t s = 0; | |
ISR (TIMER0_COMPA_vect) { | |
ctr ++; | |
/* 8Mhz/8 and once 100 uS = 0.1ms */ | |
if (ctr == 10) { | |
ctr = 0; | |
ms ++; | |
if (ms == 1000) { | |
ms = 0; | |
s ++; | |
if (s == 1000) | |
s = 0; | |
} | |
} | |
TCNT0 = 0; | |
TIFR |= 0x02; | |
} | |
inline uint16_t getctr () { | |
uint16_t c; | |
ATOMIC_BLOCK(ATOMIC_FORCEON) | |
{ | |
c = ms; | |
} | |
return c; | |
} | |
int main (void) | |
{ | |
DDRB = 0x1F; | |
PORTB = 0x1F; | |
cli(); | |
TCCR0A = 0x00; /* No outputs on compare match (COM0nx), no modes of | |
* outputs (WGMx) */ | |
TCCR0B = 0x02; /* FOCXn = 0, WGM2 = 0, Prescaler 2 = io_clk/8, 5 | |
* = io_clk / 1024 */ | |
OCR0A = 100; /* Compare value */ | |
TCNT0 = 0x00; /* Counter */ | |
TIMSK = 0x10; /* OCIE0A = 1, TOIE0 =0 */ | |
TIFR = 0x10; /* OCF0A = 1, TOV0 = 0 */ | |
sei(); | |
while (1) { | |
uint16_t c = getctr(); | |
if (c > 500) | |
PORTB = 0x1F; | |
else | |
PORTB = 0; | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment