Code:
@ device pic12F683, intrc_osc_noclkout
@ device pic12F683, wdt_on
@ device pic12F683, mclr_off
@ device pic12F683, protect_off
DEFINE OSC 8
OSCCON.0=1 'system clock select bit 1=internal 0=config word
OSCCON.4=1 'set int osc to 8MHZ
OSCCON.5=1
OSCCON.6=1
ANSEL.3=0 'set all digital
ANSEL.2=0
ANSEL.1=0
ANSEL.0=0
CMCON0=7 'turn comparators off!
ADCON0=0 'turn ADC off
GPIO.7 = 0 'enable weak pullups 'NOTE: this didn't work for one wire or i2c coms.
WPU = %00111000 ' had to use external pullup anyways (did I set it wrong?)
'===========================================
DEFINE DEBUG_REGG GPIO 'set debug port to porta
DEFINE DEBUG_BIT 0 'use pin a0 of porta for debug
DEFINE DEBUG_BAUD 9600 'set baud rate to 2400
DEFINE DEBUG_MODE 0 'communicate in true mode
'12f683 is 14bit, no scl/sda defines
SCL var GPIO.4 ' Clock pin
SDA var GPIO.5 ' Data pin
readbutton var GPIO.3
maxsensor con 4
trisio.3 = 1 ' switch logging/readout stored temps (using mclr pin, mclr disabled)
Cold_Bit VAR Raw.Bit11 ' Sign-Bit for +/- Temp. 1 = Below 0 deg C
id var byte[8]
ADDR var WORD ' 24c32a uses 2 bytes for address (24lc256 as well)
raw var word
records var word
recordaddr var word
readloops var word
readaddr var word
tempc var word
tempf var word
Sign VAR byte ' +/- sign for temp display
fsign var byte
sensor var byte
hexbyte var byte
col var byte
busy var bit
rsign var bit
CLEAR
recordaddr = 31998 'store records at end (been writing to address 0 too much)
ADDR = 2 ' normal start address
'addr = 31960 'temporary testing eeprom full
goto start
subC_TO_F:
'DISABLE ' Necessary if On Interrupt used
'tempF = tempC * 9
'tempf = DIV32 5
'ENABLE ' Necessary if On Interrupt used
tempf = (tempc */ 461)
IF rsign = 1 THEN 'if the sign bit is "1" is minus C
IF tempF > 3200 THEN
fsign="-"
tempF = tempF - 3200
'SEROUT2 sSEROUT_PIN,cBAUD,[" -",DEC C/100,".",DEC2 C," C = -",DEC F/100,".",DEC2 F," F",13]
ELSE
fsign="+"
tempF = 3200 - tempF
'SEROUT2 sSEROUT_PIN,cBAUD,[" -",DEC C/100,".",DEC2 C," C = ",DEC F/100,".",DEC2 F," F",13]
ENDIF
ELSE
tempF = tempF + 3200
fsign = "+"
'SEROUT2 sSEROUT_PIN,cBAUD,[" ",DEC C/100,".",DEC2 C," C = ",DEC F/100,".",DEC2 F," F",13]
ENDIF
RETURN
GetID:
Select Case sensor
Case 1 :LOOKUP hexbyte,[$28,$00,$3E,$D8,$05,$00,$00,$10], col
Case 2 :LOOKUP hexbyte,[$28,$E1,$E2,$D6,$05,$00,$00,$64], col
Case 3 :LOOKUP hexbyte,[$28,$3C,$75,$D6,$05,$00,$00,$0C], col
Case 4 :LOOKUP hexbyte,[$28,$4F,$62,$D8,$05,$00,$00,$F5], col
'Case 5 :LOOKUP hexbyte,[], col
'Case 6 :LOOKUP hexbyte,[], col
'Case 7: LOOKUP hexbyte,[], col
'Case 8 :LOOKUP hexbyte,[], col
'Case 9 :LOOKUP hexbyte,[], col
end select
return
IDLOOP:
for hexbyte=0 to 7 'each sensor address is 8 bytes
gosub getid 'go look up each sensors address
id[hexbyte]=col 'load the ID array with the retrieved address byte
next hexbyte
return
DisplayTemp: ' +32.0 to +257 F
IF raw.bit15 = 1 THEN ' If Cold_Bit = 1, it's below "0" deg C
rsign = 1
Sign = "-"
else
sign = "+"
endif
raw = abs(raw)
tempc = raw */ 1600
'tempf = (tempc */ 461) + 3200
debug $0D,$0A,"reading : ",dec records
if readbutton = 1 then debug " - Sensor: ",dec1 sensor
debug " ",sign, DEC (tempc / 100), ".", DEC2 tempc, " C "
gosub subc_to_f
debug " / ",fsign, DEC (tempf / 100), ".", DEC2 tempf, " F "
return
START:
pause 800
IF readbutton = 0 then readstart
ReadSensor:
OWOUT GPIO.2, 1, [$cc,$44] 'skip romcheck (only one sensor), initiate[$44] temperature conversion
Ckbsy:
OWIN GPIO.2, 4, [busy] ' Check for still busy converting
IF busy = 0 THEN ckbsy ' Still busy?, then loop
sensor = sensor + 1
if sensor = maxsensor+1 then sensor = 1
gosub idloop
'=============================================
'owout GPIO.2,1,[$cc,$BE] 'skiprom (one sensor only) send back scratchpad[$BE]
owout GPIO.2,1,[$55,str id\8,$BE] 'instructs sensors to match[$55] this[ID] and start sending back scratchpad[$BE]
'=============================================
OWIN GPIO.2, 2, [Raw.LOWBYTE,Raw.HIGHBYTE]
if addr > 31997 then
debug "eeprom full, not stored"
else
I2CWRITE SDA,SCL,$A0,ADDR,[raw.lowbyte,raw.highbyte]
pause 100
records = addr / 2
i2cwrite sda,scl,$A0,recordaddr,[records]
addr = addr + 2
endif
gosub displaytemp
goto start
readstart:
readaddr = 0
i2cread sda,scl,$A0,recordaddr,[records]
readloops = records*2
rloop:
while readaddr < readloops
pause 50
I2CREAD SDA,SCL,$A0,readaddr,[raw.lowbyte,raw.highbyte]
readaddr = readaddr + 2
records = readaddr / 2
gosub displaytemp
IF readbutton = 1 then start
wend
goto start
end
really frustrating as I really wanted to use one of these 16f77 boxes I have.
Bookmarks