|
LCD200顯示氣壓溫度
單片機(jī)源程序如下:
- #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};//顯示時(shí)間的結(jié)構(gòu)體
- volatile SerialBuffType m_Usart1Recv = SerialBuffDefault();
- unsigned char LCD_data = 0x08;//全局變量 缺省狀態(tài) 00001000使背光開啟 00000000背光關(guān)閉
- #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();//通用定時(shí)器2初始化
- TIM_Delay_Init();//(TIM3)
- TIM4_Init();//
- USART1_Init(115200);//串口1初始化
- IIC_Init();
- BMP280_Init();
-
- Set_System();//系統(tǒng)時(shí)鐘初始化
- USB_Interrupts_Config();
- Set_USBClock();
- USB_Init(); // 初始化完成后 就等待設(shè)備枚舉成功
- //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);//背光開關(guān)
- 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毫秒執(zhí)行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是否接收到數(shù)據(jù)
- {
- 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("重啟進(jìn)入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接收到一個(gè)字符
- 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);//定時(shí)器重新計(jì)數(shù)
- }
- //USART1接收字符超時(shí)回調(diào)函數(shù)
- static void TimEndHandle(void)
- {
- TIM_Close(TIM4);//關(guān)閉定時(shí)器
- m_Usart1Recv.len = m_Usart1Recv.ind;
- m_Usart1Recv.ind = 0;
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief 報(bào)告在檢查參數(shù)發(fā)生錯(cuò)誤時(shí)的源文件名和錯(cuò)誤行數(shù)
- * @param file 源文件名
- * @param line 錯(cuò)誤所在行數(shù)
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* 用戶可以增加自己的代碼用于報(bào)告錯(cuò)誤的文件名和所在行數(shù),
- 例如:printf("錯(cuò)誤參數(shù)值: 文件名 %s 在 %d行\(zhòng)r\n", file, line) */
- /* 無限循環(huán) */
- while (1)
- {
- }
- }
- #endif
- /*********************************END OF FILE**********************************/
復(fù)制代碼
所有資料51hei提供下載:
STM32F103C8T6_LCD2004_BMP280.7z
(373.16 KB, 下載次數(shù): 198)
2019-10-1 02:15 上傳
點(diǎn)擊文件名下載附件
|
|