在环境状态的检测中,温度和大气压强是2个主要的参数,为此可以通过相应的传感器BMP085来进行检测。为标记检测的时间点,还可把RTC的计时功能也组合在一起,其整个的器件构成如图1所示。 图1 整体构成 其中,BMP085是一种I2C器件,LCD屏是一种SPI器件。 显示屏与开发板的连接关系为: BLK-------PB8
CS--------PB7
RS--------PB6
RESET---PB5
SCL-------PA5
SDA------PA7 BMP085与开发板的连接关系为: SCL---PA0 SDA---PA1 以相关引脚输出高低电平的引脚定义为: #defineSCL_Set() GPIO_Pins_Set(GPIOA,GPIO_PIN_0) #defineSCL_Clr() GPIO_Pins_Reset(GPIOA,GPIO_PIN_0) #defineSDA_Set() GPIO_Pins_Set(GPIOA, GPIO_PIN_1) #defineSDA_Clr() GPIO_Pins_Reset(GPIOA,GPIO_PIN_1) 使用数据引脚进行状态读取的语句定义为: #defineIIC_SDA_IN GPIO_Input_Pin_Data_Get(GPIOA, GPIO_PIN_1) 在传感器工作期间数据引脚会不停地切换工作模式,为此配置了2个配置引脚工作模式的函数,其内容如下: void IIC_INPUT_MODE_SET()
{
GPIO_InitType GPIO_InitStructure;
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA);
GPIO_Structure_Initialize(&GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_1;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.GPIO_Current = GPIO_DS_4MA;
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
}
void IIC_OUTPUT_MODE_SET()
{
GPIO_InitType GPIO_InitStructure;
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA);
GPIO_Structure_Initialize(&GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_1;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
GPIO_InitStructure.GPIO_Current = GPIO_DS_4MA;
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
}
对BMP085进行引脚初始配置的函数为: void BMP085_Init(void)
{
GPIO_InitType GPIO_InitStructure;
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA);
GPIO_Structure_Initialize(&GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_0|GPIO_PIN_1;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
GPIO_InitStructure.GPIO_Current = GPIO_DS_4MA;
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
}
在完成传感器的数据后,需要进行相应的数值变换,其处理函数为: void bmp085Convert()
{
unsigned int ut;
unsigned long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
ut = bmp085ReadTemp();
up = bmp085ReadPressure();
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
temperature = ((b5 + 8) >> 4);
b6 = b5 - 4000;
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
x1 = (ac3 * b6)>>13;
x2 = (b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS));
if (b7 < 0x80000000) p = (b7<<1)/b4;
else p = (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
pressure = p+((x1 + x2 + 3791)>>4);
}
在以上函数的辅助下,通过下面的主程序即可实现初始的设计目标。 主程序的内容如下: int main(void)
{
RTC_Date_And_Time_Default_Value();
RTC_CLKSource_Config(RTC_CLK_LSE, RTC_CLK_FIRST_CONFIG);
RTC_Calendar_Config(DISABLE);
EXTI_PA7_Configuration();
EXTI_Interrupt_Status_Clear(EXTI_LINE7);
RTC_Calibration_Output_Config(RTC_CALIB_OUTPUT_1HZ);
RTC_Output_Mode_Config(RTC_OUTPUT_PUSHPULL);
RTC_Calibration_Output_Enable();
BMP085_Init();
Init_BMP085();
LCD_Initialize();
Lcd_Init();
LCD_Clear(BLACK);
LCD_ShowString(0,20,"t= C",YELLOW);
LCD_ShowString(0,40,"p= KPa",YELLOW);
LCD_ShowString(20,0," : :",YELLOW);
SysTick_Delay_Ms(1000);
while (1)
{
bmp085Convert();
LCD_ShowNum(24,20, temperature/10,3,YELLOW);
LCD_ShowNum(24,40, pressure/100,5,YELLOW);
SysTick_Delay_Ms(1000);
}
}
经程序的编译和下载,其数据显示效果如图2所示。 图2 测试效果 相应的设计支持有: 1)彩色OLED屏的显示驱动及图片显示 https://bbs.21ic.com/icview-3239334-1-1.html 2)RTC电子时钟 https://bbs.21ic.com/icview-3239412-1-1.html
|