Wire SDA and SDC to your pic (using 4.7k pullups) and ground A0 and A1 (or change the address in the code). For TMP101, A1 is replaced by ALERT which you can wire to an interrupt pin of the PIC.
When powered up, the TMP100 does automatic sampling. The following line can be sent to configure the sensor to 12bit resolution:
SCLpin VAR PORTB.6 SDApin VAR PORTB.4 TempH VAR Byte TempL VAR Byte I2CWRITE SDApin,SCLpin,$90,$01,[%01100100] 'Set continuous sampling at 12bit resolution I2CREAD SDApin,SCLpin,$90,$00,[TempH,TempL] 'Read temperature sample
SCLpin VAR PORTB.6 SDApin VAR PORTB.4 TempH VAR Byte TempL VAR Byte I2CWRITE SDApin,SCLpin,$90,$01,[%01100101] 'Set 12 bit resolution, turn sensor off SamplingLoop: I2CWRITE SDApin,SCLpin,$90,$01,[%11100101] 'one-shot sample PAUSE 350 I2CREAD SDApin,SCLpin,$90,$00,[TempH,TempL] 'Read temperature sample GOTO SamplingLoop
Temperature is read as a 16 bit value (TempH being MSB and TempL LSB). Here is the conversion routine to display the value on LCD (initialize your LCD first!):
TempSign VAR Byte TempDec VAR Word '******************************************************* 'Convert temperature to LCD '******************************************************* ConvertTemp: IF TempH.7 = 1 THEN TempSign = "-" TempL = TempL ^ %11110000 'bitwise invert on the 4 MSB TempL = TempL + 16 'adding 1 temperature unit TempH = TempH ^ %11111111 'bitwise invert IF TempL = 0 THEN TempH = TempH + 1 ENDIF ELSE TempSign = "+" ENDIF TempDec = 0 IF TempL.7 = 1 THEN TempDec = 5000 IF TempL.6 = 1 THEN TempDec = TempDec + 2500 IF TempL.5 = 1 THEN TempDec = TempDec + 1250 IF TempL.4 = 1 THEN TempDec = TempDec + 625 LCDOUT $FE,1, TempSign, #TempH, ".", #TempDec DIG 3, #TempDec DIG 2, #TempDec DIG 1, #TempDec DIG 0, "°C" RETURN
Re: hserout issues on the 18F26K83
Yep, I figured it probably have something to do with that.
HenrikOlsson - 22nd May 2024, 15:13Strange thing is that the code has been working fine with MCS for over 10 years but now it doesn't. So I've messed something up.