Hi again everyone, I've been trying to interface my PIC16F690 with a DS1621 Digital Thermometer. I haven't been able to find many resources and the code I have found within the forum I could not get to work. The code which I have tried to adapt now is for a DS1624 and the commands seem identical to me but yet it returns "0.00". my serial communication is working 100% and I have gone over my wiring a lot. Open to any suggestions/changes. I have got A0, A1, A2 all grounded which gives the device an address of %10010000. Aswell as 4.7k pick ups on the sda scl lines. I thought it possibly could be because I am using Port A.0 and A.1? Not sure.
Code:
DEFINE OSC 4
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 20h
DEFINE HSER_SPBRG 25
DEFINE HSER_CLROERR 1
SDA VAR PORTA.0
SCL VAR PORTA.1
i2c_read CON 1 'R/W configuration bit (1 = read)
i2c_write CON 0 'R/W configuration bit (0 = write)
i2c_out VAR BYTE 'data to sent over I2C bus
i2c_in VAR BYTE[2] 'data received over I2C bus
i2c_ack VAR BIT 'acknowledgement bit
temp VAR WORD
Hserout ["Starting"]
GOSUB Config_Register 'Set Configuration
GOSUB Start_Convert 'Start continuous conversion
TOP:
PAUSE 2000
GOSUB Read_Temp 'Read the current temperature
i2c_in[2] = i2c_in[1] >> 3 'Shift 5 decimal bits to LS position
temp = (i2c_in[1]*1000) 'PIC Doesn’t like decimals, but there is a way to work around this
HSEROUT [DEC i2c_in[0],".",DEC2 (temp ** 2048)/100,13,10]'Outputs temperature to termina
GOTO top 'Loops forever
Config_Register: 'Set continuous conversion
GOSUB I2C_START 'Start Condition
i2c_out = %10010000 'Send Address, (device= %1001, A0= 0, A1= 0, A2= 0, R/W= 0)
GOSUB I2C_TX 'Send data in “i2c_out”
i2c_out = $AC 'Send “Access Configuration” command
GOSUB I2C_TX 'Send data in “i2c_out”
i2c_out = $00 'Send “Continuous Conversion” command
GOSUB I2C_TX 'Send data in “i2c_out”
GOSUB I2C_STOP 'Stop Condition
RETURN
Start_Convert: 'Start Conversion
GOSUB I2C_START
i2c_out = %10010000 'Send Address, (device= %1001, A0= 0, A1= 0, A2= 0, R/W= 0)
GOSUB I2C_TX
i2c_out = $EE 'Send “Start to Convert” command
GOSUB I2C_TX
GOSUB I2C_STOP
RETURN
Read_Temp: 'Read temperature
GOSUB I2C_START
i2c_out = %10010000 'You must “write” the command to read the temperature before
GOSUB I2C_TX 'reading it, therefore R/W still is 0
i2c_out = $AA 'Send “Read Temperature” command
GOSUB I2C_TX
GOSUB I2C_START '* Reissue Start Condition *
i2c_out = %10010001 'Send Address, (device= %1001, A0= 0, A1= 0, A2= 0, R/W= *1*)
GOSUB I2C_TX 'Transmit address with R/W bit as 1 (“read”)
GOSUB I2C_RX 'Start getting data coming in
GOSUB I2C_STOP 'Issue stop condition
RETURN
I2C_START: 'I2C start (start communication on I2C bus)
HIGH SDA
HIGH SCL
LOW SDA
LOW SCL
RETURN
I2C_STOP: 'I2C stop (terminate communication on I2C bus)
LOW SDA
HIGH SCL
HIGH SDA
PAUSE 1
RETURN
I2C_RX: 'I2C receive -> receive data from slave
SHIFTIN SDA,SCL,0,[i2c_in[0]] 'Shift in first byte MSBpre
SHIFTOUT SDA,SCL,1,[%0\1] 'Send acknowledge (ACK) = 0
SHIFTIN SDA,SCL,0,[i2c_in[1]] 'Shift in second byte MSBpre
SHIFTOUT SDA,SCL,1,[%1\1] 'Send not acknowledge (NACK) = 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
END
Bookmarks