Code:
#CONFIG
__config _CONFIG1, _HS_OSC & _WDT_OFF & _MCLRE_OFF & _LVP_OFF & _CP_OFF
__CONFIG _CONFIG2, _WRT_OFF & _BOR21V
#ENDCONFIG
'INTERRUPT VARS
'wsave var byte $70 system 'needed for interrupt
'wsave var byte $70 SYSTEM
'wsave1 VAR BYTE $A0 SYSTEM ' location for W if in bank1
'wsave2 VAR BYTE $120 SYSTEM ' location for W if in bank2
ibit var bit
icount var byte
'GENERAL VARS
tempbit var bit
wrbit var bit
tempvar var word
tempvar2 var word
tempbyt var byte
n var byte
current var word
voltage var word
EDATA var word[4]
hbyte var byte
lbyte var byte
TIME1 var word
TIME2 var word
'HX711 specific
HX711A var Word
'MAX6675 specific
MXTemp var word
'ADS1115 specific
adrs_w var byte
adrs_r var byte
config1 var byte
config2 var byte
CNFGREG VAR BYTE
CNVRREG var byte
'EEPROM specific
wconfig var byte
rconfig var byte
address var word
ABRT_BUT var PORTA.1
STRT_BUT VAR PORTA.0
DIAG_BUT VAR PORTE.3
LED var portA.2
Hdta var PORTA.3 'HX711 pins
Hclk var PORTA.4 'HX711 pins
PIN_OUT var PORTC.3 'serial out pin
PIN_IN var PORTC.2 'serial in pin
ADC_CLK var PORTC.1 'ADC clock pin
ADC_DAT var PORTC.0 'ADC data pin
ADC_RDY var PORTA.5 'ADC alert/ready pin
EE_CLK var PORTC.5 'I2C EEPROM clock pin
EE_DAT var PORTC.4 'I2C EEPROM data pin
MXDATA var PORTB.5 'MAX6675 DATA pin (was HX711 pin)
MXCLK var PORTB.7 'MAX6675 CLOCK pin (was HX711 pin)
MXCS var PORTB.6 'MAX6675 Chip Select
MOT_PWM var PORTB.4 'MOTOR PWM OUTPUT
'INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
'INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
T1CON = %00110000 ' Prescaler = 1:1, Timer off
ANSEL=%00000000 'all analogue ports to digital
ANSELH=%00000000 'all analogue ports to digital
ADCON0.0=0 'disable ADC
CM1CON0=%00000000 'disable COMPARATOR 1
CM2CON0=%00000000 'disable COMPARATOR 2
SSPCON=%00000000 'disable SERIAL PORT
RCSTA=%00000000 'disable SERIAL PORT
PCON=%00000000 'disable BOR and ULPW
TRISA=%00101011 '(0=output, 1=input)
TRISB=%00100001 '(0=output, 1=input)
TRISC=%00000000 '(0=output, 1=input)
TRISE=%00001000 '(0=output, 1=input)
OPTION_REG=%11000101
define OSC 20
low MOT_PWM
low Hclk
HIGH MXCS
HIGH EE_CLK
for n = 0 to 9
high LED
pause 100
low LED
pause 100
next n
'setup EEPROM
wconfig=%10100000
rconfig=%10100001
'setup ADC configuration
adrs_w=$48
adrs_w=adrs_w<<1 ' Add read/write bit at end (0=write)
adrs_r=adrs_w+1 ' Add read/write bit at end (1=read)
CNFGREG=$01 ' Address of the configuration register
CNVRREG=$00 ' Address of the conversion register
waitforstart:
if STRT_BUT=0 then waitforstart
config1=%11010001 ' A+ = A1, A- = GND
config2=%11100011
I2CWRITE ADC_DAT,ADC_CLK,adrs_w,CNFGREG,[config1,config2] 'set config register
ibit=0
address=0
icount=0
pause 2
TMR1H=0
TMR1L=0
tempbyt=0
serout2 PIN_OUT,32,["THERMAL START",13,10] '19200bps
high LED
@ bsf T1CON, TMR1ON ' Start timer
'@ INT_ENABLE TMR1_INT ; enable TMR1 interrupts
pulse_test:
TIME2.highbyte=tempbyt
I2CWRITE ADC_DAT,ADC_CLK,adrs_w,[CNVRREG] 'point to conversion register
I2CREAD ADC_DAT,ADC_CLK,adrs_r,[hbyte,lbyte] 'read voltage from ACS712 (current sensor) via ADC
current.highbyte=hbyte
current.lowbyte=lbyte
EDATA[icount]=current
'switch ADC to channel A2 (batt voltage)
config1=%11100001 'A+ = A2, A- = GND
I2CWRITE ADC_DAT,ADC_CLK,adrs_w,CNFGREG,[config1,config2] 'write config register channel
serout2 PIN_OUT,32,["A",dec ibit,"-",dec icount,"-",dec address,13,10]
if icount=0 then
LOW MXCS
shiftin MXDATA,MXCLK,0,[MXTemp\16] 'read the data to MXTemp
HIGH MXCS
serout2 PIN_OUT,32,["B",dec ibit,"-",dec icount,"-",dec address,13,10]
endif
hold1:
tempbyt=TMR1H
if tempbyt<>128 then hold1
TIME2.LOWbyte=tempbyt
icount=icount+1
I2CWRITE ADC_DAT,ADC_CLK,adrs_w,[CNVRREG] 'point to conversion register
I2CREAD ADC_DAT,ADC_CLK,adrs_r,[hbyte,lbyte] 'read batt voltage through 1/2 voltage divider via ADC
voltage.highbyte=hbyte
voltage.lowbyte=lbyte
EDATA[icount]=voltage
'switch ADC to channel A1 (current)
config1=%11010001 ' A+ = A1, A- = GND
I2CWRITE ADC_DAT,ADC_CLK,adrs_w,CNFGREG,[config1,config2] 'write config register channel
tempbyt=icount & %00000011
serout2 PIN_OUT,32,["C",dec ibit,"-",dec icount,"-",dec address,13,10]
if tempbyt=3 THEN
I2CWRITE EE_DAT,EE_CLK,wconfig,address,[EDATA[0],EDATA[1],MXTemp,TIME1,EDATA[2],EDATA[3],MXTemp,TIME2]
address=address+16
serout2 PIN_OUT,32,["D",dec ibit,"-",dec icount,"-",dec address,13,10]
toggle LED
endIF
icount.bit4=0 'rollover from 15 to 0
if address=1600 then goto readout
TIME1=TIME2
hold2:
tempbyt=TMR1H
if tempbyt<>128 then hold2
icount=icount+1
goto pulse_test
readout:
'@ INT_DISABLE TMR1_INT ; enable TMR1 interrupts
low MOT_PWM
serout2 PIN_OUT,32,["READOUT",13,10]
low LED
for tempvar=0 to 105
address=tempvar<<3
I2CREAD EE_DAT,EE_CLK,rconfig,address,[current,voltage,MXTemp,TIME1]
serout2 PIN_OUT,32,[dec tempvar," ",dec current," ",dec voltage," ",MXTemp," ",TIME1.highbyte," ",TIME1.lowbyte,13,10]
next tempvar
goto waitforstart
END
Bookmarks