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

59
blink.c
View File

@ -1,39 +1,42 @@
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #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)
int main (void) volatile uint32_t jiffies = 0;
ISR(TIM1_COMPA_vect)
{
jiffies += 1;
}
int
main(void)
{ {
unsigned char counter; unsigned char counter;
/* set PORTB for output*/
/*
* set PORTB for output
*/
DDRB = 0xFF; 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++;
}
/* set PORTB.2 low */
PORTB = 0x00; PORTB = 0x00;
/* wait (10 * 120000) cycles = wait 1200000 cycles */ TCCR1A = 0;
counter = 0; TCCR1B = 0;
while (counter != 10) TCNT1 = 0;
{ OCR1A = TICKS_PER_JIFFY - 1;
/* wait (30000 x 4) cycles = wait 120000 cycles */ TCCR1B |= (1 << WGM12);
_delay_loop_2(30000); TCCR1B |= (1 << CS12);
counter++; TIMSK1 |= (1 << OCIE1A);
}
sei();
while (1) {
PORTB = 0xFF * ((jiffies/ 10) % 2);
} }
return 1; return 0;
} }