SOLVED: Converting DS1302 driver from P1 to P2 question
dgately
Posts: 1,639
Excuse my simplistic code conversion...
I'm sure there's a much simpler solution in Spin2, but I'm trying to start with a literal translation from P1 Spin. I'm trying to send out the command byte to the DS1302, one bit at a time, in the following excerpt:
dgately
I'm sure there's a much simpler solution in Spin2, but I'm trying to start with a literal translation from P1 Spin. I'm trying to send out the command byte to the DS1302, one bit at a time, in the following excerpt:
pub main()
setup()
wait_for_terminal(true)
write_a_byte(%10000101) '' $85 DS1302 read hours command
PRI write_a_byte( cmd ) | i
'' P1 dira & outa version
{{
dira[io]~~ ' set to output
repeat i from 0 to 7
outa[io] := cmd ' send the LSB bit to the IO pin ???
outa[clk]~~
cmd >>= 1
outa[clk]~
}}
'' P2 Spin2 methods
repeat i from 0 to 7
if (cmd & %00000001 == 1) ' REPLACE: outa[io] := cmd for P2 ???
pinhigh(io) ' the masked bit is 1
else
pinlow(io) ' the masked bit was 0
pinhigh(clk)
cmd >>= 1
pinlow(clk)
The Spin2 version does not yet work (timing issue or just bad coding?)dgately

Comments
pri wr_byte(cmd) repeat 8 pinw(io, cmd) pinh(clk) cmd >>= 1 pinl(clk)Since io is a single pin, the pinw() function will only write bit0 of cmd.Changed my code, still not getting good results, but at least I think I understand how that piece of code works. Now, on to the rest of the code.
Here's a code snippet with commented-out working P1 code (compiles with flexprop):
VAR byte datar ' a GLOBAL var PRI readByte|i '' REMOVE P1 dira & outa {{ datar~ repeat i from 0 to 7 if ina[io] == 1 datar |= |< i ' set bit outa[clk]~~ outa[clk]~ }} '' P2 REWRITE datar := 0 ' set datar to zero repeat i from 0 to 7 if (pinr(io) == 1) ' is the pin high? datar |= |< i ' yes, set the corresponding bit to 1 pinh(clk) ' strobe the clock pinl(clk)OK... that's the correct P2 replacement, but it still just returns with datar as 0. Is there any other difference from the P1 code?
One more try and it works:
PRI readByte|i '' REMOVE P1 dira & outa {{ datar~ repeat i from 0 to 7 if ina[io] == 1 datar |= |< i ' set bit outa[clk]~~ outa[clk]~ }} datar := 0 ' set datar to zero repeat i from 0 to 7 if (pinr(io) == 1) ' is the pin high? datar |= decod i ' yes, set the corresponding bit to 1 pinh(clk) ' strobe the clock pinl(clk)pri read_byte() : result | i pinf(io) ' set to input repeat i from 0 to 7 result.[i] := pinr(io) pinh(clk) pinl(clk)Thx