Code:
' DEVICE CONFIGURATION
#CONFIG
CONFIG OSC = IRCIO
CONFIG FCMEN = OFF
CONFIG IESO = OFF
CONFIG PWRTEN = OFF
CONFIG BOREN = OFF
CONFIG BORV = 20
CONFIG WDTEN = OFF
CONFIG WDPS = 512
CONFIG WINEN = OFF
CONFIG PWMPIN = OFF
CONFIG LPOL = HIGH ; PWM0, 2, 4 and 6 are active-high
CONFIG HPOL = HIGH ; PWM1, 3, 5 and 7 are active-high
CONFIG T1OSCMX = ON ; Low-power Timer1 operation when microcontroller is in Sleep mode
CONFIG MCLRE = OFF
CONFIG STVREN = ON ; Stack full/underflow will cause Reset
CONFIG LVP = OFF ; Low-voltage ICSP disabled
CONFIG DEBUG = OFF ; Background debugger disabled; RB6 and RB7 configured as general purpose I/O pins
CONFIG CP0 = OFF ; Block 0 (000200-000FFFh) not code-protected
CONFIG CP1 = OFF ; Block 1 (001000-001FFF) not code-protected
CONFIG CP2 = OFF ; Block 2 (002000-002FFFh) not code-protected
CONFIG CP3 = OFF ; Block 3 (003000-003FFFh) not code-protected
CONFIG CPB = OFF ; Boot Block (000000-0001FFh) not code-protected
CONFIG CPD = OFF ; Data EEPROM not code-protected
CONFIG WRT0 = OFF ; Block 0 (000200-000FFFh) not write-protected
CONFIG WRT1 = OFF ; Block 1 (001000-001FFF) not write-protected
CONFIG WRT2 = OFF ; Block 2 (002000-002FFFh) not write-protected
CONFIG WRT3 = OFF ; Block 3 (003000-003FFFh) not write-protected
CONFIG WRTC = OFF ; Configuration registers (300000-3000FFh) not write-protected
CONFIG WRTB = OFF ; Boot Block (000000-0001FFh) not write-protected
CONFIG WRTD = OFF ; Data EEPROM not write-protected
CONFIG EBTR0 = OFF ; Block 0 (000200-000FFFh) not protected from table reads executed in other blocks
CONFIG EBTR1 = OFF ; Block 1 (001000-001FFF) not protected from table reads executed in other blocks
CONFIG EBTR2 = OFF ; Block 2 (002000-002FFFh) not protected from table reads executed in other blocks
CONFIG EBTR3 = OFF ; Block 3 (003000-003FFFh) not protected from table reads executed in other blocks
CONFIG EBTRB = OFF ; Boot Block (000000-0001FFh) not protected from table reads executed in other blocks
#ENDCONFIG
' DEFINEs
DEFINE OSC 8 ' Internal 8MHz
' Pin ALIASES
LED1 VAR PORTB.0 ' Orange Lights
GRNLED VAR PORTB.4 '
ClockPin VAR PORTA.6 ' IIC BME280
DataPin VAR PORTA.7 ' IIC BME280
' CONSTANTS
' --- BME280 Registers---
BMEADDR CON $EC ' Chip Address
ID CON $D0 ' Chip ID (0x60)
ctrl_hum CON $F2 ' Humidity Control Register
stat CON $F3 ' Status Register
ctrl_meas CON $F4 ' Temperature/Pressure Control Register
config CON $F5 ' Cofiguration
cal_st_addr CON $88 ' Calibration Start Address
' VARIABLES
FLAG VAR BYTE
IIC_RX VAR BYTE[8] ' IIC Received Data BME280
i var byte
T1 VAR LONG
T2 VAR LONG
T3 VAR LONG
adc_T VAR LONG
var1 VAR LONG
var2 VAR LONG
T VAR LONG
adc_Tr3 VAR LONG
' T1l1 VAR LONG
' T2r11 VAR LONG
' adc_Tr4 VAR LONG
' T1r12 VAR LONG
' T3r14 VAR LONG
t_fine VAR LONG
' TT VAR LONG
' INITIALIZE REGISTERS
OSCCON = %01110000 ' Set internal osc to 8MHZ
ANSEL0 = %00000000 ' Set to Digital PortA
' -HW RS232 Setup-
TXSTA = $20 ' Enable transmit, BRGH = 0
SPBRG = 51 ' 9600 Baud @ 8MHz, 0.16%
SPBRGH = 0
BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
' -HW Timer Setup-
TMR0H = 0 ' HighByte of preload value
TMR0L = 0 ' LowByte of preload value (the order is important!)
INTCON.2 = 0 ' Clear the flag bit
T0CON = %10000111 ' Setup and Enable TMR0 (8.388s)
TRISA = %00000000 ' Set ports as outputs (2-4 I/P)
TRISB = %00000000
TRISC = %00000000 ' Set ports as outputs
CLEAR
' PROGRAM Start
PORTA = 0 ' Clear PORTA
PORTB = 0
PORTC = 0
IIC_RX = 0
Init:
GOSUB P500 ' Let everything settle
GOSUB VER_BME ' Is the BME280 Connected. Verify
MAIN:
GOSUB GETTEMPBME ' Get the temperature reading from BME280
GOSUB P1
GOTO MAIN
' Go back to mainloop
END
GETTEMPBME:
'I2CREAD DataPin, ClockPin, BMEADDR, stat ,[IIC_RX] ' Check to see if busy Converting Measurements, or writing data
'If IIC_RX[0] != 0 THEN ' If not, it should read as 0
' GOSUB P100
' GOSUB GETTEMPBME
'ENDIF
' DO
' IF FLAG.0 = 1 THEN GOSUB P100 ' If flag is set the BME280 was not ready (Try again)
' TOGGLE LED1
' I2CREAD DataPin, ClockPin, BMEADDR, stat ,[IIC_RX] ' Check to see if busy Converting Measurements, or writing data
' FLAG.0 = 1
' LOOP WHILE IIC_RX[0] != 0 '
' FLAG.0 = 0 ' Clear Flag
' I2CREAD DataPin, ClockPin, BMEADDR, $F7 ,[STR IIC_RX\8] ' Read Temp/Hum/Press registers
arraywrite IIC_RX,[ $55 ,$2F ,$D0 ,$82 ,$D7 ,$90 ,$85 ,$A6 ]
for i=0 to 6
HSEROUT[hex2 IIC_RX[i],","]
next
HSEROUT[hex2 IIC_RX[i],13,10]
' GOSUB P1
adc_T.Byte0 = IIC_RX[5] ' Store Raw Temerature data
adc_T.Byte1 = IIC_RX[4]
adc_T.Byte2 = IIC_RX[3]
' GOSUB P1
adc_T=adc_T >> 4
var1 = ((((adc_T >> 3) - (T1 << 1))) * (T2)) >> 11
var2 = (((((adc_T >> 4) - (T1)) *((adc_T >> 4) - (T1))) >>12) *(T3)) >>14
t_fine = var1 + var2
T = ((t_fine * 5) + 128)>>8
HSEROUT["T " ,#T/100,".",#T//100,13,10]
RETURN
VER_BME:
' I2CREAD DataPin, ClockPin, BMEADDR, ID ,[IIC_RX] ' Read the BME's ID
' IF IIC_RX[0] != $60 THEN ERROR2 ' Cannot Connect to the BME280
' ' -Read Calibration Data for Temperature-
' I2CREAD DataPin, ClockPin, BMEADDR, cal_st_addr ,[STR IIC_RX\6] '
arraywrite IIC_RX,[$6C ,$6E ,$77 ,$66 ,$32 ,$00]
for i=0 to 4
HSEROUT[hex2 IIC_RX[i],","] ' Display -> 6C 6E 77 66 32 00 0D 0A
next
HSEROUT[hex2 IIC_RX[i],13,10]
T1.Byte0 = IIC_RX[0] ' Calibration data into Unsigned Word
T1.Byte1 = IIC_RX[1]
T2.Byte0 = IIC_RX[2] ' Calibration data into Signed Long
T2.Byte1 = IIC_RX[3]
T3.Byte0 = IIC_RX[4] ' Calibration data into Signed Long
T3.Byte1 = IIC_RX[5]
' -Initialise the Sensor-
' I2CWRITE DataPin, ClockPin, BMEADDR, config ,[%10101000] ' Config Setting (1000ms Standby, IIR Filter @ 4)
' I2CWRITE DataPin, ClockPin, BMEADDR, ctrl_hum ,[%00000010] ' Humidity Setting (x2 Oversampling)
' I2CWRITE DataPin, ClockPin, BMEADDR, ctrl_meas ,[%01001011] ' Temp/Pres Settings (x2 Oversampling, Normal Mode)
' GOSUB P250
RETURN
ERROR1:
DO
TOGGLE LED1 ' Flash the Orange LED every 1/2 second
GOSUB P500
LOOP
ERROR2: ' BME280 Connection Error
LED1 = 1
GOSUB P250
LED1 = 0
GOSUB P250
LED1 = 1
GOSUB P250
LED1 = 0
GOSUB P1
GOTO ERROR2
P100:
PAUSE 100 ' Just a pause..
RETURN
P250:
PAUSE 250 ' Just a pause..
RETURN
P500:
PAUSE 500 ' Just a pause..
RETURN
P1:
PAUSE 1000 ' Just a pause..
RETURN
Bookmarks