程序运行没问题,但放进仿真后,1602一直刷新不显示。
#include <reg52.h>
#include <intrins.h>
#include "delay.h"
#include "drv_1602.h"
#include "drv_dht22.h"
#include "drv_pcf8591.h"
#include "drv_key.h"
#include "bp_sprintf.h"
sbit fan = P3^0;
sbit alarm = P1^6;
sbit led_yellow = P1^7;
unsigned char str[20];
unsigned char key_value;
dht22_data_t dht_data;
unsigned long time_20ms=0xaaaaaaaa; //定时器计数
unsigned char count =0; //采集次数计数
unsigned char temp_thres = 50;
unsigned char humi_thres = 50;
float Volt=0.0; //检测电压
xdata float sumVolt,midV; //用于滤波 中间变量
float Acurrent=0.0; //检测电流
xdata float sumAcur,midA; //用于滤波 中间变量
float BatCap=80; //容量初始化
void Init_Timer0(void);
void uartSendStr(unsigned char *s,unsigned char length);
void UART_Init(void);
void uartSendByte(unsigned char dat);
void thres_set(void)
{
unsigned char key_value;
lcd_clear();
lcd_disp_string(0, 0, "Threshold Set");
while(1)
{
key_value = key_check();
if(key_value & 0x02 && temp_thres < 100)
temp_thres++;
else if(key_value & 0x04 && temp_thres > 0)
temp_thres--;
else if(key_value & 0x01)
break;
bp_sprintf(str, "TEMP THRES:%b02u%cC", temp_thres, 0xdf);
lcd_disp_string(0, 1, str);
}
lcd_clear();
lcd_disp_string(0, 0, "Threshold Set");
while(1)
{
key_value = key_check();
if(key_value & 0x02 && humi_thres < 100)
humi_thres++;
else if(key_value & 0x04 && humi_thres > 0)
humi_thres--;
else if(key_value & 0x01)
break;
bp_sprintf(str, "HUMI THRES:%b02u%cC", humi_thres, 0xdf);
lcd_disp_string(0, 1, str);
}
lcd_clear();
}
void main(void)
{
lcd_init();
lcd_clear();
Init_Timer0(); //定时器0初始化
UART_Init();
uartSendStr("reday ok!!",10);
while (1) //主循环
{
dht22_get_data(&dht_data);
key_value = key_check();
bp_sprintf(str, "T:%4.1f%cC", dht_data.temp, 0xdf);
lcd_disp_string(0, 0, str);
bp_sprintf(str, "H:%4.1f%c", dht_data.humi, '%');
lcd_disp_string(0, 1, str);
if(key_value & 0x01)
{
thres_set();
}
if(dht_data.temp >= 40)
fan = 0;
else
fan = 1;
if(dht_data.temp > temp_thres || (dht_data.humi > humi_thres))
alarm = 0;
else
alarm = 1;
midV=(float)pcf8591_get_data(1)*5.13/255*2; //计算出电压 *2表示分压值
Acurrent=(float)pcf8591_get_data(0)*5.13/255; //计算出电流
if(Acurrent>2.6) //如果电流转换后的电压值超过2.6
{midA=(Acurrent-2.6)/0.185;} //电流模块 电压转换计算
else
{midA=0;}
if(midA < 0.1) midA = 0; //防止参考电压变化造成影响
sumAcur = sumAcur + midA; //多次测量求平均
sumVolt = sumVolt + midV; //多次测量求平均
count++;//采集次数
if(count >= 5)
{
count = 0;
Acurrent = sumAcur/5; //Q求平均
if(Acurrent < 0.1) Acurrent= 0;//滤波微小波动
sumAcur = 0;
Volt = sumVolt/5; //求平均
if(Volt<1)Volt=0; //滤除小波动
sumVolt =0;
if(Volt>4.15) //电压值对比
{BatCap = 0.99;}//容量
else if(Volt<3.4)
{BatCap =0;}
else
{BatCap = (Volt-3.4)/(4.15-3.4);}//正常情况下计算比例
}
bp_sprintf(str,"The BatCap: %2.0f%%",BatCap*100);//打印电池容量值
lcd_disp_string(9,0,str);//显示第一行
bp_sprintf(str,"V:%3.2fv A:%3.2fA",Volt,Acurrent);//打印电压电流值
lcd_disp_string(9,1,str);//显示第二行
if(BatCap < 25)
led_yellow = 0;
else
led_yellow = 1;
}
}
void Init_Timer0(void)
{
//**All notes can be deleted and modified**//
TMOD |= 0x10; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TH0=(65536-20000)/256; //重新赋值 20ms
TL0=(65536-20000)%256;
EA=1; //总中断打开
ET0=1; //定时器中断打开
TR0=1; //定时器开关打开
}
void Timer0_isr(void) interrupt 1
{
TH0=(65536-20000)/256; //重新赋值 20ms
TL0=(65536-20000)%256;
time_20ms++;
}
void UART_Init(void)
{
SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重装
TH1 = 0xFD; // TH1: 重装值 9600 波特率 晶振 11.0592MHz
TL1 = TH1;
TR1 = 1; // TR1: timer 1 打开
EA = 1; //打开总中断
ES = 1; //打开串口中断
}
单独再LCD上进行字符显示,可以显示吗?
这一段可以显示,加入带注释的代码后单独拿出一段都无法显示。
#include <reg52.h>
#include <intrins.h>
#include "delay.h"
#include "drv_1602.h"
#include "drv_dht22.h"
#include "drv_pcf8591.h"
#include "drv_key.h"
#include "bp_sprintf.h"
sbit fan = P3^0;
sbit alarm = P1^6;
sbit led_yellow = P1^7;
unsigned char str[20];
unsigned char key_value;
dht22_data_t dht_data;
unsigned char ad_value_1;
unsigned char ad_value_2;
unsigned char temp_thres = 50;
unsigned char humi_thres = 50;
void thres_set(void)
{
unsigned char key_value;
lcd_clear();
lcd_disp_string(0, 0, "Threshold Set");
while(1)
{
key_value = key_check();
if(key_value & 0x02 && temp_thres < 100)
temp_thres++;
else if(key_value & 0x04 && temp_thres > 0)
temp_thres--;
else if(key_value & 0x01)
break;
bp_sprintf(str, "TEMP THRES:%b02u%cC", temp_thres, 0xdf);
lcd_disp_string(0, 1, str);
}
lcd_clear();
lcd_disp_string(0, 0, "Threshold Set");
while(1)
{
key_value = key_check();
if(key_value & 0x02 && humi_thres < 100)
humi_thres++;
else if(key_value & 0x04 && humi_thres > 0)
humi_thres--;
else if(key_value & 0x01)
break;
bp_sprintf(str, "HUMI THRES:%b02u%cC", humi_thres, 0xdf);
lcd_disp_string(0, 1, str);
}
lcd_clear();
}
void main(void)
{
lcd_init();
lcd_clear();
while(1)
{
ad_value_1 = pcf8591_get_data(0);
ad_value_2 = pcf8591_get_data(1);
dht22_get_data(&dht_data);
key_value = key_check();
bp_sprintf(str, "T:%4.1f%cC", dht_data.temp, 0xdf);
lcd_disp_string(0, 0, str);
bp_sprintf(str, "H:%4.1f%c", dht_data.humi, '%');
lcd_disp_string(0, 1, str);
bp_sprintf(str, "P:%b03u%c", (unsigned char)((unsigned int)ad_value_1 * 100 / 255), '%');
lcd_disp_string(9, 0, str);
if(key_value & 0x01)
{
thres_set();
}
if(dht_data.temp > temp_thres - 10)
fan = 0;
else
fan = 1;
if(dht_data.temp > temp_thres || (ad_value_1 < 2 && ad_value_2 > 100) || (dht_data.humi > humi_thres))
alarm = 0;
else
alarm = 1;
if(ad_value_1 < 2 && ad_value_2 > 100)
lcd_disp_string(10, 1, "S-C");
else
lcd_disp_string(10, 1, " ");
if(ad_value_1 < 25)
led_yellow = 0;
else
led_yellow = 1;
}
}
struct
{
int a;
char b;
short c;
} record;
record.a = 273;
若record变量的首地址为0xC008,则地址0xC008的内容是0X ( ) (只填写2个阿拉伯数字)
答案 11