key and emit block the calling thread. cdc-emit, cdc-key
begin Edit EVALUATE while SaveFile repeat
time!, time@, and .time (YYYY-MM-DDTHH:MM:SS ISO 8601).
led1! ( n -- ) sets LED1 (green)
led2! ( n -- ) sets LED2 (yellow)
led3! ( n -- ) sets LED3 (red)
led1@ ( -- n ) gets LED1 (green)
led2@ ( -- n ) gets LED2 (yellow)
led3@ ( -- n ) gets LED3 (red)
switch1? ( -- n ) gets switch1 (button A), closed=TRUE
dport! ( n -- ) sets the digital output port (D0=bit0 .. D15=bit15).
dport@ ( -- n ) gets the digital input/output port (D0=bit0 .. D15=bit15).
dpin! ( n a -- ) sets the digital output port pin a (D0=0 .. D15=15, A0=16 .. A6=23)
dpin@ ( a -- n ) gets the digital input/output port pin a
dmod ( u a -- ) sets the pin mode: 0 in, 1 in pull-up, 2 in pull-down, 3 out push pull, 4 out open drain,
5 out push pull PWM, 6 input capture, 7 output compare, 8 I2C, 9 USART, 10 analog
EXTImod ( u a -- ) Sets for pin a (D11, D12, D13) the EXTI mode u: 0 rising, 1 falling, 2 both edges, 3 none
EXTIwait ( u a -- ) Wait for EXTI interrupt on pin a (D11, D12, D13), timeout u in [ms]
pwmpin! ( u a -- ) sets the digital output port pin a (D5=5, D6=6, D9=9, D10=10, D14=14, and D15=15) to a PWM value u (0..1000).
Default frequency is 1 kHz, TIMER3/TIMER4
pwmprescale ( u -- ) Sets the PWM prescale for TIMER3/TIMER4. 42 kHz / prescale, default 42 -> PWM frequency 1 kHz
ICOCprescale ( u -- ) Sets the input capture / output compare prescale for TIMER2. default 42 -> 42 MHz / 42 = 1 MHz, timer resolution 1 us
ICOCperiod! ( u -- ) Sets the input capture / output compare (TIMER2) period. default $FFFFFFFF (4'294'967'295).
When the up counter reaches the period, the counter is set to 0.
For prescale 32 the maximum time is about 1 h 11 m
ICOCcount! ( -- u ) Sets the input capture / output compare counter for TIMER2
ICOCcount@ ( u -- ) Gets the input capture / output compare counter for TIMER2
ICOCstart ( -- ) Starts the ICOC period
ICOCstop ( -- ) Stops the ICOC period
OCmod ( u a -- ) Sets for pin a (D0, D1) the Output Compare mode u: 0 frozen, 1 active level on match,
2 inactive level on match, 3 toggle on match, 4 forced active, 5 forced inactive
OCstart ( u a -- ) Starts the output compare mode for pin a with pulse u
OCstop ( a -- ) Stops output compare for pin a
ICstart ( u -- ) Starts input capture u: 0 rising edge, 1 falling edge, 2 both edges
ICstop ( -- ) Stops input capture
waitperiod ( -- ) wait for the end of the TIMER2 period
OCwait ( a -- ) wait for the end of output capture on pin a
ICwait ( u -- u ) wait for the end of input capture with timeout u, returns counter u
apin@ ( a -- u ) gets the analog input port pin (A0=0 .. A6=6). Returns a 12 bit value (0..4095)
3 15 dmod \ set D15 (SCL) to Output 3 5 dmod \ set D5 to Output 3 6 dmod \ set D6 to Output 3 9 dmod \ set D9 to Output 3 10 dmod \ set D10 to Output 3 11 dmod \ set D11 to output 3 12 dmod \ set D12 to output 3 13 dmod \ set D13 to outputremap D15, D5, .. D13
create port-map 15 , 5 , 6 , 9 , 10 , 11 , 12 , 13 , : pin ( n -- n ) \ gets the Dx pin number cells port-map + @ ;
: left ( -- )
7 0 do
1 i pin dpin!
100 osDelay drop
0 i pin dpin!
loop
;
|
: right ( -- )
8 1 do
1 8 i - pin dpin!
100 osDelay drop
0 8 i - pin dpin!
loop
;
|
: knigthrider ( -- )
begin
left right
key?
until \ key pressed
0 0 dpin!
key drop \ eat key
;
|
3 16 dmod \ set A0 to Output 3 17 dmod \ set A1 to Output 3 18 dmod \ set A2 to Output 3 19 dmod \ set A3 to Output 3 20 dmod \ set A4 to Output 3 21 dmod \ set A5 to output 3 2 dmod \ set D2 (SCK) to output 3 4 dmod \ set D4 (MOSI) to outputremap D15, D5, .. D13
create port-map 16 , 17 , 18 , 19 , 20 , 21 , 2 , 4 ,
apin@ ( a -- u ) returns the ADC value (12 bit, 0 .. 4095) from one of the analog pins A0 to A5 (0 .. 5). Here I use the A0 to control the neopixel blue led brightness.
: neo-blue ( -- )
begin
0 apin@ 16 / neopixel!
10 osDelay drop
key? until
key drop
;
apin@ ( a -- u ) returns the ADC value (12 bit, 0 .. 4095) from one of the analog pins A0 to A5 (0 .. 5). Here I use the A0 to control the delay.
: left ( -- )
7 0 do
1 i pin dpin!
0 apin@ 10 / osDelay drop \ delay depends on A0
0 i pin dpin!
loop
;
|
: right ( -- )
8 1 do
1 8 i - pin dpin!
0 apin@ 10 / osDelay drop \ delay depends on A0
0 8 i - pin dpin!
loop
;
|
pwmprescale from 42 kHz (value 1) down to 0.5 Hz (64000).
5 6 dmod \ set D6 to PWM
: pwm ( -- )
begin
0 apin@ 4 / 6 pwmpin!
10 osDelay drop
key?
until
key drop
;
| 0° | 1 ms | 50 |
| 45° | 1.5 ms | 75 |
| 90° | 2 ms | 100 |
| 180° | 3 ms | 150 |
| 270 | 4 ms | 200 |
| 0° | 1 ms | 50 |
| 90° | 1.5 ms | 75 |
| 180° | 2 ms | 100 |
| 270° | 2.5 ms | 150 |
840 pwmprescale
5 5 dmod \ set D5 to PWM
: servo ( -- )
begin
130 40 do
i 5 pwmpin!
i neopixel!
i 40 = if
1000 \ give some more time to get back
else
200
then
osDelay drop
10 +loop
key? until
key drop
;
: period ( -- )
5000000 ICOCperiod! \ 5 s period
ICOCstart
begin
waitperiod
cr .time
key? until
key drop
;
7 0 dmod \ output compare for D0
7 1 dmod \ output compare for D1
: oc-toggle ( -- )
5000000 ICOCperiod! \ 5 s period
ICOCstart
3 0 OCmod 1000000 0 OCstart \ toggle D0 after 1 s
3 1 OCmod 2000000 1 OCstart \ toggle D1 after 2 s
begin
waitperiod
cr .time
key? until
key drop
;
When you abort (hit any key) the program, the timer still runs and controls the port pins. To stop the port pins:
0 OCstop 1 OCstopOr change the prescale to make it faster or slower:
1 ICOCprescale
: ic-test ( -- )
6 17 dmod \ input capture on A1
ICOCstart
2 ICstart \ both edges
ICOCcount@ ( -- count )
begin
2000 \ 2 s timeout
ICwait ( -- old-capture capture )
cr
dup 0= if
." timeout" drop
else
dup rot ( -- capture capture old-capture )
- 1000 / . ." ms"
then
key? until
key drop
drop
ICstop
;
: exti-test ( -- )
2 11 EXTImod \ both edges on D11
begin
2000 11 EXTIwait \ wait for edge on D11 with 2 s timeout
cr
0= if
11 dpin@ if
." rising edge"
else
." falling edge"
then
else
." timeout"
then
key? until
key drop
;
A0 D15, A1 D16, A2 D17, A3 D18, A4 D19, A5 D20, A6 D21
| Red | D6 | PA8 |
| Green | D4 | PC7 |
| Blue | D3? | PD7 (PG7?) |

| I | Attachment | History | Action | Size | Date | Who | Comment |
|---|---|---|---|---|---|---|---|
| |
portenta-h7.jpg | r1 | manage | 103.1 K | 2022-06-20 - 10:29 | PeterSchmid |