Last active
May 17, 2021 19:07
-
-
Save TG9541/da17844a758862ddd4772e95fdcf2e30 to your computer and use it in GitHub Desktop.
modbus_relay8 board reduced duty cycle experiment with STM8 eForth (https://github.com/TG9541/stm8ef)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\ Modbus_Relay X8 V1.2 - experiment with reduced relay solenoid current | |
\ use STM8S Low density device, e.g. STM8S103F3P6 | |
\res MCU: STM8S103 | |
\res export PB_IDR PD_IDR | |
\res export PC_ODR PC_DDR PC_CR1 | |
\res export PE_ODR PE_DDR PE_CR1 | |
\res export TIM1_CR1 TIM1_IER TIM1_SR1 | |
\res export TIM1_ARRH TIM1_CCMR3 TIM1_CCR3H | |
\res export INT_TIM1 INT_TIM1CC | |
\ TIM1 bit constants | |
0 CONSTANT UIE \ bit.pos | |
3 CONSTANT CC3IEF \ bit.pos | |
32000 CONSTANT T1REL \ reload for rate "T1REL/(16MHz_HSI)" | |
#require ]C! | |
#require ]B! | |
#require ]CB | |
#require ]BC | |
#require :NVM | |
\ compile RRC (1,X) | |
: RRC ( c1 -- c2 ) $6601 , ; IMMEDIATE | |
NVM | |
: out! ( c -- ) | |
RRC [ PE_ODR 5 ]CB | |
RRC [ PC_ODR 1 ]CB | |
RRC [ PC_ODR 2 ]CB | |
RRC [ PC_ODR 3 ]CB | |
RRC [ PC_ODR 4 ]CB | |
RRC [ PC_ODR 5 ]CB | |
RRC [ PC_ODR 6 ]CB | |
RRC [ PC_ODR 7 ]CB | |
DROP | |
; | |
\ read inputs - invert NPN optocouplers | |
: in@ ( -- c ) | |
-1 | |
[ PD_IDR 0 ]BC RRC | |
[ PD_IDR 2 ]BC RRC | |
[ PD_IDR 3 ]BC RRC | |
[ PD_IDR 4 ]BC RRC | |
[ PB_IDR 3 ]BC RRC | |
[ PB_IDR 2 ]BC RRC | |
[ PB_IDR 1 ]BC RRC | |
[ PB_IDR 0 ]BC RRC | |
NOT | |
; | |
\ initialize GPIOs for 8 relay outputs | |
: outinit ( -- ) | |
[ $00 PC_ODR ]C! [ $FE PC_DDR ]C! [ $FE PC_CR1 ]C! | |
[ $00 PE_ODR ]C! [ $20 PE_DDR ]C! [ $20 PE_CR1 ]C! | |
; | |
VARIABLE full \ 8 bit output pattern for full duty cycle | |
VARIABLE redu \ 8 bit output pattern for reduced duty cycle | |
\ TIM1 Update Event | |
:NVM | |
SAVEC | |
[ 0 TIM1_SR1 UIE ]B! \ clear interrupt | |
full @ out! \ first pattern | |
IRET | |
[ OVERT ( xt ) INT_TIM1 ! | |
\ TIM1CC Capture/Compare Event | |
:NVM | |
SAVEC | |
[ 0 TIM1_SR1 CC3IEF ]B! \ clear interrupt | |
redu @ out! \ second pattern | |
IRET | |
[ OVERT ( xt ) INT_TIM1CC ! | |
: duty! ( c -- ) \ set "relais hold" duty cycle | |
T1REL M* 100 UM/MOD TIM1_CCR3H 2C! DROP | |
; | |
\ Init TIM1 for update and CC interrupts | |
: init ( -- ) | |
outinit | |
0 full ! 0 redu ! | |
$60 TIM1_CCMR3 C! \ PWM mode 1 | |
T1REL TIM1_ARRH 2C! \ set rate: T1REL/(16MHz_HSI) | |
50 duty! \ 50% duty cycle | |
[ 1 TIM1_CR1 UIE ]B! \ enable timer | |
[ 1 TIM1_IER UIE ]B! \ enable timer update interupt | |
[ 1 TIM1_IER CC3IEF ]B! \ enable timer CC interupt | |
; | |
: stop ( -- ) | |
[ 0 TIM1_CR1 UIE ]B! \ disable timer | |
[ 0 TIM1_IER UIE ]B! \ disble timer update interupt | |
; | |
\ ' init 'BOOT ! | |
RAM | |
\\ | |
init | |
50 duty! | |
1 redu ! | |
1 full ! | |
0 redu \ reduced duty cycle (LED should be dim) | |
30 duty! \ duty cycle too low - relay turns into a buzzer | |
99 duty! \ relay gets full current |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment