added stepper management by ULN2003 Darlington's array

This commit is contained in:
eddyem 2014-07-29 22:51:52 +04:00
parent 915b2cfdc7
commit 68e217e032
9 changed files with 929 additions and 0 deletions

34
stepper_ULN2003/Makefile Normal file
View File

@ -0,0 +1,34 @@
NAME=testproj
SDCC=sdcc
CCFLAGS=-DSTM8S105 -I../ -I/usr/share/sdcc/include -mstm8 --out-fmt-ihx
LDFLAGS= -mstm8 --out-fmt-ihx -lstm8
FLASHFLAGS=-cstlinkv2 -pstm8s105
SRC=$(wildcard *.c)
# ATTENTION: FIRST in list should be file with main()
OBJ=$(SRC:%.c=%.rel)
TRASH=$(OBJ) $(SRC:%.c=%.rst) $(SRC:%.c=%.asm) $(SRC:%.c=%.lst)
TRASH+=$(SRC:%.c=%.sym) $(NAME).lk $(NAME).map
INDEPENDENT_HEADERS=../stm8l.h ports_definition.h Makefile
all: $(NAME).ihx
$(SRC) : %.c : %.h $(INDEPENDENT_HEADERS)
@touch $@
%.h: ;
clean:
rm -f $(TRASH)
load: $(NAME).ihx
stm8flash $(FLASHFLAGS) -w $(NAME).ihx
%.rel: %.c
$(SDCC) $(CCFLAGS) -c $<
$(NAME).ihx: $(OBJ)
$(SDCC) $(LDFLAGS) $(OBJ) -o $(NAME).ihx
.PHONY: all

View File

@ -0,0 +1,205 @@
/*
* interrupts.c
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "ports_definition.h"
#include "main.h"
#include "stepper.h"
// Top Level Interrupt
INTERRUPT_HANDLER(TLI_IRQHandler, 0){}
// Auto Wake Up Interrupt
INTERRUPT_HANDLER(AWU_IRQHandler, 1){}
// Clock Controller Interrupt
INTERRUPT_HANDLER(CLK_IRQHandler, 2){}
// External Interrupt PORTA
INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3){}
// External Interrupt PORTB
INTERRUPT_HANDLER(EXTI_PORTB_IRQHandler, 4){}
// External Interrupt PORTC
INTERRUPT_HANDLER(EXTI_PORTC_IRQHandler, 5){}
// External Interrupt PORTD
INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6){
}
// External Interrupt PORTE
INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7){}
#ifdef STM8S903
// External Interrupt PORTF
INTERRUPT_HANDLER(EXTI_PORTF_IRQHandler, 8){}
#endif // STM8S903
#if defined (STM8S208) || defined (STM8AF52Ax)
// CAN RX Interrupt routine.
INTERRUPT_HANDLER(CAN_RX_IRQHandler, 8){}
// CAN TX Interrupt routine.
INTERRUPT_HANDLER(CAN_TX_IRQHandler, 9){}
#endif // STM8S208 || STM8AF52Ax
// SPI Interrupt routine.
INTERRUPT_HANDLER(SPI_IRQHandler, 10){}
// Timer1 Update/Overflow/Trigger/Break Interrupt
INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11){
if(TIM1_SR1 & TIM_SR1_UIF){ // update interrupt
Global_time++; // increase timer
}
TIM1_SR1 = 0; // clear all interrupt flags
}
// Timer1 Capture/Compare Interrupt routine.
INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12){}
#ifdef STM8S903
// Timer5 Update/Overflow/Break/Trigger Interrupt
INTERRUPT_HANDLER(TIM5_UPD_OVF_BRK_TRG_IRQHandler, 13){}
// Timer5 Capture/Compare Interrupt
INTERRUPT_HANDLER(TIM5_CAP_COM_IRQHandler, 14){}
#else // STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x
volatile char Nustep = 0; // microstep number
/*
* 0 0000
* 1 0001
* 2 0010
* 3 0011
* 4 0100
* 5 0101
* 6 0110
* 7 0111
* 8 1000
* 9 1001
*10 1010
*11 1011
*12 1100
*13 1101
*14 1110
*15 1111
*/
// microsteps: DCBA = 1000, 1100, 0100, 0110, 0010, 0011, 0001, 1001
char usteps[8] = {8, 12, 4, 6, 2, 3, 1, 9};
volatile char Ustep = 0;
// Timer2 Update/Overflow/Break Interrupt
INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13){ // generate pulses for stepper CLK
char tmp;
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++;
}
}
if(Nsteps == 0){
stop_motor();
}
}
}
// Timer2 Capture/Compare Interrupt
INTERRUPT_HANDLER(TIM2_CAP_COM_IRQHandler, 14){
}
#endif // STM8S903
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
defined(STM8S005) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8AF626x)
// Timer3 Update/Overflow/Break Interrupt
INTERRUPT_HANDLER(TIM3_UPD_OVF_BRK_IRQHandler, 15){}
// Timer3 Capture/Compare Interrupt
INTERRUPT_HANDLER(TIM3_CAP_COM_IRQHandler, 16){}
#endif // STM8S208, STM8S207 or STM8S105 or STM8AF62Ax or STM8AF52Ax or STM8AF626x
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S103) || \
defined(STM8S003) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8S903)
// UART1 TX Interrupt
INTERRUPT_HANDLER(UART1_TX_IRQHandler, 17){}
// UART1 RX Interrupt
INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18){}
#endif // STM8S208 or STM8S207 or STM8S103 or STM8S903 or STM8AF62Ax or STM8AF52Ax
// I2C Interrupt
INTERRUPT_HANDLER(I2C_IRQHandler, 19){}
#if defined(STM8S105) || defined(STM8S005) || defined (STM8AF626x)
// UART2 TX interrupt
INTERRUPT_HANDLER(UART2_TX_IRQHandler, 20){}
// UART2 RX interrupt
INTERRUPT_HANDLER(UART2_RX_IRQHandler, 21){
U8 rb;
if(UART2_SR & UART_SR_RXNE){ // data received
rb = UART2_DR; // read received byte & clear RXNE flag
while(!(UART2_SR & UART_SR_TXE));
UART2_DR = rb; // echo received symbol
UART_rx[UART_rx_cur_i++] = rb; // put received byte into cycled buffer
if(UART_rx_cur_i == UART_rx_start_i){ // Oops: buffer overflow! Just forget old data
UART_rx_start_i++;
check_UART_pointer(UART_rx_start_i);
}
check_UART_pointer(UART_rx_cur_i);
}
}
#endif // STM8S105 or STM8AF626x
#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
// UART3 TX interrupt
INTERRUPT_HANDLER(UART3_TX_IRQHandler, 20){}
// UART3 RX interrupt
INTERRUPT_HANDLER(UART3_RX_IRQHandler, 21){}
#endif // STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax
#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
// ADC2 interrupt
INTERRUPT_HANDLER(ADC2_IRQHandler, 22){}
#else
// ADC1 interrupt
INTERRUPT_HANDLER(ADC1_IRQHandler, 22){
}
#endif // STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax
#ifdef STM8S903
// Timer6 Update/Overflow/Trigger Interrupt
INTERRUPT_HANDLER(TIM6_UPD_OVF_TRG_IRQHandler, 23){}
#else // STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF52Ax or STM8AF62Ax or STM8AF626x
// Timer4 Update/Overflow Interrupt
INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23){}
#endif // STM8S903
// Eeprom EEC Interrupt
INTERRUPT_HANDLER(EEPROM_EEC_IRQHandler, 24){}

View File

@ -0,0 +1,144 @@
/*
* interrupts.h
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __INTERRUPTS_H__
#define __INTERRUPTS_H__
#include "stm8l.h"
// Top Level Interrupt
INTERRUPT_DEFINITION(TLI_IRQHandler, 0);
// Auto Wake Up Interrupt
INTERRUPT_DEFINITION(AWU_IRQHandler, 1);
// Clock Controller Interrupt
INTERRUPT_DEFINITION(CLK_IRQHandler, 2);
// External Interrupt PORTA
INTERRUPT_DEFINITION(EXTI_PORTA_IRQHandler, 3);
// External Interrupt PORTB
INTERRUPT_DEFINITION(EXTI_PORTB_IRQHandler, 4);
// External Interrupt PORTC
INTERRUPT_DEFINITION(EXTI_PORTC_IRQHandler, 5);
// External Interrupt PORTD
INTERRUPT_DEFINITION(EXTI_PORTD_IRQHandler, 6);
// External Interrupt PORTE
INTERRUPT_DEFINITION(EXTI_PORTE_IRQHandler, 7);
#ifdef STM8S903
// External Interrupt PORTF
INTERRUPT_DEFINITION(EXTI_PORTF_IRQHandler, 8);
#endif // STM8S903
#if defined (STM8S208) || defined (STM8AF52Ax)
// CAN RX Interrupt routine.
INTERRUPT_DEFINITION(CAN_RX_IRQHandler, 8);
// CAN TX Interrupt routine.
INTERRUPT_DEFINITION(CAN_TX_IRQHandler, 9);
#endif // STM8S208 || STM8AF52Ax
// SPI Interrupt routine.
INTERRUPT_DEFINITION(SPI_IRQHandler, 10);
// Timer1 Update/Overflow/Trigger/Break Interrupt
INTERRUPT_DEFINITION(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11);
// Timer1 Capture/Compare Interrupt routine.
INTERRUPT_DEFINITION(TIM1_CAP_COM_IRQHandler, 12);
#ifdef STM8S903
// Timer5 Update/Overflow/Break/Trigger Interrupt
INTERRUPT_DEFINITION(TIM5_UPD_OVF_BRK_TRG_IRQHandler, 13);
// Timer5 Capture/Compare Interrupt
INTERRUPT_DEFINITION(TIM5_CAP_COM_IRQHandler, 14);
#else // STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x
// Timer2 Update/Overflow/Break Interrupt
INTERRUPT_DEFINITION(TIM2_UPD_OVF_BRK_IRQHandler, 13);
// Timer2 Capture/Compare Interrupt
INTERRUPT_DEFINITION(TIM2_CAP_COM_IRQHandler, 14);
#endif // STM8S903
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
defined(STM8S005) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8AF626x)
// Timer3 Update/Overflow/Break Interrupt
INTERRUPT_DEFINITION(TIM3_UPD_OVF_BRK_IRQHandler, 15);
// Timer3 Capture/Compare Interrupt
INTERRUPT_DEFINITION(TIM3_CAP_COM_IRQHandler, 16);
#endif // STM8S208, STM8S207 or STM8S105 or STM8AF62Ax or STM8AF52Ax or STM8AF626x
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S103) || \
defined(STM8S003) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8S903)
// UART1 TX Interrupt
INTERRUPT_DEFINITION(UART1_TX_IRQHandler, 17);
// UART1 RX Interrupt
INTERRUPT_DEFINITION(UART1_RX_IRQHandler, 18);
#endif // STM8S208 or STM8S207 or STM8S103 or STM8S903 or STM8AF62Ax or STM8AF52Ax
// I2C Interrupt
INTERRUPT_DEFINITION(I2C_IRQHandler, 19);
#if defined(STM8S105) || defined(STM8S005) || defined (STM8AF626x)
// UART2 TX interrupt
INTERRUPT_DEFINITION(UART2_TX_IRQHandler, 20);
// UART2 RX interrupt
INTERRUPT_DEFINITION(UART2_RX_IRQHandler, 21);
#endif // STM8S105 or STM8AF626x
#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
// UART3 TX interrupt
INTERRUPT_DEFINITION(UART3_TX_IRQHandler, 20);
// UART3 RX interrupt
INTERRUPT_DEFINITION(UART3_RX_IRQHandler, 21);
#endif // STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax
#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
// ADC2 interrupt
INTERRUPT_DEFINITION(ADC2_IRQHandler, 22);
#else // STM8S105, STM8S103 or STM8S903 or STM8AF626x
// ADC1 interrupt
INTERRUPT_DEFINITION(ADC1_IRQHandler, 22);
#endif // STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax
#ifdef STM8S903
// Timer6 Update/Overflow/Trigger Interrupt
INTERRUPT_DEFINITION(TIM6_UPD_OVF_TRG_IRQHandler, 23);
#else // STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF52Ax or STM8AF62Ax or STM8AF626x
// Timer4 Update/Overflow Interrupt
INTERRUPT_DEFINITION(TIM4_UPD_OVF_IRQHandler, 23);
#endif // STM8S903
// Eeprom EEC Interrupt
INTERRUPT_DEFINITION(EEPROM_EEC_IRQHandler, 24);
#endif // __INTERRUPTS_H__

230
stepper_ULN2003/main.c Normal file
View File

@ -0,0 +1,230 @@
/*
* blinky.c
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "ports_definition.h"
#include "interrupts.h"
#include "main.h"
#include "stepper.h"
unsigned long Global_time = 0L; // global time in ms
U16 paused_val = 500; // interval between LED flashing
U8 UART_rx[UART_BUF_LEN]; // cycle buffer for received data
U8 UART_rx_start_i = 0; // started index of received data (from which reading starts)
U8 UART_rx_cur_i = 0; // index of current first byte in rx array (to which data will be written)
/**
* Send one byte through UART
* @param byte - data to send
*/
void UART_send_byte(U8 byte){
while(!(UART2_SR & UART_SR_TXE)); // wait until previous byte transmitted
UART2_DR = byte;
}
void uart_write(char *str){
while(*str){
while(!(UART2_SR & UART_SR_TXE));
UART2_CR2 |= UART_CR2_TEN;
UART2_DR = *str++;
}
}
/**
* Read one byte from Rx buffer
* @param byte - where to store readed data
* @return 1 in case of non-empty buffer
*/
U8 UART_read_byte(U8 *byte){
if(UART_rx_start_i == UART_rx_cur_i) // buffer is empty
return 0;
*byte = UART_rx[UART_rx_start_i++];
check_UART_pointer(UART_rx_start_i);
return 1;
}
void printUint(U8 *val, U8 len){
unsigned long Number = 0;
U8 i = len;
char ch;
U8 decimal_buff[12]; // max len of U32 == 10 + \n + \0
if(len > 4 || len == 3 || len == 0) return;
for(i = 0; i < 12; i++)
decimal_buff[i] = 0;
decimal_buff[10] = '\n';
ch = 9;
switch(len){
case 1:
Number = *((U8*)val);
break;
case 2:
Number = *((U16*)val);
break;
case 4:
Number = *((unsigned long*)val);
break;
}
do{
i = Number % 10L;
decimal_buff[ch--] = i + '0';
Number /= 10L;
}while(Number && ch > -1);
uart_write((char*)&decimal_buff[ch+1]);
}
U8 readInt(int *val){
unsigned long T = Global_time;
unsigned long R = 0;
int readed;
U8 sign = 0, rb, ret = 0, bad = 0;
do{
if(!UART_read_byte(&rb)) continue;
if(rb == '-' && R == 0){ // negative number
sign = 1;
continue;
}
if(rb < '0' || rb > '9') break; // number ends with any non-digit symbol that will be omitted
ret = 1; // there's at least one digit
R = R * 10L + rb - '0';
if(R > 0x7fff){ // bad value
R = 0;
bad = 0;
}
}while(Global_time - T < 10000); // wait no longer than 10s
if(bad || !ret) return 0;
readed = (int) R;
if(sign) readed *= -1;
*val = readed;
return 1;
}
void error_msg(char *msg){
uart_write("\nERROR: ");
uart_write(msg);
UART_send_byte('\n');
}
int main() {
unsigned long T = 0L;
int Ival;
U8 rb;
CFG_GCR |= 1; // disable SWIM
// Configure clocking
CLK_CKDIVR = 0; // F_HSI = 16MHz, f_CPU = 16MHz
// Configure timer 1 - systick
// prescaler = f_{in}/f_{tim1} - 1
// set Timer1 to 1MHz: 1/1 - 1 = 15
TIM1_PSCRH = 0;
TIM1_PSCRL = 15; // LSB should be written last as it updates prescaler
// auto-reload each 1ms: TIM_ARR = 1000 = 0x03E8
TIM1_ARRH = 0x03;
TIM1_ARRL = 0xE8;
// interrupts: update
TIM1_IER = TIM_IER_UIE;
// auto-reload + interrupt on overflow + enable
TIM1_CR1 = TIM_CR1_APRE | TIM_CR1_URS | TIM_CR1_CEN;
// Configure pins
// PC2 - PP output (on-board LED)
PORT(LED_PORT, DDR) |= LED_PIN;
PORT(LED_PORT, CR1) |= LED_PIN;
// PD5 - UART2_TX
PORT(UART_PORT, DDR) |= UART_TX_PIN;
PORT(UART_PORT, CR1) |= UART_TX_PIN;
// Configure UART
// 8 bit, no parity, 1 stop (UART_CR1/3 = 0 - reset value)
// 57600 on 16MHz: BRR1=0x11, BRR2=0x06
UART2_BRR1 = 0x11; UART2_BRR2 = 0x06;
UART2_CR2 = UART_CR2_TEN | UART_CR2_REN | UART_CR2_RIEN; // Allow RX/TX, generate ints on rx
// enable all interrupts
enableInterrupts();
set_stepper_speed(1000);
setup_stepper_pins();
// Loop
do{
if((Global_time - T > paused_val) || (T > Global_time)){
T = Global_time;
PORT(LED_PORT, ODR) ^= LED_PIN; // blink on-board LED
}
if(UART_read_byte(&rb)){ // buffer isn't empty
switch(rb){
case 'h': // help
case 'H':
uart_write("\nPROTO:\n+/-\tLED period\nS/s\tset/get Mspeed\nm\tget steps\nx\tstop\np\tpause/resume\nM\tmove motor\na\tadd Nstps\n");
break;
case '+':
paused_val += 100;
if(paused_val > 10000)
paused_val = 500; // but not more than 10s
break;
case '-':
paused_val -= 100;
if(paused_val < 100) // but not less than 0.1s
paused_val = 500;
break;
case 'S': // set stepper speed
if(readInt(&Ival) && Ival > MIN_STEP_LENGTH)
set_stepper_speed(Ival);
else
error_msg("bad speed");
break;
case 's': // get stepper speed
printUint((U8*)&Stepper_speed, 2);
break;
case 'm': // how much steps there is to the end of moving
printUint((U8*)&Nsteps, 4);
break;
case 'M': // move motor
if(Nsteps){
error_msg("moving!");
break;
}
if(readInt(&Ival) && Ival)
move_motor(Ival);
else{
error_msg("bad Nsteps");
}
break;
case 'x': // stop
stop_motor();
break;
case 'p': // pause/resume
pause_resume();
break;
case 'a': // add N steps
if(readInt(&Ival) && Ival){
add_steps(Ival);
}else{
error_msg("bad value");
}
break;
}
}
}while(1);
}

41
stepper_ULN2003/main.h Normal file
View File

@ -0,0 +1,41 @@
/*
* blinky.h
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __MAIN_H__
#define __MAIN_H__
extern unsigned long Global_time; // global time in ms
#define UART_BUF_LEN 8 // max 7 bytes transmited in on operation
#define MIN_STEP_LENGTH 125 // max speed == 1/(125us*16) = 500 steps per second
extern U8 UART_rx[];
extern U8 UART_rx_start_i;
extern U8 UART_rx_cur_i;
void UART_send_byte(U8 byte);
void uart_write(char *str);
void printUint(U8 *val, U8 len);
void error_msg(char *msg);
#define check_UART_pointer(x) if(x == UART_BUF_LEN) x = 0;
#endif // __MAIN_H__

View File

@ -0,0 +1,43 @@
/*
* ports_definition.h - definition of ports pins & so on
*
* Copyright 2014 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __PORTS_DEFINITION_H__
#define __PORTS_DEFINITION_H__
#include "stm8l.h"
// macro for using in port constructions like PORT(LED_PORT, ODR) = xx
#define CONCAT(a, b) a ## _ ## b
#define PORT(a, b) CONCAT(a , b)
// on-board LED
#define LED_PORT PC
#define LED_PIN GPIO_PIN2
// UART2_TX
#define UART_PORT PD
#define UART_TX_PIN GPIO_PIN5
/***** Stepper motor *****/
// Clocking
#define STP_PORT PB // PB0..3 -- pins A..D of stepper
#endif // __PORTS_DEFINITION_H__

99
stepper_ULN2003/stepper.c Normal file
View File

@ -0,0 +1,99 @@
/*
* stepper.c
*
* Copyright 2014 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "ports_definition.h"
#include "stepper.h"
volatile long Nsteps = 0; // Number of steps
volatile char Dir = 0; // direction of moving: 0/1
U16 Stepper_speed = 0; // length of one MICROstep in us
/**
* Setup pins of stepper motor (all - PP out)
*/
void setup_stepper_pins(){
// PB0..3
PORT(STP_PORT, DDR) |= 0x0f;
PORT(STP_PORT, CR1) |= 0x0f;
}
/**
* Set speed of stepper motor
* @param Sps - period (in us) of one MICROstep
*/
void set_stepper_speed(U16 SpS){
Stepper_speed = SpS;
// Configure timer 2 to generate signals for CLK
TIM2_PSCR = 4; // 1MHz
TIM2_ARRH = SpS >> 8; // set speed
TIM2_ARRL = SpS & 0xff;
TIM2_IER = TIM_IER_UIE; // update interrupt enable
TIM2_CR1 |= TIM_CR1_APRE | TIM_CR1_URS; // auto reload + interrupt on overflow & RUN
}
void move_motor(int Steps){
if(Steps < 0){
Dir = 1;
Steps *= -1;
}else
Dir = 0;
Nsteps = (long)Steps; // multiply by 3 (to get number of full steps)
TIM2_CR1 |= TIM_CR1_CEN; // turn on timer
}
void stop_motor(){
TIM2_CR1 &= ~TIM_CR1_CEN; // Turn off timer
Nsteps = 0;
PORT(STP_PORT, ODR) &= 0xf0; // turn off power
uart_write("stop\n");
}
void pause_resume(){
if(Nsteps == 0) return; // motor is stopped
if(TIM2_CR1 & TIM_CR1_CEN){ // pause
TIM2_CR1 &= ~TIM_CR1_CEN;
uart_write("pause\n");
}else{ // resume
TIM2_CR1 |= TIM_CR1_CEN;
uart_write("resume\n");
}
}
void add_steps(int Steps){
long S;
// pause
TIM2_CR1 &= ~TIM_CR1_CEN;
if(Nsteps == 0){ // motor is stopped - just move it
move_motor(Steps);
return;
}
S = (long)Steps;
Nsteps += S;
// now change direction
if(Nsteps < 0){
uart_write("reverce\n");
Dir = !Dir; // invert direction
Nsteps *= -1L;
}
// resume if Nsteps != 0
if(Nsteps)
TIM2_CR1 |= TIM_CR1_CEN;
}

40
stepper_ULN2003/stepper.h Normal file
View File

@ -0,0 +1,40 @@
/*
* stepper.h
*
* Copyright 2014 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __STEPPER_H__
#define __STEPPER_H__
#include "ports_definition.h"
#include "main.h"
extern volatile long Nsteps;
extern U16 Stepper_speed;
extern volatile char Dir;
void setup_stepper_pins();
void set_stepper_speed(U16 SpS);
void move_motor(int Steps);
void stop_motor();
void pause_resume();
void add_steps(int Steps);
#endif // __STEPPER_H__

View File

@ -0,0 +1,93 @@
:2080A000AE5007F6AA0FF7AE5008F6AA0FF7815202160590CF000EAE530CA604F7909E0F21
:2080C00001AE530DF74FAE530E909FF7AE5301A601F7AE5300F6AA84F75B02811E03A300B8
:2080E000002E0B3501000D1E03501F032004725F000D16035F905D2A015A90CF000BCF004C
:20810000097210530081AE5300F6A4FEF7725F000C725F000B725F000A725F0009AE5005FF
:20812000F6A4F0F7AE81E589CD83475B0281CE000B2607CE0009272C2000AE5300F695A52B
:20814000012711A4FEAE5300F7AE81EB89CD83475B0220109EAA01AE5300F7AE81F289CDCD
:2081600083475B028172115300CE000B260FCE0009260A1E0389CD80DC5B02206716035F3D
:20818000905D2A015A72B9000B9FC9000A979EC900099590CF000BCF0009CE000BA300006B
:2081A000C6000AA200C60009A2002E2AAE81FA89CD83475B02C6000DA0014F49C7000D906E
:2081C000CE000B90504FC2000A974FC200099590CF000BCF0009CE000B2605CE000927043D
:2081E000721053008173746F700A0070617573650A00726573756D650A007265766572630F
:03820000650A000C
:078ABE0000000000000000B1
:20820300808080808080808080AE5255F644241B90CE001C72A90001C6001BA90097C60090
:208223001AA9009590CF001CCF001AAE52557F80805202AE5302F66B027B02442503CC82BA
:20824300D07B02A4FEAE5302F7AE5005F6A4F06B01AE00119FCB0019979EA90095F61A0173
:20826300AE5005F7725D000D272C725C0019C60019A1072D4B725F001990CE000B72A9007E
:2082830001C6000AA90097C60009A9009590CF000BCF0009202A725A0019C60019A1002E9E
:2082A3001F3507001990CE000B72A90001C6000AA90097C60009A9009590CF000BCF000963
:2082C300CE000B2608CE00092603CD81065B028080808080805204AE5240F66B047B04A5C4
:2082E30020274DAE5241F66B017B04A4804D27FDAE52417B01F7AE00011F02C6002197C668
:2083030000214CC700214F9572FB027B01F7C60020C10021260FC600204CC70020A108265A
:1683230004725F0020C60021A1082604725F00215B048080808044
:0A8AC50000080C040602030109007A
:20800000820080838200000082008203820082048200820582008206820082078200820820
:20802000820082098200820A82000000820000008200820B8200820C820082338200823493
:20804000820082D3820082D4820082D58200000082000000820082D6820082D7820082D803
:208060008200833682008337820083388200000082000000820000008200000082000000C2
:1D808300AE00082707724F00005A26F9AE00192709D68ABDD700085A26F7CC80808C
:03808000CC857F2D
:20833900AE5240F64824F9AE52417B03F781160390F64D2718AE5240F64824F9AE5245F651
:20835900AA08F790F6905CAE5241F720E3815202C60021C1002026034F20271605AE000188
:208379001F01C6002097C600204CC700204F9572FB01F690F7C60020A1082604725F0020B5
:20839900A6015B0281521C5F1F031F017B21A1042303CC84767B21A1032603CC84760D21A6
:2083B9002603CC8476961C00051F124F5F9772FB127F4CA10C25F51E121C000AA60AF77BFF
:2083D90021A101270E7B21A102271C7B21A104272120301E1FF66B185F0F151F027B186BD9
:2083F900047B156B01201C161F90FE5F17031F0120111E1FE6036B1CE602FE6B031F017BFF
:208419001C6B04A6096B114B0A5F894B001E07891E0789CD88395B089F887B126B15840AF6
:20843900115F417B144172FB12AB30F74B0A5F894B001E07891E0789CD88B65B081F0317C6
:20845900011E0326041E0127067B11A1FF2CB87B114C5F9772FB1289CD83475B025B1C8199
:20847900521ACE001C1F05CE001A1F035F1F091F070F0C0F0E0F0D961C000B89CD83675B06
:20849900024D2603CC851D7B0BA12D260E1E09260A1E072606A6016B0C20697B0BA130248B
:2084B90003CC853F7B0BA1392303CC853FA6016B0E1E09891E09894B0A5F894B00CD895547
:2084D9005B081F1917177B0B0F155F90977B15909572F9199F1918979E19179572A2003043
:2084F9009FA2006B109EA20017096B077B106B08AE7FFF13094F12084F120724075F1F090C
:208519001F070F0D90CE001C72F205C6001B120495C6001A12039790A327109EA2009FA21A
:20853900002403CC84900D0D26040D0E26034F201A7B09887B0B6B03846B010D0C27051EB7
:2085590001501F011E1D1601FFA6015B1A81AE87A389CD83475B021E0389CD83475B024B65
:208579000ACD83398481520C5F1F031F0172107F60AE50C67FAE52607FAE5261A60FF7AE0D
:208599005262A603F7AE5263A6E8F7AE5254A601F7AE5250A685F7AE500CF6AA04F7AE507F
:2085B9000DF6AA04F7AE5011F6AA20F7AE5012F6AA20F7AE5242A611F7AE5243A606F7AEE9
:2085D9005245A62CF79A4BE84B03CD80AF5B02CD80A0CE001C72F0031F0AC6001B12026BE9
:2085F90009C6001A1201CE001E905F88130B909F120A909E12015B012511CE001C1303C601
:20861900001B1202C6001A12012411CE001C1F03CE001A1F01AE500AF6A804F7961C00057E
:2086390089CD83675B024D27A97B056B0C7B0CA12B27637B0CA12D2603CC86CB7B0CA14888
:2086590027487B0CA14D2603CC872E7B0CA1532603CC86E77B0CA1612603CC87787B0CA1F1
:208679006827277B0CA16D2603CC87207B0CA1702603CC87727B0CA1732603CC87127B0CC4
:20869900A1782603CC876CCC85EBAE87AC89CD83475B02CC85EBCE001E1C0064CF001EA3EE
:2086B90027102203CC85EB35F4001F3501001ECC85EBCE001E1D0064CF001EA300642503A8
:2086D900CC85EB35F4001F3501001ECC85EB961C000689CD84795B024D27121E06A3007D3B
:2086F9002D0B1E0689CD80AF5B02CC85EBAE881289CD85675B02CC85EBAE000E4B0289CD65
:20871900839E5B03CC85EBAE00094B0489CD839E5B03CC85EBCE000B2605CE0009270CAEB2
:20873900881C89CD85675B02CC85EB961C000689CD84795B024D270F1E06270B1E0689CDE6
:2087590080DC5B02CC85EBAE882489CD85675B02CC85EBCD8106CC85EBCD812ECC85EB96F8
:208779001C000689CD84795B024D270F1E06270B1E0689CD81655B02CC85EBAE882F89CD81
:2087990085675B02CC85EB5B0C810A4552524F523A20000A50524F544F3A0A2B2F2D094CA7
:2087B900454420706572696F640A532F73097365742F676574204D73706565640A6D09674C
:2087D90065742073746570730A780973746F700A700970617573652F726573756D650A4D4F
:2087F900096D6F7665206D6F746F720A6109616464204E737470730A0062616420737065E1
:208819006564006D6F76696E672100626164204E7374657073006261642076616C7565009D
:088ACF000000000001F40000AA
:2088390052040F040F017B0B484F494D262E160D1E0B905859170D1F0B1E09130D7B0812E8
:208859000C7B07120B240D160D1E0B549056170D1F0B20080C017B016B0420CA7B046B0358
:208879001E09130D7B08120C7B07120B2513160972F20D7B08120C977B07120B9517091FE5
:2088990007160D1E0B549056170D1F0B7B036B020A030D0226CA1E0916075B048152125F06
:2088B9001F051F03A6206B027B15484F496B0116171E1590585917171F157B036B0B1E0431
:2088D900887B076B0F84080E59090B1F047B0E6B067B0B6B030D01271A7B06AA016B127B70
:2088F900056B117B046B107B036B0F16111705160F17031E05131B7B04121A7B03121925A0
:208919002B160572F21B7B04121A6B087B03121917056B037B086B047B18AA0190977B173F
:2089390090957B16977B159517171F150A020D022703CC88C11E1716155B128152409096EA
:20895900905C961C00431F091E09E603961C00471F0B1E0B1F111E111F131E1388E60197D1
:20897900844290FF72A900021E09E6031E0B1F3F1E3F1F0D1E0D1F0F1E0F88E603978442F8
:2089990090FF965C1F151E15F66B1B1E09F697160B90E603429F1B1B1E15F71E15F66B1C1B
:2089B9001E09E60197160B90E602429F1B1C1E15F79096905C93FE1F1D1E09E6011E0B1FE9
:2089D9001F1E1F1F211E211F231E2388E60397844272FB1D90FF93FE1F251E09E6021E0B2C
:2089F9001F271E271F291E291F2B1E2B88E60297844272FB2590FF16091E09E6021E0B1F4D
:208A19002F1E2F1F311E311F331E3388E6019784429F90F71E095C1F351E09E60290971E62
:208A39000BE60390421E35FF160B1E09E6031E0B1F371E371F3D1E3D88F69784429F90F748
:208A59001E0B5C1F2D1E09E60390971E0BE60290421E2DFF1E0B1C00037F1E091C00037F3C
:208A7900965CE6036B08E6026B07E6016B06F616431739164572F9071719887B07193B6B78
:208A9900198419396B171619EF021617FFE603E602FE16491E4772F9199F1918979E1917B4
:058AB90095515B4081B6
:00000001FF