led1! ( n -- ) sets LED1 (red) led1@ ( -- n ) gets LED1 (red) neopixel! ( rgb -- ) sets the neopixel RGB led ($ff0000 red, $00ff00 green, $0000ff blue) neopixel@ ( -- rgb ) gets the neopixel RGB led ($ff0000 red, $00ff00 green, $0000ff blue) switch1? ( -- flag ) gets switch1 (button A), closed=TRUE switch2? ( -- flag ) gets switch2 (button B), closed=TRUE switch3? ( -- flag ) gets switch3 (button C), closed=TRUE switchuser? ( -- flag ) gets user button, 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=22) 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 (D5, D6, D11, D13) the EXTI mode u: 0 rising, 1 falling, 2 both edges, 3 none EXTIwait ( u a -- ) Wait for EXTI interrupt on pin a (D5, D6, D11, D13), timeout u in [ms] pwmpin! ( u a -- ) sets the digital output port pin a (D0=0, D1=1, A4=20) to a PWM value u (0..1000). Default frequency is 1 kHz, TIMER1 pwmprescale ( u -- ) Sets the PWM prescale for TIMER1. 32 kHz / prescale, default 32 -> PWM frequency 1 kHz ICOCprescale ( u -- ) Sets the input capture / output compare prescale for TIMER2. default 32 -> 32 MHz / 32 = 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 (D5, D6, D13) 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 for pin A5: 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 .. A5=5). 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 32 kHz (value 1) down to 0.5 Hz (64000).
5 0 dmod \ set D0 to PWM : pwm ( -- ) begin 0 apin@ 4 / 0 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 |
640 pwmprescale 5 0 dmod \ set D0 to PWM : servo ( -- ) begin 130 40 do i 0 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 ;
640 pwmprescale 5 3 dmod \ set D3 to PWM : slowservo ( -- ) begin 100 50 do i 3 pwmpin! 50 osDelay drop 1 +loop 50 100 do i 3 pwmpin! 50 osDelay drop -1 +loop key? until key drop ;
: period ( -- ) 5000000 ICOCperiod! \ 5 s period ICOCstart begin waitperiod cr .time key? until key drop ;
7 5 dmod \ output compare for D5 7 6 dmod \ output compare for D6 7 13 dmod \ output compate for D13 : oc-toggle ( -- ) 5000000 ICOCperiod! \ 5 s period ICOCstart 3 5 OCmod 1000000 5 OCstart \ toggle D5 after 1 s 3 6 OCmod 2000000 5 OCstart \ toggle D6 after 2 s 3 13 OCmod 3000000 13 OCstart \ toggle D13 after 3 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:
5 OCstop 5 OCstop 13 OCstopOr change the prescale to make it faster or slower:
1 ICOCprescale
: ic-test ( -- ) 6 21 dmod \ input capture on A5 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 5 EXTImod \ both edges on D5 begin 2000 5 EXTIwait \ wait for edge on D5 (button C) with 2 s timeout cr 0= if 5 dpin@ if ." rising edge" else ." falling edge" then else ." timeout" then key? until key drop ;
SCL D15, SDA D14 SCK D2, MO D4, MI D3, RX D0, TX D1The anlog pins can be used as digital pins too:
A0 D16, A1 D17, A2 D18, A3 D19, A4 D20, A5 D21
Feather Left | Feather Connector | WB Feather | WB Connector | WB Port |
---|---|---|---|---|
Reset | JP1.16 | \RESET | J1.16 | |
3.3 V | JP1.15 | 3V3 | J1.15 | |
Aref or Mode | JP1.14 | \MODE MCU_USER | J1.14 | PH3 |
GND | JP1.13 | GND | J1.13 | |
A0 (D16) | JP1.12 | A0 | J1.12 | PA4 |
A1 (D17) | JP1.11 | A1 | J1.11 | PA5 |
A2 (D18) | JP1.10 | A2 | J1.10 | PA6 |
A3 (D19) | JP1.9 | A3 | J1.9 | PA7 |
A4 or D24 (D20) | JP1.8 | A4 | J1.8 | PA8 |
A5 or D25 (D21) | JP1.7 | A5 | J1.7 | PA0 |
SCK or D2 | JP1.6 | SCK | J1.6 | PB3 |
MOSI or D4 | JP1.5 | MOSI | J1.5 | PB5 |
MISO or D3 | JP1.4 | MISO | J1.4 | PB4 |
RX or D0 | JP1.3 | RX | J1.3 | PA10 |
TX or D1 | JP1.2 | TX | J1.2 | PA9 |
* | JP1.1 | NC | J1.1 |
Feather Right | Feather Connector | WB Feather | WB Connector | WB Port |
---|---|---|---|---|
Bat | JP3.1 | LIPO+ | J2.12 | |
En | JP3.2 | EN (3.3 V) | J2.11 | |
USB | JP3.3 | VBUS | J2.10 | |
D13 red LED | JP3.4 | D8 | J2.9 | PA1 |
D12 | JP3.5 | D7 red LED | J2.8 | PB2 |
D11 | JP3.6 | D6 | J2.7 | PB8 |
D10 | JP3.7 | D5 | J2.6 | PB9 |
D9 | JP3.8 | D4 | J2.5 | PB1 |
D6 | JP3.9 | D3 | J2.4 | PA3 |
D5 | JP3.10 | D2 | J2.3 | PA2 |
SCL or D15 | JP3.11 | SCL | J2.2 | PB6 |
SDA or D14 | JP3.12 | SDA | J2.1 | PB7 |
I | Attachment | History | Action | Size | Date | Who | Comment |
---|---|---|---|---|---|---|---|
jpg | WB_Feather_header.jpg | r1 | manage | 83.5 K | 2021-06-25 - 08:41 | PeterSchmid | |
jpg | WB_Feather_oled_sdcard.jpg | r1 | manage | 382.8 K | 2021-06-25 - 08:41 | PeterSchmid |