refactoring

This commit is contained in:
hg
2013-01-29 12:39:36 +01:00
parent 6970336f53
commit 314c6eba8d
10 changed files with 677 additions and 365 deletions

View File

@ -3,101 +3,30 @@
#include <avr/interrupt.h>
#include <util/delay.h>
#include "main.h"
#include "clock.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;
volatile clock_t clock;
volatile opTime_t opTime;
extern volatile clock_t clock;
volatile uint8_t tick;
volatile uint16_t captValue = 0;
volatile uint16_t gapValue = 0;
volatile uint8_t valid = 0;
volatile uint8_t tack = 0;
volatile uint8_t step = 0;
uint16_t validCnt = 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) {
uint16_t tmpCnt = TCNT1;
@ -110,40 +39,26 @@ ISR(INT1_vect) {
tick = 1;
}
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;
void setValid(uint8_t x) {
valid = x;
if (valid == 1) {
validCnt++;
clockSetUseLocalClock(1);
} else if (valid == 2) {
timeValidCnt++;
clockSetUseLocalClock(0);
stepperEnable();
} else {
clockSetUseLocalClock(1);
}
}
uint8_t getValid() {
return valid;
}
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
TCCR1A = 0;
@ -158,35 +73,13 @@ int main() {
// esd prot
DDRD |= (1 << PD5) | (1 << PD6);
// valid led
DDRB |= (1 << PB0);
TCCR0 = (1 << CS02) | (1 << CS00);
TIMSK |= (1 << TOIE0);
// position switches
DDRB &= ~(1 << PB1);
DDRB &= ~(1 << PB2);
PORTB |= (1 << PB1) | (1 << PB2);
uartdrvInit();
clockInit();
stepperInit();
validLedInit();
// dark switch
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);
setValid(0);
sei();
@ -195,9 +88,6 @@ int main() {
uint8_t bit = 0;
uint8_t state = 0;
uint16_t validCnt = 0;
uint16_t timeValidCnt = 0;
uint8_t ledToggle = 0;
while (1) {
@ -209,11 +99,10 @@ int main() {
if (gap > START_GAP_MIN) {
if (valid == 0) {
validCnt++;
valid = 1;
setValid(1);;
}
state = 0;
clock.second = 0;
clockClearSecond();
}
if (pulse > ZERO_WIDTH_MIN && pulse < ZERO_WIDTH_MAX) {
@ -222,7 +111,7 @@ int main() {
bit = 1;
} else {
bit = 2;
valid = 0;
setValid(0);;
}
@ -230,7 +119,7 @@ int main() {
switch (state) {
case 0:
if (bit != 0)
valid = 0;
setValid(0);
break;
case 1:
break;
@ -272,7 +161,7 @@ int main() {
break;
case 20:
if (bit != 1)
valid = 0;
setValid(0);
break;
case 21:
rd.minuteOneCnt = 0;
@ -316,7 +205,7 @@ int main() {
rd.minuteOneCnt += bit;
if (rd.minuteOneCnt % 2 != 0) {
printf("MINUTE INVALID\n");
valid = 0;
setValid(0);
} else {
printf("MINUTE %u %u\n", rd.minuteDigit0, rd.minuteDigit1);
}
@ -358,7 +247,7 @@ int main() {
rd.hourOneCnt += bit;
if (rd.hourOneCnt % 2 != 0) {
printf("HOUR INVALID\n");
valid = 0;
setValid(0);
} else {
printf("HOUR %u %u\n", rd.hourDigit0, rd.hourDigit1);
}
@ -485,23 +374,21 @@ int main() {
rd.dateOneCnt += bit;
if (rd.dateOneCnt % 2 != 0) {
printf("DATE INVALID\n");
valid = 0;
setValid(0);
} else {
printf("DAY %u %u\n", rd.dayDigit0, rd.dayDigit1);
printf("WEEKDAY %u\n", rd.weekdayDigit0);
printf("MONTH %u %u\n", rd.monthDigit0, rd.monthDigit1);
printf("YEAR %u %u\n", rd.yearDigit0, rd.yearDigit1);
clock.minute = rd.minuteDigit0 + rd.minuteDigit1 * 10;
clock.hour = rd.hourDigit0 + rd.hourDigit1 * 10;
clock.day = rd.dayDigit0 + rd.dayDigit1 * 10;
clock.weekday = rd.weekdayDigit0;
clock.month = rd.monthDigit0 + rd.monthDigit1 * 10;
clock.year = rd.yearDigit0 + rd.yearDigit1 * 10;
clockSetClock(rd.yearDigit0 + rd.yearDigit1 * 10,
rd.monthDigit0 + rd.monthDigit1 * 10,
rd.dayDigit0 + rd.dayDigit1 * 10,
rd.weekdayDigit0,
rd.hourDigit0 + rd.hourDigit1 * 10,
rd.minuteDigit0 + rd.minuteDigit1 * 10);
valid = 2;
timeValidCnt++;
setValid(2);
}
break;
case 59:
@ -511,202 +398,32 @@ int main() {
break;
}
printf("% 4d:%02d:%02d %02d:%02d:%02d %02d.%02d.%02d %d %d %02d %d %d %d %d %d %d %d\n",
opTime.hour, opTime.minute, opTime.second,
printf("%02d:%02d:%02d %02d.%02d.%02d %d %d %02d %d %d %d %d %d\n",
clock.hour, clock.minute, clock.second,
clock.day, clock.month, clock.year, clock.weekday,
valid, state, bit, pulse, gap, validCnt, timeValidCnt,
bufferReadIdx, bufferWriteIdx);
valid, state, bit, pulse, gap, validCnt, timeValidCnt);
state++;
} else {
printf("% 4d:%02d:%02d %02d:%02d:%02d %02d.%02d.%02d %d %d %02d %d %d %d %d %d %d %d\n",
opTime.hour, opTime.minute, opTime.second,
printf("%02d:%02d:%02d %02d.%02d.%02d %d %d %02d %d %d %d %d %d\n",
clock.hour, clock.minute, clock.second,
clock.day, clock.month, clock.year, clock.weekday,
valid, state, bit, pulse, gap, validCnt, timeValidCnt,
bufferReadIdx, bufferWriteIdx);
valid, state, bit, pulse, gap, validCnt, timeValidCnt);
}
}
static uint8_t oldPositionSwitch = 255;
uint8_t positionSwitch = PINB & ((1 << PB1) | (1 << PB2));
if (oldPositionSwitch != positionSwitch) {
oldPositionSwitch = positionSwitch;
printf("POS: %02d\n", positionSwitch);
if (clockNextMinute()) {
opTime_t opTime = clockGetOpTime();
printf("% 4d:%02d:%02d\n", opTime.hour, opTime.minute, opTime.second);
}
if ((timeValidCnt > 0) && (step != 0)) {
step = 0;
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;
break;
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 (clockNextStep()) {
stepperEngine();
}
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;
}
if (clockNextTack()) {
validLedEngine(valid);
}
}