Thanks mackrackit and misamilanovic for the replies. I did not succeed in using I2CWRITE but was successful using SHIFTIN/SHIFOUT.
Check out my blog post HERE for more details.
But here is the PICBASIC code for a 16F88 that counts from 0-9 on all four digits.
Code:
'// for 4MHZ
DEFINE OSC 4
OSCCON=%01101000
'// for 8MHZ
'DEFINE OSC 8
'OSCCON=%01111000
While OSCCON.2=0:Wend
CMCON = 7 '// PORTA = digital I/O
'OPTION_REG.7 = 0 '// Enable PORTB pull-ups
ADCON1 = 7
ANSEL=%00000000 '// set all analog pins to digital
ANSEL = 0 '// disable ADCs
TRISB.3 = 0
TRISB.2 = 0
'----------------------------------------------------------------------------
led var PORTA.1
SDA VAR PORTB.3
SCL VAR PORTB.2
i2c_dev var byte 'I2C device Address
i2c_out VAR BYTE 'data to sent over I2C bus
i2c_ack VAR BIT 'acknowledgement bit
i var byte
segment VAR byte
seg_val var byte
'----------------------------------------------------------------------------
i2c_dev = %01110000 '(112 = %01110000 = $70)
gosub startup
GOSUB init_display 'initialize the display module
main:
GOSUB COUNTER
goto main
END
counter:
for i = 0 to 9
lookup i, [189,9,117,109,201,236,248,13,253,205],seg_val
for segment = 1 to 4
GOSUB I2C_START
if segment = 1 then
i2c_out = %01110000 : gosub i2C_tx : i2c_out = %00000001 : gosub i2C_tx
i2c_out = seg_val : gosub i2c_tx
gosub i2c_stop
endif
if segment = 2 then
i2c_out = %01110000 : gosub i2C_tx : i2c_out = %00000010 : gosub i2C_tx
i2c_out = seg_val : gosub i2c_tx
gosub i2c_stop
endif
if segment = 3 then
i2c_out = %01110000 : gosub i2C_tx : i2c_out = %00000011 : gosub i2C_tx
i2c_out = seg_val : gosub i2c_tx
gosub i2c_stop
endif
if segment = 4 then
i2c_out = %01110000 : gosub i2C_tx : i2c_out = %00000100 : gosub i2C_tx
i2c_out = seg_val : gosub i2c_tx
gosub i2c_stop
endif
next segment
pause 100
next i
return
bit_cycle:
for i = 0 to 255
gosub i2c_start
i2c_out = %01110000 : gosub i2C_tx : i2c_out = %00000001 : gosub i2C_tx
i2c_out = i
gosub i2C_tx
gosub i2c_stop
pause 20
next i
return
init_display: 'initialize the SAA1064 module and flash all segments
GOSUB I2C_START 'Start the I2C communication
i2c_out = %01110000 : GOSUB I2C_TX 'send device byte
i2c_out = %00000000 : GOSUB I2C_TX 'send instruction byte
i2c_out = %00011111 : GOSUB I2C_TX 'send control byte
GOSUB I2C_STOP 'Stop i2C transmission
pause 1000 ' leave segments on for 1 sec then turn off
GOSUB I2C_START
i2c_out = %01110000 : gosub i2C_tx
i2c_out = %00000000 : gosub i2C_tx
i2c_out = %00000001 : GOSUB I2C_TX
GOSUB I2C_STOP
pause 500
GOSUB I2C_START
i2c_out = %01110000 : gosub i2C_tx
i2c_out = %00000000 : gosub i2C_tx
i2c_out = %00010111 : GOSUB I2C_TX
GOSUB I2C_STOP
RETURN
I2C_START:
HIGH SDA
HIGH SCL
LOW SDA
RETURN
I2C_STOP: 'I2C stop (terminate communication on I2C bus)
HIGH SCL
HIGH SDA
PAUSE 1
RETURN
I2C_TX: 'I2C transmit -> send data to the slave
SHIFTOUT SDA,SCL,1,[i2c_out] 'Shift out “i2c_out” MSBfirst
SHIFTIN SDA,SCL,0,[i2c_ack\1] 'Receive ACK bit
RETURN
'// start-up led sequence
startup:
for i = 0 to 4
high led
pause 50
low led
pause 30
next i
pause 300
Return
Bookmarks