要求:在1602上顯示某一變量的數值,正常范圍,并判斷當前狀態
如:t:50 60
comfort 40
表示當前溫度為50,正常溫度為40-60,當前溫度正常
紅綠字為自己添加的代碼,代碼可通過keil編譯,但在單片機上無法正常顯示。如果將綠字部分注釋掉單片機就可以正常運行實現功能
**********************************************************************************
* 【函數功能】: DA-AD試驗
* 【使用說明】: 單片機:STC89C52
串口波特率:9600
串口一共發送4個字節
1 光敏電阻
2 熱敏電阻
3 懸空
4 可調0-5V電壓(藍色電位器調節)
**********************************************************************************/
/********************************************************************************************/
#include<reg52.h> //包含單片機寄存器的頭文件
#include <I2C.H>
#define uchar unsigned char
#define PCF8591 0x90 //PCF8591 地址
/********IO引腳定義***********************************************************/
sbit LCD_RS=P1^0;//定義引腳
sbit LCD_RW=P1^1;
sbit LCD_E=P1^2;
/********宏定義***********************************************************/
#define LCD_Data P0
#define Busy 0x80 //用于檢測LCD狀態字中的Busy標識
/********數據定義*************************************************************/
unsigned char code uctech[] = {"Thank"};
unsigned char code net[] = {"you!"};
unsigned char code clear[] = {" "};
unsigned char code net1[] = {"t:"};
unsigned char code net2[] = {"l:"};
unsigned char code net3[] = {"v:"};
unsigned char t;
//溫度
uchar code limit[]={"60"};
uchar code limit1[]={"40"};
unsigned char code condition[]={"too hot"};
uchar code condition1[]={"comfort"};
uchar code condition2[]={"too cold"};
//光
uchar code limit2[]={"100"};
uchar code limit3[]={"60"};
unsigned char code condition3[]={"too bright"};
uchar code condition4[]={"comfort"};
uchar code condition5[]={"too dim"};
//電壓
uchar code limit4[]={"200"};
uchar code limit5[]={"100"};
unsigned char code condition6[]={"too big"};
uchar code condition7[]={"comfort"};
uchar code condition8[]={"too small"};
// 變量定義
unsigned char AD_CHANNEL;
unsigned long xdata LedOut[8];
unsigned int D[32];
sbit P2_0 = P2^0;
sbit P2_1 = P2^1;
sbit P2_2 = P2^2;
sbit P2_3 = P2^3;
unsigned char data a;
//按鈕
sbit K1=P3^7;
sbit K2=P3^6;
sbit K3=P3^5;
sbit K4=P3^4;
//unsigned char date ;
/*uchar code table[]={0xC0,0xF9,0xA4,0xB0,
0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,
0xC6,0xA1,0x86,0x8E}; */
// 函數申明
//void delay(uchar i);
bit DACconversion(unsigned char sla,unsigned char c, unsigned char Val);
bit ISendByte(unsigned char sla,unsigned char c);
unsigned char IRcvByte(unsigned char sla);
void Initial_com(void);
void WriteDataLCD(unsigned char WDLCD); //寫數據
void WriteCommandLCD(unsigned char WCLCD,BuysC); //寫命令
unsigned char ReadDataLCD(void); //讀數據
unsigned char ReadStatusLCD(void); //讀狀態
void LCDInit(void); //初始化
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData); //相應坐標顯示字節內容
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData); //相應坐標開始顯示一串內容
void Delay5Ms(void); //延時
void Delay400Ms(void); //延時
//******************************************************************/
/*void delay_1ms()
{
unsigned char temp=249;
while(--temp);
temp=249;
while(--temp);
} */
main()
{
Initial_com();
Delay400Ms(); //啟動等待,等LCD講入工作狀態
LCDInit(); //初始化
Delay5Ms(); //延時片刻(可不要)
DisplayListChar(0, 1, uctech);
ReadDataLCD();
while(1)
{
/********以下AD-DA處理*************/
switch(AD_CHANNEL)
{
case 0: ISendByte(PCF8591,0x41);
D[0]=IRcvByte(PCF8591); //ADC0 模數轉換1 電壓
break;
case 1: ISendByte(PCF8591,0x42);
D[1]=IRcvByte(PCF8591); //ADC1 模數轉換2 光
break;
case 2: ISendByte(PCF8591,0x43);
D[2]=IRcvByte(PCF8591); //ADC2 模數轉換3 溫度
break;
case 3: ISendByte(PCF8591,0x40);
D[3]=IRcvByte(PCF8591); //ADC3 模數轉換4 可調0-5v
break;
case 4: DACconversion(PCF8591,0x40, D[4]); //DAC 數模轉換
break;
}
D[4]=D[3]; //把模擬輸入采樣的信號 通過數模轉換輸出
if(K1==0)
{
a=1;
DisplayListChar(0, 1, clear);
DisplayListChar(0, 0, clear);
DisplayListChar(14, 0, limit);
DisplayListChar(14, 1, limit1);
}
if(K2==0)
{
a=2; //0
DisplayListChar(0, 1, clear);
DisplayListChar(0, 0, clear);
DisplayListChar(14, 0, limit2);
DisplayListChar(14, 1, limit3);
}
if(K3==0)
{
a=3; //1
DisplayListChar(0, 1, clear);
DisplayListChar(0, 0, clear);
DisplayListChar(14, 0, limit4);
DisplayListChar(14, 1, limit5);;
}
if(K4==0)
{
a=4; //1
DisplayListChar(0, 1, clear);
DisplayListChar(0, 0, clear);
}
if(a==1)
{
DisplayListChar(0, 0, net1);
DisplayOneChar(2, 0,D[2]/100+0x30);
DisplayOneChar(3, 0,D[2]%100/10+0x30);
DisplayOneChar(4, 0,D[2]%10+0x30);
if(D[2]>60)
{
DisplayListChar(0, 1, condition);
}
if(D[2]<40)
{
DisplayListChar(0, 1, condition2);
}
else
{
DisplayListChar(0, 1, condition1);
}
}
if(a==2)
{
DisplayListChar(0, 0, net2);
DisplayOneChar(2, 0,D[1]/100+0x30);
DisplayOneChar(3, 0,D[1]%100/10+0x30);
DisplayOneChar(4, 0,D[1]%10+0x30);
if(D[1]>100)
{
DisplayListChar(0, 1, condition3);
}
if(D[1]<60)
{
DisplayListChar(0, 1, condition5);
}
else
{
DisplayListChar(0, 1, condition4);
}
}
if(a==3)
{
DisplayListChar(0, 0, net3);
DisplayOneChar(2, 0,D[0]/100+0x30);
DisplayOneChar(3, 0,D[0]%100/10+0x30);
DisplayOneChar(4, 0,D[0]%10+0x30);
if(D[0]>200)
{
DisplayListChar(0, 1, condition6);
}
if(D[0]<100)
{
DisplayListChar(0, 1, condition8);
}
else
{
DisplayListChar(0, 1, condition7);
}
}
if(a==4)
{
DisplayListChar(0, 0, net1);
DisplayOneChar(2, 0,D[2]/100+0x30);
DisplayOneChar(3, 0,D[2]%100/10+0x30);
DisplayOneChar(4, 0,D[2]%10+0x30);
DisplayListChar(0, 1, net2);
DisplayOneChar(2, 1,D[1]/100+0x30);
DisplayOneChar(3, 1,D[1]%100/10+0x30);
DisplayOneChar(4, 1,D[1]%10+0x30);
DisplayListChar(6, 0, net3);
DisplayOneChar(8, 0,D[0]/100+0x30);
DisplayOneChar(9, 0,D[0]%100/10+0x30);
DisplayOneChar(10, 0,D[0]%10+0x30);
}
if(++AD_CHANNEL>4) AD_CHANNEL=0;
/********以下將AD的值通過串口發送出去*************/
/* delay(200);
SBUF=D[0];
delay(200);
SBUF=D[1];
delay(200);
SBUF=D[2];
delay(200);
SBUF=D[3];
delay(200);
if(RI)
{
date=SBUF; //單片機接受
SBUF=date; //單片機發送
RI=0;
}*/
}
}
/*void delay(uchar i)
{
uchar j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}*/
/*******************************************************************
DAC 變換, 轉化函數
*******************************************************************/
bit DACconversion(unsigned char sla,unsigned char c, unsigned char Val)
{
Start_I2c(); //啟動總線
SendByte(sla); //發送器件地址
if(ack==0)return(0);
SendByte(c); //發送控制字節
if(ack==0)return(0);
SendByte(Val); //發送DAC的數值
if(ack==0)return(0);
Stop_I2c(); //結束總線
return(1);
}
/*******************************************************************
ADC發送字節[命令]數據函數
*******************************************************************/
bit ISendByte(unsigned char sla,unsigned char c)
{
Start_I2c(); //啟動總線
SendByte(sla); //發送器件地址
if(ack==0)return(0);
SendByte(c); //發送數據
if(ack==0)return(0);
Stop_I2c(); //結束總線
return(1);
}
/*******************************************************************
ADC讀字節數據函數
*******************************************************************/
unsigned char IRcvByte(unsigned char sla)
{ unsigned char c;
Start_I2c(); //啟動總線
SendByte(sla+1); //發送器件地址
if(ack==0)return(0);
c=RcvByte(); //讀取數據0
Ack_I2c(1); //發送非就答位
Stop_I2c(); //結束總線
return(c);
}
//*****串口初始化函數***********
//******************************
void Initial_com(void)
{
EA=1; //開總中斷
ES=1; //允許串口中斷
ET1=1; //允許定時器T1的中斷
TMOD=0x20; //定時器T1,在方式2中斷產生波特率
PCON=0x00; //SMOD=0
SCON=0x50; // 方式1 由定時器控制
TH1=0xfd; //波特率設置為9600
TL1=0xfd;
TR1=1; //開定時器T1運行控制位
}
/***********寫數據********************************************************/
void WriteDataLCD(unsigned char WDLCD)
{
ReadStatusLCD(); //檢測忙
LCD_Data = WDLCD;
LCD_RS = 1;
LCD_RW = 0;
LCD_E = 0; //若晶振速度太高可以在這后加小的延時
LCD_E = 0; //延時
LCD_E = 1;
}
/***********寫指令********************************************************/
void WriteCommandLCD(unsigned char WCLCD,BuysC) //BuysC為0時忽略忙檢測
{
if (BuysC) ReadStatusLCD(); //根據需要檢測忙
LCD_Data = WCLCD;
LCD_RS = 0;
LCD_RW = 0;
LCD_E = 0;
LCD_E = 0;
LCD_E = 1;
}
/***********讀數據********************************************************/
unsigned char ReadDataLCD(void)
{
LCD_RS = 1;
LCD_RW = 1;
LCD_E = 0;
LCD_E = 0;
LCD_E = 1;
return(LCD_Data);
}
/***********讀狀態*******************************************************/
unsigned char ReadStatusLCD(void)
{
LCD_Data = 0xFF;
LCD_RS = 0;
LCD_RW = 1;
LCD_E = 0;
LCD_E = 0;
LCD_E = 1;
while (LCD_Data & Busy); //檢測忙信號
return(LCD_Data);
}
/***********初始化********************************************************/
void LCDInit(void)
{
LCD_Data = 0;
WriteCommandLCD(0x38,0); //三次模式設置,不檢測忙信號
Delay5Ms();
WriteCommandLCD(0x38,0);
Delay5Ms();
WriteCommandLCD(0x38,0);
Delay5Ms();
WriteCommandLCD(0x38,1); //顯示模式設置,開始要求每次檢測忙信號
WriteCommandLCD(0x08,1); //關閉顯示
WriteCommandLCD(0x01,1); //顯示清屏
WriteCommandLCD(0x06,1); //顯示光標移動設置
WriteCommandLCD(0x0C,1); //顯示開及光標設置
}
/***********按指定位置顯示一個字符*******************************************/
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
if (Y) X |= 0x40; //當要顯示第二行時地址碼+0x40;
X |= 0x80; //算出指令碼
WriteCommandLCD(X, 0); //這里不檢測忙信號,發送地址碼
WriteDataLCD(DData);
}
/***********按指定位置顯示一串字符*****************************************/
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
unsigned char ListLength;
ListLength = 0;
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
while (DData[ListLength]>=0x20){ //若到達字串尾則退出
if (X <= 0xF){ //X坐標應小于0xF
DisplayOneChar(X, Y, DData[ListLength]); //顯示單個字符
ListLength++;
X++;
}
}
}
/***********短延時********************************************************/
void Delay5Ms(void)
{
unsigned int TempCyc = 5552;
while(TempCyc--);
}
/***********長延時********************************************************/
void Delay400Ms(void)
{
unsigned char TempCycA = 5;
unsigned int TempCycB;
while(TempCycA--){
TempCycB=7269;
while(TempCycB--);
}
}
|