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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
93
src/uartdrv.c
Normal file
93
src/uartdrv.c
Normal file
@ -0,0 +1,93 @@
|
||||
#include <avr/io.h>
|
||||
#include <stdio.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#include "uartdrv.h"
|
||||
#include "debug.h"
|
||||
|
||||
|
||||
volatile uartdrvCmdBuf_t uartdrvCmdBuf;
|
||||
|
||||
|
||||
|
||||
|
||||
void uartdrvInit() {
|
||||
|
||||
// UART
|
||||
// TXD
|
||||
DDRD |= (1 << PD1);
|
||||
|
||||
// transmitter enable
|
||||
UCSRB = (1 << TXEN) | (1 << RXEN) | (1 << RXCIE);
|
||||
// 8 data bit, no parity, 1 stop bit
|
||||
UCSRC = (1 << UCSZ1) | (1 << UCSZ0);
|
||||
// 19200 Baud @ 16MHz
|
||||
UBRRL = 51;
|
||||
UBRRH = 0;
|
||||
FILE *mystdout = fdevopen(uartdrvPutchar, NULL);
|
||||
stdout = mystdout;
|
||||
|
||||
uartdrvCmdBuf.idx = 0;
|
||||
uartdrvCmdBuf.cmd = 0;
|
||||
uartdrvCmdBuf.cmdStart = 0;
|
||||
uartdrvCmdBuf.cmdDelay = 0;
|
||||
uartdrvCmdBuf.cmdReady = 0;
|
||||
}
|
||||
|
||||
void uartdrvWrite(uint8_t o) {
|
||||
loop_until_bit_is_clear(PIND, PD2);
|
||||
loop_until_bit_is_set(UCSRA, UDRE);
|
||||
UDR = o;
|
||||
}
|
||||
|
||||
|
||||
int uartdrvPutchar(char c, FILE *stream) {
|
||||
if (c == '\n')
|
||||
uartdrvPutchar('\r', stream);
|
||||
uartdrvWrite((uint8_t) c);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void uartdrvCmdCycle() {
|
||||
if (uartdrvCmdBuf.cmdStart == 1) {
|
||||
uartdrvCmdBuf.cmdDelay++;
|
||||
if (uartdrvCmdBuf.cmdDelay > UART_CMD_CYCLES) {
|
||||
uartdrvCmdBuf.idx = 0;
|
||||
uartdrvCmdBuf.cmdStart = 0;
|
||||
uartdrvCmdBuf.cmdDelay= 0;
|
||||
// myPrintf("clear invalid cmd\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void uartdrvCmdReceived() {
|
||||
uartdrvWrite(0x7f);
|
||||
uartdrvWrite(uartdrvCmdBuf.cmd);
|
||||
uartdrvWrite(0);
|
||||
uartdrvWrite(0);
|
||||
uartdrvWrite(0);
|
||||
}
|
||||
|
||||
ISR(USART_RXC_vect) {
|
||||
uint8_t c = UDR;
|
||||
if (uartdrvCmdBuf.cmdReady != 1) {
|
||||
if (uartdrvCmdBuf.idx == 0) {
|
||||
uartdrvCmdBuf.cmdStart = 1;
|
||||
uartdrvCmdBuf.cmd = c;
|
||||
uartdrvCmdBuf.idx++;
|
||||
} else if (uartdrvCmdBuf.idx >= 1 && uartdrvCmdBuf.idx <= UART_CMD_BUF_SIZE) {
|
||||
uartdrvCmdBuf.buf[uartdrvCmdBuf.idx - 1] = c;
|
||||
uartdrvCmdBuf.idx++;
|
||||
}
|
||||
|
||||
if (uartdrvCmdBuf.idx == (UART_CMD_BUF_SIZE + 1)) {
|
||||
|
||||
uartdrvCmdBuf.idx = 0;
|
||||
uartdrvCmdBuf.cmdStart = 0;
|
||||
uartdrvCmdBuf.cmdDelay = 0;
|
||||
uartdrvCmdBuf.cmdReady = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
32
src/uartdrv.h
Normal file
32
src/uartdrv.h
Normal file
@ -0,0 +1,32 @@
|
||||
#ifndef UARTDRV_H_
|
||||
#define UARTDRV_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
#define UART_CMD_BUF_SIZE 4
|
||||
#define UART_CMD_CYCLES 10
|
||||
|
||||
typedef struct uartdrvCmdBuf_s {
|
||||
uint8_t idx;
|
||||
uint8_t cmdStart;
|
||||
uint8_t cmdDelay;
|
||||
uint8_t cmdReady;
|
||||
uint8_t cmd;
|
||||
uint8_t buf[UART_CMD_BUF_SIZE];
|
||||
} uartdrvCmdBuf_t;
|
||||
|
||||
|
||||
extern void uartInitCallback();
|
||||
extern void uartWriteCallback(uint8_t o);
|
||||
|
||||
int uartdrvPutchar(char c, FILE *stream);
|
||||
void uartdrvInit();
|
||||
void uartdrvWrite(uint8_t o);
|
||||
|
||||
void uartdrvCmdCycle();
|
||||
|
||||
void uartdrvCmdReceived();
|
||||
|
||||
#endif /* UARTDRV_H_ */
|
Reference in New Issue
Block a user