|
根据原理图在proteus中仿真,但是代码在实物上可以跑,仿真就没有显示了
原理图和proteus图
单片机代码如下,纯小白,希望帮帮
#include <reg52.h>
#define LCD1602_DB P2//lcd相关端口
#define state_wait 0 //系统等待按键信号状态
#define state_temp_choose 1//等待光电传感器信号,选择温度状态
/***lcd相关端口***/
sbit LCD1602_RS = P0^7;
sbit LCD1602_RW = P0^6;
sbit LCD1602_E = P0^5;
sbit key_start = P3^5;//启动按键
sbit temp_up = P2^4;//继电器开关
sbit temp_set = P3^4;//光电传感器读取
void InitLcd1602();//lcd初始化
void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);//lcd显示函数
int readtemp();//读取温度传感器值,实际温度*10
int now_time = 0;
int state = state_wait;//系统状态
int set_temp = 0;//设定温度
char my_char[11] = "temp = 00.0";
char my_time[10] = "time = 000";
int show_flag = 0;//防止lcd刷新过于频繁,1秒刷新一次
void main()
{
InitLcd1602();//lcd初始化
LcdShowStr(1, 0, "System ready");//lcd显示系统准备完成
/***定时器相关配置,定时50ms,方便计算***/
TMOD = 0x01;//启动定时器0
TH0 = (65535-45872)/256;
TL0 = (65535-45872)%256;
EA = 1;
ET0 = 1;
while(state == state_wait)//检测启动按键
{
if(key_start == 0)
{
state = state_temp_choose;
TR0 = 1;//启动定时器
}
}
LcdShowStr(1, 0, " ");//lcd显示系统启动
while(state == state_temp_choose)//检测光电传感器数据
{
if(temp_set == 0)
{
set_temp = readtemp();
TR0 = 0;//停止计时
}
}
set_temp = readtemp();
my_char[7] = (int)(set_temp%1000/100) + '0';//提取温度十位并转换
my_char[8] = (int)(set_temp%100/10) + '0';//提取温度个位并转换
my_char[10] = (int)(set_temp%10) + '0';//显示小数点后一位
LcdShowStr(1, 1, my_char);//lcd显示系统确定温度成功
while(1);
}
void T0_time() interrupt 1 //50ms中断,每1秒(20次进入)刷新时间与温度显示
{
TH0 = (65535-45872)/256;
TL0 = (65535-45872)%256;
show_flag ++;
if(show_flag >= 20)
{
show_flag = 0;
now_time++;
my_time[7] = (int)(now_time/100) + '0';//提取时间百位并转换
my_time[8] = (int)(now_time%100/10) + '0';//提取时间十位并转换
my_time[9] = (int)(now_time%10) + '0';//提取时间个位并转换
LcdShowStr(1, 0, my_time);//lcd显示时间
set_temp = readtemp();
my_char[7] = (int)(set_temp/100) + '0';//提取温度十位并转换
my_char[8] = (int)(set_temp%100/10) + '0';//提取温度个位并转换
my_char[10] = (int)(set_temp%10) + '0';//显示小数点后一位
LcdShowStr(1, 1, my_char);//lcd显示温度
}
}
int readtemp()
{
return 345;
}
void LcdWaitReady()//lcd等待
{
unsigned char sta;
LCD1602_DB = 0xFF;
LCD1602_RS = 0;
LCD1602_RW = 1;
do{
LCD1602_E = 1;
sta = LCD1602_DB;
LCD1602_E = 0;
} while (sta & 0x80);
}
void LcdWriteCmd(unsigned char cmd)//lcd写指令
{
LcdWaitReady();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdWriteDat(unsigned char dat)//lcd写数据
{
LcdWaitReady();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdSetCursor(unsigned char x, unsigned char y)//设定坐标
{
unsigned char addr;
if (y == 0)
addr = 0x00 + x;
else
addr = 0x40 + x;
LcdWriteCmd(addr | 0x80);
}
void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)//字符串显示代码
{
LcdSetCursor(x, y);
while (*str != '\0')
{
LcdWriteDat(*str++);
}
}
void InitLcd1602()//lcd初始化
{
LcdWriteCmd(0x38);
LcdWriteCmd(0x0C);
LcdWriteCmd(0x06);
LcdWriteCmd(0x01);
}
|
|