diff --git a/src/stepper.c b/src/stepper.c index be4437e..89a258e 100644 --- a/src/stepper.c +++ b/src/stepper.c @@ -6,6 +6,7 @@ #include "clock.h" #include "stepper.h" +#include "validLed.h" #define STEPPER_REG_1_DIR DDRC #define STEPPER_REG_1 PORTC @@ -50,19 +51,19 @@ static inline void minuteMotorDisable() { } -static inline void hourMotorForward() { +static inline void hourMotorBackward() { STEPPER_REG_1 &= ~(1 << HOUR_MOTOR_DIRECTION); } -static inline void hourMotorBackward() { +static inline void hourMotorForward() { STEPPER_REG_1 |= (1 << HOUR_MOTOR_DIRECTION); } -static inline void minuteMotorBackward() { +static inline void minuteMotorForward() { STEPPER_REG_1 &= ~(1 << MINUTE_MOTOR_DIRECTION); } -static inline void minuteMotorForward() { +static inline void minuteMotorBackward() { STEPPER_REG_1 |= (1 << MINUTE_MOTOR_DIRECTION); } @@ -113,12 +114,6 @@ void stepperInit() { } -//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() { @@ -127,22 +122,111 @@ void stepperEnable() { } void stepperEngine() { - static uint8_t stepperState = 0; + static uint8_t stepperState = 100; - static uint8_t currentMinutePosition = 0; - static uint8_t minuteStepPosition = 0; + static uint16_t currentMinutePosition = 0; + static uint16_t minuteStepPosition = 0; static int8_t minuteDirection = 0; - static uint8_t currentHourPosition = 0; - static uint8_t hourStepPosition = 0; + static uint16_t currentHourPosition = 0; + static uint16_t hourStepPosition = 0; static int8_t hourDirection = 0; if (clockNextStep()) { + static uint8_t oldPositionSwitch = 255; + uint8_t positionSwitch = PINB & ((1 << PB1) | (1 << PB2)); + uint8_t minutePositionSwitch = (positionSwitch & (1 << PB2)) != 0; + uint8_t hourPositionSwitch = (positionSwitch & (1 << PB1)) != 0; + if (oldPositionSwitch != positionSwitch) { + oldPositionSwitch = positionSwitch; + printf("POS: %02d\n", positionSwitch); + printf("MinuteSwitch: %d\n", minutePositionSwitch); + printf("HourSwitch: %d\n", hourPositionSwitch); + + } + + clock_t clock = clockGetClock(); switch (stepperState) { + case 100: + printf("100 Minute zeroing "); + minuteMotorEnable(); + minuteMotorBackward(); + currentMinutePosition = 0; + stepperState = 101; + break; + case 101: + printf("101 "); + currentMinutePosition++; + printf("%d\n", currentMinutePosition); + minuteMotorPulse(); + if (minutePositionSwitch == 0) { + stepperState = 102; + } + if (currentMinutePosition > 500) { + stepperState = 200; + } + break; + case 102: + printf("102 Minute zeroed\n"); + currentMinutePosition = 0; + minuteMotorDisable(); + stepperState = 103; + break; + case 103: + printf("103 Hour zeroing "); + hourMotorEnable(); + hourMotorBackward(); + currentHourPosition = 0; + stepperState = 104; + break; + case 104: + printf("104 "); + currentHourPosition++; + printf("%d\n", currentHourPosition); + hourMotorPulse(); + if (hourPositionSwitch == 0) { + stepperState = 105; + } + if (currentHourPosition > 500) { + stepperState = 201; + } + break; + case 105: + printf("105 Hour zeroed\n"); + currentHourPosition = 0; + hourMotorDisable(); + stepperState = 0; + break; + case 200: + printf("200 Minute zeroing error\n"); + { + static uint8_t ledToggle = 0; + if (ledToggle != 0) { + ledToggle = 0; + validLedDisable(); + } else { + ledToggle = 1; + validLedEnable(); + } + } + break; + case 201: + printf("201 Hour zeroing error\n"); + { + static uint8_t ledToggle = 0; + if (ledToggle != 0) { + ledToggle = 0; + validLedDisable(); + } else { + ledToggle = 1; + validLedEnable(); + } + } + break; case 0: //printf("0 "); if (stepperEnableFlag == 1) { diff --git a/src/validLed.c b/src/validLed.c index ad3d68d..bdb5803 100644 --- a/src/validLed.c +++ b/src/validLed.c @@ -39,11 +39,11 @@ uint8_t isNotDark() { return DARK_SWITCH_IN_REG & (1 << DARK_SWITCH); } -static inline void validLedEnable() { +void validLedEnable() { VALID_LED_REG |= (1 << VALID_LED); } -static inline void validLedDisable() { +void validLedDisable() { VALID_LED_REG &= ~(1 << VALID_LED); } diff --git a/src/validLed.h b/src/validLed.h index 5dadb79..8c9b6b0 100644 --- a/src/validLed.h +++ b/src/validLed.h @@ -7,5 +7,7 @@ void validLedInit(); void validLedEngine(); void validLedSetStatus(uint8_t x); +void validLedEnable(); +void validLedDisable(); #endif /* VALIDLED_H_ */