Board Support Package

Defaults: Digital port pins D0 to D7 are push pull outputs, D8 to D15 are inputs with pull-up resistors.

led1!    ( n -- )      sets LED1 (blue)
led2!    ( n -- )      sets LED2 (green)
led3!    ( n -- )      sets LED3 (red)
led1@    ( -- n )      gets LED1 (blue)
led2@    ( -- n )      gets LED2 (green)
led3@    ( -- n )      gets LED3 (red)

switch1? ( -- n )      gets switch1, closed=TRUE
switch2? ( -- n )      gets switch2, closed=TRUE
switch3? ( -- n )      gets switch3, 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 (D0=0 .. D15=15)
dpin@    ( a -- n )    gets the digital input/output port pin 
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

pwmpin!  ( u a -- )    sets the digital output port pin (D3=3, D5=5, D6=6, D9=9, D10=10, D11=11) to a PWM value (0..1000). Frequency is 10 kHz 

apin@    ( a -- u )    gets the analog input port pin (A0 .. A5). Returns a 12 bit value (0..4095) 

Using the Digital Port

This example is very similar to the McForth#Knight_Rider program. dport! and dport@ set and get all 16 digital pins (D0 to D15) at once. You have to press the SW1 push button til D0 is set to cancel the operation.

: left ( -- ) 
  7 0 do  
    dport@ shl dport!  
    100 osDelay drop  
  loop 
;
: right ( -- )
  7 0 do  
    dport@ shr dport!
    100 osDelay drop  
  loop 
;
: knigthrider ( -- )
  1 dport! 
  begin 
    left right 
    switch1? \ or key?
  until 
  0 dport!
;

Single port pin variant (no side effects on port pins D8 to D15):

: left ( -- ) 
  7 0 do
    1 i dpin! 
    100 osDelay drop  
    0 i dpin!
  loop 
;
: right ( -- )
  8 1 do  
    1 8 i - dpin! 
    100 osDelay drop  
    0 8 i - dpin!
  loop 
;
: knigthrider ( -- )
  begin 
    left right 
    switch1? 
  until 
  0 0 dpin!
;

Using the ADC (analog pin)

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 dpin! 
    0 apin@ 10 / osDelay drop  \ delay depends on A0
    0 i dpin!
  loop 
;

: right ( -- )
  8 1 do  
    1 8 i - dpin! 
    0 apin@ 10 / osDelay drop  \ delay depends on A0
    0 8 i - dpin!
  loop 
;

The left or right word takes about 125 us, the knightrider loop about 50 us.

CH1 yellow: D0 pin
CH2 blue: D1 pin
TEK0012.png

-- Peter Schmid - 2020-04-11

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng TEK0012.png r1 manage 3.6 K 2020-04-16 - 14:22 PeterSchmid  
Edit | Attach | Watch | Print version | History: r7 < r6 < r5 < r4 < r3 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r7 - 2020-05-23 - PeterSchmid
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback