From 440039cb23f786573940ec1eafa665f9de561eae Mon Sep 17 00:00:00 2001 From: root Date: Mon, 29 Nov 2021 12:40:33 +0000 Subject: fish --- docs/stm32f103c8.pdf | Bin 0 -> 2006428 bytes stm32/app/hands.c | 8 +++ stm32/app/main.c | 1 + stm32/app/motor.c | 171 +++++++++++++++++++++++++++++++++++++++++++-------- stm32/app/project.h | 3 +- 5 files changed, 158 insertions(+), 25 deletions(-) create mode 100644 docs/stm32f103c8.pdf diff --git a/docs/stm32f103c8.pdf b/docs/stm32f103c8.pdf new file mode 100644 index 0000000..cbe5419 Binary files /dev/null and b/docs/stm32f103c8.pdf differ diff --git a/stm32/app/hands.c b/stm32/app/hands.c index 8261b1b..f1543ac 100644 --- a/stm32/app/hands.c +++ b/stm32/app/hands.c @@ -88,6 +88,7 @@ void hands_tick (void) m = time_to_metric (e, u); +#if 0 hands_pos[0] = calc_hour_pos (&m); if ((e.s & 31) <5) { @@ -95,10 +96,16 @@ if ((e.s & 31) <5) { } else { hands_pos[1] = calc_minute_pos (&m); } +#endif + + hands_pos[0] = calc_second_pos (&m); + hands_pos[1] = calc_minute_pos (&m); + //hands_pos[1] = calc_second_pos (&m); hands_ready = 1; +#if 0 for (i = 0, p = 0; i < HANDS; ++i) { if (hands_pos[i] != old_pos[i]) p++; @@ -112,5 +119,6 @@ if ((e.s & 31) <5) { time_print_metric ("Metric: ", &m, NULL); printf ("Hands: hour %4d/%4d min %4d/%4d\r\n", hands_pos[0], MOTOR_STEPS, hands_pos[1], MOTOR_STEPS); } +#endif } diff --git a/stm32/app/main.c b/stm32/app/main.c index 31f220d..b8dbf43 100644 --- a/stm32/app/main.c +++ b/stm32/app/main.c @@ -13,6 +13,7 @@ int main (void) rcc_periph_clock_enable (RCC_GPIOA); rcc_periph_clock_enable (RCC_GPIOB); rcc_periph_clock_enable (RCC_GPIOC); + rcc_periph_clock_enable (RCC_GPIOD); rcc_periph_clock_enable (RCC_USART1); rcc_periph_clock_enable (RCC_AFIO); rcc_periph_clock_enable (RCC_PWR); diff --git a/stm32/app/motor.c b/stm32/app/motor.c index ca9fa13..621812d 100644 --- a/stm32/app/motor.c +++ b/stm32/app/motor.c @@ -4,6 +4,8 @@ static unsigned motor_pos[HANDS]; static unsigned off[HANDS]; +#ifdef HBRIDGE + #define M1_A GPIO11 #define M1_A_PORT GPIOB #define M1_B GPIO12 @@ -13,13 +15,32 @@ static unsigned off[HANDS]; #define M1_D GPIO14 #define M1_D_PORT GPIOB +#else + +#define M1_DIR GPIO0 +#define M1_DIR_PORT GPIOB +#define M1_STEP GPIO1 +#define M1_STEP_PORT GPIOB +#define M1_EN GPIO2 +#define M1_EN_PORT GPIOB + +#define M2_DIR GPIO10 +#define M2_DIR_PORT GPIOC +#define M2_STEP GPIO11 +#define M2_STEP_PORT GPIOC +#define M2_EN GPIO12 +#define M2_EN_PORT GPIOC + + #define ADJUST_CCW GPIO0 #define ADJUST_CCW_PORT GPIOC #define ADJUST_CW GPIO1 #define ADJUST_CW_PORT GPIOC +#endif +#ifdef HBRIDGE static void coils (unsigned m, int a, int b, int c, int d) { @@ -63,17 +84,17 @@ b^=d; //printf ("Motor %d: %+d %+d\r\n", m, a - b, c - d); } +#endif static void step (unsigned m, int d) { unsigned s = motor_pos[m]; - if (d < 0) d += MOTOR_STEPS; - - s += d; + s += MOTOR_STEPS + d; s %= MOTOR_STEPS; +#ifdef HBRIDGE #if 0 // full step switch (s & 3) { @@ -123,6 +144,35 @@ break; } #endif +#else + +if (d<0) { +if (!m) { + SET(M1_DIR); + SET(M1_STEP); + delay_us(1); + CLEAR(M1_STEP); +} else { + SET(M2_DIR); + SET(M2_STEP); + delay_us(1); + CLEAR(M2_STEP); +} +} else if (d>0) { +if (!m) { + CLEAR(M1_DIR); + SET(M1_STEP); + delay_us(1); + CLEAR(M1_STEP); +} else { + CLEAR(M2_DIR); + SET(M2_STEP); + delay_us(1); + CLEAR(M2_STEP); +} +} + +#endif if (!m) @@ -135,47 +185,61 @@ break; -#define ADJ_P 2500 - void motor_tick (void) { static uint32_t last,blast; unsigned i, d; - static unsigned adjusting = ADJ_P; /*5 seconds*/ + static unsigned adjusting =0; + static unsigned zero=8000; unsigned hp[2]; + static unsigned s; - if ((ticks - last) < 2) return; - last=ticks; - if ((ticks - blast) > 100) { - if (!GET(ADJUST_CCW)) { - adjusting=ADJ_P; + //if ((ticks - last) < 1) return; + //last=ticks; - motor_pos[1]+=MOTOR_PHASES; - motor_pos[1]%=MOTOR_STEPS; + if ((ticks - blast) > 1000) { + + if (!GET(ADJUST_CCW)) { + adjusting=8000; } if (!GET(ADJUST_CW)) { - adjusting=ADJ_P; - - motor_pos[1]+=MOTOR_STEPS-MOTOR_PHASES; - motor_pos[1]%=MOTOR_STEPS; + zero=8000; } blast=ticks; - } - +#ifndef HBRIDGE + if (adjusting) { + adjusting--; + + if (adjusting) { + SET(M1_EN); + SET(M2_EN); + } else { + CLEAR(M1_EN); + CLEAR(M2_EN); + } + + motor_pos[0]=motor_pos[1]=0; + step(0,0); + step(1,0); + + return; + } +#endif + - if (adjusting || !hands_ready) { - if (adjusting) adjusting--; + if (zero || !hands_ready) { + if (zero) zero--; hp[0]=0; hp[1]=0; } else { @@ -186,6 +250,36 @@ void motor_tick (void) //printf("HANDS: %d -> %d %d -> %d\r\n",hands_pos[0],motor_pos[0],hands_pos[1],motor_pos[1]); +#if 0 +{ +unsigned static r=0; +r++; +r%=6000; + +if (r<3000) { +hp[0]=0; +} +} +#endif + +{ unsigned static s=0; + + +if (!s) { + printf("%4d %4d hp[0]=%6d mp[0]=%6d hp[1]=%6d mp[1]=%6d\r\n", + zero,adjusting, hp[0],motor_pos[0],hp[1],motor_pos[1]); +} + + + + +s++; +s%=500; +} + + + + for (i = 0; i < HANDS; ++i) { d = MOTOR_STEPS; d+=hp[i]; @@ -200,6 +294,9 @@ void motor_tick (void) step (i, -1); } } + + + } @@ -208,13 +305,40 @@ void motor_tick (void) void motor_init (void) { + MAP_INPUT_PU(ADJUST_CCW); + MAP_INPUT_PU(ADJUST_CW); + +#ifdef HBRIDGE MAP_OUTPUT_PP (M1_A); MAP_OUTPUT_PP (M1_B); MAP_OUTPUT_PP (M1_C); MAP_OUTPUT_PP (M1_D); - MAP_INPUT_PU(ADJUST_CCW); - MAP_INPUT_PU(ADJUST_CW); + +#else + + MAP_OUTPUT_PP (M1_EN); + MAP_OUTPUT_PP (M1_DIR); + MAP_OUTPUT_PP (M1_STEP); + + MAP_OUTPUT_PP (M2_EN); + MAP_OUTPUT_PP (M2_DIR); + MAP_OUTPUT_PP (M2_STEP); + + SET(M1_EN); + CLEAR(M1_STEP); + CLEAR(M1_DIR); + delay_us(2); + CLEAR(M1_EN); + + + SET(M2_EN); + CLEAR(M2_STEP); + CLEAR(M2_DIR); + delay_us(2); + CLEAR(M2_EN); +#endif + motor_pos[0] = BKP_DR8; @@ -223,5 +347,4 @@ void motor_init (void) step (0, 0); step (1, 0); - } diff --git a/stm32/app/project.h b/stm32/app/project.h index 97b992c..471683b 100644 --- a/stm32/app/project.h +++ b/stm32/app/project.h @@ -37,7 +37,8 @@ #define HANDS 2 //#define MOTOR_STEPS (1080 * 2) //#define MOTOR_STEPS (720 ) -#define MOTOR_STEPS (1080 ) +#define USTEP 16 +#define MOTOR_STEPS (720*USTEP) #define MOTOR_PHASES 6 #include "board.h" -- cgit v1.2.3