Skip to content

Commit

Permalink
basic UI with EEPROM store and PWM output
Browse files Browse the repository at this point in the history
  • Loading branch information
TG9541 committed May 11, 2020
1 parent efe86b5 commit 3fa5c62
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 56 deletions.
56 changes: 56 additions & 0 deletions TIM2PWM
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#require WIPE

\res MCU: STM8S103
\res export TIM2_PSCR TIM2_CCMR1 TIM2_CCMR2 TIM2_CCMR3
\res export TIM2_CCER1 TIM2_CCER2 TIM2_CR1 TIM2_ARRH
\res export TIM2_CCR1H TIM2_CCR2H TIM2_CCR3H

NVM

VARIABLE FREQ \ PWM frequency * 1
VARIABLE DECA \ PWM frequency: factor log10
VARIABLE DUTY \ PWM duty cycle

\
: T2Pre ( n -- )
( n ) TIM2_PSCR C! \ TIMx prescaler (2^n)
;

\ Set Timer2 reload value
: T2Reload ( n -- )
TIM2_ARRH 2C!
;

\ Set PWM2 compare value
: PWM2 ( n -- )
TIM2_CCR2H 2C!
;

HERE
8 C, 0 , 62500 , \ 10^0
5 C, 7 , 41248 , \ 10^1
2 C, 61 , 2305 , \ 10^2
0 C, 24 , 27136 , \ 10^3
0 C, 2 , 28928 , \ 10^4
0 C, 0 , 16000 , \ 10^5
( here ) CONSTANT CTIM2

: CALCPWM ( -- )
DECA @ 0= IF
FREQ @ DUP 10 < IF 0 ELSE 10 100 WITHIN IF 1 ELSE 2 THEN THEN
ELSE
DECA @ 2+
THEN
5 * CTIM2 + DUP C@ T2pre 1+ 2@ FREQ @ UM/MOD NIP DUP T2reload
DUTY @ 100 */ PWM2
;

\ Init Timer2 with prescaler CC PWM2
: TIM2PWM ( -- )
$60 TIM2_CCMR2 C! \ TIMx OC2M = PWM mode 1
$10 TIM2_CCER1 C! \ TIMx CC2 enable
1 TIM2_CR1 C!
CALCPWM
;

WIPE RAM
78 changes: 22 additions & 56 deletions XY-LPWM/board.fs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
\ STM8S103 Timer2 PWM
\ refer to github.com/TG9541/stm8ef/blob/master/LICENSE.md

#include STARTTEMP

\res MCU: STM8S103
\res export TIM2_PSCR TIM2_CCMR1 TIM2_CCMR2 TIM2_CCMR3
\res export TIM2_CCER1 TIM2_CCER2 TIM2_CR1 TIM2_ARRH
\res export TIM2_CCR1H TIM2_CCR2H TIM2_CCR3H
#require TIM2PWM

#require WIPE
#require LED7FIRST
#require ]B!

Expand All @@ -16,75 +12,44 @@ $4010 CONSTANT EE_FREQ
$4012 CONSTANT EE_DECA
$4014 CONSTANT EE_DUTY

: TARGET NVM ;

TARGET

VARIABLE AUPDA
VARIABLE UPDA
VARIABLE INCR
VARIABLE DECA
VARIABLE FREQ
VARIABLE DUTY

\
: T2Pre ( n -- )
( n ) TIM2_PSCR C! \ TIMx prescaler (2^n)
;

\ Set Timer2 reload value
: T2Reload ( n -- )
TIM2_ARRH 2C!
;

\ Set PWM2 compare value
: PWM2 ( n -- )
TIM2_CCR2H 2C!
;

\ convert duty cycle [1/1000] to PWM reload value
: setduty ( n -- n )
TIM2_ARRH 2C@ 1000 */
;


\ Init Timer2 with prescaler CC PWM2
: T2init ( n -- )
( n ) T2Pre
$60 TIM2_CCMR2 C! \ TIMx OC2M = PWM mode 1
$10 TIM2_CCER1 C! \ TIMx CC2 enable
1 TIM2_CR1 C!
;
NVM
VARIABLE STOR \ neg. number starts EEPROM store sequence
VARIABLE UPDA \ resquest LCD update
VARIABLE INCR \ progressive increment/decrement

: UI ( -- )
\ key release resets the progressive key-hold increment
BKEY 0= IF
1 INCR !
THEN

UPDA @ 0< IF
1 UPDA +!
UPDA @ IF
\ show symbol SET while/after keys are pressed, store values to EEPROM
STOR @ 0< IF
1 STOR +!
STOR @ IF
[ 1 LCDSYM 1+ 5 ]B!
ELSE
ULOCK
FREQ @ EE_FREQ !
DECA @ EE_DECA !
DUTY @ EE_DUTY !
LOCK
[ 0 LCDSYM 1+ 5 ]B!
1 UPDA !
THEN
THEN

\ STM8 eForth board keys with auto-repeat use ASCII codes
0 UPDA @ < IF CR 0 DUP UPDA ! 0 1 ELSE ?KEY THEN IF
\ UPDA request LCD refresh, ?KEY produces ASCII codes with auto-repeat
UPDA @ IF CR 0 DUP UPDA ! 0 1 ELSE ?KEY THEN IF
INCR @ ( c inc )
OVER 72 ( "H" ) = IF DUP FREQ @ + 1000 MIN FREQ ! NIP 237 SWAP THEN
OVER 68 ( "D" ) = IF FREQ @ OVER - 0 MAX FREQ ! NIP 237 SWAP THEN
OVER 66 ( "B" ) = IF DUP DUTY @ + 99 MIN DUTY ! NIP 17 SWAP THEN
OVER 65 ( "A" ) = IF DUTY @ OVER - 1 MAX DUTY ! NIP 17 SWAP THEN

OVER DUP 65 73 WITHIN NOT AND IF
-100 UPDA !

\ detect a keypress (c=0 signals LCD update)
OVER ( c/lim ) IF
-100 STOR !
THEN

( c/lim inc) 3 + MIN INCR !
Expand Down Expand Up @@ -117,7 +82,7 @@ VARIABLE DUTY
THEN

( deca ) DROP DUTY ? CR

CALCPWM
THEN
;

Expand All @@ -132,11 +97,12 @@ VARIABLE DUTY
4 LCDSYM !
1 UPDA !
[ ' UI ] LITERAL BG !
2 T2init
TIM2PWM
hi
;

' init 'BOOT !
ENDTEMP
WIPE RAM

\\ Example:

Expand Down

0 comments on commit 3fa5c62

Please sign in to comment.