initial
This commit is contained in:
110
src/main.c
Normal file
110
src/main.c
Normal file
@ -0,0 +1,110 @@
|
||||
#include <stdint.h>
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
#include "uartdrv.h"
|
||||
|
||||
|
||||
|
||||
typedef struct clock_s {
|
||||
uint8_t hour;
|
||||
uint8_t minute;
|
||||
uint8_t second;
|
||||
} clock_t;
|
||||
|
||||
volatile clock_t clock;
|
||||
volatile uint8_t tick;
|
||||
volatile uint16_t captValue = 0;
|
||||
volatile uint16_t gapValue = 0;
|
||||
|
||||
ISR(TIMER2_OVF_vect) {
|
||||
//tick = 1;
|
||||
|
||||
clock.second++;
|
||||
if (clock.second >= 60) {
|
||||
clock.second = 0;
|
||||
clock.minute++;
|
||||
}
|
||||
if (clock.minute >= 60) {
|
||||
clock.minute = 0;
|
||||
clock.hour++;
|
||||
}
|
||||
if (clock.hour >= 24) {
|
||||
clock.hour = 0;
|
||||
}
|
||||
}
|
||||
|
||||
ISR(INT0_vect) {
|
||||
uint16_t tmpCnt = TCNT1;
|
||||
gapValue = tmpCnt - captValue;
|
||||
TCNT1 = 0;
|
||||
}
|
||||
|
||||
ISR(INT1_vect) {
|
||||
captValue = TCNT1;
|
||||
tick = 1;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
uartdrvInit();
|
||||
|
||||
clock.hour = 0;
|
||||
clock.minute = 0;
|
||||
clock.second = 0;
|
||||
|
||||
|
||||
TCCR2 = 0b00000101;
|
||||
ASSR |= (1 << AS2);
|
||||
TIMSK |= (1 << TOIE2) | (1 << TICIE1);
|
||||
|
||||
TCCR1A = 0;
|
||||
TCCR1B = (1 << CS12) | (1 << CS10);
|
||||
|
||||
PIND &= ~(1 << PD2);
|
||||
PIND &= ~(1 << PD3);
|
||||
PORTD |= (1 << PD2) | (1 << PD3);
|
||||
|
||||
MCUCR |= (1 << ISC11) | (1 << ISC10) | (1 << ISC01);
|
||||
GICR |= (1 << INT0) | (1 << INT1);
|
||||
|
||||
|
||||
sei();
|
||||
|
||||
printf("Hello world!\n");
|
||||
|
||||
|
||||
uint8_t start = 0;
|
||||
uint8_t bit = 0;
|
||||
|
||||
while (1) {
|
||||
if (tick != 0) {
|
||||
tick = 0;
|
||||
|
||||
uint16_t pulse = (((uint32_t) captValue) * 1000) / 15625;
|
||||
uint16_t gap = (((uint32_t) gapValue) * 1000) / 15625;
|
||||
|
||||
if (gap > 1500) {
|
||||
start = 1;
|
||||
clock.second = 0;
|
||||
}
|
||||
|
||||
if (pulse > 70 && pulse < 130) {
|
||||
bit = 0;
|
||||
} else if (pulse > 170 && pulse < 230) {
|
||||
bit = 1;
|
||||
} else {
|
||||
bit = 2;
|
||||
}
|
||||
|
||||
printf("%02d %02d %02d %u %u %u %u \n", clock.hour, clock.minute, clock.second, start, bit, pulse, gap);
|
||||
|
||||
|
||||
|
||||
start = 0;
|
||||
//printf("tick\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user