From 7de4c0f78fe202fc504ac41a8e5611a77524cc34 Mon Sep 17 00:00:00 2001 From: eddyem Date: Thu, 8 Jun 2017 12:16:05 +0300 Subject: [PATCH] alpha version --- Makefile | 3 +- includes.h | 32 +++++ main.c | 55 ++++---- proto.c | 96 +++++++++----- scorpio2.c.tags => scorpio.c.tags | 119 +++++++----------- scorpio.hex | 201 ++++++++++++++++++++++++++++++ scorpio2.hex | 158 ----------------------- stepper.c | 134 ++++++++++++++------ stepper.h | 6 +- 9 files changed, 475 insertions(+), 329 deletions(-) rename scorpio2.c.tags => scorpio.c.tags (88%) create mode 100644 scorpio.hex delete mode 100644 scorpio2.hex diff --git a/Makefile b/Makefile index 040fd0b..4ccedb3 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,13 @@ ### https://habrahabr.ru/post/247663/ -NAME = scorpio2 +NAME = scorpio OBJCOPY = avr-objcopy OBJDUMP = avr-objdump CC = avr-gcc OPTIMIZE= -Os DEFS = -DBAUD=9600 +DEFS += -DEBUG LIBS = SRC=$(wildcard *.c) diff --git a/includes.h b/includes.h index 9b4307e..be6ec1f 100644 --- a/includes.h +++ b/includes.h @@ -36,5 +36,37 @@ #include "uart.h" #include "stepper.h" +#ifdef EBUG +#define DBG(x) usart_send(x) +#else +#define DBG(x) +#endif + +// #define () do{}while(0) +#if defined (__AVR_ATmega8535__) + // original MCU + #define STPRS_OFF() do{PORTD |= 0xfc; PORTC |= 0x0f;}while(0) + #define DDRAB DDRA + #define PORTAB PORTA + #define LED1_PIN (_BV(0)) + #define LED2_PIN (_BV(1)) + #define LED3_PIN (_BV(2)) + #define FLAT_PIN (_BV(5)) + #define NEON_PIN (_BV(6)) + #define SHTR_PIN (_BV(7)) +#else + // arduino devboard with stepper + #define STPRS_OFF() do{PORTD |= 0xfc; PORTC &= 0xf0;}while(0) + #define DDRAB DDRB + #define PORTAB PORTB + #define FLAT_PIN (_BV(0)) + #define NEON_PIN (_BV(1)) + #define SHTR_PIN (_BV(2)) + #define LED1_PIN (_BV(3)) + #define LED2_PIN (_BV(4)) + #define LED3_PIN (_BV(5)) +#endif + +#define PORTAB_PINS (FLAT_PIN | NEON_PIN | SHTR_PIN | LED1_PIN | LED2_PIN | LED3_PIN) #endif // __INCLUDES_H__ diff --git a/main.c b/main.c index 59e883f..08b9711 100644 --- a/main.c +++ b/main.c @@ -22,10 +22,7 @@ */ #include "includes.h" - -#define LED_PIN (_BV(5)) - - +/* volatile uint16_t Milliseconds = 0, Seconds = 0, days = 0; void print_time(){ @@ -35,21 +32,16 @@ void print_time(){ usart_send("s"); printUint((uint8_t*)&Milliseconds, 2); usart_send("ms\n"); -} +}*/ int main() { - // LED for debug - DDRB |= LED_PIN; /** setup all other pins **/ - PORTD |= 0xfc; // turn off steppers before configuring to output + STPRS_OFF(); // turn off steppers before configuring to output DDRD = 0xfc; // steppers - PORTD |= 0x0f; DDRC = 0x0f; // steppers diagram // 328p have no port A - #if defined (__AVR_ATmega8535__) - DDRA = 0xe0; // flat, neon, shutter - #endif - + PORTAB |= FLAT_PIN | NEON_PIN | SHTR_PIN; // turn all off + DDRAB = PORTAB_PINS; /** USART config **/ // set baudrate (using macros from util/setbaud.h) @@ -65,11 +57,13 @@ int main() { UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0); // Enable RX and TX, enable RX interrupt /** setup timer 0 - system timer **/ - // set prescaler to 64 and start the timer + // set prescaler to 8 and start the timer (2MHz*256 = 0.128ms period) #if defined (__AVR_ATmega8535__) - TCCR0 |= _BV(CS01) | _BV(CS00); + //TCCR0 |= _BV(CS01) | _BV(CS00); // /64 + TCCR0 |= _BV(CS01); #else - TCCR0B |= _BV(CS01) | _BV(CS00); + //TCCR0B |= _BV(CS01) | _BV(CS00); + TCCR0B |= _BV(CS01); #endif TIMSK0 |= _BV(TOIE0); @@ -81,22 +75,33 @@ int main() { while(1){ wdt_reset(); if(stepper_pulse) stepper_process(); - // testing blinking - remove later - if(Milliseconds == 500) PORTB |= LED_PIN; - else if(Milliseconds == 0) PORTB &= ~LED_PIN; if(usart_flags & U_RX_COMPLETE) process_string(); } return 0; } +uint8_t LEDs[3] = {20,20,20}; // LEDs shining time + ISR(TIMER0_OVF_vect){ - TCNT0 += 6; - if(++Milliseconds == 1000){ - Milliseconds = 0; - if(++Seconds == 86400){ - Seconds = 0; - ++days; + static uint8_t shi_counter = 0;/* tick_ctr = 0; + TCNT0 += 6; // 0.125ms period + if(++tick_ctr == 8){ + tick_ctr = 0; + if(++Milliseconds == 1000){ + Milliseconds = 0; + if(++Seconds == 86400){ + Seconds = 0; + ++days; + } } + }*/ + if(shi_counter == 0){ // turn all LEDs on + PORTAB |= LED1_PIN | LED2_PIN | LED3_PIN; } + // now check which LEDs we need to turn off + if(shi_counter == LEDs[0]) PORTAB &= ~LED1_PIN; + if(shi_counter == LEDs[1]) PORTAB &= ~LED2_PIN; + if(shi_counter == LEDs[2]) PORTAB &= ~LED3_PIN; + ++shi_counter; } diff --git a/proto.c b/proto.c index a75369b..b095fcf 100644 --- a/proto.c +++ b/proto.c @@ -35,65 +35,100 @@ uint8_t move_motor(char *cmd){ cmd = omit_whitespace(cmd+1); int16_t steps; if(!readInt(cmd, &steps)) return 0; + #ifdef EBUG usart_send("Move motor "); printUint((uint8_t*)&N, 1); usart_send(" for "); print_long((uint32_t)steps); usart_send("steps\n"); - return stepper_move(N, steps); + #endif + if(steps) return stepper_move(N, steps); + else{ // steps == 0 - just check endswitches + stepper_get_esw(N); + return 0; + } } -extern void print_time(); +/** + * Switch relay on/off depending on cmd value + * 1 - on, 0 - off + * @param pin - pin to change + * @param N - second symbol of command ([2 N ...]) + */ +uint8_t relay(char *cmd, uint8_t pin, char N){ + if(*cmd == '-'){ // just check + char ans[] = "[2 N St=1]\n"; + ans[3] = N; + if(PORTAB & pin) ans[8] = '0'; // off + usart_send(ans); + return 1; + } + if(*cmd == '0'){ // turn OFF + PORTAB |= pin; + return 1; + } + if(*cmd == '1'){ // turn ON + PORTAB &= ~pin; + return 1; + } + return 0; +} + +//extern void print_time(); +extern uint8_t LEDs[3]; // LEDs shining time + +void LEDshine(char *cmd, uint8_t N){ + int16_t s; + if(!readInt(cmd, &s)) return; + if(s < 0 || s > 255) return; + LEDs[N] = (uint8_t)s; +} /** * process commands from user buffer * @return 1 if all OK */ -uint8_t process_commands(){ - char *cmd = omit_whitespace(&rx_buffer[1]); - switch(*cmd){ - case 't': - print_time(); - return 1; - break; - case '2': - cmd = omit_whitespace(cmd + 1); - break; - default: - return 0; - } +uint8_t process_commands(char *cmd){ + cmd = omit_whitespace(cmd + 1); if(*cmd > '0' && *cmd < '7') return move_motor(cmd); - switch(*cmd){ + char s = *cmd; + cmd = omit_whitespace(cmd + 1); + switch(s){ case '0': - usart_send("restart"); + DBG("restart"); break; case '7': - usart_send("Shutter"); + DBG("Shutter"); + relay(cmd, SHTR_PIN, '7'); break; case '8': - usart_send("Neon"); + DBG("Neon"); + relay(cmd, NEON_PIN, '8'); break; case '9': - usart_send("Flat"); + DBG("Flat"); + relay(cmd, FLAT_PIN, '9'); break; case 'a': - cmd = omit_whitespace(cmd + 1); return stepper_ch_speed(cmd); break; case 'b': - usart_send("LED1"); + DBG("LED1"); + LEDshine(cmd, 0); break; case 'c': - usart_send("LED2"); + DBG("LED2"); + LEDshine(cmd, 1); break; case 'd': - usart_send("LED3"); + DBG("LED3"); + LEDshine(cmd, 2); break; default: return 0; } - usart_send("\n"); + DBG("\n"); return 1; } @@ -102,24 +137,27 @@ void process_string(){ uint8_t noerr = 1, oldflags = usart_flags; usart_flags &= ~(U_RX_COMPLETE | U_RX_OVERFL | U_RX_ERROR); if(oldflags & U_RX_OVERFL){ - usart_send("Input buffer overflow\n"); + DBG("Input buffer overflow\n"); noerr = 0; } if(oldflags & U_RX_ERROR){ - usart_send("Rx error\n"); + DBG("Rx error\n"); noerr = 0; } if(rx_bufsize < 3 || rx_buffer[0] != '[' || rx_buffer[rx_bufsize - 2] != ']'){ + //if(rx_buffer[0] == 't'){ print_time(); return; } rx_bufsize = 0; - usart_send("Enter \"[cmd]\"\n"); + DBG("Enter \"[cmd]\"\n"); noerr = 0; } if(noerr){ // echo back given string rx_buffer[rx_bufsize] = 0; + char *cmd = omit_whitespace(&rx_buffer[1]); + if(*cmd != '2') return; uint8_t rbs = rx_bufsize; rx_bufsize = 0; usart_send(rx_buffer); rx_buffer[rbs - 2] = 0; - process_commands(); + process_commands(cmd); } } diff --git a/scorpio2.c.tags b/scorpio.c.tags similarity index 88% rename from scorpio2.c.tags rename to scorpio.c.tags index 607d4a7..5dcab50 100644 --- a/scorpio2.c.tags +++ b/scorpio.c.tags @@ -8,6 +8,7 @@ AVR_STATUS_REG BADISR_vectÌ65536Ö0 BAUDÌ65536Ö0 BAUD_TOLÌ65536Ö0 +DBGÌ131072Í(x)Ö0 DD0Ì65536Ö0 DD1Ì65536Ö0 DD2Ì65536Ö0 @@ -16,36 +17,28 @@ DD4 DD5Ì65536Ö0 DD6Ì65536Ö0 DD7Ì65536Ö0 +DDRABÌ65536Ö0 +EBUGÌ65536Ö0 EMPTY_INTERRUPTÌ131072Í(vector)Ö0 +FLAT_PINÌ65536Ö0 FUSEMEMÌ65536Ö0 FUSESÌ65536Ö0 -F_CPUÌ65536Ö0 -INFINITYÌ65536Ö0 ISRÌ131072Í(vector,...)Ö0 ISR_ALIASÌ131072Í(vector,tgt)Ö0 ISR_ALIASOFÌ131072Í(v)Ö0 ISR_BLOCKÌ65536Ö0 ISR_NAKEDÌ65536Ö0 ISR_NOBLOCKÌ65536Ö0 -LED_PINÌ65536Ö0 +LED1_PINÌ65536Ö0 +LED2_PINÌ65536Ö0 +LED3_PINÌ65536Ö0 +LEDsÌ16384Ö0Ïuint8_t +LEDsÌ32768Ö0Ïuint8_t +LEDshineÌ16Í(char *cmd, uint8_t N)Ö0Ïvoid LOCKBITSÌ65536Ö0 LOCKBITS_DEFAULTÌ65536Ö0 LOCKMEMÌ65536Ö0 -M_1_PIÌ65536Ö0 -M_2_PIÌ65536Ö0 -M_2_SQRTPIÌ65536Ö0 -M_EÌ65536Ö0 -M_LN10Ì65536Ö0 -M_LN2Ì65536Ö0 -M_LOG10EÌ65536Ö0 -M_LOG2EÌ65536Ö0 -M_PIÌ65536Ö0 -M_PI_2Ì65536Ö0 -M_PI_4Ì65536Ö0 -M_SQRT1_2Ì65536Ö0 -M_SQRT2Ì65536Ö0 -MillisecondsÌ16384Ö0Ïvolatile uint16_t -NANÌ65536Ö0 +NEON_PINÌ65536Ö0 PIN0Ì65536Ö0 PIN1Ì65536Ö0 PIN2Ì65536Ö0 @@ -62,7 +55,10 @@ PORT4 PORT5Ì65536Ö0 PORT6Ì65536Ö0 PORT7Ì65536Ö0 +PORTABÌ65536Ö0 +PORTAB_PINSÌ65536Ö0 RX_BUFFER_SIZEÌ65536Ö0 +SHTR_PINÌ65536Ö0 SIGNALÌ131072Í(vector)Ö0 SPÌ65536Ö0 SPLÌ65536Ö0 @@ -75,9 +71,13 @@ SREG_S SREG_TÌ65536Ö0 SREG_VÌ65536Ö0 SREG_ZÌ65536Ö0 -SecondsÌ16384Ö0Ïuint16_t +STPRS_OFFÌ131072Í()Ö0 +Steps_leftÌ16384Ö0Ïuint16_t +Steps_leftÌ32768Ö0Ïuint16_t TIMER0_OVF_vectÌ16Í(void)Ö0Ïvoid TIMER0_OVF_vectÌ1024Í(void)Ö0ÏÓ void +TIMER1_COMPA_vectÌ16Í(void)Ö0Ïvoid +TIMER1_COMPA_vectÌ1024Í(void)Ö0ÏÓ void TX_BUFFER_SIZEÌ65536Ö0 UBRRH_VALUEÌ65536Ö0 UBRRL_VALUEÌ65536Ö0 @@ -92,6 +92,7 @@ U_RX_ERROR U_RX_OVERFLÌ65536Ö0 U_TX_COMPLETEÌ65536Ö0 U_TX_ERRORÌ65536Ö0 +UstepÌ16384Ö0Ïint8_t WDTO_120MSÌ65536Ö0 WDTO_15MSÌ65536Ö0 WDTO_1SÌ65536Ö0 @@ -136,8 +137,6 @@ _SFR_MEM8 _SFR_MEM_ADDRÌ131072Í(sfr)Ö0 _SFR_WORDÌ131072Í(sfr)Ö0 _STDC_PREDEF_HÌ65536Ö0 -_UTIL_DELAY_BASIC_H_Ì65536Ö0 -_UTIL_DELAY_H_Ì65536Ö0 _VECTORÌ131072Í(N)Ö0 _WD_CHANGE_BITÌ65536Ö0 _WD_CONTROL_REGÌ65536Ö0 @@ -150,7 +149,6 @@ __ATOMIC_HLE_RELEASE __ATOMIC_RELAXEDÌ65536Ö0 __ATOMIC_RELEASEÌ65536Ö0 __ATOMIC_SEQ_CSTÌ65536Ö0 -__ATTR_CONST__Ì65536Ö0 __AVR_LIBC_DATE_Ì65536Ö0 __AVR_LIBC_DATE_STRING__Ì65536Ö0 __AVR_LIBC_MAJOR__Ì65536Ö0 @@ -253,7 +251,7 @@ __GNUG__ __GXX_ABI_VERSIONÌ65536Ö0 __GXX_RTTIÌ65536Ö0 __GXX_WEAK__Ì65536Ö0 -__HAS_DELAY_CYCLESÌ65536Ö0 +__INCLUDES_H__Ì65536Ö0 __INT16_CÌ131072Í(c)Ö0 __INT16_MAX__Ì65536Ö0 __INT16_TYPE__Ì65536Ö0 @@ -306,7 +304,6 @@ __LDBL_MIN__ __LONG_LONG_MAX__Ì65536Ö0 __LONG_MAX__Ì65536Ö0 __LP64__Ì65536Ö0 -__MATH_HÌ65536Ö0 __MMX__Ì65536Ö0 __OPTIMIZE_SIZE__Ì65536Ö0 __OPTIMIZE__Ì65536Ö0 @@ -353,6 +350,7 @@ __STDC_ISO_10646__ __STDC_NO_THREADS__Ì65536Ö0 __STDC__Ì65536Ö0 __STDINT_H_Ì65536Ö0 +__STEPPER_H__Ì65536Ö0 __STRINGIFYÌ131072Í(x)Ö0 __UART_H__Ì65536Ö0 __UINT16_CÌ131072Í(c)Ö0 @@ -416,37 +414,12 @@ __unix __unix__Ì65536Ö0 __x86_64Ì65536Ö0 __x86_64__Ì65536Ö0 -_delay_loop_1Ì16Í(uint8_t __count)Ö0Ïvoid -_delay_loop_1Ì1024Í(uint8_t __count)Ö0Ïinline void -_delay_loop_2Ì16Í(uint16_t __count)Ö0Ïvoid -_delay_loop_2Ì1024Í(uint16_t __count)Ö0Ïinline void -_delay_msÌ16Í(double __ms)Ö0Ïvoid -_delay_msÌ1024Í(double __ms)Ö0Ïinline void -_delay_usÌ16Í(double __us)Ö0Ïvoid -_delay_usÌ1024Í(double __us)Ö0Ïinline void -acosfÌ65536Ö0 -asinfÌ65536Ö0 -atan2fÌ65536Ö0 -atanfÌ65536Ö0 bit_is_clearÌ131072Í(sfr,bit)Ö0 bit_is_setÌ131072Í(sfr,bit)Ö0 -cbrtfÌ65536Ö0 -ceilfÌ65536Ö0 +check_endswÌ16Í()Ö0Ïuint8_t cliÌ131072Í()Ö0 -copysignfÌ65536Ö0 -cosfÌ65536Ö0 -coshfÌ65536Ö0 -daysÌ16384Ö0Ïuint16_t -expfÌ65536Ö0 -fabsfÌ65536Ö0 -fdimfÌ65536Ö0 -floorfÌ65536Ö0 -fmafÌ65536Ö0 -fmaxfÌ65536Ö0 -fminfÌ65536Ö0 -fmodfÌ65536Ö0 -frexpfÌ65536Ö0 -hypotfÌ65536Ö0 +cur_motorÌ16384Ö0Ïuint8_t +directionÌ16384Ö0Ïuint8_t int16_tÌ4096Ö0Ïsigned int int32_tÌ4096Ö0Ïsigned int int64_tÌ4096Ö0Ïsigned int @@ -462,48 +435,43 @@ int_least64_t int_least8_tÌ4096Ö0Ïint8_t intmax_tÌ4096Ö0Ïint64_t intptr_tÌ4096Ö0Ïint16_t -isfinitefÌ65536Ö0 -isinffÌ65536Ö0 -isnanfÌ65536Ö0 -ldexpfÌ65536Ö0 linuxÌ65536Ö0 -log10fÌ65536Ö0 -logfÌ65536Ö0 loop_until_bit_is_clearÌ131072Í(sfr,bit)Ö0 loop_until_bit_is_setÌ131072Í(sfr,bit)Ö0 -lrintfÌ65536Ö0 -lroundfÌ65536Ö0 mainÌ16Í()Ö0Ïint move_motorÌ16Í(char *cmd)Ö0Ïuint8_t -omit_watespaceÌ16Í(char *str)Ö0Ïchar * -omit_watespaceÌ1024Í(char *str)Ö0Ïchar * -powfÌ65536Ö0 +omit_whitespaceÌ16Í(char *str)Ö0Ïchar * +omit_whitespaceÌ1024Í(char *str)Ö0Ïchar * printUintÌ16Í(uint8_t *val, uint8_t len)Ö0Ïvoid printUintÌ1024Í(uint8_t *val, uint8_t len)Ö0Ïvoid print_longÌ16Í(int32_t Number)Ö0Ïvoid print_longÌ1024Í(int32_t Number)Ö0Ïvoid -print_timeÌ16Í()Ö0Ïvoid -print_timeÌ1024Í()Ö0Ïvoid -process_commandsÌ16Í()Ö0Ïuint8_t +process_commandsÌ16Í(char *cmd)Ö0Ïuint8_t process_stringÌ16Í()Ö0Ïvoid process_stringÌ1024Í()Ö0Ïvoid readIntÌ16Í(char *buff, int16_t *val)Ö0Ïuint8_t readIntÌ1024Í(char *buff, int16_t *val)Ö0Ïuint8_t +relayÌ16Í(char *cmd, uint8_t pin, char N)Ö0Ïuint8_t retiÌ131072Í()Ö0 -roundfÌ65536Ö0 rx_bufferÌ16384Ö0Ïchar rx_bufferÌ32768Ö0Ïchar rx_bufsizeÌ16384Ö0Ïvolatile uint8_t rx_bufsizeÌ32768Ö0Ïvolatile uint8_t seiÌ131072Í()Ö0 -signbitfÌ65536Ö0 -sinfÌ65536Ö0 -sinhfÌ65536Ö0 -sqrtfÌ65536Ö0 -squarefÌ65536Ö0 -tanfÌ65536Ö0 -tanhfÌ65536Ö0 -truncfÌ65536Ö0 +stepper_ch_speedÌ16Í(char *spd)Ö0Ïuint8_t +stepper_ch_speedÌ1024Í(char *spd)Ö0Ïuint8_t +stepper_get_eswÌ16Í(uint8_t Nmotor)Ö0Ïvoid +stepper_get_eswÌ1024Í(uint8_t Nmotor)Ö0Ïvoid +stepper_moveÌ16Í(uint8_t Nmotor, int16_t Nsteps)Ö0Ïuint8_t +stepper_moveÌ1024Í(uint8_t Nmotor, int16_t Nsteps)Ö0Ïuint8_t +stepper_processÌ16Í()Ö0Ïvoid +stepper_processÌ1024Í()Ö0Ïvoid +stepper_pulseÌ16384Ö0Ïvolatile uint8_t +stepper_pulseÌ32768Ö0Ïvolatile uint8_t +stepper_setupÌ16Í()Ö0Ïvoid +stepper_setupÌ1024Í()Ö0Ïvoid +stop_motorsÌ16Í()Ö0Ïvoid +stop_motorsÌ1024Í()Ö0Ïvoid tx_bufferÌ16384Ö0Ïchar tx_bufsizeÌ16384Ö0Ïvolatile uint8_t tx_idxÌ16384Ö0Ïuint8_t @@ -527,6 +495,7 @@ usart_flags usart_flagsÌ32768Ö0Ïvolatile uint8_t usart_sendÌ16Í(char *Str)Ö0Ïint usart_sendÌ1024Í(char *Str)Ö0Ïint +ustepsÌ16384Ö0Ïconst uint8_t wdt_disableÌ131072Í()Ö0 wdt_enableÌ131072Í(value)Ö0 wdt_resetÌ131072Í()Ö0 diff --git a/scorpio.hex b/scorpio.hex new file mode 100644 index 0000000..abca6d4 --- /dev/null +++ b/scorpio.hex @@ -0,0 +1,201 @@ +:100000000C9434000C9451000C9451000C94510049 +:100010000C9451000C9451000C9451000C9451001C +:100020000C9451000C9451000C9451000C94B601A6 +:100030000C9451000C9451000C9451000C945100FC +:100040000C9498030C9451000C942D030C946203AF +:100050000C9451000C9451000C9451000C945100DC +:100060000C9451000C94510011241FBECFEFD8E026 +:10007000DEBFCDBF11E0A0E0B1E0E8EBFBE002C0E5 +:1000800005900D92AA3BB107D9F722E0AAEBB1E0A7 +:1000900001C01D92A530B207E1F70E942A050C9419 +:1000A000DA050C94000086B184FF04C085FF04C00B +:1000B00080E0089581E0089582E00895E1E8F0E0AD +:1000C00080818860808380ED97E0909389008093A1 +:1000D0008800808182608083EFE6F0E0808182602A +:1000E000808388B1806388B90895CF93DF9300D06F +:1000F00000D0CDB7DEB7BE016D5F7F4F0E94E60234 +:100100008823B1F16B817C81683F8FEF780764F1C0 +:100110006F3F8FE7780741F1665F7F4F8FEF9FEF6B +:100120000E946B057A83698380916F008D7F809335 +:100130006F0089819A819093890080938800109242 +:1001400085001092840080916F00826080936F0020 +:100150008CE091E00E94C50162E0CE0101960E9410 +:10016000660287EB91E002C08EE191E00E94C5013A +:1001700080E00F900F900F900F90DF91CF91089536 +:10018000CF93DF93CDB7DEB72C970FB6F894DEBFD1 +:100190000FBECDBF9FEF980F973020F59BB19C6F9E +:1001A0009BB99BB122E030E0082E01C0220F0A94D7 +:1001B000EAF7209529232BB99CE0EFE2F1E0DE017C +:1001C000119601900D929A95E1F7805D8C830E94C3 +:1001D0005300811101C083E0805D8987CE010196C3 +:1001E0000E94C5012C960FB6F894DEBF0FBECDBF9E +:1001F000DF91CF9108958091BB010E94C0008BB127 +:100200008C6F8BB988B1807F88B9EFE6F0E0808190 +:100210008D7F80831092BA011092E3011092E20167 +:100220001092BD011092BB0108951F93CF93DF93ED +:10023000182FEB018FEF810F863008F04AC020970E +:1002400009F447C08091E2019091E301892B09F004 +:1002500040C080916F008D7F80936F008BB18C6F59 +:100260008BB988B1807F88B92BB182E090E0012EF4 +:1002700001C0880F0A94EAF7809582238BB90E9407 +:1002800053001093BB01882379F0813021F41C16B0 +:100290001D0664F01CC0D7FF1AC0D195C195D109C5 +:1002A00081E08093BC0104C0D7FDF7CF1092BC0160 +:1002B000D093E301C093E201109285001092840074 +:1002C00080916F00826080936F0081E003C00E9484 +:1002D000FB0080E0DF91CF911F9108951092BA0149 +:1002E00088B1E091BD010E2E000CFF0BEC5FFE4FBC +:1002F000807F9081892B88B90E9453002091BC0196 +:100300009091BD012223B9F0915097FD03C09093C5 +:10031000BD010DC097E09093BD012091E2013091A5 +:10032000E301215031093093E3012093E201813050 +:10033000B1F40C94FB009F5F98301CF49093BD01C6 +:100340000CC01092BD012091E2013091E3012150D7 +:1003500031093093E3012093E201823051F380911F +:10036000E2019091E301892B21F308951F920F92EE +:100370000FB60F9211248F9381E08093BA018F9171 +:100380000F900FBE0F901F9018952091000120FF35 +:10039000FCCF209100012A7F209300011092BE0122 +:1003A0001092BF01DC018091BF01803278F48D9101 +:1003B000882361F0E091BF01F0E0EF53FE4F8083AE +:1003C0008091BF018F5F8093BF01EDCF8091BF010E +:1003D000803251F41092BF018091000181608093BE +:1003E000000181E090E008958091C10080628093D7 +:1003F000C10080E090E008958F929F92AF92BF92EB +:10040000DF92EF92FF920F931F93CF93DF93CDB7BD +:10041000DEB72C970FB6F894DEBF0FBECDBF1C869B +:1004200097FF0AC090958095709561957F4F8F4F8B +:100430009F4FDD24D39401C0D12C0BE03AE0832EF2 +:10044000912CA12CB12C1FEF100FEE24E394F12C72 +:10045000EC0EFD1EE10EF11CA50194010E94A10508 +:10046000605DF7016083B901CA01611571058105FD +:10047000910521F0012F1111E6CF0EC0112361F07B +:10048000DD2051F01EEF100FE1E0F0E0EC0FFD1F5A +:10049000E10FF11D8DE2808381E090E08C0F9D1FC4 +:1004A000810F911D0E94C5012C960FB6F894DEBFF6 +:1004B0000FBECDBFDF91CF911F910F91FF90EF90B5 +:1004C000DF90BF90AF909F908F9008958F929F92F2 +:1004D000AF92BF92CF92DF92FF920F931F93CF9371 +:1004E000DF93CDB7DEB72B970FB6F894DEBF0FBE04 +:1004F000CDBFDC01633009F44AC08FEF860F843032 +:1005000008F045C0FE013196CE010C968F01119284 +:100510008E179F07E1F7623039F0643051F0613097 +:1005200069F46C9170E002C06D917C9180E090E084 +:1005300008C06D917D918D919C9103C060E070E049 +:10054000CB016E012BE0C20ED11C29E0F22E3AE065 +:10055000832E912CA12CB12CFA94A50194010E9418 +:100560007F05605DF60162936F01B901CA016115F3 +:1005700071058105910519F0FFEFFF12EDCF8F2D69 +:10058000FF0C990B0196800F911F0E94C5012B96BD +:100590000FB6F894DEBF0FBECDBFDF91CF911F9194 +:1005A0000F91FF90DF90CF90BF90AF909F908F9072 +:1005B0000895FC012081203211F40196FACF37EF23 +:1005C000320F3230D0F32D30C1F308950F931F93C3 +:1005D000CF93DF93EB01FC0120812D3219F40196BA +:1005E00011E001C010E0FC0120E030E0A90181E051 +:1005F000019190ED900F9A30C0F4AAE0B0E00E9413 +:10060000C005DC01CB01800F911DA11DB11D9C0116 +:10061000AD01205331094109510980E0211590E8CD +:1006200039074105510524F310C081110EC0112373 +:1006300039F050954095309521953F4F4F4F5F4F82 +:10064000209729F03983288302C080E001C081E02F +:10065000DF91CF911F910F9108951F920F920FB6C6 +:100660000F9211248F939F93EF93FF939091C60065 +:100670008091C0008C71C1F4E091C00181E08E0FC7 +:100680008093C001F0E0EC51FE4F90839A3021F44A +:100690008091000182600BC08091C001803249F4DA +:1006A00080910001826103C0809100018A60809383 +:1006B0000001FF91EF919F918F910F900FBE0F90CE +:1006C0001F9018951F920F920FB60F9211248F93BF +:1006D0009F93EF93FF938091BF018823B1F0E09146 +:1006E000BE0181E08E0F8093BE01F0E0EF53FE4F1C +:1006F00080818093C6009091BE018091BF019813C4 +:100700000EC01092BE011092BF018091C1008F7D7A +:100710008093C10080910001816080930001FF916E +:10072000EF919F918F910F900FBE0F901F90189592 +:100730001F920F920FB60F9211248F939F93809167 +:10074000E101811103C085B1886385B990910101F0 +:100750008091E101981301C02B9890910201809142 +:10076000E101981301C02C98909103018091E1015F +:10077000981301C02D988091E1018F5F8093E10172 +:100780009F918F910F900FBE0F901F901895CF9350 +:10079000DF9300D01F92CDB7DEB7FC01908180EDD2 +:1007A000890F8B839153963078F5CF0101960E9483 +:1007B000D902BE016F5F7F4F0E94E602882321F1BC +:1007C0008BE391E00E94C50161E0CE0103960E9497 +:1007D000660287E491E00E94C50169817A81072E53 +:1007E000000C880B990B0E94FC018DE491E00E94A3 +:1007F000C50169817A818B816115710519F00E94AB +:10080000150103C00E94C00080E00F900F900F9070 +:10081000DF91CF910895CF93DF93CDB7DEB72C97BB +:100820000FB6F894DEBF0FBECDBFFC0180818D32C4 +:10083000A1F48CE0E4E5F1E0DE01119601900D9267 +:100840008A95E1F74C8385B1682311F080E38987AD +:10085000CE0101960E94C5010BC0803319F485B109 +:10086000682B05C0813331F485B16095682365B983 +:1008700081E001C080E02C960FB6F894DEBF0FBE79 +:10088000CDBFDF91CF9108951F93CF93DF9300D019 +:10089000CDB7DEB7162FBE016F5F7F4F0E94E60215 +:1008A000882359F089819A818F3F910509F028F4B6 +:1008B000E12FF0E0EF5FFE4F80830F900F90DF910C +:1008C000CF911F9108950F931F93CF9301960E948C +:1008D000D902FC01C0812FEC2C0F263028F4CF91D7 +:1008E0001F910F910C94C70301960E94D9028C01AD +:1008F000C93389F15CF4C733E9F01CF5C03309F062 +:1009000045C080E691E00E94C5013AC0C23651F16F +:100910003CF4C136D9F5CF911F910F910C9475001D +:10092000C33631F1C43691F584E891E00E94C501E7 +:1009300062E023C088E691E00E94C50147E364E0DD +:1009400006C080E791E00E94C50148E362E0C8016B +:100950000E940B0415C085E791E00E94C50149E3A0 +:1009600061E0F5CF8AE791E00E94C50160E005C033 +:100970008FE791E00E94C50161E0C8010E94440434 +:1009800087EB91E00E94C50181E001C080E0CF913A +:100990001F910F9108950F931F93CF9380910001A2 +:1009A00081FF54C0C091000180910001857E809339 +:1009B0000001C4FF06C089E891E00E94C50180E003 +:1009C00001C081E0C3FF05C080EA91E00E94C5013B +:1009D00080E09091C001933060F09091E4019B35EC +:1009E00041F4E091C001F0E0EE51FE4F90819D3561 +:1009F00049F01092C0018AEA91E0CF911F910F91C6 +:100A00000C94C501882311F1E091C001F0E0EC5194 +:100A1000FE4F108285EE91E00E94D9028C01FC010C +:100A20008081823399F4C091C0011092C00184EE9C +:100A300091E00E94C501EC2FF0E0EE51FE4F1082D4 +:100A4000C801CF911F910F910C946304CF911F9116 +:100A50000F9108958BB18C6F8BB988B1807F88B965 +:100A60008CEF8AB9CFE0C7B985B1876085B98FE3CC +:100A700084B91092C50087E68093C4008091C000BD +:100A80008D7F8093C00086E08093C20088E98093C8 +:100A9000C10085B5826085BD80916E0081608093C4 +:100AA0006E000E945E00789488E190E00FB6F894A2 +:100AB000A895809360000FBEC0936000A8958091B8 +:100AC000BA0181110E946E018091000181FFF6CF71 +:100AD0000E94CB04F3CFAA1BBB1B51E107C0AA1F86 +:100AE000BB1FA617B70710F0A61BB70B881F991FCF +:100AF0005A95A9F780959095BC01CD010895A1E282 +:100B00001A2EAA1BBB1BFD010DC0AA1FBB1FEE1F87 +:100B1000FF1FA217B307E407F50720F0A21BB30BD2 +:100B2000E40BF50B661F771F881F991F1A9469F74E +:100B300060957095809590959B01AC01BD01CF01AA +:100B40000895052E97FB1EF400940E94B80557FDEA +:100B500007D00E947F0507FC03D04EF40C94B80523 +:100B600050954095309521953F4F4F4F5F4F0895D9 +:100B700090958095709561957F4F8F4F9F4F089509 +:100B80000E94CB05A59F900DB49F900DA49F800D52 +:100B9000911D11240895A29FB001B39FC001A39F8E +:100BA000700D811D1124911DB29F700D811D1124A6 +:080BB000911D0895F894FFCF98 +:100BB80001141414080C0406020301095370656536 +:100BC80064206368616E67656420746F2000426109 +:100BD800642073706565642076616C75650A005BD6 +:100BE8003220302053743D305D0A004D6F76652009 +:100BF8006D6F746F72200020666F722000737465C9 +:100C080070730A005B32204E2053743D315D0A0038 +:100C180072657374617274005368757474657200D8 +:100C28004E656F6E00466C6174004C454431004C53 +:100C3800454432004C45443300496E707574206257 +:100C48007566666572206F766572666C6F770A00E6 +:100C58005278206572726F720A00456E7465722050 +:0A0C6800225B636D645D220A000048 +:00000001FF diff --git a/scorpio2.hex b/scorpio2.hex deleted file mode 100644 index ec36fbf..0000000 --- a/scorpio2.hex +++ /dev/null @@ -1,158 +0,0 @@ -:100000000C9434000C9451000C9451000C94510049 -:100010000C9451000C9451000C9451000C9451001C -:100020000C9451000C9451000C9451000C94D0008D -:100030000C9451000C9451000C9451000C945100FC -:100040000C94CD020C9451000C9447020C947C0249 -:100050000C9451000C9451000C9451000C945100DC -:100060000C9451000C94510011241FBECFEFD8E026 -:10007000DEBFCDBF11E0A0E0B1E0E4E2F9E002C0F4 -:1000800005900D92AE39B107D9F721E0AEE9B1E0A4 -:1000900001C01D92AA3EB207E1F70E94D4030C945E -:1000A00090040C940000E1E8F0E080818860808397 -:1000B00088EE93E09093890080938800808182602D -:1000C0008083EFE6F0E08081826080830895CF93A3 -:1000D000DF9300D000D0CDB7DEB7BE016D5F7F4F9C -:1000E0000E9400028823B1F16B817C81683F8FEF11 -:1000F000780764F16F3F8FE7780741F1665F7F4FC4 -:100100008FEF9FEF0E9421047A83698380916F00B3 -:100110008D7F80936F0089819A819093890080936D -:100120008800109285001092840080916F00826098 -:1001300080936F0081E091E00E94DF0062E0CE01D9 -:1001400001960E9480018CE991E002C083E191E078 -:100150000E94DF0080E00F900F900F900F90DF91D2 -:10016000CF910895482F8FEF840F8630A0F4672B2E -:1001700091F09BB19C6F9BB998B19F6098B95BB1AE -:1001800022E030E0C90101C0880F4A95EAF78523D3 -:100190008BB980E0089581E0089510929E01089542 -:1001A0001F920F920FB60F9211248F9381E08093CC -:1001B0009E018F910F900FBE0F901F901895209168 -:1001C000000120FFFCCF209100012A7F2093000135 -:1001D0001092A0011092A101DC018091A101803256 -:1001E00078F48D91882361F0E091A101F0E0ED5564 -:1001F000FE4F80838091A1018F5F8093A101EDCF9D -:100200008091A101803251F41092A10180910001EE -:1002100081608093000181E090E008958091C100A9 -:1002200080628093C10080E090E008958F929F9259 -:10023000AF92BF92DF92EF92FF920F931F93CF93F3 -:10024000DF93CDB7DEB72C970FB6F894DEBF0FBEA5 -:10025000CDBF1C8697FF0AC09095809570956195DB -:100260007F4F8F4F9F4FDD24D39401C0D12C0BE0E3 -:100270003AE0832E912CA12CB12C1FEF100FEE240D -:10028000E394F12CEC0EFD1EE10EF11CA50194018E -:100290000E945704605DF7016083B901CA016115CE -:1002A00071058105910521F0012F1111E6CF0EC0D6 -:1002B000112361F0DD2051F01EEF100FE1E0F0E0BE -:1002C000EC0FFD1FE10FF11D8DE2808381E090E0D6 -:1002D0008C0F9D1F810F911D0E94DF002C960FB681 -:1002E000F894DEBF0FBECDBFDF91CF911F910F916C -:1002F000FF90EF90DF90BF90AF909F908F90089508 -:100300008F929F92AF92BF92CF92DF92FF920F9304 -:100310001F93CF93DF93CDB7DEB72B970FB6F8942B -:10032000DEBF0FBECDBFDC01633009F44AC08FEFE2 -:10033000860F843008F045C0FE013196CE010C9640 -:100340008F0111928E179F07E1F7623039F0643008 -:1003500051F0613069F46C9170E002C06D917C9154 -:1003600080E090E008C06D917D918D919C9103C0DB -:1003700060E070E0CB016E012BE0C20ED11C29E0E1 -:10038000F22E3AE0832E912CA12CB12CFA94A501E7 -:1003900094010E943504605DF60162936F01B9011A -:1003A000CA01611571058105910519F0FFEFFF1272 -:1003B000EDCF8F2DFF0C990B0196800F911F0E949E -:1003C000DF002B960FB6F894DEBF0FBECDBFDF91D6 -:1003D000CF911F910F91FF90DF90CF90BF90AF9082 -:1003E0009F908F900895FC012081203211F4019696 -:1003F000FACF37EF320F3230D0F32D30C1F30895FA -:100400000F931F93CF93DF93EB01FC0120812D32DB -:1004100019F4019611E001C010E0FC0120E030E089 -:10042000A90181E0019190ED900F9A30C0F4AAE00B -:10043000B0E00E947604DC01CB01800F911DA11D6C -:10044000B11D9C01AD01205331094109510980E0E2 -:10045000211590E839074105510524F310C0811199 -:100460000EC0112339F050954095309521953F4F9E -:100470004F4F5F4F209729F03983288302C080E0D7 -:1004800001C081E0DF91CF911F910F9108951F92DC -:100490000F920FB60F9211248F939F93EF93FF93B8 -:1004A0009091C6008091C0008C71C1F4E091A201CE -:1004B00081E08E0F8093A201F0E0E753FE4F90831E -:1004C0009A3021F48091000182600BC08091A201DA -:1004D000803249F480910001826103C08091000163 -:1004E0008A6080930001FF91EF919F918F910F900F -:1004F0000FBE0F901F9018951F920F920FB60F927C -:1005000011248F939F93EF93FF938091A1018823F0 -:10051000B1F0E091A00181E08E0F8093A001F0E0A6 -:10052000ED55FE4F80818093C6009091A00180918F -:10053000A10198130EC01092A0011092A101809108 -:10054000C1008F7D8093C100809100018160809304 -:100550000001FF91EF919F918F910F900FBE0F902F -:100560001F90189562E083EC91E00E94800184E284 -:1005700091E00E94DF0062E085EC91E00E94800142 -:1005800086E291E00E94DF0062E087EC91E00E9449 -:10059000800188E291E00C94DF001F920F920FB669 -:1005A0000F9211242F938F939F9386B58A5F86BDF8 -:1005B0008091C7019091C80101969093C8018093E2 -:1005C000C701883E934069F41092C8011092C70198 -:1005D0008091C5019091C60101969093C6018093C8 -:1005E000C5019F918F912F910F900FBE0F901F907B -:1005F0001895CF93DF9300D01F92CDB7DEB7FC01E3 -:10060000908180ED890F8B839153963010F080E0BC -:1006100029C0CF0101960E94F301BE016F5F7F4F99 -:100620000E940002882399F38CE291E00E94DF008F -:1006300061E0CE0103960E94800188E391E00E9470 -:10064000DF0069817A81072E000C880B990B0E94CC -:1006500016018EE391E00E94DF0069817A818B812F -:100660000E94B2000F900F900F90DF91CF910895EC -:100670008AEC91E00E94F301FC012081223331F0E9 -:10068000243709F042C00E94B2023DC001960E9488 -:10069000F301FC0190818FEC890F863018F4CF01B3 -:1006A0000C94F902993309F144F49733C1F0D4F46E -:1006B000903359F585E491E020C09236C9F044F4B6 -:1006C000913619F5CF0101960E94F3010C94670051 -:1006D000933689F09436C9F489E691E00EC08DE432 -:1006E00091E00BC085E591E008C08AE591E005C086 -:1006F0008FE591E002C084E691E00E94DF008CE982 -:1007000091E00E94DF0081E0089580E00895CF939A -:100710008091000181FF46C0C091000180910001DD -:10072000857E80930001C4FF06C08EE691E00E94A2 -:10073000DF0080E001C081E0C3FF05C085E891E0F3 -:100740000E94DF0080E09091A201933060F09091D0 -:10075000C9019B3541F4E091A201F0E0E953FE4F5D -:1007600090819D3539F01092A2018FE891E0CF91F0 -:100770000C94DF008823B1F0E091A201F0E0E75390 -:10078000FE4F1082C091A2011092A20189EC91E06B -:100790000E94DF00EC2FF0E0E953FE4F1082CF9172 -:1007A0000C943803CF910895259A8BB18C6F8BB937 -:1007B0008CEF8AB98BB18F608BB9CFE0C7B910923B -:1007C000C50087E68093C4008091C0008D7F809330 -:1007D000C00086E08093C20088E98093C10085B59F -:1007E000836085BD80916E00816080936E000E9461 -:1007F0005300789488E190E00FB6F894A895809320 -:1008000060000FBEC0936000A89580919E01811189 -:100810000E94CD008091C7019091C801843F914012 -:1008200011F42D9A07C08091C7019091C801892BBE -:1008300009F42D988091000181FFE6CF0E94870383 -:10084000E3CFAA1BBB1B51E107C0AA1FBB1FA61702 -:10085000B70710F0A61BB70B881F991F5A95A9F769 -:1008600080959095BC01CD010895A1E21A2EAA1B96 -:10087000BB1BFD010DC0AA1FBB1FEE1FFF1FA21750 -:10088000B307E407F50720F0A21BB30BE40BF50B4D -:10089000661F771F881F991F1A9469F760957095D6 -:1008A000809590959B01AC01BD01CF010895052E67 -:1008B00097FB1EF400940E946E0457FD07D00E941F -:1008C000350407FC03D04EF40C946E04509540950B -:1008D000309521953F4F4F4F5F4F089590958095EC -:1008E000709561957F4F8F4F9F4F08950E948104AF -:1008F000A59F900DB49F900DA49F800D911D112474 -:100900000895A29FB001B39FC001A39F700D811DE8 -:100910001124911DB29F700D811D1124911D089508 -:04092000F894FFCF79 -:10092400015370656564206368616E676564207453 -:100934006F20004261642073706565642076616C89 -:1009440075650A00640073006D730A004D6F766567 -:10095400206D6F746F72200020666F7220007374B4 -:100964006570730A007265737461727400536875FC -:1009740074746572004E656F6E00466C6174004C51 -:10098400454431004C454432004C45443300496EE3 -:1009940070757420627566666572206F766572661E -:1009A4006C6F770A005278206572726F720A004584 -:0E09B4006E74657220225B636D645D220A0022 -:00000001FF diff --git a/stepper.c b/stepper.c index e2f24d5..c09975b 100644 --- a/stepper.c +++ b/stepper.c @@ -24,6 +24,17 @@ #include "includes.h" /* + * Half-step mode: + * D |----|----| | | | | |----| + * D | | |----|----|----|----|----| | + * C | |----|----|----| | | | | + * C |----| | | |----|----|----|----| + * B | | | |----|----|----| | | + * B |----|----|----| | | |----|----| + * A | | | | | |----|----|----| + * A |----|----|----|----|----| | | | + * + * In full-step mode pulse rises sequentally: D->C->B->A * 0 0000 * 1 0001 * 2 0010 @@ -41,26 +52,36 @@ *14 1110 *15 1111 */ -// microsteps: DCBA = 1000, 1100, 0100, 0110, 0010, 0011, 0001, 1001 -- for ULN -// what a shit is this > DCBA = 0001, 0010, 0110, 1010, 1001, 1000, 0100, 0000 - bipolar +// winding1: [13], winding2: [24] +// microsteps: [1234] = 1000, 1100, 0100, 0110, 0010, 0011, 0001, 1001 -- for ULN +// [1324] = 1000, 1010, 0010, 0110, 0100, 0101, 0001, 1001 - bipolar // 1000, 1010, 0010, 0110, 0100, 0101, 0001, 1001 - half-step // 1010, 0110, 0101, 1001 - full step -static const uint8_t usteps[8] = {8, 12, 4, 6, 2, 3, 1, 9}; // ULN - unipolar -static volatile char Ustep = 0; // current microstep count +static const uint8_t usteps[8] = {8, 12, 4, 6, 2, 3, 1, 9}; // ULN - unipolar, active 1 +//static const uint8_t usteps[8] = {7, 3, 11, 9, 13, 12, 14, 6}; // unipolar, active is 0 -uint8_t stepper_pulse = 0; +static int8_t Ustep = 0; // current microstep count +uint16_t Steps_left; // steps left to proceed (absolute value) +static uint8_t direction = 0; // ==1 if rotate CCW +static uint8_t cur_motor = 0; // current motor number + +volatile uint8_t stepper_pulse = 0; // interrupt flag, used in main.c + +static void stop_motors(); void stepper_setup(){ TCCR1B |= _BV(WGM12); // configure timer1 for CTC mode, TOP is OCR1A - OCR1A = 1000; // set the CTC compare value - 2kHz (means 1kHz) + OCR1A = 2000; // set the CTC compare value - 1kHz TCCR1B |= _BV(CS11); // start the timer at 16MHz/8 = 2MHz //TCCR1B |= _BV(CS12) | _BV(CS10); // /1024 == 15625Hz //OCR1A = 15625; TIMSK1 |= _BV(OCIE1A); // enable the CTC interrupt + PORTC |= _BV(4) | _BV(5); // enable pullup } /** * Change TIM1 speed + * Period = 4 * 65535/(spd + 10) microseconds */ uint8_t stepper_ch_speed(char *spd){ int16_t newval; @@ -71,10 +92,12 @@ uint8_t stepper_ch_speed(char *spd){ OCR1A = O; TCNT1 = 0; // reset counter TIMSK1 |= _BV(OCIE1A); + #ifdef EBUG usart_send("Speed changed to "); printUint((uint8_t*)&O, 2); usart_send("\n"); - }else usart_send("Bad speed value\n"); + #endif + }else DBG("Bad speed value\n"); } return 0; } @@ -84,39 +107,53 @@ uint8_t stepper_ch_speed(char *spd){ * @return 0 if none pressed, 1 if "-", 2 if "+" */ static uint8_t check_endsw(){ + // PC4 - "-", PC5 - "+" + uint8_t pc = PINC; + if(0 == (pc & _BV(4))) return 1; + if(0 == (pc & _BV(5))) return 2; return 0; } /** * move stepper number Nmotor by Nsteps steps - * @return 0 if all OK - * 1 if Nmotor or Nsteps are bad values - * 2 if motor already on endswitch in given direction + * @return 1 if all OK, 0 if error occured */ uint8_t stepper_move(uint8_t Nmotor, int16_t Nsteps){ - if(!Nmotor || Nmotor > 6 || !Nsteps) return 1; + if(!Nmotor || Nmotor > 6 || !Nsteps || Steps_left) return 0; + TIMSK1 &= ~_BV(OCIE1A); // disable timer interrupt // turn all OFF - PORTD |= 0xfc; - PORTC |= 0x0f; + STPRS_OFF(); // turn on the motor we need - PORTD &= 2 << Nmotor; + PORTD &= ~(2 << Nmotor); uint8_t c = check_endsw(); + cur_motor = Nmotor; if(c){ if(c == 1){if(Nsteps > 0) c = 0;} else if(Nsteps < 0) c = 0; } if(c){ - PORTD |= 0xfc; - return 2; // already at end-switch in given direction + stop_motors(); + return 0; // already at end-switch in given direction } - return 0; + if(Nsteps < 0){ // CCW + Nsteps = -Nsteps; + direction = 1; + }else direction = 0; // CW + Steps_left = Nsteps; + TCNT1 = 0; // reset counter + TIMSK1 |= _BV(OCIE1A); + return 1; } - -static void stop_motor(uint8_t Nmotor){ +static void stop_motors(){ + stepper_get_esw(cur_motor); // turn off all pulses to place motor in free state & prevent undesirable behaviour - PORTD |= 0xfc; - PORTC |= 0x0f; + STPRS_OFF(); + TIMSK1 &= ~_BV(OCIE1A); // disable timer interrupt + stepper_pulse = 0; + Steps_left = 0; + Ustep = 0; + cur_motor = 0; } /** @@ -124,26 +161,45 @@ static void stop_motor(uint8_t Nmotor){ */ void stepper_process(){ stepper_pulse = 0; - // change steps - /*if(TIM2_SR1 & TIM_SR1_UIF){ - TIM2_SR1 &= ~TIM_SR1_UIF; // take off flag - tmp = PORT(STP_PORT, ODR) & 0xf0; - PORT(STP_PORT, ODR) = tmp | usteps[Ustep]; - if(Dir){ - if(++Ustep > 7){ - Ustep = 0; - --Nsteps; - } - }else{ - if(--Ustep < 0){ - Ustep = 7; - --Nsteps; - } + uint8_t port = PORTC & 0xf0; // save old port state & clear clocking + PORTC = port | usteps[Ustep]; + uint8_t sw = check_endsw(); // 1 - "-", 2 - "+", 0 - none + if(direction){ // CCW + if(--Ustep < 0){ + Ustep = 7; + --Steps_left; } - if(Nsteps == 0){ - stop_motor(); + if(sw == 1){ + stop_motors(); + return; } - }*/ + }else{ // CW + if(++Ustep > 7){ + Ustep = 0; + --Steps_left; + } + if(sw == 2){ + stop_motors(); + return; + } + } + if(Steps_left == 0) stop_motors(); +} + +/** + * get end-switches state for all motors or only Nth + * @param Nmotor - number of given motor + */ +void stepper_get_esw(uint8_t Nmotor){ + if(Nmotor == 0 || Nmotor > 7) return; // no running motor + PORTD |= 0xfc; + PORTD &= ~(2 << Nmotor); // [2 1 St=2] + char str[] = "[2 0 St=0]\n"; // 3 - motor number, 5 - endswitch (3 if none) + str[3] = Nmotor + '0'; + uint8_t sw = check_endsw(); + if(sw == 0) sw = 3; + str[8] = sw + '0'; + usart_send(str); } /** diff --git a/stepper.h b/stepper.h index c1e1276..26a4410 100644 --- a/stepper.h +++ b/stepper.h @@ -27,14 +27,16 @@ #include -extern uint8_t stepper_pulse; +extern volatile uint8_t stepper_pulse; +extern uint16_t Steps_left; // setup timer void stepper_setup(); void stepper_process(); uint8_t stepper_ch_speed(char *spd); - uint8_t stepper_move(uint8_t Nmotor, int16_t Nsteps); +void stepper_get_esw(uint8_t Nmotor); + #endif // __STEPPER_H__