Finally worked out deciseconds

This commit is contained in:
Neale Pickett 2013-06-30 14:19:41 -06:00
parent 27a9eb281a
commit a5936d5590
2 changed files with 40 additions and 37 deletions

View File

@ -39,4 +39,4 @@ clean:

75
blink.c
View File

@ -1,39 +1,42 @@
#include <avr/io.h>
#include <util/delay.h>
int main (void)
#include <avr/interrupt.h>
/* This only works out for a 16MHz or 8MHz clock */
#define CLOCK_HZ 16000000
#define TICKS_PER_SECOND (CLOCK_HZ / 256)
#define TICKS_PER_JIFFY (TICKS_PER_SECOND / 10)
volatile uint32_t jiffies = 0;
ISR(TIM1_COMPA_vect)
{
unsigned char counter;
/* set PORTB for output*/
DDRB = 0xFF;
while (1)
{
/* set PORTB.2 high */
PORTB = 0xFF;
/* wait (10 * 120000) cycles = wait 1200000 cycles */
counter = 0;
while (counter != 10)
{
/* wait (30000 x 4) cycles = wait 120000 cycles */
_delay_loop_2(30000);
counter++;
jiffies += 1;
}
int
main(void)
{
unsigned char counter;
/*
* set PORTB for output
*/
DDRB = 0xFF;
PORTB = 0x00;
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = TICKS_PER_JIFFY - 1;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << OCIE1A);
sei();
while (1) {
PORTB = 0xFF * ((jiffies/ 10) % 2);
}
/* set PORTB.2 low */
PORTB = 0x00;
/* wait (10 * 120000) cycles = wait 1200000 cycles */
counter = 0;
while (counter != 10)
{
/* wait (30000 x 4) cycles = wait 120000 cycles */
_delay_loop_2(30000);
counter++;
}
}
return 1;
}
return 0;
}