zkt_PiratesDen
New Member
This wouldnt fit is a PM so posting it here.
Code:
prograu I2C
' pic 16F87
'SENDS START CONDITION,3 BYTES, THEN STOP CONDITION ON I2C BuS TO SLAVE.
' PORTB.0 IS SCL LINE. PORTA.7 IS SDA LINE. DELAYS ARE FOR BETTER SCOPE DISPLAY
'10k pullup resistors required on sda and scl pins
dim tmp,bit,bite as byte
' not implemented yet
'const tda_adr as byte=$88
'const sub_adr as byte[9]=($00,$01,$02,$02,$02,$02,$02,$02,$02)
'const data as byte[9]=($00,$00,$01,$03,$05,$07,$09,$0b,$0D)
uain:
osccon=%01100010 'cpu clock(Fosc)=4 uhz
while iofs=1 ' wait for clock to stabilize
wend
cmcon=7 ' turn off adc
portb.0=0 ' portb.0 scl
porta.7=0 ' porta.7 sda
' trisb=0 => portb is output. since portb=0 portb.7=logic 0
' trisb=128=> portb.7 is input. with nothing to pull the voltage low
' Rpu will will pull the voltage high and portb.7=high
test_loop:
I2C_Start:
trisb=1
trisa=128
delay_us(50)
trisa=0
delay_us(50)
trisb=0
for bite=1 to 3 ' send 3 bytes on I2C bus
if bite=1 then tmp=%10001000'tda chip address
end if
if bite=2 then tmp=%00000000 'tda sub_adr=#00000000=input selector
end if
if bite=3 then tmp=%00000010 ' tda_data=#00000000=select input#4
end if
for bit= 0 to 7
trisb=0
delay_us(50)
if tmp 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
tmp=tmp << 1 ' shift current tda_data bit to uSB of tup
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(50)
trisb=1
delay_us(50)
trisb=0
next bite
delay_ms(900)
for bit= 0 to 7 ' send another data byte to select input#4
trisb=0
delay_us(50)
if tmp and 128=128 then
trisa=128
else trisa=0
end if
tmp=tmp << 1
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(50)
trisb=1
delay_us(50)
trisb=0
delay_ms(999)
i2c_stop:
delay_us(40)
trisb=1
trisa=0
delay_us(20)
trisa=1
delay_us(20)
goto test_loop ' scope shows on- off of audio input- it lives !
end.