PDA

View Full Version : I2CWRITE to control a I2C device



bbubel
- 16th August 2006, 14:26
This is a good example on using I2CWRITE to control a I2C device. Feel free to use this code for your own use.

' The LTC6904 is a 1kHz - 68 MHz Serial Port Programmable Oscilator
' the Interface is a I2C serial bus
' The program sweeps the frequency from 3.7 to 4.2 MHz


define OSC 20 'Define the Osc to 20 MHz
define I2C_SCLOUT 1

'establish Variables
SDA Var PORTB.0
SCL Var PORTB.1
freq_count vaR WORD
lofreq var word
I2C_out Var Word
I2C-outlo Var I2C_out.BYTE0
I2C-outHi Var I2C_out.BYTE1
FREQ Var word
b0 var freq.byte0
b1 var freq.byte1
'---------------------------------------------------------------

TRISB = %00000000 ' Set PORTB to all output
PORTB = %00000000 ' Clear PORTB
freq = $0
freq_count = 0
lofreq = 0
'---------------------------------------------------------------
MAIN:
gosub SET_freq
gosub WR_freq
Pause 1000 'Demo purpose only
Goto Main
stop

'********* LTC6904 Subroutines **************************

'---------------------------------------------------------------------
WR_freq: 'Set the LO freq

I2C_out = $2E 'Set LTC6904 address to 2E Hex
I2Cwrite SDA, SCL, I2C-outlo, b1,[b0], I2C_err

return
I2C_err:
stop
'--------------------------------------------------------------------
SET_Freq:
freq_count = freq_count+10 'Increment 10.5KHz
lofreq = 860 + freq_count 'Offset 3.66 Mhz
lofreq = lofreq << 2 'Shift left 2 bits
Freq = lofreq | $B002 ' Bitwise "OR" with OCT (1011) and CNF(10)
if freq_count < 150 then exit '3.7 to 4.2 MHz
freq_count = 0
exit:
return
'************************************************* ***
'

end

bbubel
- 17th August 2006, 15:13
Variables I2C-outlo and I2C-outHi are incorrect they should be I2C_outlo and I2C_outHi.
When I was cleaning up the code for posting I did not notice this error. It was only when I compiled the code later that the error became apparent.