Você está na página 1de 3

'===============================================================================

' Project: BMP180 Barometric Pressure Sensor test


' Created: 2012-01-18
' Edited:
' By:
Glen Aidukas
' Description:
' Test code to read the BMP180 Barometric Pressure Sensor temperature & pressur
e.
' NOTE: This code only reads the RAW, uncalibrated data.
' BMP180 breakout board tested on was from:
'
http://www.dsscircuits.com/barometric-pressure-sensor-bmp180.html
' Datasheet for the BMA180 can be found here:
'
http://www.dsscircuits.com/images/datasheets/BST-BMP180-DS000-07.pdf
'------------------------------------------------------------------------------'=====[ Compiler Directives ]===================================================
$crystal
= 18432000
$regfile
= "m1284pdef.dat"
' ATMEGA1284P on a BahBots.com controller
$baud
= 38400
$hwstack
= 64
$swstack
= 64
$framesize = 64
'------------------------------------------------------------------------------'=====[ Global Vars & Constants ]===============================================
dim

count

dim
dim
dim

BMP180_UTemp as integer ' RAW temperature data


BMP180_UTempa as byte at BMP180_UTemp + 0 overlay
BMP180_UTempb as byte at BMP180_UTemp + 1 overlay

dim
dim
dim
dim

BMP180_UP
BMP180_UPa
BMP180_UPb
BMP180_UPc

dim
dim
dim

BMP180_UPstart as long
BMP180_UPdelta as long
BMP180_timeout as byte ' keep from locking up on failed I2C connections

dim

BMP180_status as byte

Const BMP180_W
Const BMP180_R

as word

as
as
as
as

long
' RAW uncalibrated pressure data
byte at BMP180_UP + 0 overlay
byte at BMP180_UP + 1 overlay
byte at BMP180_UP + 2 overlay

= &HEE
= &HEF

' BMP180 Write address


' BMP180 Read address

'------------------------------------------------------------------------------'=====[ Config misc stuff ]=====================================================


$LIB "I2C_TWI.LBX"
Config Scl = Portc.0
Config Sda = Portc.1
Config Twi = 400000
I2cinit

'
'
'
'

lib for using the hardware I2C. Comment out for soft I2C
needed if using soft I2C
needed if using soft I2C
set the I2C bus to 'fast' speed

print "{027}[2J{027}[1;1H";
print "Starting";
gosub BMP180_ReadTemp
waitms 500
print "..."
print

' ansi clear screen

gosub BMP180_ReadPressure
BMP180_UPstart= BMP180_UP
'------------------------------------------------------------------------------'*****[ Start of main loop ]****************************************************
do
gosub BMP180_ReadTemp
gosub BMP180_ReadPressure
BMP180_UPdelta= BMP180_UP - BMP180_UPstart
print
print
print
print
print
print
print

' get delta since starting

"{027}[3;1H";
"Count

' ANSI command to move cursor to line 3, col 1


: "; count;
"{027}[K"
"{027}[K"
"Raw Temp
: "; BMP180_UTemp;
"{027}[K"
"Raw Pressure : "; BMP180_UP;
"{027}[K"
"Pressure Delta : "; BMP180_UPdelta;
"{027}[K"
"{027}[K"

waitms 250
incr count
Loop
End
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'=====[ Read the raw temp from the BMP180 ]=====================================
BMP180_ReadTemp:
' read the temp
I2cstart
I2cwbyte BMP180_W
I2cwbyte &HF4
I2cwbyte &H2E
I2cstop
waitms 5
I2cstart
I2cwbyte BMP180_W
I2cwbyte &HF6
I2cstart
I2cwbyte BMP180_R
I2crbyte BMP180_UTempb, Ack
I2crbyte BMP180_UTempa, Nack
I2cstop
return
'------------------------------------------------------------------------------'=====[ Read the raw pressure from the BMP180 ]=================================

BMP180_ReadPressure:
' start the pressue measurement
I2cstart
I2cwbyte BMP180_W
I2cwbyte &HF4
I2cwbyte &B11_110100 ' set to ultra high resolution: 8 time over sampling
I2cstop
' check and wait for completion
BMP180_timeout= 100 ' time out after about 100ms
do
waitms 1
I2cstart
I2cwbyte BMP180_W
I2cwbyte &HF4
I2cstart
I2cwbyte BMP180_R
I2crbyte BMP180_status, Nack
decr BMP180_timeout
loop until BMP180_status.5 = 0 or BMP180_timeout = 0 ' wait for completion
if BMP180_timeout = 0 then
BMP180_UP= -1
else
' now read the pressure
I2cstart
I2cwbyte BMP180_W
I2cwbyte &HF6
I2cstart
I2cwbyte BMP180_R
I2crbyte BMP180_UPc,
I2crbyte BMP180_UPb,
I2crbyte BMP180_UPa,
I2cstop
shift BMP180_UP, right,
endif

data

Ack
Ack
Nack
5, signed

return
'-------------------------------------------------------------------------------