STC15单片机
实验名称:读取BMP180气压传感器,并在UART串口打印数值
实验内容:
UART串口9600波特率
printf函数打印信息(putchar)
#define FOSC 22118400L //晶振频率 22.1184MHz
实验器材:
STC15W408AS_DIP16 x1
BMP180气压传感器 x1
实验心得:
主要参考 BMP085 arduino测试代码
注意 BMP180 的 IIC 地址是 0xEE
========================================
BMP180 读写部分
参考bmp180.ino
========================================
void bmp180_init()
{
i2c_bmp180_start();
bmp180_calibration();
i2c_bmp180_stop();
}
打印信息:
[17:16:37.017]接收←>New Messages...
### BMP180 Calibration Data ###
ac1 ac2 ac3 ac4 ac5 ac6
001a 0006 00fc 0044 00c6 00ff
b1 b2 b5 mb mc md
0019 0073 0000 001c 0000 0000
### BMP180 Read Data ###
read_ut: 27466
read_up: 0
### BMP180 Cal Data ###
T: 32.1 C
P: 1000.3 hPa
Altitude: 108.4 m
main.c
- #include "config.h"
- #include "delay.h"
- #include "uart.h"
- #include "i2c_bmp180.h"
- #define LED_ON P12 = 0
- #define LED_OFF P12 = 1
- bit timer_flag_5s = 0; //5s定时触发标志位
- uint8 delay_count = 0; //delay延迟计数器
- void bmp180_loop()
- {
- uint16 read_ut;
- uint32 read_up;
- float temperature, pressure, atm, altitude;
-
- i2c_bmp180_start();
- read_ut = bmp180_read_ut(); //MUST be called first
- read_up = bmp180_read_up();
- i2c_bmp180_stop();
- pressure = bmp180_calc_pval(read_up);
- temperature = bmp180_calc_tval(read_ut);
- atm = pressure / 101325; // "standard atmosphere"
- altitude = bmp180_calc_altitude(pressure); //Uncompensated caculation - in Meters
- i2c_bmp180_stop();
- if(DEBUG_MODE) printf(">New Messages...\r\n");
- if(DEBUG_MODE) printf("\r\n### BMP180 Calibration Data ###\r\n");
- if(DEBUG_MODE) printf("ac1 ac2 ac3 ac4 ac5 ac6\r\n");
- if(DEBUG_MODE) printf("%04bx %04bx %04bx %04bx %04bx %04bx\r\n", t_bmp180_cal_dat.ac1, t_bmp180_cal_dat.ac2, t_bmp180_cal_dat.ac3, t_bmp180_cal_dat.ac4, t_bmp180_cal_dat.ac5, t_bmp180_cal_dat.ac6);
- if(DEBUG_MODE) printf("b1 b2 b5 mb mc md\r\n");
- if(DEBUG_MODE) printf("%04bx %04bx %04bx %04bx %04bx %04bx\r\n", t_bmp180_cal_dat.b1, t_bmp180_cal_dat.b2, t_bmp180_cal_dat.b5, t_bmp180_cal_dat.mb, t_bmp180_cal_dat.mc, t_bmp180_cal_dat.md);
- if(DEBUG_MODE) printf("\r\n### BMP180 Read Data ###\r\n");
- if(DEBUG_MODE) printf("read_ut: %u\r\n", read_ut);
- if(DEBUG_MODE) printf("read_up: %u\r\n", read_up);
- if(DEBUG_MODE) printf("\r\n### BMP180 Cal Data ###\r\n");
- if(DEBUG_MODE) printf("T: %4.1f C\r\n", temperature);
- if(DEBUG_MODE) printf("P: %6.1f hPa\r\n", pressure/100.0f);
- //if(DEBUG_MODE) printf("Std P: %6.1f hPa\r\n", atm);
- if(DEBUG_MODE) printf("Altitude: %4.1f m\r\n", altitude);
- }
- //主函数
- void main(void)
- {
- //设置STC15W408AS单片机端口为标准模式
- Config_Port();
- //串口初始化,波特率9600
- UART_Config(9600);
- //初始化bmp180传感器
- bmp180_init();
- LED_OFF; //LED指示灯熄灭
-
- // 事件处理循环
- while(1)
- {
- if (timer_flag_5s) //每隔5s执行以下分支
- {
- LED_ON; //LED指示灯点亮
- bmp180_loop(); //读取bmp180数据
- LED_OFF; //LED指示灯熄灭
- timer_flag_5s = 0;
- }
- //延时周期200ms
- delay_x_ms(200);
- //5s定时触发标志位
- if(25 == ++delay_count)
- {
- delay_count = 0;
- timer_flag_5s = 1;
- }
- }
- }
复制代码 原理图: 无
仿真: 无
代码:
UART_PRINTF_BMP180_STC15W408AS.zip
(11.88 KB, 下载次数: 21)
|