|
根據原理圖在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);
}
|
|