zkt
Newbie level 4
The following code runs fine when the value of bite is set inside the sub procedure
I2C_send_byte. But when set prior to calling the sub, sda does not change when the code runs in the sub. And scl changes properly inside and outside the sub. Of the two ways of writing the IF line neither one works. I am stuck.
Any help appreciated.
I2C_send_byte. But when set prior to calling the sub, sda does not change when the code runs in the sub. And scl changes properly inside and outside the sub. Of the two ways of writing the IF line neither one works. I am stuck.
Any help appreciated.
Code:
program Sunday
' pic 16F87
dim bite,bit,tmp as byte
'dim const tda_adr as byte=%10001000
sub procedure I2C_send_byte
'bite=%10101010 excluding this line causes sda to not change
for bit= 0 to 7
trisb=0
delay_us(50)
'If ((bite AND 128)=128) Then
if bite and 128=128 then
trisa=128 'set tda high if current tda_data bit is high
else trisa=0 ' else set tda low
end if
bite=bite<< 1 ' shift current tda_data bit to MSB of byte being sent
delay_us(50)
trisb=1
delay_us(100)
next bit
trisb=0
delay_us(50)
ack:
trisa=128
delay_us(50)
trisb=1
delay_us(100)
trisb=0
delay_us(100)
end sub
main:
osccon=%01100010
while iofs=1
wend ' porta.7 sda
cmcon=7
portb.0=0 ' portb.0 scl
porta.7=0
gosub I2C_start
bite=%10101010
I2C_send_byte
gosub I2C_stop
I2C_Start:
trisb=1 ' trisb=0 => portb is output. since portb=0 portb.7= logic 0
trisa=128
delay_us(50)
trisa=0 ' trisb=128=> portb.7 is input. with nothing to pull the voltage low
delay_us(50) ' Rpu will will pull the voltage high and portb.7= high
trisb=0
return
i2c_stop:
delay_us(50)
trisb=1
trisa=0
delay_us(50)
trisa=128
delay_us(50)
return
end.