refactoring
This commit is contained in:
159
src/clock.c
Normal file
159
src/clock.c
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <avr/io.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
|
#include "clock.h"
|
||||||
|
|
||||||
|
|
||||||
|
volatile clock_t clock;
|
||||||
|
volatile opTime_t opTime;
|
||||||
|
|
||||||
|
volatile uint8_t useLocalClock = 1;
|
||||||
|
|
||||||
|
volatile uint8_t nextSecond = 0;
|
||||||
|
volatile uint8_t nextMinute = 0;
|
||||||
|
|
||||||
|
ISR(TIMER2_OVF_vect) {
|
||||||
|
clock.second++;
|
||||||
|
nextSecond = 1;
|
||||||
|
|
||||||
|
if (useLocalClock == 1) {
|
||||||
|
if (clock.second >= 60) {
|
||||||
|
clock.second = 0;
|
||||||
|
clock.minute++;
|
||||||
|
|
||||||
|
nextMinute = 1;
|
||||||
|
}
|
||||||
|
if (clock.minute >= 60) {
|
||||||
|
clock.minute = 0;
|
||||||
|
clock.hour++;
|
||||||
|
}
|
||||||
|
if (clock.hour >= 24) {
|
||||||
|
clock.hour = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
opTime.second++;
|
||||||
|
|
||||||
|
if (opTime.second >= 60) {
|
||||||
|
opTime.second = 0;
|
||||||
|
opTime.minute++;
|
||||||
|
}
|
||||||
|
if (opTime.minute >= 60) {
|
||||||
|
opTime.minute = 0;
|
||||||
|
opTime.hour++;
|
||||||
|
}
|
||||||
|
if (opTime.hour >= 24) {
|
||||||
|
opTime.hour = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void clockInit() {
|
||||||
|
clock.hour = 0;
|
||||||
|
clock.minute = 0;
|
||||||
|
clock.second = 0;
|
||||||
|
|
||||||
|
opTime.hour = 0;
|
||||||
|
opTime.minute = 0;
|
||||||
|
opTime.second = 0;
|
||||||
|
|
||||||
|
// seconds
|
||||||
|
TCCR2 = 0b00000101;
|
||||||
|
ASSR |= (1 << AS2);
|
||||||
|
TIMSK |= (1 << TOIE2) | (1 << TICIE1);
|
||||||
|
|
||||||
|
// heartbeat
|
||||||
|
TCCR0 = (1 << CS02) | (1 << CS00);
|
||||||
|
TIMSK |= (1 << TOIE0);
|
||||||
|
}
|
||||||
|
|
||||||
|
opTime_t clockGetOpTime() {
|
||||||
|
return opTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clockSetUseLocalClock(uint8_t x) {
|
||||||
|
useLocalClock = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clockSetClock(uint8_t year, uint8_t month, uint8_t day, uint8_t weekday, uint8_t hour, uint8_t minute) {
|
||||||
|
cli();
|
||||||
|
clock.year = year;
|
||||||
|
clock.month = month;
|
||||||
|
clock.day = day;
|
||||||
|
clock.weekday = weekday;
|
||||||
|
clock.hour = hour;
|
||||||
|
clock.minute = minute;
|
||||||
|
sei();
|
||||||
|
}
|
||||||
|
|
||||||
|
void clockClearSecond() {
|
||||||
|
cli();
|
||||||
|
clock.second = 0;
|
||||||
|
sei();
|
||||||
|
}
|
||||||
|
|
||||||
|
clock_t clockGetClock() {
|
||||||
|
cli();
|
||||||
|
clock_t c = clock;
|
||||||
|
sei();
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t clockNextSecond() {
|
||||||
|
if (nextSecond != 0) {
|
||||||
|
nextSecond = 0;
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t clockNextMinute() {
|
||||||
|
if (nextMinute != 0) {
|
||||||
|
nextMinute = 0;
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
volatile uint8_t tack = 0;
|
||||||
|
volatile uint8_t step = 0;
|
||||||
|
|
||||||
|
|
||||||
|
uint8_t clockNextStep() {
|
||||||
|
if (step != 0) {
|
||||||
|
step = 0;
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t clockNextTack() {
|
||||||
|
if (tack != 0) {
|
||||||
|
tack = 0;
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ISR(TIMER0_OVF_vect) {
|
||||||
|
static uint8_t subDivTack = 0;
|
||||||
|
subDivTack++;
|
||||||
|
if (subDivTack >= SUB_DIV_LED) {
|
||||||
|
subDivTack = 0;
|
||||||
|
tack = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t subDivStepper = 0;
|
||||||
|
subDivStepper++;
|
||||||
|
if (subDivStepper >= SUB_DIV_STEPPER) {
|
||||||
|
subDivStepper = 0;
|
||||||
|
step = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
43
src/clock.h
Normal file
43
src/clock.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#ifndef CLOCK_H_
|
||||||
|
#define CLOCK_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define SUB_DIV_LED 30
|
||||||
|
#define SUB_DIV_STEPPER 3
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct clock_s {
|
||||||
|
uint8_t hour;
|
||||||
|
uint8_t minute;
|
||||||
|
uint8_t second;
|
||||||
|
uint8_t day;
|
||||||
|
uint8_t weekday;
|
||||||
|
uint8_t month;
|
||||||
|
uint8_t year;
|
||||||
|
} clock_t;
|
||||||
|
|
||||||
|
typedef struct opTime_s {
|
||||||
|
uint8_t second;
|
||||||
|
uint8_t minute;
|
||||||
|
uint16_t hour;
|
||||||
|
} opTime_t;
|
||||||
|
|
||||||
|
|
||||||
|
void clockInit();
|
||||||
|
opTime_t clockGetOpTime();
|
||||||
|
|
||||||
|
uint8_t clockNextStep();
|
||||||
|
uint8_t clockNextTack();
|
||||||
|
uint8_t clockNextSecond();
|
||||||
|
uint8_t clockNextMinute();
|
||||||
|
|
||||||
|
void clockSetUseLocalClock(uint8_t x);
|
||||||
|
void clockClearSecond();
|
||||||
|
void clockSetClock(uint8_t year, uint8_t month, uint8_t day, uint8_t weekday, uint8_t hour, uint8_t minute);
|
||||||
|
clock_t clockGetClock();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* CLOCK_H_ */
|
391
src/main.c
391
src/main.c
@ -3,101 +3,30 @@
|
|||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
#include "clock.h"
|
||||||
#include "uartdrv.h"
|
#include "uartdrv.h"
|
||||||
|
#include "stepper.h"
|
||||||
|
#include "validLed.h"
|
||||||
|
|
||||||
|
|
||||||
#define START_GAP_MIN 1500
|
|
||||||
#define ONE_WIDTH_MIN 170
|
|
||||||
#define ONE_WIDTH_MAX 260
|
|
||||||
#define ZERO_WIDTH_MIN 70
|
|
||||||
#define ZERO_WIDTH_MAX 150
|
|
||||||
|
|
||||||
#define SUB_DIV_LED 30
|
|
||||||
#define SUB_DIV_STEPPER 3
|
|
||||||
|
|
||||||
#define STEPS_PER_MINUTE 3
|
|
||||||
#define STEPS_PER_HOUR 7
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct receivedData_s {
|
|
||||||
uint8_t minuteDigit0;
|
|
||||||
uint8_t minuteDigit1;
|
|
||||||
uint8_t minuteOneCnt;
|
|
||||||
uint8_t hourDigit0;
|
|
||||||
uint8_t hourDigit1;
|
|
||||||
uint8_t hourOneCnt;
|
|
||||||
uint8_t dayDigit0;
|
|
||||||
uint8_t dayDigit1;
|
|
||||||
uint8_t weekdayDigit0;
|
|
||||||
uint8_t monthDigit0;
|
|
||||||
uint8_t monthDigit1;
|
|
||||||
uint8_t yearDigit0;
|
|
||||||
uint8_t yearDigit1;
|
|
||||||
uint8_t dateOneCnt;
|
|
||||||
} receivedData_t;
|
|
||||||
|
|
||||||
typedef struct clock_s {
|
|
||||||
uint8_t hour;
|
|
||||||
uint8_t minute;
|
|
||||||
uint8_t second;
|
|
||||||
uint8_t day;
|
|
||||||
uint8_t weekday;
|
|
||||||
uint8_t month;
|
|
||||||
uint8_t year;
|
|
||||||
} clock_t;
|
|
||||||
|
|
||||||
typedef struct opTime_s {
|
|
||||||
uint8_t second;
|
|
||||||
uint8_t minute;
|
|
||||||
uint16_t hour;
|
|
||||||
} opTime_t;
|
|
||||||
|
|
||||||
|
|
||||||
receivedData_t rd;
|
receivedData_t rd;
|
||||||
|
|
||||||
volatile clock_t clock;
|
extern volatile clock_t clock;
|
||||||
volatile opTime_t opTime;
|
|
||||||
volatile uint8_t tick;
|
volatile uint8_t tick;
|
||||||
volatile uint16_t captValue = 0;
|
volatile uint16_t captValue = 0;
|
||||||
volatile uint16_t gapValue = 0;
|
volatile uint16_t gapValue = 0;
|
||||||
volatile uint8_t valid = 0;
|
volatile uint8_t valid = 0;
|
||||||
volatile uint8_t tack = 0;
|
uint16_t validCnt = 0;
|
||||||
volatile uint8_t step = 0;
|
uint16_t timeValidCnt = 0;
|
||||||
|
|
||||||
extern uint8_t bufferReadIdx;
|
|
||||||
extern uint8_t bufferWriteIdx;
|
|
||||||
|
|
||||||
ISR(TIMER2_OVF_vect) {
|
|
||||||
clock.second++;
|
|
||||||
|
|
||||||
if (valid != 2) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
opTime.second++;
|
|
||||||
|
|
||||||
if (opTime.second >= 60) {
|
|
||||||
opTime.second = 0;
|
|
||||||
opTime.minute++;
|
|
||||||
}
|
|
||||||
if (opTime.minute >= 60) {
|
|
||||||
opTime.minute = 0;
|
|
||||||
opTime.hour++;
|
|
||||||
}
|
|
||||||
if (opTime.hour >= 24) {
|
|
||||||
opTime.hour = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(INT0_vect) {
|
ISR(INT0_vect) {
|
||||||
uint16_t tmpCnt = TCNT1;
|
uint16_t tmpCnt = TCNT1;
|
||||||
@ -110,40 +39,26 @@ ISR(INT1_vect) {
|
|||||||
tick = 1;
|
tick = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR(TIMER0_OVF_vect) {
|
|
||||||
static uint8_t subDivTack = 0;
|
void setValid(uint8_t x) {
|
||||||
subDivTack++;
|
valid = x;
|
||||||
if (subDivTack >= SUB_DIV_LED) {
|
if (valid == 1) {
|
||||||
subDivTack = 0;
|
validCnt++;
|
||||||
tack = 1;
|
clockSetUseLocalClock(1);
|
||||||
|
} else if (valid == 2) {
|
||||||
|
timeValidCnt++;
|
||||||
|
clockSetUseLocalClock(0);
|
||||||
|
stepperEnable();
|
||||||
|
} else {
|
||||||
|
clockSetUseLocalClock(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t subDivStepper = 0;
|
uint8_t getValid() {
|
||||||
subDivStepper++;
|
return valid;
|
||||||
if (subDivStepper >= SUB_DIV_STEPPER) {
|
|
||||||
subDivStepper = 0;
|
|
||||||
step = 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
uartdrvInit();
|
|
||||||
|
|
||||||
clock.hour = 0;
|
|
||||||
clock.minute = 0;
|
|
||||||
clock.second = 0;
|
|
||||||
|
|
||||||
opTime.hour = 0;
|
|
||||||
opTime.minute = 0;
|
|
||||||
opTime.second = 0;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// seconds
|
|
||||||
TCCR2 = 0b00000101;
|
|
||||||
ASSR |= (1 << AS2);
|
|
||||||
TIMSK |= (1 << TOIE2) | (1 << TICIE1);
|
|
||||||
|
|
||||||
// signal measurment
|
// signal measurment
|
||||||
TCCR1A = 0;
|
TCCR1A = 0;
|
||||||
@ -158,35 +73,13 @@ int main() {
|
|||||||
// esd prot
|
// esd prot
|
||||||
DDRD |= (1 << PD5) | (1 << PD6);
|
DDRD |= (1 << PD5) | (1 << PD6);
|
||||||
|
|
||||||
// valid led
|
|
||||||
DDRB |= (1 << PB0);
|
|
||||||
TCCR0 = (1 << CS02) | (1 << CS00);
|
|
||||||
TIMSK |= (1 << TOIE0);
|
|
||||||
|
|
||||||
// position switches
|
uartdrvInit();
|
||||||
DDRB &= ~(1 << PB1);
|
clockInit();
|
||||||
DDRB &= ~(1 << PB2);
|
stepperInit();
|
||||||
PORTB |= (1 << PB1) | (1 << PB2);
|
validLedInit();
|
||||||
|
|
||||||
// dark switch
|
setValid(0);
|
||||||
DDRD &= ~(1 << PD7);
|
|
||||||
PORTD |= (1 << PD7);
|
|
||||||
|
|
||||||
// stepper
|
|
||||||
DDRC |= (1 << PC4) | (1 << PC3) | (1 << PC2) | (1 << PC1) | (1 << PC0);
|
|
||||||
DDRA |= (1 << PA0) | (1 << PA1);
|
|
||||||
|
|
||||||
PORTC &= ~(1 << PC3);
|
|
||||||
PORTC &= ~(1 << PC2);
|
|
||||||
PORTC &= ~(1 << PC1);
|
|
||||||
PORTC &= ~(1 << PC0);
|
|
||||||
|
|
||||||
PORTA &= ~(1 << PA0);
|
|
||||||
PORTA &= ~(1 << PA1);
|
|
||||||
|
|
||||||
PORTC &= ~(1 << PC4);
|
|
||||||
_delay_ms(1);
|
|
||||||
PORTC |= (1 << PC4);
|
|
||||||
|
|
||||||
sei();
|
sei();
|
||||||
|
|
||||||
@ -195,9 +88,6 @@ int main() {
|
|||||||
|
|
||||||
uint8_t bit = 0;
|
uint8_t bit = 0;
|
||||||
uint8_t state = 0;
|
uint8_t state = 0;
|
||||||
uint16_t validCnt = 0;
|
|
||||||
uint16_t timeValidCnt = 0;
|
|
||||||
uint8_t ledToggle = 0;
|
|
||||||
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
@ -209,11 +99,10 @@ int main() {
|
|||||||
|
|
||||||
if (gap > START_GAP_MIN) {
|
if (gap > START_GAP_MIN) {
|
||||||
if (valid == 0) {
|
if (valid == 0) {
|
||||||
validCnt++;
|
setValid(1);;
|
||||||
valid = 1;
|
|
||||||
}
|
}
|
||||||
state = 0;
|
state = 0;
|
||||||
clock.second = 0;
|
clockClearSecond();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pulse > ZERO_WIDTH_MIN && pulse < ZERO_WIDTH_MAX) {
|
if (pulse > ZERO_WIDTH_MIN && pulse < ZERO_WIDTH_MAX) {
|
||||||
@ -222,7 +111,7 @@ int main() {
|
|||||||
bit = 1;
|
bit = 1;
|
||||||
} else {
|
} else {
|
||||||
bit = 2;
|
bit = 2;
|
||||||
valid = 0;
|
setValid(0);;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -230,7 +119,7 @@ int main() {
|
|||||||
switch (state) {
|
switch (state) {
|
||||||
case 0:
|
case 0:
|
||||||
if (bit != 0)
|
if (bit != 0)
|
||||||
valid = 0;
|
setValid(0);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
break;
|
break;
|
||||||
@ -272,7 +161,7 @@ int main() {
|
|||||||
break;
|
break;
|
||||||
case 20:
|
case 20:
|
||||||
if (bit != 1)
|
if (bit != 1)
|
||||||
valid = 0;
|
setValid(0);
|
||||||
break;
|
break;
|
||||||
case 21:
|
case 21:
|
||||||
rd.minuteOneCnt = 0;
|
rd.minuteOneCnt = 0;
|
||||||
@ -316,7 +205,7 @@ int main() {
|
|||||||
rd.minuteOneCnt += bit;
|
rd.minuteOneCnt += bit;
|
||||||
if (rd.minuteOneCnt % 2 != 0) {
|
if (rd.minuteOneCnt % 2 != 0) {
|
||||||
printf("MINUTE INVALID\n");
|
printf("MINUTE INVALID\n");
|
||||||
valid = 0;
|
setValid(0);
|
||||||
} else {
|
} else {
|
||||||
printf("MINUTE %u %u\n", rd.minuteDigit0, rd.minuteDigit1);
|
printf("MINUTE %u %u\n", rd.minuteDigit0, rd.minuteDigit1);
|
||||||
}
|
}
|
||||||
@ -358,7 +247,7 @@ int main() {
|
|||||||
rd.hourOneCnt += bit;
|
rd.hourOneCnt += bit;
|
||||||
if (rd.hourOneCnt % 2 != 0) {
|
if (rd.hourOneCnt % 2 != 0) {
|
||||||
printf("HOUR INVALID\n");
|
printf("HOUR INVALID\n");
|
||||||
valid = 0;
|
setValid(0);
|
||||||
} else {
|
} else {
|
||||||
printf("HOUR %u %u\n", rd.hourDigit0, rd.hourDigit1);
|
printf("HOUR %u %u\n", rd.hourDigit0, rd.hourDigit1);
|
||||||
}
|
}
|
||||||
@ -485,23 +374,21 @@ int main() {
|
|||||||
rd.dateOneCnt += bit;
|
rd.dateOneCnt += bit;
|
||||||
if (rd.dateOneCnt % 2 != 0) {
|
if (rd.dateOneCnt % 2 != 0) {
|
||||||
printf("DATE INVALID\n");
|
printf("DATE INVALID\n");
|
||||||
valid = 0;
|
setValid(0);
|
||||||
} else {
|
} else {
|
||||||
printf("DAY %u %u\n", rd.dayDigit0, rd.dayDigit1);
|
printf("DAY %u %u\n", rd.dayDigit0, rd.dayDigit1);
|
||||||
printf("WEEKDAY %u\n", rd.weekdayDigit0);
|
printf("WEEKDAY %u\n", rd.weekdayDigit0);
|
||||||
printf("MONTH %u %u\n", rd.monthDigit0, rd.monthDigit1);
|
printf("MONTH %u %u\n", rd.monthDigit0, rd.monthDigit1);
|
||||||
printf("YEAR %u %u\n", rd.yearDigit0, rd.yearDigit1);
|
printf("YEAR %u %u\n", rd.yearDigit0, rd.yearDigit1);
|
||||||
|
|
||||||
clock.minute = rd.minuteDigit0 + rd.minuteDigit1 * 10;
|
clockSetClock(rd.yearDigit0 + rd.yearDigit1 * 10,
|
||||||
clock.hour = rd.hourDigit0 + rd.hourDigit1 * 10;
|
rd.monthDigit0 + rd.monthDigit1 * 10,
|
||||||
clock.day = rd.dayDigit0 + rd.dayDigit1 * 10;
|
rd.dayDigit0 + rd.dayDigit1 * 10,
|
||||||
clock.weekday = rd.weekdayDigit0;
|
rd.weekdayDigit0,
|
||||||
clock.month = rd.monthDigit0 + rd.monthDigit1 * 10;
|
rd.hourDigit0 + rd.hourDigit1 * 10,
|
||||||
clock.year = rd.yearDigit0 + rd.yearDigit1 * 10;
|
rd.minuteDigit0 + rd.minuteDigit1 * 10);
|
||||||
|
|
||||||
|
setValid(2);
|
||||||
valid = 2;
|
|
||||||
timeValidCnt++;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 59:
|
case 59:
|
||||||
@ -511,202 +398,32 @@ int main() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("% 4d:%02d:%02d %02d:%02d:%02d %02d.%02d.%02d %d %d %02d %d %d %d %d %d %d %d\n",
|
printf("%02d:%02d:%02d %02d.%02d.%02d %d %d %02d %d %d %d %d %d\n",
|
||||||
opTime.hour, opTime.minute, opTime.second,
|
|
||||||
clock.hour, clock.minute, clock.second,
|
clock.hour, clock.minute, clock.second,
|
||||||
clock.day, clock.month, clock.year, clock.weekday,
|
clock.day, clock.month, clock.year, clock.weekday,
|
||||||
valid, state, bit, pulse, gap, validCnt, timeValidCnt,
|
valid, state, bit, pulse, gap, validCnt, timeValidCnt);
|
||||||
bufferReadIdx, bufferWriteIdx);
|
|
||||||
|
|
||||||
state++;
|
state++;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
printf("% 4d:%02d:%02d %02d:%02d:%02d %02d.%02d.%02d %d %d %02d %d %d %d %d %d %d %d\n",
|
printf("%02d:%02d:%02d %02d.%02d.%02d %d %d %02d %d %d %d %d %d\n",
|
||||||
opTime.hour, opTime.minute, opTime.second,
|
|
||||||
clock.hour, clock.minute, clock.second,
|
clock.hour, clock.minute, clock.second,
|
||||||
clock.day, clock.month, clock.year, clock.weekday,
|
clock.day, clock.month, clock.year, clock.weekday,
|
||||||
valid, state, bit, pulse, gap, validCnt, timeValidCnt,
|
valid, state, bit, pulse, gap, validCnt, timeValidCnt);
|
||||||
bufferReadIdx, bufferWriteIdx);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (clockNextMinute()) {
|
||||||
static uint8_t oldPositionSwitch = 255;
|
opTime_t opTime = clockGetOpTime();
|
||||||
uint8_t positionSwitch = PINB & ((1 << PB1) | (1 << PB2));
|
printf("% 4d:%02d:%02d\n", opTime.hour, opTime.minute, opTime.second);
|
||||||
if (oldPositionSwitch != positionSwitch) {
|
|
||||||
oldPositionSwitch = positionSwitch;
|
|
||||||
printf("POS: %02d\n", positionSwitch);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((timeValidCnt > 0) && (step != 0)) {
|
if (clockNextStep()) {
|
||||||
step = 0;
|
stepperEngine();
|
||||||
|
|
||||||
static uint8_t stepperState = 0;
|
|
||||||
|
|
||||||
static uint8_t currentMinutePosition = 0;
|
|
||||||
static uint8_t minuteStepPosition = 0;
|
|
||||||
static int8_t minuteDirection = 0;
|
|
||||||
|
|
||||||
static uint8_t currentHourPosition = 0;
|
|
||||||
static uint8_t hourStepPosition = 0;
|
|
||||||
static int8_t hourDirection = 0;
|
|
||||||
|
|
||||||
switch (stepperState) {
|
|
||||||
case 0:
|
|
||||||
//printf("0 ");
|
|
||||||
// is current minute position different from desired one?
|
|
||||||
minuteStepPosition = clock.minute * STEPS_PER_MINUTE;
|
|
||||||
|
|
||||||
if (currentMinutePosition != minuteStepPosition) {
|
|
||||||
printf("M:\n");
|
|
||||||
|
|
||||||
stepperState = 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
printf("1 ");
|
|
||||||
// switch on the minute motor and set the direction
|
|
||||||
PORTA |= (1 << PA1);
|
|
||||||
|
|
||||||
if (currentMinutePosition > minuteStepPosition) {
|
|
||||||
printf("b\n");
|
|
||||||
minuteDirection = -1;
|
|
||||||
PORTC &= ~(1 << PC2);
|
|
||||||
} else {
|
|
||||||
printf("f\n");
|
|
||||||
minuteDirection = 1;
|
|
||||||
PORTC |= (1 << PC2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stepperState = 2;
|
if (clockNextTack()) {
|
||||||
break;
|
validLedEngine(valid);
|
||||||
case 2:
|
|
||||||
printf("2 ");
|
|
||||||
|
|
||||||
// move one step
|
|
||||||
|
|
||||||
printf("d: %d, c: %d\n", minuteStepPosition, currentMinutePosition);
|
|
||||||
|
|
||||||
currentMinutePosition += minuteDirection;
|
|
||||||
|
|
||||||
PORTC |= (1 << PC3);
|
|
||||||
_delay_us(50);
|
|
||||||
PORTC &= ~(1 << PC3);
|
|
||||||
|
|
||||||
if (currentMinutePosition == minuteStepPosition) {
|
|
||||||
stepperState = 3;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
printf("3\n");
|
|
||||||
|
|
||||||
// switch off the minute motor
|
|
||||||
PORTA &= ~(1 << PA1);
|
|
||||||
|
|
||||||
stepperState = 4;
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
printf("4 ");
|
|
||||||
|
|
||||||
// is current hour position different from desired one?
|
|
||||||
hourStepPosition = clock.hour * STEPS_PER_HOUR;
|
|
||||||
|
|
||||||
if (currentHourPosition != hourStepPosition) {
|
|
||||||
printf(" H:\n");
|
|
||||||
|
|
||||||
stepperState = 5;
|
|
||||||
} else {
|
|
||||||
// done
|
|
||||||
printf("\n");
|
|
||||||
stepperState = 0;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
printf("5 ");
|
|
||||||
|
|
||||||
// switch on the hour motor and set the direction
|
|
||||||
PORTA |= (1 << PA0);
|
|
||||||
|
|
||||||
if (currentHourPosition > hourStepPosition) {
|
|
||||||
printf("b\n");
|
|
||||||
hourDirection = -1;
|
|
||||||
PORTC |= (1 << PC0);
|
|
||||||
} else {
|
|
||||||
printf("f\n");
|
|
||||||
hourDirection = 1;
|
|
||||||
PORTC &= ~(1 << PC0);
|
|
||||||
}
|
|
||||||
|
|
||||||
stepperState = 6;
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
printf("6 ");
|
|
||||||
|
|
||||||
// move one step
|
|
||||||
|
|
||||||
printf("d: %d, c: %d\n", hourStepPosition, currentHourPosition);
|
|
||||||
|
|
||||||
currentHourPosition += hourDirection;
|
|
||||||
|
|
||||||
PORTC |= (1 << PC1);
|
|
||||||
_delay_us(50);
|
|
||||||
PORTC &= ~(1 << PC1);
|
|
||||||
|
|
||||||
if (currentHourPosition == hourStepPosition) {
|
|
||||||
stepperState = 7;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 7:
|
|
||||||
printf("7\n");
|
|
||||||
|
|
||||||
// switch off the hour motor
|
|
||||||
PORTA &= ~(1 << PA0);
|
|
||||||
|
|
||||||
stepperState = 8;
|
|
||||||
break;
|
|
||||||
case 8:
|
|
||||||
printf("8\n");
|
|
||||||
|
|
||||||
// done
|
|
||||||
stepperState = 0;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
printf("default\n");
|
|
||||||
stepperState = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (tack != 0) {
|
|
||||||
tack = 0;
|
|
||||||
|
|
||||||
uint8_t light = PIND & (1 << PD7);
|
|
||||||
switch (valid) {
|
|
||||||
case 0:
|
|
||||||
PORTB &= ~(1 << PB0);
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
if (ledToggle != 0) {
|
|
||||||
ledToggle = 0;
|
|
||||||
PORTB &= ~(1 << PB0);
|
|
||||||
} else {
|
|
||||||
ledToggle = 1;
|
|
||||||
if (light != 0) {
|
|
||||||
PORTB |= (1 << PB0);
|
|
||||||
} else {
|
|
||||||
PORTB &= ~(1 << PB0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
if (light != 0) {
|
|
||||||
PORTB |= (1 << PB0);
|
|
||||||
} else {
|
|
||||||
PORTB &= ~(1 << PB0);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
32
src/main.h
Normal file
32
src/main.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#ifndef MAIN_H_
|
||||||
|
#define MAIN_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define START_GAP_MIN 1500
|
||||||
|
#define ONE_WIDTH_MIN 170
|
||||||
|
#define ONE_WIDTH_MAX 260
|
||||||
|
#define ZERO_WIDTH_MIN 70
|
||||||
|
#define ZERO_WIDTH_MAX 150
|
||||||
|
|
||||||
|
typedef struct receivedData_s {
|
||||||
|
uint8_t minuteDigit0;
|
||||||
|
uint8_t minuteDigit1;
|
||||||
|
uint8_t minuteOneCnt;
|
||||||
|
uint8_t hourDigit0;
|
||||||
|
uint8_t hourDigit1;
|
||||||
|
uint8_t hourOneCnt;
|
||||||
|
uint8_t dayDigit0;
|
||||||
|
uint8_t dayDigit1;
|
||||||
|
uint8_t weekdayDigit0;
|
||||||
|
uint8_t monthDigit0;
|
||||||
|
uint8_t monthDigit1;
|
||||||
|
uint8_t yearDigit0;
|
||||||
|
uint8_t yearDigit1;
|
||||||
|
uint8_t dateOneCnt;
|
||||||
|
} receivedData_t;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* MAIN_H_ */
|
267
src/stepper.c
Normal file
267
src/stepper.c
Normal file
@ -0,0 +1,267 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <avr/io.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <util/delay.h>
|
||||||
|
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
#include "clock.h"
|
||||||
|
#include "stepper.h"
|
||||||
|
|
||||||
|
#define STEPPER_REG_1_DIR DDRC
|
||||||
|
#define STEPPER_REG_1 PORTC
|
||||||
|
|
||||||
|
#define HOUR_MOTOR_DIRECTION PC0
|
||||||
|
#define HOUR_MOTOR_PULSE PC1
|
||||||
|
#define MINUTE_MOTOR_DIRECTION PC2
|
||||||
|
#define MINUTE_MOTOR_PULSE PC3
|
||||||
|
|
||||||
|
#define STEPPER_CTRL_RESET PC4
|
||||||
|
|
||||||
|
#define STEPPER_REG_2_DIR DDRA
|
||||||
|
#define STEPPER_REG_2 PORTA
|
||||||
|
|
||||||
|
#define HOUR_MOTOR_ENABLE PA0
|
||||||
|
#define MINUTE_MOTOR_ENABLE PA1
|
||||||
|
|
||||||
|
#define STEPPER_REG_3_DIR DDRB
|
||||||
|
#define STEPPER_REG_3 PORTB
|
||||||
|
|
||||||
|
#define HOUR_POSITION_SWITCH PB1
|
||||||
|
#define MINUTE_POSITION_SWITCH PB2
|
||||||
|
|
||||||
|
|
||||||
|
uint8_t stepperEnableFlag = 0;
|
||||||
|
|
||||||
|
|
||||||
|
static inline void hourMotorEnable() {
|
||||||
|
STEPPER_REG_2 |= (1 << HOUR_MOTOR_ENABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void hourMotorDisable() {
|
||||||
|
STEPPER_REG_2 &= ~(1 << HOUR_MOTOR_ENABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void minuteMotorEnable() {
|
||||||
|
STEPPER_REG_2 |= (1 << MINUTE_MOTOR_ENABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void minuteMotorDisable() {
|
||||||
|
STEPPER_REG_2 &= ~(1 << MINUTE_MOTOR_ENABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static inline void hourMotorForward() {
|
||||||
|
STEPPER_REG_1 &= ~(1 << HOUR_MOTOR_DIRECTION);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void hourMotorBackward() {
|
||||||
|
STEPPER_REG_1 |= (1 << HOUR_MOTOR_DIRECTION);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void minuteMotorForward() {
|
||||||
|
STEPPER_REG_1 &= ~(1 << MINUTE_MOTOR_DIRECTION);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void minuteMotorBackward() {
|
||||||
|
STEPPER_REG_1 |= (1 << MINUTE_MOTOR_DIRECTION);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static inline void hourMotorPulse() {
|
||||||
|
STEPPER_REG_1 |= (1 << HOUR_MOTOR_PULSE);
|
||||||
|
_delay_us(50);
|
||||||
|
STEPPER_REG_1 &= ~(1 << HOUR_MOTOR_PULSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void minuteMotorPulse() {
|
||||||
|
STEPPER_REG_1 |= (1 << MINUTE_MOTOR_PULSE);
|
||||||
|
_delay_us(50);
|
||||||
|
STEPPER_REG_1 &= ~(1 << MINUTE_MOTOR_PULSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static inline void stepperCtrlReset() {
|
||||||
|
STEPPER_REG_1 &= ~(1 << STEPPER_CTRL_RESET);
|
||||||
|
_delay_ms(1);
|
||||||
|
STEPPER_REG_1 |= (1 << STEPPER_CTRL_RESET);
|
||||||
|
}
|
||||||
|
|
||||||
|
void stepperInit() {
|
||||||
|
// stepper
|
||||||
|
STEPPER_REG_1_DIR |= (1 << STEPPER_CTRL_RESET) |
|
||||||
|
(1 << MINUTE_MOTOR_PULSE) | (1 << MINUTE_MOTOR_DIRECTION) |
|
||||||
|
(1 << HOUR_MOTOR_PULSE) | (1 << HOUR_MOTOR_DIRECTION);
|
||||||
|
STEPPER_REG_2_DIR |= (1 << MINUTE_MOTOR_ENABLE) | (1 << HOUR_MOTOR_ENABLE);
|
||||||
|
|
||||||
|
STEPPER_REG_1 &= ~(1 << MINUTE_MOTOR_PULSE);
|
||||||
|
STEPPER_REG_1 &= ~(1 << MINUTE_MOTOR_DIRECTION);
|
||||||
|
STEPPER_REG_1 &= ~(1 << HOUR_MOTOR_PULSE);
|
||||||
|
STEPPER_REG_1 &= ~(1 << HOUR_MOTOR_DIRECTION);
|
||||||
|
|
||||||
|
STEPPER_REG_2 &= ~(1 << MINUTE_MOTOR_ENABLE);
|
||||||
|
STEPPER_REG_2 &= ~(1 << HOUR_MOTOR_ENABLE);
|
||||||
|
|
||||||
|
// position switches
|
||||||
|
STEPPER_REG_3_DIR &= ~(1 << MINUTE_POSITION_SWITCH);
|
||||||
|
STEPPER_REG_3_DIR &= ~(1 << HOUR_POSITION_SWITCH);
|
||||||
|
|
||||||
|
// pull-up on
|
||||||
|
STEPPER_REG_3 |= (1 << MINUTE_POSITION_SWITCH) | (1 << HOUR_POSITION_SWITCH);
|
||||||
|
|
||||||
|
|
||||||
|
stepperCtrlReset();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//static uint8_t oldPositionSwitch = 255;
|
||||||
|
//uint8_t positionSwitch = PINB & ((1 << PB1) | (1 << PB2));
|
||||||
|
//if (oldPositionSwitch != positionSwitch) {
|
||||||
|
// oldPositionSwitch = positionSwitch;
|
||||||
|
// printf("POS: %02d\n", positionSwitch);
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
void stepperEnable() {
|
||||||
|
stepperEnableFlag = 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
void stepperEngine() {
|
||||||
|
|
||||||
|
static uint8_t stepperState = 0;
|
||||||
|
|
||||||
|
static uint8_t currentMinutePosition = 0;
|
||||||
|
static uint8_t minuteStepPosition = 0;
|
||||||
|
static int8_t minuteDirection = 0;
|
||||||
|
|
||||||
|
static uint8_t currentHourPosition = 0;
|
||||||
|
static uint8_t hourStepPosition = 0;
|
||||||
|
static int8_t hourDirection = 0;
|
||||||
|
|
||||||
|
clock_t clock = clockGetClock();
|
||||||
|
|
||||||
|
switch (stepperState) {
|
||||||
|
case 0:
|
||||||
|
//printf("0 ");
|
||||||
|
if (stepperEnableFlag == 1) {
|
||||||
|
// is current minute position different from desired one?
|
||||||
|
minuteStepPosition = clock.minute * STEPS_PER_MINUTE;
|
||||||
|
|
||||||
|
if (currentMinutePosition != minuteStepPosition) {
|
||||||
|
printf("M:\n");
|
||||||
|
|
||||||
|
stepperState = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
printf("1 ");
|
||||||
|
// switch on the minute motor and set the direction
|
||||||
|
minuteMotorEnable();
|
||||||
|
|
||||||
|
if (currentMinutePosition > minuteStepPosition) {
|
||||||
|
printf("b\n");
|
||||||
|
minuteDirection = -1;
|
||||||
|
minuteMotorBackward();
|
||||||
|
} else {
|
||||||
|
printf("f\n");
|
||||||
|
minuteDirection = 1;
|
||||||
|
minuteMotorForward();
|
||||||
|
}
|
||||||
|
|
||||||
|
stepperState = 2;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
printf("2 ");
|
||||||
|
|
||||||
|
// move one step
|
||||||
|
|
||||||
|
printf("d: %d, c: %d\n", minuteStepPosition, currentMinutePosition);
|
||||||
|
|
||||||
|
currentMinutePosition += minuteDirection;
|
||||||
|
|
||||||
|
minuteMotorPulse();
|
||||||
|
|
||||||
|
if (currentMinutePosition == minuteStepPosition) {
|
||||||
|
stepperState = 3;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
printf("3\n");
|
||||||
|
|
||||||
|
// switch off the minute motor
|
||||||
|
minuteMotorDisable();
|
||||||
|
|
||||||
|
stepperState = 4;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
printf("4 ");
|
||||||
|
|
||||||
|
// is current hour position different from desired one?
|
||||||
|
hourStepPosition = clock.hour * STEPS_PER_HOUR;
|
||||||
|
|
||||||
|
if (currentHourPosition != hourStepPosition) {
|
||||||
|
printf(" H:\n");
|
||||||
|
|
||||||
|
stepperState = 5;
|
||||||
|
} else {
|
||||||
|
// done
|
||||||
|
printf("\n");
|
||||||
|
stepperState = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
printf("5 ");
|
||||||
|
|
||||||
|
// switch on the hour motor and set the direction
|
||||||
|
hourMotorEnable();
|
||||||
|
|
||||||
|
if (currentHourPosition > hourStepPosition) {
|
||||||
|
printf("b\n");
|
||||||
|
hourDirection = -1;
|
||||||
|
hourMotorBackward();
|
||||||
|
} else {
|
||||||
|
printf("f\n");
|
||||||
|
hourDirection = 1;
|
||||||
|
hourMotorForward();
|
||||||
|
}
|
||||||
|
|
||||||
|
stepperState = 6;
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
printf("6 ");
|
||||||
|
|
||||||
|
// move one step
|
||||||
|
|
||||||
|
printf("d: %d, c: %d\n", hourStepPosition, currentHourPosition);
|
||||||
|
|
||||||
|
currentHourPosition += hourDirection;
|
||||||
|
|
||||||
|
hourMotorPulse();
|
||||||
|
|
||||||
|
if (currentHourPosition == hourStepPosition) {
|
||||||
|
stepperState = 7;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
printf("7\n");
|
||||||
|
|
||||||
|
// switch off the hour motor
|
||||||
|
hourMotorDisable();
|
||||||
|
|
||||||
|
stepperState = 8;
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
printf("8\n");
|
||||||
|
|
||||||
|
// done
|
||||||
|
stepperState = 0;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("default\n");
|
||||||
|
stepperState = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
18
src/stepper.h
Normal file
18
src/stepper.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#ifndef STEPPER_H_
|
||||||
|
#define STEPPER_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "main.h"
|
||||||
|
#include "clock.h"
|
||||||
|
|
||||||
|
#define STEPS_PER_MINUTE 3
|
||||||
|
#define STEPS_PER_HOUR 7
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void stepperInit();
|
||||||
|
void stepperEnable();
|
||||||
|
void stepperEngine();
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* STEPPER_H_ */
|
@ -6,21 +6,28 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define BUFFER_SIZE 256
|
volatile uint8_t buffer[UART_TX_BUFFER_SIZE+5];
|
||||||
volatile uint8_t buffer[BUFFER_SIZE+5];
|
|
||||||
volatile uint8_t bufferReadIdx = 0;
|
volatile uint8_t bufferReadIdx = 0;
|
||||||
volatile uint8_t bufferWriteIdx = 0;
|
volatile uint8_t bufferWriteIdx = 0;
|
||||||
|
|
||||||
void uartdrvInit() {
|
static inline void enableDataRegisterEmptyInterrupt() {
|
||||||
|
UCSRB |= (1 << UDRIE);
|
||||||
|
}
|
||||||
|
|
||||||
// UART
|
static inline void disableDataRegisterEmptyInterrupt() {
|
||||||
|
UCSRB &= ~(1 << UDRIE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void uartdrvInit() {
|
||||||
// TXD
|
// TXD
|
||||||
DDRD |= (1 << PD1);
|
DDRD |= (1 << PD1);
|
||||||
|
|
||||||
// transmitter enable
|
// transmitter enable
|
||||||
UCSRB = (1 << TXEN);
|
UCSRB = (1 << TXEN);
|
||||||
|
|
||||||
// 8 data bit, no parity, 1 stop bit
|
// 8 data bit, no parity, 1 stop bit
|
||||||
UCSRC = (1 << UCSZ1) | (1 << UCSZ0);
|
UCSRC = (1 << UCSZ1) | (1 << UCSZ0);
|
||||||
|
|
||||||
// 2400 Baud @ 16MHz
|
// 2400 Baud @ 16MHz
|
||||||
UBRRL = 0xa0;
|
UBRRL = 0xa0;
|
||||||
UBRRH = 0x01;;
|
UBRRH = 0x01;;
|
||||||
@ -30,16 +37,16 @@ void uartdrvInit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void uartdrvWrite(uint8_t o) {
|
void uartdrvWrite(uint8_t o) {
|
||||||
// loop_until_bit_is_set(UCSRA, UDRE);
|
if (bufferWriteIdx == UART_TX_BUFFER_SIZE - 1) {
|
||||||
// UDR = o;
|
while (bufferReadIdx == UART_TX_BUFFER_SIZE);
|
||||||
if (bufferWriteIdx == BUFFER_SIZE - 1) {
|
|
||||||
while (bufferReadIdx == BUFFER_SIZE);
|
|
||||||
} else {
|
} else {
|
||||||
while (bufferReadIdx == bufferWriteIdx + 1);
|
while (bufferReadIdx == bufferWriteIdx + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer[bufferWriteIdx] = o;
|
buffer[bufferWriteIdx] = o;
|
||||||
bufferWriteIdx++;
|
bufferWriteIdx++;
|
||||||
UCSRB |= (1 << UDRIE);
|
|
||||||
|
enableDataRegisterEmptyInterrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -55,10 +62,10 @@ ISR(USART_UDRE_vect) {
|
|||||||
if (bufferReadIdx != bufferWriteIdx) {
|
if (bufferReadIdx != bufferWriteIdx) {
|
||||||
UDR = buffer[bufferReadIdx];
|
UDR = buffer[bufferReadIdx];
|
||||||
bufferReadIdx++;
|
bufferReadIdx++;
|
||||||
if (bufferReadIdx > BUFFER_SIZE) {
|
if (bufferReadIdx > UART_TX_BUFFER_SIZE) {
|
||||||
bufferReadIdx = 0;
|
bufferReadIdx = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
UCSRB &= ~(1 << UDRIE);
|
disableDataRegisterEmptyInterrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,28 +5,12 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
#define UART_CMD_BUF_SIZE 4
|
|
||||||
#define UART_CMD_CYCLES 10
|
|
||||||
|
|
||||||
typedef struct uartdrvCmdBuf_s {
|
#define UART_TX_BUFFER_SIZE 256
|
||||||
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);
|
int uartdrvPutchar(char c, FILE *stream);
|
||||||
void uartdrvInit();
|
void uartdrvInit();
|
||||||
void uartdrvWrite(uint8_t o);
|
void uartdrvWrite(uint8_t o);
|
||||||
|
|
||||||
void uartdrvCmdCycle();
|
|
||||||
|
|
||||||
void uartdrvCmdReceived();
|
|
||||||
|
|
||||||
#endif /* UARTDRV_H_ */
|
#endif /* UARTDRV_H_ */
|
||||||
|
75
src/validLed.c
Normal file
75
src/validLed.c
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <avr/io.h>
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define VALID_LED_REG_DIR DDRB
|
||||||
|
#define VALID_LED_REG PORTB
|
||||||
|
|
||||||
|
#define VALID_LED PB0
|
||||||
|
|
||||||
|
#define DARK_SWITCH_REG_DIR DDRD
|
||||||
|
#define DARK_SWITCH_OUT_REG PORTD
|
||||||
|
#define DARK_SWITCH_IN_REG PIND
|
||||||
|
|
||||||
|
#define DARK_SWITCH PD7
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
uint8_t isNotDark() {
|
||||||
|
return DARK_SWITCH_IN_REG & (1 << DARK_SWITCH);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void validLedEnable() {
|
||||||
|
PORTB |= (1 << PB0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void validLedDisable() {
|
||||||
|
PORTB &= ~(1 << PB0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void validLedInit() {
|
||||||
|
// validLed
|
||||||
|
VALID_LED_REG_DIR |= (1 << VALID_LED);
|
||||||
|
|
||||||
|
// dark switch
|
||||||
|
DARK_SWITCH_REG_DIR &= ~(1 << DARK_SWITCH);
|
||||||
|
// pull-up on
|
||||||
|
DARK_SWITCH_OUT_REG |= (1 << DARK_SWITCH);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void validLedEngine(uint8_t valid) {
|
||||||
|
static uint8_t ledToggle = 0;
|
||||||
|
|
||||||
|
uint8_t light = isNotDark();
|
||||||
|
|
||||||
|
switch (valid) {
|
||||||
|
case 0:
|
||||||
|
validLedDisable();
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
if (ledToggle != 0) {
|
||||||
|
ledToggle = 0;
|
||||||
|
validLedDisable();
|
||||||
|
} else {
|
||||||
|
ledToggle = 1;
|
||||||
|
if (light != 0) {
|
||||||
|
validLedEnable();
|
||||||
|
} else {
|
||||||
|
validLedDisable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
if (light != 0) {
|
||||||
|
validLedEnable();
|
||||||
|
} else {
|
||||||
|
validLedDisable();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
10
src/validLed.h
Normal file
10
src/validLed.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef VALIDLED_H_
|
||||||
|
#define VALIDLED_H_
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void validLedInit();
|
||||||
|
void validLedEngine(uint8_t valid);
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* VALIDLED_H_ */
|
Reference in New Issue
Block a user