Created
July 17, 2020 15:20
-
-
Save andrewsclapp/1ec4ad0d58f16062781b304c8a1a2cd6 to your computer and use it in GitHub Desktop.
blinky code
This file contains hidden or 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
\res MCU: STM8S103 | |
\res export PA_ODR PA_DDR PA_CR1 | |
\res export PB_ODR PB_DDR PB_CR1 | |
\res export PC_ODR PC_DDR PC_CR1 | |
\res export PD_ODR PD_DDR PD_CR1 | |
\res export PE_ODR PE_DDR PE_CR1 | |
\res export PF_ODR PF_DDR PF_CR1 | |
#require lib/]B! | |
#require target/WIPE | |
\ pin 1 - AIN6 | |
\ pin 2 - VSS/GND | |
\ pin 3 - VCAP | |
\ pin 4 - VDD | |
\ pin 5 - PA3/OUT | |
\ pin 6 - PB4/NC | |
\ pin 7 - PC3/NC | |
\ pin 8 - SWIM | |
\ | |
\ 1) Read value of pot on AIN6 - not quite standard: | |
\ 10bits 10..1023 - at bottom of 10K is 100 ohm resistor | |
\ 2) Convert AIN to some number of about 0.5 to 50ish seconds delay time | |
\ 3) Make a random decision on/off | |
\ 4) Turn on or leave off randomly the pin for the determined delay | |
\ 5) repeat | |
\ Note: The PA2, PB0, PB1, PB2, PB3, PB6, PB7, | |
\ PC1, PC2, PC7, PD0, PD2, PD4, PD7, PE5 and PF4 | |
\ GPIOs should be configured after device reset in | |
\ output push-pull mode with output low-state to reduce | |
\ the device’s consumption and to improve its EMC immunity. | |
\ The GPIOsmentioned above are not connected to pins, | |
\ and they are in input-floating mode after adevice reset. | |
NVM | |
: PININIT ( -- ) | |
[ 1 PA_DDR 2 ]B! \ set up pin for OUPUT | |
[ 1 PA_CR1 2 ]B! \ set up pin for P-P | |
[ 0 PA_ODR 2 ]B! \ set output low | |
[ 1 PB_DDR 0 ]B! \ set up pin for OUPUT | |
[ 1 PB_CR1 0 ]B! \ set up pin for P-P | |
[ 0 PB_ODR 0 ]B! \ set up pin for OUPUT | |
[ 1 PB_DDR 1 ]B! \ set up pin for OUPUT | |
[ 1 PB_CR1 1 ]B! \ set up pin for P-P | |
[ 0 PB_ODR 1 ]B! \ set up pin for OUPUT | |
[ 1 PB_DDR 2 ]B! \ set up pin for OUPUT | |
[ 1 PB_CR1 2 ]B! \ set up pin for P-P | |
[ 0 PB_ODR 2 ]B! \ set up pin for OUPUT | |
[ 1 PB_DDR 3 ]B! \ set up pin for OUPUT | |
[ 1 PB_CR1 3 ]B! \ set up pin for P-P | |
[ 0 PB_ODR 3 ]B! \ set up pin for OUPUT | |
[ 1 PB_DDR 6 ]B! \ set up pin for OUPUT | |
[ 1 PB_CR1 6 ]B! \ set up pin for P-P | |
[ 0 PB_ODR 6 ]B! \ set up pin for OUPUT | |
[ 1 PB_DDR 7 ]B! \ set up pin for OUPUT | |
[ 1 PB_CR1 7 ]B! \ set up pin for P-P | |
[ 0 PB_ODR 7 ]B! \ set up pin for OUPUT | |
[ 1 PC_DDR 1 ]B! \ set up pin for OUPUT | |
[ 1 PC_CR1 1 ]B! \ set up pin for P-P | |
[ 0 PC_ODR 1 ]B! \ set up pin for OUPUT | |
[ 1 PC_DDR 2 ]B! \ set up pin for OUPUT | |
[ 1 PC_CR1 2 ]B! \ set up pin for P-P | |
[ 0 PC_ODR 2 ]B! \ set up pin for OUPUT | |
[ 1 PC_DDR 7 ]B! \ set up pin for OUPUT | |
[ 1 PC_CR1 7 ]B! \ set up pin for P-P | |
[ 0 PC_ODR 7 ]B! \ set up pin for OUPUT | |
[ 1 PD_DDR 0 ]B! \ set up pin for OUPUT | |
[ 1 PD_CR1 0 ]B! \ set up pin for P-P | |
[ 0 PD_ODR 0 ]B! \ set up pin for OUPUT | |
[ 1 PD_DDR 2 ]B! \ set up pin for OUPUT | |
[ 1 PD_CR1 2 ]B! \ set up pin for P-P | |
[ 0 PD_ODR 2 ]B! \ set up pin for OUPUT | |
[ 1 PD_DDR 4 ]B! \ set up pin for OUPUT | |
[ 1 PD_CR1 4 ]B! \ set up pin for P-P | |
[ 0 PD_ODR 4 ]B! \ set up pin for OUPUT | |
[ 1 PD_DDR 7 ]B! \ set up pin for OUPUT | |
[ 1 PD_CR1 7 ]B! \ set up pin for P-P | |
[ 0 PD_ODR 7 ]B! \ set up pin for OUPUT | |
[ 1 PE_DDR 5 ]B! \ set up pin for OUPUT | |
[ 1 PE_CR1 5 ]B! \ set up pin for P-P | |
[ 0 PE_ODR 5 ]B! \ set up pin for OUPUT | |
[ 1 PF_DDR 4 ]B! \ set up pin for OUPUT | |
[ 1 PF_CR1 4 ]B! \ set up pin for P-P | |
[ 0 PF_ODR 4 ]B! \ set up pin for OUPUT | |
[ 1 PA_DDR 3 ]B! \ set up pin for OUPUT - our output pin | |
[ 1 PA_CR1 3 ]B! \ set up pin for P-P | |
; | |
PININIT | |
RAM | |
WIPE | |
\res MCU: STM8S103 | |
\res export PA_ODR PA_DDR PA_CR1 | |
#require target/WIPE | |
NVM | |
\ Control LED, use: off is 0 OUT! on is 1 OUT! | |
: LED! ( b -- ) | |
PA_ODR 3 B! | |
; | |
: ADCREAD 6 ADC! ADC@ ; | |
\ get another fake random number from a seed | |
: RND ( s -- r ) | |
DUP $13 / XOR | |
$FFFF AND DUP | |
$17 * XOR | |
$FFFF AND | |
dup dup 5 / XOR | |
DROP | |
; | |
: NXTRND | |
TIM RND TIM RND * 100 MOD | |
; \ must seed RND, why not TIM? | |
\ : 1ms 222 0 do loop ; \ pretty close to 1ms | |
\ : blink 1 LED! 1ms 0 LED! 1ms ; \ for scope test | |
\ 2000 blinks | |
: 10ms 2158 0 do loop ; \ pretty close to 10ms | |
\ : blink 1 LED! 10ms 0 LED! 10ms ; \ for scope test | |
\ 1000 blinks | |
\ : 100ms 10 0 do 10ms loop ; \ close to .1s | |
\ : blink 1 LED! 100ms 0 LED! 100ms ; \ for scope test | |
\ 20 blinks | |
\ : 1s 100 0 do 10ms loop ; \ close to 1s | |
\ : blink 1 LED! 1s 0 LED! 1s ; \ for scope test | |
\ 10 blinks | |
\ adjustable random blink | |
: adjblink | |
NXTRND 50 < IF | |
1 LED! | |
THEN | |
ADCREAD 5 * | |
0 do 10ms loop | |
0 LED! | |
10 0 do 10ms loop | |
; | |
: INIT ( -- ) | |
TIM RND \ do the seedful | |
[ ' adjblink ] LITERAL BG ! | |
hi | |
; | |
' INIT 'BOOT ! | |
RAM | |
WIPE | |
#require PERSIST | |
PERSIST | |
RESET | |
\ COLD |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment