BMP180 Luchtdruk (hoogte) en Temperatuur Sensor
Proton Basic code voor de BMP085 en de BMP180 Luchtdruk sensor.
Deze code werkt niet zondermeer op de Proton Amicus versie.
Deze ondersteunt nog geen signed variablen (Sbyte, SWord, SDWord)
UPDATE Juli 2014!!
Sinds begin Mei 2014 geeft de nieuwe 3.5.6.7. (full) Proton (PICBasic) versie de mogelijkheid om zonder de USB licentie stick met de 18F25K20 en de 18F25K22 te werken. Hiermee is de AMICUS18 Compiler in wezen overbodig geworden.
De full Proton 3.5.6.7. versie ondersteunt signed variablen!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | '**************************************************************** '* Name : BMP085-BMP180.bas * '* Author : www.Knutselaar.eu * '* Date : 4/Apr/2014 * '* Version : 1.0 * '* Notes : * '* : * '**************************************************************** Device 18F25K20 XTAL = 20 CONFIG_START FOSC = HS ;external oscillator FCMEN = OFF ;Fail-Safe Clock Monitor disabled IESO = OFF ;internal external switchover mode PWRT = On ;power-up timer BOREN = On ;brown-out reset BORV = 27 ;brown-out reset value (2,7V) WDTEN = OFF ;watchdog timer WDTPS = 128 ;1:128 WDT prescalar PBADEN = OFF ;analog port B<4:0> STVREN = On ;stack overflow reset LVP = OFF ;low voltage programming XINST = OFF ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode) Debug = OFF ;no debug CONFIG_END ALL_DIGITAL TRUE Symbol SCL = PORTC.3 ' I2C clock pin Symbol SDA = PORTC.4 ' I2C data pin Dim i2c_Reg As Byte 'I2C register adres Dim x As Byte 'gemiddelde teller Dim wAC1 As Word 'I2c uitlezing Dim wAC2 As Word 'I2c uitlezing Dim wAC3 As Word 'I2c uitlezing Dim wAC4 As Word 'I2c uitlezing Dim wAC5 As Word 'I2c uitlezing Dim wAC6 As Word 'I2c uitlezing Dim wB1 As Word 'I2c uitlezing Dim wB2 As Word 'I2c uitlezing Dim wMB As Word 'I2c uitlezing Dim wMC As Word 'I2c uitlezing Dim wMD As Word 'I2c uitlezing Symbol OSS = 3 'resolutie setting Dim OSS1 As Byte 'resolutie variabele Dim OSSdel As Byte 'resolutie delay Dim AC1 As DWord 'Calibratie data Dim AC2 As DWord 'Calibratie data Dim AC3 As DWord 'Calibratie data Dim AC4 As DWord 'Calibratie data Dim AC5 As DWord 'Calibratie data Dim AC6 As DWord 'Calibratie data Dim B1 As DWord 'Calibratie data Dim B2 As DWord 'Calibratie data Dim MB As DWord 'Calibratie data Dim MC As DWord 'Calibratie data Dim MD As DWord 'Calibratie data Dim X1 As SDword 'Calibratie variabele Dim X2 As SDword 'Calibratie variabele Dim B5 As SDword 'Calibratie variabele Dim B6 As SDword 'Calibratie variabele Dim X3 As SDword 'Calibratie variabele Dim B3 As SDword 'Calibratie variabele Dim B4 As DWord 'Calibratie variabele Dim B7 As DWord 'Calibratie variabele Dim UT As Word 'ongecompenseerde temperatuur Dim UP As DWord 'ongecompenseerde pressure Dim float1 As Float 'reken variable Dim float2 As Float 'reken variable Dim Alt As SWord 'hoogte in meters x 10 Dim AltAv As SDWord 'gemiddelde hoogte x 10 Dim T As SWord 'graden celcius x 10 Dim Pr As DWord 'luchtdruk in Pa Cls Clear Print At 1 , 1 , " Knutselaar.eu " DelayMS 1000 GoSub read_BMP_eeprom 'uitlezen eeprom data (calibratie data) main: For x = 1 To 20 'gemiddelde uit 20 metingen GoSub get_BMP_temp 'niet gecompenseerde temperatuur uitlezen GoSub calc_BMP_temp 'gecompenseerde temperatuur GoSub get_BMP_pressure 'niet gecompenseerde druk uitlezen GoSub calc_BMP_press 'gecompenseerde druk GoSub calc_BMP_Alt 'hoogte t.o.v. gemiddeld zee niveau AltAv = AltAv + Alt '(niet gecompenseerd voor luchtdruk verschillen) Next x AltAv = AltAv/20 Print At 1 , 1 , SDEC AltAv/10,".",DEC1 AltAv," m ",SDEC2 T/10,".",DEC1 T , 223, "C " Print At 2 , 1 , Dec Pr/100,".",DEC1 Pr/10," Pa " AltAv = 0 Toggle LATA.5 GoTo main '------Start subroutines ------------------------------------------- read_BMP_eeprom: i2c_Reg =$AA 'Start adres van de BMP085 Calibratie data I2CIn SDA,SCL,$EF,i2c_Reg, [wAC1,wAC2,wAC3,wAC4,wAC5,wAC6,wB1,wB2,wMB,wMC,wMD] 'Read 11 words out of sensor MSB then LSB AC1 = (wAC1.HighByte<<8) + wAC1.LowByte AC2 = (wAC2.HighByte<<8) + wAC2.LowByte AC2 = AC2-65536 AC3 = (wAC3.HighByte<<8) + wAC3.LowByte AC3 = AC3-65536 AC4 = (wAC4.HighByte<<8) + wAC4.LowByte AC5 = (wAC5.HighByte<<8) + wAC5.LowByte AC6 = (wAC6.HighByte<<8) + wAC6.LowByte B1 = (wB1.HighByte<<8) + wB1.LowByte B2 = (wB2.HighByte<<8) + wB2.LowByte MB = (wMB.HighByte<<8) + wMB.LowByte MB = MB - 65536 MC = (wMC.HighByte<<8) + wMC.LowByte MC = MC - 65536 MD = (wMD.HighByte<<8) + wMD.LowByte Return get_BMP_temp: UT = 0 i2c_Reg = $F4 'control register adres I2COut SDA,SCL,$EE,i2c_Reg,[$2E] 'schrijf $2E voor temperature conversie DelayMS 10 'Delay 10ms na write i2c_Reg = $F6 '$F6 is het resultaat register MSB I2CIn SDA,SCL,$EF,i2c_Reg,[UT] 'Lees temperatuur MSB, LSB. Return get_BMP_pressure: i2c_Reg = $F4 'control register adres OSS1 = $34+(OSS << 6) I2COut SDA,SCL,$EE,i2c_Reg,[OSS1] 'Write $34+(oss << 6) voor pressure conversie If OSS = 3 Then OSSdel = 30 Else OSSdel = 10 End If DelayMS OSSdel 'Delay 30mS voor HiRes resultaat (oss=3)) i2c_Reg = $F6 '$F6 is het resultaat register MSB I2CIn SDA,SCL,$EF,i2c_Reg,[UP] 'Lees pressure MSB, LSB, XLSB, $F9 ($F9 is niet nodig). UP = UP >> (16 - OSS) 'verwijder $F9 van resultaat (>>8), en schuif resultaat terug naar 16 tot 19 Bits (OSS waarde afhankelijk) Return calc_BMP_temp: 'temperatuur berekenen met X1 = (UT - AC6) 'calibratie gegevens, formules X1 = (X1 *AC5)/32768 'uit de BMP035 datasheet. X2 = (MC * 2048) X2 = X2/(X1 + MD) B5 = X1 + X2 T = (B5 + 8)/16 'temperatuur in °C*10 Return calc_BMP_press: 'pressure berekenen met B6 = B5 - 4000 'calibratie gegevens, formules 'uit de BMP035 data sheet. X1 = (B6*B6)/4096 X1 = (B2 * X1)/2048 X2 = (AC2 * B6)/2048 X3 = X1 + X2 B3 = (AC1 * 4)+ X3 B3 = (B3 << OSS) + 2 B3 = B3/4 X1 = (AC3 * B6)/8192 X2 = (B6 * B6)/4096 X2 = (B1 * X2)/65536 X3 = (X1 + X2) X3 = (X3 + 2)/4 X3 = X3 + $8000 B4 = (AC4*X3)/$8000 B7 = UP - B3 B7 = B7*(50000 >> OSS) If B7 < $80000000 Then Pr = (B7 * 2) / B4 Else Pr = (B7 / B4) * 2 EndIf X1 = Pr /$100 X1 = X1 * X1 X1 = (X1 * 3038)/$10000 X2 = -7357 * Pr X2 = X2/$10000 X3 = X1 + X2 + 3791 Pr = Pr + (X3/$10) 'druk in Pa Return calc_BMP_Alt: float1 = Pr/100 float1 = float1/1013.25 float2 = 0.1902949571 float1 = Pow float1,float2 float2 = 1 - float1 Alt = 443300 * float2 'hoogte in meters x 10 Return End |