craiglindley
Well-known member
Just wondering
\ enable twi
: +twi ( prescaler bitrate -- )
TWBR c!
03 and TWSR c!
;
\ some random initialization. Works fine with 8 MHz
: twi.default
7f 3 +twi ;
\ turn off twi
: -twi ( -- )
0 TWCR c!
;
\ wait for twi finish
: twi.wait ( -- )
begin
TWCR c@ 80 and
until
;
\ send start condition
: twi.start ( -- )
[ 1 7 lshift
1 5 lshift or
1 2 lshift or ] literal
TWCR c!
twi.wait
;
\ A named port pin puts a bitmask on stack, wherin the set bit indicates which
\ bit of the port register corresponds to the pin.
\ And then puts the address of its port on stack too.
\ Use it this way:
\ PORTD 7 portpin: PD.7 ( define portD pin #7)
\ PD.7 high ( turn portD pin #7 on, i.e. set it high-level)
\ PD.7 low ( turn portD pin #7 off, i.e. set it low-level)
\ PD.7 <ms> pulse ( turn portD pin #7 for <ms> high and low)
\ the following words are for "real" IO pins only
\ PD.7 pin_output ( set DDRD so that portD pin #7 is output)
\ PD.7 pin_input ( set DDRD so that portD pin #7 is input)
\ PD.7 pin_high? ( true if pinD pin #7 is high)
\ PD.7 pin_low? ( true if pinD pin #7 is low)
\
\ multi bit operation
\ PORTD F portpin PD.F ( define the lower nibble of port d )
\ PD.F pin@ ( get the lower nibble bits )
\ 5 PD.F pin! ( put the lower nibble bits, do not change the others )
hex
\ At compiletime:
\ Store combination of portaddress and bit number in a cell and give it a name.
\ At runtime:
\ Get pinmask and portaddress on stack.
: portpin: create ( C: "ccc" portadr n -- ) ( R: -- pinmask portadr )
1 swap lshift
8 lshift or , \ packed value
does> i@ \ get packed value
dup 8 rshift swap ff and \
;