|
META TOPICPARENT |
name="MecrispCubeH74x" |
%DASHBOARD{ section="banner" |
|
pwmpin! ( u a -- ) sets the digital output port pin a (D3=3, D4=4, D5=5, D6=6, D9=9, and D10=10) 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 |
> > | pwmprescale ( u -- ) Sets the PWM prescale for TIMER3/TIMER4. 60 kHz / prescale, default 60 -> 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 |
> > | ICOCprescale ( u -- ) Sets the input capture / output compare prescale for TIMER2. default 60 -> 60 MHz / 60 = 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 |
|
Control the Neopixel |
|
< < | 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. |
> > | apin@ ( a -- u ) returns the ADC value (16 bit, 0 .. 65535) 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! |
> > | 0 apin@ 256 / neopixel! |
| 10 osDelay drop
key? until
key drop |
|
Control the Knightrider Pace |
|
< < | 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. |
> > | apin@ ( a -- u ) returns the ADC value (12 bit, 0 .. 65535) 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 apin@ 160 / 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 apin@ 160 / osDelay drop \ delay depends on A0 |
| 0 8 i - pin dpin!
loop
; |
| Using the PWM (Analog Output Pins)
Six port pins are supported so far. |
|
< < | The 16 bit timers TIM3 (D5 and D6) and TIM4 (D9, D10, D14, and D15) are used for the timebase, time resolution is 1 us (42 MHz SysClk divided by 42). The PWM scale is from 0 (0 % duty cycle) to 1000 (100 % duty cycle), this results in a PWM frequency of 1 kHz. If you need higher PWM frequencies, decrease the divider and/or the scale. |
> > | The 16 bit timers TIM1 (D3, D4, D5 and D6) and TIM4 (D9, D10) are used for the timebase, time resolution is 1 us (60 MHz SysClk divided by 60). The PWM scale is from 0 (0 % duty cycle) to 1000 (100 % duty cycle), this results in a PWM frequency of 1 kHz. If you need higher PWM frequencies, decrease the divider and/or the scale. |
| |
|
< < | PWM port pins: D5 (TIM3CH2), D6 (TIM3CH1), D9 (TIM4CH3), D10 (TIM4CH4), D14 (TIM4CH2), and D15 (TIM4CH1). |
> > | PWM port pins: D3 (TIM1CH2), D4 (TIM1CH1), D5 (TIM1CH3), D6 (TIM1CH4), D9 (TIM4CH2), D10 (TIM4CH1). |
| |
|
< < | Simple test program to set brightness of a LED on pin D6 with a potentiometer on A0. Default PWM frequency is 1 kHz (prescaler set to 42). You can set the prescale with the word pwmprescale from 42 kHz (value 1) down to 0.5 Hz (64000). |
> > | Simple test program to set brightness of a LED on pin D6 with a potentiometer on A0. Default PWM frequency is 1 kHz (prescaler set to 60). You can set the prescale with the word pwmprescale from 60 kHz (value 1) down to 0.5 Hz (64000). |
|
5 6 dmod \ set D6 to PWM
|
|
A servo pulse of 1.5 ms width will typically set the servo to its "neutral" position (typically half of the specified full range), a pulse of 1.0 ms will set it to 0°, and a pulse of 2.0 ms to 90° (for a 90° servo). The physical limits and timings of the servo hardware varies between brands and models, but a general servo's full angular motion will travel somewhere in the range of 90° – 180° and the neutral position (45° or 90°) is almost always at 1.5 ms. This is the "standard pulse servo mode" used by all hobby analog servos. |
|
< < | The BSPs default PWM frequency is 1 kHz, 50 Hz is 20 times slower. The divider is therefore 42 * 20 = 840. |
> > | The BSPs default PWM frequency is 1 kHz, 50 Hz is 20 times slower. The divider is therefore 60 * 20 = 1200. |
|
0° |
1 ms |
50 |
45° |
1.5 ms |
75 |
90° |
2 ms |
100 |
|
|
|
|
< < | 840 pwmprescale |
> > | 1200 pwmprescale |
| 5 5 dmod \ set D5 to PWM
: servo ( -- ) |
| |
|
< < | D11, D12, and D13 can be used as an EXTI line. EXTIs are external interrupt lines, D13 uses EXTI1 (EXTI Line1 interrupt), D12 EXIT2, and D11 EXTI3. |
> > | D0, D11, and D12 can be used as an EXTI line. EXTIs are external interrupt lines, D0 uses EXTI7 (EXTI Line7 interrupt), D11 EXIT5, and D12 EXTI6. |
|
: exti-test ( -- )
|