LCD200显示气压温度
单片机源程序如下:
- #include "bsp.h"
- #include "usart.h"
- #include "usb_lib.h"
- #include "hw_config.h"
- #include "memory.h"
- #include "usbio.h"
- #include "delay.h"
- #include "timer.h"
- #include "mpuiic.h"
- #include "mpu9250.h"
- #include "bmp280.h"
- #include "lcd1602.h"
- #include "1602iic.h"
- #include "LiquidCrystal_I2C.h"
- extern uint8_t USB_Received_Flag;
- uint8_t USB_ReceData[64];
- uint8_t IDxxx[32];
- Update_Set_TypeDef update;
- my_time TIME={0};//显示时间的结构体
- volatile SerialBuffType m_Usart1Recv = SerialBuffDefault();
- unsigned char LCD_data = 0x08;//全局变量 缺省状态 00001000使背光开启 00000000背光关闭
- #define N 12
- u32 bmp280_tempValue[N]={0};
- u32 bmp280_pressValue[N]={0};
- s32 bmp280_temp;
- u32 bmp280_press;
- u8 nCount=0,nFlag=0;
- char buf[20]={0};
- static void Usart1_RecvOneChar(u8 RecvCharacter);
- static void TimEndHandle(void);
- int main(void)
- {
- uint32_t i=0;
- // NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x5000);
- // ENABLE_INT();//使能全局中断
-
- delay_init();
- LED_Init();//初始化
- TIM2_Init();//通用定时器2初始化
- TIM_Delay_Init();//(TIM3)
- TIM4_Init();//
- USART1_Init(115200);//串口1初始化
- IIC_Init();
- BMP280_Init();
-
- Set_System();//系统时钟初始化
- USB_Interrupts_Config();
- Set_USBClock();
- USB_Init(); // 初始化完成后 就等待设备枚举成功
- //USB打开的中断
- /********************************************
- IMR_MSK (CNTR_CTRM | CNTR_WKUPM | CNTR_SUSPM | CNTR_ERRM | CNTR_SOFM \
- | CNTR_ESOFM | CNTR_RESETM )
- ********************************************/
-
- //LCDI2C_init(0x27,16,2);//初始化LCD1602
- LCDI2C_init(0x27,20,4);//初始化LCD2004
- // Quick 3 blinks of backlight
- for( i = 0; i< 3; i++)
- {
- LCDI2C_setBacklight(1);//背光开关
- Delay(250);
- LCDI2C_setBacklight(0);
- Delay(250);
- }
- LCDI2C_setBacklight(1); // finish with backlight on
- Delay(2000);
- LCDI2C_clear();
- LCDI2C_write_String("STM32F103C8T6_TEST ");
- Usart1_SetIRQCallBack(Usart1_RecvOneChar);
- TIM4_SetIRQCallBack(TimEndHandle);
- MemSet((u8 *)&update,0,sizeof(Update_Set_TypeDef));
- if(EE_IsHaveStoreData(STORE_FLAG,STORE_ADDRESS))
- {
- EE_FlashReadFlag(STORE_FLAG,STORE_ADDRESS,(u8 *)&update,sizeof(Update_Set_TypeDef));
- Get_ChipID(IDxxx);
- if(update.OldOrNew==0xaa)
- {
- if(!strstr((char *)IDxxx,(char *)update.IDxxx))
- {
- while(1)
- {
- delay_ms(5000);
- printf("Eorror\r\n");
- }
-
- }
- }
- }
- else
- {
- printf("ReadStoerEorror\r\n");
- }
- i=0xffffff;
- while(i--); //等待usb枚举完成
-
- printf("\r\nAPP 初始化完成\r\n");
- while(1)
- {
- if(IS_TIMEOUT_10MS(3, 100))//间隔1000毫秒执行1次
- {
- LED0=!LED0;
- BMP280_ReadPressureTemperature(&bmp280_press,&bmp280_temp);//bmp280获取气压值和温度
- bmp280_tempValue[nCount]=bmp280_temp;
- bmp280_pressValue[nCount]=bmp280_press;
- nCount++;
- if(nCount>=N)
- {
- nCount=0;
- bmp280_temp=FILTER_median(bmp280_tempValue,N,1);//中位值平均滤波
- bmp280_press=FILTER_median(bmp280_pressValue,N-1,0);//中位值滤波
- MemSet((u8 *)buf,0,20);
- sprintf(buf,"press:%d",bmp280_press);
- printf("bmp280_%s\r\n",buf);
- LCDI2C_Show_Str(0,2,buf);//(2004)
- MemSet((u8 *)buf,0,20);
- sprintf(buf,"temp:%d",bmp280_temp);
- printf("bmp280_%s\r\n",buf);
- LCDI2C_Show_Str(0,3,buf);//(2004)
- }
- TIME.second++;
- if(TIME.second>59)
- {
- TIME.second=0;
- TIME.minute++;
- if(TIME.minute>59)
- {
- TIME.minute=0;
- TIME.hour++;
- if(TIME.hour>23)
- {
- TIME.hour=0;
- TIME.date++;
- }
- }
- }
- MemSet((u8 *)buf,0,20);
- sprintf(buf,"run %03d %02d:%02d:%02d",TIME.date,TIME.hour,TIME.minute,TIME.second);
- LCDI2C_Show_Str(0,1,buf);//(2004)
- }
- if(USB_Received_Flag)//判断usb是否接收到数据
- {
- USB_Received_Flag=0;
- USB_GetData(USB_ReceData,64);
- printf("%s\r\n",USB_ReceData);
- //delay_ms(20);
- USB_SendData(USB_ReceData,64);
- }
- if(m_Usart1Recv.len > 0)
- {
- if(m_Usart1Recv.len > 1)
- {
- printf("%s",m_Usart1Recv.buf);
- }
- else
- {
- switch (m_Usart1Recv.buf[0])
- {
- case 0x6f: //o
- i2cWriteByte(BACKLIGHT_ON);
- break;
- case 0x63: //c
- i2cWriteByte(BACKLIGHT_OFF);
- break;
- case 0x02: //Ctrl+b
- //功能待定
- update.boot_type=1;//1为Bootloader
- //EE_FlashWrite(STORE_FLAG,STORE_ADDRESS,(u8 *)&update,sizeof(Update_Set_TypeDef));//写flash
- printf("重启进入Bootloader模式\r\n");
- break;
- default :
- printf("Error 0x%02x\r\n",m_Usart1Recv.buf[0]);
- break;
- }
- }
- MemSet((u8 *)&m_Usart1Recv.buf,0,USART1_BUFF_LANGTH);
- m_Usart1Recv.len = 0;
- }
- }
- }
- //USART1接收到一个字符
- static void Usart1_RecvOneChar(u8 RecvCharacter)
- {
- if(m_Usart1Recv.ind >= USART1_BUFF_LANGTH)
- return;
-
- if(m_Usart1Recv.len > 0)
- return;
-
- m_Usart1Recv.buf[m_Usart1Recv.ind++] = RecvCharacter;
- TIM_Open(TIM4);//定时器重新计数
- }
- //USART1接收字符超时回调函数
- static void TimEndHandle(void)
- {
- TIM_Close(TIM4);//关闭定时器
- m_Usart1Recv.len = m_Usart1Recv.ind;
- m_Usart1Recv.ind = 0;
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief 报告在检查参数发生错误时的源文件名和错误行数
- * @param file 源文件名
- * @param line 错误所在行数
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* 用户可以增加自己的代码用于报告错误的文件名和所在行数,
- 例如:printf("错误参数值: 文件名 %s 在 %d行\r\n", file, line) */
- /* 无限循环 */
- while (1)
- {
- }
- }
- #endif
- /*********************************END OF FILE**********************************/
复制代码
所有资料51hei提供下载:
STM32F103C8T6_LCD2004_BMP280.7z
(373.16 KB, 下载次数: 198)
|