Blame | Last modification | View Log | RSS feed
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdlib.h>
#include "config.h"
#include "uart.h"
#ifndef NULL
#define NULL ((void *)0)
#endif
/* ------------------------------------------------------------------------- */
#define UART_BAUD_RATE 9600
volatile uint8_t tmr0_ovf = 0;
volatile uint32_t systime = 0;
volatile uint16_t sendtime = 1000;
int main(void) {
/*
DDR : 1 = Output, 0 = Input
PORT: 1 = Pullup for Input, otherwise set output
PIN : Read input pin
*/
/*
PB0 -
PB1 -
PB2 -
PB3 -
PB4 -
PB5 -
PB6 -
PB7 -
*/
DDRB = 0B11111111;
PORTB = 0B00000000;
/*
PC0 -
PC1 -
PC2 -
PC3 -
PC4 -
PC5 -
*/
DDRC = 0B11111111;
PORTC = 0B00000000;
/*
PD0 -
PD1 -
PD2 -
PD3 -
PD4 -
PD5 - Input PcInt - * I/O Interrupt PCINT21
PD6 - Output - * Status LED
PD7 - Input, Pullup - * Button
*/
DDRD = 0B01011111;
PORTD = 0B10000000;
TIMSK0 = (1<<TOIE0); // Enable timer overflow
TCNT0 = 0x00; // Set Timer0 initial value to 0
TCCR0B = (1<< CS00) ; // /1 prescaler
uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
wdt_enable(WDTO_1S); // Watchdog for 1 sec
sei(); // Enable interrupts
sbi(PIND, PD6);
_delay_ms(500);
cbi(PIND, PD6);
char buffer[7];
for(;;){
wdt_reset();
if (rbi(PIND, PD7))
cbi(PORTD, PD6);
else
sbi(PORTD, PD6);
if (!sendtime) {
ltoa(systime, buffer, 10);
uart_puts(buffer);
uart_puts("\r\n");
sendtime=1000;
xbi(PORTD, PD6);
}
_delay_ms(1);
}
return 0;
}
ISR(TIMER0_OVF_vect) {
tmr0_ovf++;
// Clk/1 TCCR0B = (1<< CS00);
//20.0Mhz, 1ms = 78ovf
//16.5Mhz, 1ms = 64ovf
//16.0Mhz, 1ms = 62ovf
//12.0Mhz, 1ms = 46ovf
if (tmr0_ovf>=64) {
systime++;
tmr0_ovf = 0;
if (sendtime)
sendtime--;
}
}