很简单的程序,直接上代码了
memset(buf, 0, sizeof(buf)); sprintf(buf,"wea:%s",weather); OLED_P8x16Str(0u,4u,(uint8_t *)buf); memset(buf, 0, sizeof(buf)); sprintf(buf,"Tem:%d",temperature); OLED_P8x16Str(0u,6u,(uint8_t *)buf);
效果如下
但细想一下,其实这段代码是有BUG的,当温度由十位变个位时,就会导致个位数不会被擦除,还有就是天气状况由多字母变少字母,后边不会被覆盖,所以先写个行清除函数仿照全屏填充函数写一个
/*********************OLED全屏************************************/ void OLED_Fill(uint8_t bmp_dat) { uint8_t y,x; for(y=0;y<8;y++) { OLED_WrCmd(0xb0+y); OLED_WrCmd(0x01); OLED_WrCmd(0x10); for(x=0;x<X_WIDTH;x++) OLED_WrDat(bmp_dat); } }
/*********************OLED某一行填充************************************/ void OLED_Fill_ONE(uint8_t y,uint8_t bmp_dat) { uint8_t x; OLED_WrCmd(0xb0+y); OLED_WrCmd(0x01); OLED_WrCmd(0x10); for(x=0;x<X_WIDTH;x++) OLED_WrDat(bmp_dat); }
完整while(1)
while (1) { if(time6_count>119) { time6_count = 0; Get_Weather_SendData();//上传数据 HAL_Delay(200);//等待平台回复 } if(usart1_rx.flag)//接收到数据 { ipd_rx = ESP8266_GetIPD();//解析数据 if(ipd_rx.len>0)//判断是否为网络下发数据 { printf("\r\n\r\nipd_rx.IPD_buff:%s\r\n\r\n",ipd_rx.IPD_buff); //获取温度 //我这里只写了零上的温度,零下的懒狗的我先不写了 weatheradr= strstr((char *)ipd_rx.IPD_buff, "temperature");//找到"temperature" memcpy(buf,weatheradr+14,2); temperature = 0; if(buf[1]!=0x22)//温度两位数 { temperature += (buf[1]-0x30); temperature += ((buf[0]-0x30)*10); } else//温度一位数 { temperature += (buf[0]-0x30); } //获取天气代码 weatheradr= strstr((char *)ipd_rx.IPD_buff, "code");//找到"code" memcpy(buf,weatheradr+7,2); code = 0; if(buf[1]!=0x22)//0x22为双引号的ASCII码 { code += (buf[1]-0x30); code += ((buf[0]-0x30)*10); } else { code += (buf[0]-0x30); } //获取天气 memset(weather, 0, sizeof(weather)); weatheradr= strstr((char *)ipd_rx.IPD_buff, "text");//找到"code" memcpy(buf,weatheradr+7,10); for(i=0;i<10;i++) { if(buf[i]==0x22) { memcpy(weather,buf,i); break; } } printf("\r\n\r\ntemperature:%d℃\r\n",temperature); printf("code:%d\r\n",code); printf("weather:%s\r\n\r\n",weather); memset(buf, 0, sizeof(buf)); sprintf(buf,"wea:%s",weather); OLED_Fill_ONE(4u,0x00); OLED_P8x16Str(0u,4u,(uint8_t *)buf); memset(buf, 0, sizeof(buf)); sprintf(buf,"Tem:%d",temperature); OLED_Fill_ONE(6u,0x00); OLED_P8x16Str(0u,6u,(uint8_t *)buf); } Clear_Usart(&usart1_rx);//清空缓存 } /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ }
下篇尝试下把天气图标显示出来,但不晓得单片机内部能不能放得下