Fixed error in ULN stepper management + added simple work with bipolar steppers by two L9110s H-bridge drivers

This commit is contained in:
Eddy 2014-08-07 01:31:38 +04:00
parent 68e217e032
commit f923706b9b
12 changed files with 227 additions and 31 deletions

55
DRUM/CD74HC154_LEDs.c Normal file
View File

@ -0,0 +1,55 @@
/*
* CD74HC154_LEDs.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 "CD74HC154_LEDs.h"
// bits array: contains numbers of LED's bits to shine (0..6) with zero 4th bit or 0x1x for OFF
U8 LED_bits[LEDS_AMOUNT];
/**
* Initialize bit array for LEDs blinking
* @param mask - bitmap mask for LEDs: 1 - LED is ON, 0 - LED is OFF, mask &= 0x3f (6 LEDs)!
*/
void set_LEDs(U16 mask){
U8 i;
for(i = 0; i < LEDS_AMOUNT; i++, mask >>= 1)
LED_bits[i] = (mask & 1) ? i : 0x10; // to turn all LEDs off we set !E to high level
}
U8 current_LED = 0;
/**
* Send next binary value to demultiplexer
* we have cycle of LEDS_AMOUNT values, blink them consecutively (don't forget
* to reduce LEDs resistor according to LEDS_AMOUNT)
*/
void blink_next_LED(){
PORT(LEDS_PORT, ODR) = LED_bits[current_LED++];
if(current_LED == LEDS_AMOUNT)
current_LED = 0;
}
// turn off all LEDs
void reset_LEDs(){
U8 i;
for(i = 0; i < LEDS_AMOUNT; i++)
LED_bits[i] = 0x10;
}

35
DRUM/CD74HC154_LEDs.h Normal file
View File

@ -0,0 +1,35 @@
/*
* CD74HC154_LEDs.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 __CD74HC154_LEDS_H__
#define __CD74HC154_LEDS_H__
#include "ports_definition.h"
#define LEDS_AMOUNT 6 // we have only 6 LEDs on drum
void set_LEDs(U16 mask);
void blink_next_LED();
void reset_LEDs();
#endif // __CD74HC154_LEDS_H__

View File

@ -9,7 +9,7 @@ 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).ihx $(NAME).lk $(NAME).map
TRASH+=$(SRC:%.c=%.sym) $(NAME).lk $(NAME).map
INDEPENDENT_HEADERS=../stm8l.h ports_definition.h Makefile
all: $(NAME).ihx

View File

@ -1 +1 @@
This is a table-based generator of simplest waveforms
Kids' drum

View File

@ -22,6 +22,7 @@
#include "ports_definition.h"
#include "main.h"
#include "noicegen.h"
#include "CD74HC154_LEDs.h"
// Top Level Interrupt
INTERRUPT_HANDLER(TLI_IRQHandler, 0){}
@ -43,8 +44,14 @@ INTERRUPT_HANDLER(EXTI_PORTB_IRQHandler, 4){
}
// External Interrupt PORTC
INTERRUPT_HANDLER(EXTI_PORTC_IRQHandler, 5){
INTERRUPT_HANDLER(EXTI_PORTC_IRQHandler, 5){// PC1 & PC2 - boom
U8 P = 0;
if(PC_IDR & GPIO_PIN1){ // first touch sensor, 100Hz
change_period(10000); P = 1;
}else if(PC_IDR & GPIO_PIN2){ // second touch sensor, 40Hz
change_period(25000); P = 1;
}
if(P) play_snd();
}
// External Interrupt PORTD
@ -54,7 +61,6 @@ INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6){
// External Interrupt PORTE
INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7){
}
#ifdef STM8S903
@ -173,6 +179,7 @@ INTERRUPT_HANDLER(TIM6_UPD_OVF_TRG_IRQHandler, 23){}
INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23){
if(TIM4_SR & TIM_SR1_UIF){ // update interrupt
Global_time++; // increase timer
blink_next_LED(); // and switch to next LED
}
TIM4_SR = 0; // clear all interrupt flags
}

View File

@ -19,11 +19,11 @@
* MA 02110-1301, USA.
*/
#include "stm8l.h"
#include "ports_definition.h"
#include "interrupts.h"
#include "main.h"
#include "noicegen.h"
#include "CD74HC154_LEDs.h"
/*
* 0 0000
@ -45,9 +45,9 @@
*/
unsigned long Global_time = 0L, boom_start = 0L; // global time in ms
unsigned int boom_length = 100; // length of "boom" in ms
U16 boom_length = 100; // length of "boom" in ms
U16 paused_val = 500; // interval between LED flashing
U8 snd_i = 0, bank_i = 0; // number of sample in sound, number in sine vawe
U8 bank_i = 0; // number of sample in meander (even/odd)
U8 sample_flag = 0; // flag is set in interrupt -> next sample in sound
#ifdef UART
@ -116,28 +116,23 @@ void printUint(U8 *val, U8 len){
uart_write((char*)&decimal_buff[ch+1]);
}
U8 readInt(int *val){
U8 readInt(U16 *val){
unsigned long T = Global_time;
unsigned long R = 0;
int readed;
U8 sign = 0, rb, ret = 0, bad = 0;
U16 readed;
U8 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
if(R > 0xffff){ // bad value
R = 0;
bad = 0;
bad = 1;
}
}while(Global_time - T < 10000); // wait no longer than 10s
if(bad || !ret) return 0;
readed = (int) R;
if(sign) readed *= -1;
readed = (U16) R;
*val = readed;
return 1;
}
@ -155,7 +150,7 @@ int main() {
unsigned long T = 0L;
unsigned int I;
U8 cur_vol;
int Ival;
U16 Ival;
#ifdef UART
U8 rb;
#endif
@ -171,6 +166,8 @@ int main() {
TIM4_IER = TIM_IER_UIE;
// auto-reload + interrupt on overflow + enable
TIM4_CR1 = TIM_CR1_APRE | TIM_CR1_URS | TIM_CR1_CEN;
// EXTI: PC1 & PC2 == "boom"
EXTI_CR1 = 0x10; // PCIS = 01 - rising edge only
#ifdef UART
// Configure pins
// PC2 - PP output (on-board LED)
@ -181,6 +178,9 @@ int main() {
PORT(UART_PORT, ODR) &= ~UART_TX_PIN; // turn off N push-down
//PORT(UART_PORT, CR1) |= UART_TX_PIN;
#endif
// LEDS on DRUM
PORT(LEDS_PORT, DDR) |= 0x1f;
PORT(LEDS_PORT, CR1) |= 0x1f;
PC_DDR |= GPIO_PIN1; // setup timer's output
PC_ODR &= ~GPIO_PIN1;
#ifdef UART
@ -231,6 +231,7 @@ int main() {
"P/p\tBoom\n"
"F\tSet frequency\n"
"L\tChange boom length (in ms)\n"
"l\tblink LEDs by mask"
);
break;
break;
@ -246,7 +247,7 @@ int main() {
break;
case 'F':
if(readInt(&Ival) && Ival > 64){
change_period(((U16)Ival) >> 4); // F*4 for 16 array values
change_period(Ival >> 4); // F*4 for 16 array values
}else error_msg("bad period");
break;
case 'P':
@ -258,6 +259,11 @@ int main() {
boom_length = Ival;
}else error_msg("bad length");
break;
case 'l':
if(readInt(&Ival) && (Ival == (Ival & 0x3f))){
set_LEDs(Ival);
}else error_msg("bad bitmask");
break;
}
}
#endif

View File

@ -22,9 +22,11 @@
#ifndef __MAIN_H__
#define __MAIN_H__
#include "stm8l.h"
extern unsigned long Global_time, boom_start; // global time in ms
extern U8 sample_flag; // flag is set in interrupt -> next sample in sound
extern U8 snd_i, bank_i;
extern U8 bank_i;
#define UART_BUF_LEN 8 // max 7 bytes transmited in on operation
#define MIN_STEP_LENGTH 9 // max speed, microseconds for one microstep

View File

@ -36,7 +36,7 @@ void configure_timers(){
//TIM1_CCMR1 = 0x70; // OC1M = 111b - PWM mode 2 ( 0 -> 1)
TIM1_CCER1 = 1; // Channel 1 is on. Active is high
//TIM1_CCER1 = 3; // Channel 1 is on. Active is low
// default period: near 32ms
// default period: near 33ms (30Hz)
TIM2_ARRH = 127; TIM2_ARRL = 0;
// interrupts: update for timer 2, none for timer 1
TIM1_IER = 0;

View File

@ -31,10 +31,10 @@
void configure_timers();
// change period (in us)
#define change_period(F) do{TIM2_ARRH = F >> 8; TIM2_ARRL = F;}while(0)
#define change_period(F) do{TIM2_ARRH = F >> 8; TIM2_ARRL = F & 0xff;}while(0)
#define change_CCR(C) do{TIM1_CCR1H = 0; TIM1_CCR1L = C;}while(0)
// change CCR value. U = Vcc *
#define play_snd() do{boom_start = Global_time; snd_i = 0; bank_i = 0; \
#define play_snd() do{boom_start = Global_time; bank_i = 0; \
TIM1_CR1 = TIM_EN; TIM2_CR1 = TIM_EN;}while(0)
#define stop_snd() do{TIM1_CR1 |= TIM_CR1_OPM; TIM2_CR1 = 0;}while(0)

View File

@ -37,6 +37,8 @@
#define UART_PORT PD
#define UART_TX_PIN GPIO_PIN5
// LEDs on drum: PB0..3 - LED number, PB4 - on(0)/off(1)
#define LEDS_PORT PB

View File

@ -39,7 +39,7 @@ $Descr A3 16535 11693
encoding utf-8
Sheet 1 3
Title ""
Date "28 jul 2014"
Date "30 jul 2014"
Rev ""
Comp ""
Comment1 ""
@ -1004,8 +1004,6 @@ NoConn ~ 12800 3600
NoConn ~ 12800 3500
NoConn ~ 12800 3400
NoConn ~ 12800 3300
Wire Wire Line
12800 3100 12800 3200
$Comp
L GND #PWR?
U 1 1 53D78C57
@ -1021,10 +1019,9 @@ Wire Wire Line
13000 3250 13000 3200
Wire Wire Line
13000 3200 12800 3200
Connection ~ 12800 3200
Text Notes 2050 3450 0 60 ~ 0
LEDs address 3\n2\n1\n0
Text Notes 2150 3250 0 60 ~ 0
Text Notes 2650 3050 0 60 ~ 0
DRUM0\nDRUM1
Text Label 12800 3000 0 60 ~ 0
PB3
@ -1034,7 +1031,7 @@ Text Label 12800 2800 0 60 ~ 0
PB1
Text Label 12800 2700 0 60 ~ 0
PB0
Text Label 10900 4100 0 60 ~ 0
Text Label 12800 3100 0 60 ~ 0
PB4
Text Label 10900 4250 0 60 ~ 0
PB5
@ -1052,4 +1049,8 @@ Wire Wire Line
10900 4250 10800 4250
Wire Wire Line
10800 4100 10900 4100
Text Label 10900 4100 0 60 ~ 0
PF4
Text Notes 2400 3350 0 60 ~ 0
LEDs off
$EndSCHEMATC

88
DRUM/testproj.ihx Normal file
View File

@ -0,0 +1,88 @@
:2080A0008080808080880F01AE500BF6A5022712AE530DA627F7AE530EA610F7A6016B0128
:2080C0002014A5042710AE530DA661F7AE530EA6A8F7A6016B010D01271E90CE0011CE0089
:2080E0000F90CF0015CF0013725F001BAE5250A685F7AE5300A685F784808080808080AE68
:208100005302F6A501270A3501001CA4FEAE5302F78080808080805204AE5240F66B047BD9
:2081200004A5202751AE5241F66B017B04A4804D27FD7B0188CD81F484AE00011F02C600E7
:208140001E97C6001E4CC7001E4F9572FB027B01F7C6001DC1001E2612C6001D4CC7001D82
:20816000C6001DA1082604725F001DC6001EA1082604725F001E5B048080AE5342F64424B5
:208180001E90CE001172A90001C60010A90097C6000FA9009590CF0011CF000FCD8781AE3C
:2081A00053427F8080AE52607FAE5261A603F7AE530CA604F7AE52627FAE5263A610F7AE7E
:2081C00052657FAE5266A608F7AE5258A660F7AE525CA601F7AE530DA67FF7AE530E7FAEA9
:1481E00052547FAE5301A601F7AE526DF6AA80AE526DF78154
:208000008200808382000000820080A0820080A1820080A2820080A3820080A4820080A57E
:20802000820080FA820080FB8200000082000000820080FC820080FD820080FE820080FF45
:2080400082008112820081138200811482000000820000008200811582008116820081178F
:20806000820081798200817A820081A48200000082000000820000008200000082000000D6
:1D808300AE000E2707724F00005A26F9AE00122709D68A45D7000E5A26F7CC8080FF
:03808000CC8407A6
:2081F400AE52417B03F7AE5240F6A54027F881160390F64D2710905CAE5241F7AE5240F67D
:20821400A54026ED20F6815202C6001EC1001D26034F20271605AE00011F01C6001D97C6BC
:20823400001D4CC7001D4F9572FB01F690F7C6001DA1082604725F001DA6015B0281521C77
:208254005F1F101F0E7B21A1042303CC83287B21A1032603CC83280D212603CC8328965CD1
:208274001F1B4F5F9772FB1B7F4CA10C25F51E1B1C000AA60AF77B21A101270E7B21A10299
:20829400271C7B21A104272120301E1FF66B1A5F0F171F0F7B1A6B117B176B0E201C161FB6
:2082B40090FE5F17101F0E20111E1FE6036B16E602FE6B101F0E7B166B11A6096B0D4B0A7A
:2082D4005F894B001E14891E1489CD87C55B089F887B0E6B13840A0D5F417B124172FB1BA6
:2082F400AB30F74B0A5F894B001E14891E1489CD88425B081F10170E1E1026041E0E27069B
:208314007B0DA1FF2CB87B0D4C5F9772FB1B89CD82035B025B1C815217CE00111F0ACE007C
:208334000F1F085F1F061F040F020F01961C000389CD821B5B024D276B7B03A1302403CC05
:2083540083DA7B03A1392303CC83DAA6016B021E06891E06894B0A5F894B00CD88E15B0876
:208374001F1617147B030F125F90977B12909572F9169F1915979E19149572A200309FA2E8
:20839400006B0D9EA20017066B047B0D6B05AEFFFF13064F12054F120424095F1F061F0429
:2083B400A6016B0190CE001172F20AC60010120995C6000F12089790A327109EA2009FA2C2
:2083D400002403CC83400D0126040D0226034F200716061E1AFFA6015B1781AE86BB89CDBB
:2083F40082035B021E0389CD82035B024B0ACD81F4848152115F1F061F04AE7F60F6AA015A
:20841400AE7F60F7AE50C67FAE5345A607F7AE5346A67DF7AE5341A601F7AE5340A685F7F3
:20843400AE50A0A610F7AE500CF6AA04AE500CF7AE500DF6AA04AE500DF7AE5011F6AA20AE
:20845400AE5011F7AE500FF6A4DFAE500FF7AE5007F6AA1FAE5007F7AE5008F6AA1FAE50F5
:2084740008F7AE500CF6AA02AE500CF7AE500AF6A4FDAE500AF7AE5242A611F7AE5243A6C5
:2084940006F7AE5245A62CF7CD81A59A725D001C2603CC8520C60012C000169097C60011FF
:2084B400C200159095C60010C2001495C6000FC2001390C300172214CE0011C30015C600A4
:2084D40010C20014C6000FC200132410AE5250F6AA08AE5250F7AE53007F2030935858581A
:2084F4005890CE001765A61089100285725D001B270EAE52657FAE52667F725F001B200C60
:20851400AE52657FAE5266F73501001BCE001172F0061F10C6001012056B0FC6000F1204ED
:20853400CE0019905F881311909F1210909E12015B012511CE00111306C600101205C600D6
:208554000F12042414CE00111F06CE000F1F04AE500AF6A804AE500AF7965C89CD821B5BBD
:20857400024D2603CC84A07B01A12B273EA12D2759A1462603CC85FDA1482723A14C2603DD
:20859400CC8658A1502603CC8637A1682711A16C2603CC8686A1702603CC8637CC84A0AECA
:2085B40086C489CD82035B02CC84A0CE00191C0064CF0019CE0019A327102203CC84A035DB
:2085D400F4001A35010019CC84A0CE00191D0064CF0019CE0019A300642503CC84A035F4BB
:2085F400001A35010019CC84A0965C5C89CD832B5B024D27221E02A30040231B1E02A610B2
:20861400629E0F0CAE530DF71E02A610620F0A9FAE530EF7CC84A0AE872689CD83EF5B02C0
:20863400CC84A090CE0011CE000F90CF0015CF0013725F001BAE5250A685F7AE5300A6850A
:20865400F7CC84A0965C5C89CD832B5B024D27161E02A303E8240F1E02A3000123081E02F6
:20867400CF0017CC84A0AE873189CD83EF5B02CC84A0965C5C89CD832B5B024D27197B03DB
:20869400A43F6B090F081E021308260B1E0289CD87485B02CC84A0AE873C89CD83EF5B02C4
:2086B400CC84A05B1181010A4552524F523A20000A50524F544F3A0A2B2F2D094C45442073
:2086D400706572696F640A502F7009426F6F6D0A4609536574206672657175656E63790AF3
:2086F4004C094368616E676520626F6F6D206C656E6774682028696E206D73290A6C096233
:208714006C696E6B204C454473206279206D61736B0062616420706572696F640062616477
:14873400206C656E67746800626164206269746D61736B005D
:118A46000000000000000000006401F40000000000C6
:208748005206AE00091F050F015F7B019772FB057B0AA4016B040F03160327067B016B0210
:208768002004A6106B027B02F70C011E09541F097B01A10625D35B06815202AE00091F015E
:20878800C6002097C600204CC700204F9572FB01F6AE5005F7C60020A1062604725F002051
:1D87A8005B02815202AE00091F014F5F9772FB0188A610F7844CA10625F15B028158
:018A5700001E
:2087C50052040F020F017B0B484F494D262E160D1E0B905859170D1F0B1E09130D7B08125F
:2087E5000C7B07120B240D160D1E0B549056170D1F0B20080C017B016B0220CA7B026B04D0
:208805001E09130D7B08120C7B07120B2513160972F20D7B08120C977B07120B9517091F59
:2088250007160D1E0B549056170D1F0B7B046B030A040D0326CA1E0916075B048152125F76
:208845001F051F03A6206B027B15484F496B0116171E1590585917171F157B036B0F1E04A1
:20886500887B076B1384081259090F1F047B126B067B0F6B030D01271A7B06AA016B0A7BD8
:20888500056B097B046B087B036B0716091705160717031E05131B7B04121A7B031219253C
:2088A5002B160572F21B7B04121A6B0C7B03121917056B037B0C6B047B18AA0190977B17AC
:2088C50090957B16977B159517171F150A020D022703CC884D1E1716155B128152409096D3
:2088E500905C961C00431F0B1E0BE603961C00471F151E151F171E171F3F1E3F88E60197CA
:20890500844290FF72A900021E0BE6031E151F111E111F131E131F191E1988E6039784429C
:2089250090FF965C1F1B1E1BF66B1D1E0BF697161590E603429F1B1D1E1BF71E1BF66B1E65
:208945001E0BE60197161590E602429F1B1E1E1BF79096905C93FE1F1F1E0BE6011E151F3B
:20896500211E211F231E231F251E2588E60397844272FB1F90FF93FE1F271E0BE6021E1584
:208985001F291E291F2B1E2B1F2F1E2F88E60297844272FB2790FF160B1E0BE6021E151FA1
:2089A500311E311F331E331F351E3588E6019784429F90F71E0B5C1F371E0BE60290971EC5
:2089C50015E60390421E37FF16151E0BE6031E151F3D1E3D1F051E0588F69784429F90F7FF
:2089E5001E155C1F2D1E0BE60390971E15E60290421E2DFF1E151C00037F1E0B1C00037F8F
:208A0500965CE6036B0AE6026B09E6016B08F61643170D164572F909173B887B09190F6B18
:208A25003B84190D6B39163BEF021639FFFE16491E4772F93B9F193A979E193995515B40B6
:018A450081AF
:00000001FF