?? lcd顯示pc-mcu串口通信.c
字號:
/*******************************************************************************
功能:液晶HY12864T2顯示。
說明:顯示兩行字符串,words1[],words2[]。
制作:電子科大huang_api@sina.com
*******************************************************************************/
#include <string.h>
#include <reg52.h>
#include <intrins.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
///////控制引腳RS為P32. RW為P33. E為P34//////////
sbit RS = P3^2;
sbit RW = P3^3;
sbit E = P3^4;
sbit KEY= P3^5;
sbit LED= P1^5;
bit flg_over=0; //文章顯示完畢標(biāo)志位,1為顯示完畢
bit flg_key=1;//按鍵發(fā)生標(biāo)志位,1為按鍵發(fā)生
uchar page;//頁碼變量
uchar *p;//顯示文章首地址指針
uchar data word[10] ={0};
/****************************延時函數(shù)***************************
名稱:delay()
輸入:uchar m
輸出:無
***************************************************************/
void delay(uchar ms)
{ // 延時子程序
uchar i;
while(ms--)
{
i=250;
while(i--)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
/******************************LCD狀態(tài)檢測函數(shù)**************************************
名稱:lcd_state()
功能:lcd狀態(tài)忙碌判斷
參數(shù):P2口為數(shù)據(jù)口
輸入:無
輸出:返回狀態(tài)標(biāo)志state位
************************************************************************************/
bit lcd_state(void)
{ // 測試LCD忙碌狀態(tài)
bit state;
RS = 0;//command
RW = 1;//read
E = 1;
_nop_();
_nop_();
_nop_();
_nop_();
state = (bit)(P2 & 0x80);
E = 0;
return state;
}
/******************************LCD指令寫入函數(shù)**************************************
名稱:lcd_w_cmd()
功能:寫指令到LCD數(shù)據(jù)口P2
參數(shù):P2口為數(shù)據(jù)口
輸入:待寫指令
輸出:無
************************************************************************************/
void lcd_w_cmd(uchar cmd)
{ // 寫入指令數(shù)據(jù)到LCD
while(lcd_state());
RS = 0;//command
RW = 0;//write
P2 = cmd;
_nop_();
_nop_();
E = 1;
_nop_();
_nop_();
_nop_();
_nop_();
E = 0;
}
/******************************LCD狀態(tài)檢測函數(shù)**************************************
名稱:lcd_adder()
功能:lcd顯示地址設(shè)置
參數(shù):P2口為數(shù)據(jù)口
輸入:一字節(jié)待設(shè)置的顯示地址長度0~15
輸出:無
************************************************************************************/
void lcd_adder(uchar adder)
{ //設(shè)定顯示位置
lcd_w_cmd(adder);
}
/******************************LCD數(shù)據(jù)寫入函數(shù)**************************************
名稱:lcd_w_date()
功能:寫數(shù)據(jù)到LCD數(shù)據(jù)口P2
參數(shù):P2口為數(shù)據(jù)口
輸入:一字節(jié)待寫顯示數(shù)據(jù)
輸出:無
************************************************************************************/
void lcd_w_date(uchar dat)
{ //寫入字符顯示數(shù)據(jù)到LCD
while(lcd_state());
RS = 1;//date
RW = 0;//write
P2 = dat;
_nop_();
_nop_();
E = 1;
_nop_();
_nop_();
_nop_();
_nop_();
E = 0;
}
/******************************LCD初始化函數(shù)**************************************
名稱:lcd_init()
功能:初始化LCD模塊
參數(shù):初始化指令
輸入:無
輸出:無
************************************************************************************/
void lcd_init(void)
{
EA=0;
page=48; //頁碼起始值設(shè)定
p=word; //全局指針變量P為待顯示字符串地址
lcd_w_cmd(0x38); //LCD初始化設(shè)定
delay(5);
lcd_w_cmd(0x0c); //
delay(5);
lcd_w_cmd(0x06); //
delay(5);
lcd_w_cmd(0x01); //清除LCD的顯示內(nèi)容
delay(5);
}
/******************************LCD顯示函數(shù)**************************************
名稱:lcd_display()
功能:顯示words1[],words2[]字符串!
參數(shù):P2口為數(shù)據(jù)口,*word1,*word2為待顯示字符串指針,adder1,adder2為兩行顯示起始地址
說明:word為待顯示字符串首地址;dder為顯示起始地址;str為起始顯示字符的順序位置;len_s為顯示長度
輸入:待顯示的數(shù)據(jù)的指針,顯示地址
輸出:無
*******************************************************************************/
lcd_display(uchar *word,uchar adder,uint str,uchar len_s)
{
lcd_adder(adder); //0~7
while(str<len_s)
{
if(word[str]=='\0'){flg_over=1;return 0;}
else // 顯示字符
{ lcd_w_date(word[str]);
str++;
}
}
}
/******************************顯示字符長度測量函數(shù)*****************************
名稱:words_len()
功能:測試顯示字符串長度!
參數(shù):待測字符串地址為輸入?yún)?shù)
輸入:待測試字符串的指針
輸出:字符串長度
*******************************************************************************/
uchar words_len(uchar *s)
{
return(strlen(s));
}
/******************************顯示一個單一字符函數(shù)*****************************
名稱:word_display()
功能:顯示單一一個字符
參數(shù):待測顯示字符地址為輸入?yún)?shù)
輸入:待顯示字符的地址
輸出:無
*******************************************************************************/
word_display(uchar *c,uchar adder_c)
{
lcd_adder(adder_c);
_nop_();
_nop_();
lcd_w_date(*c);
}
/*******************************鍵盤識別函數(shù)************************************
名稱:key()
功能:識別鍵盤動作
參數(shù):識別鍵盤動作返回布爾類型值
輸入:無
輸出:BIT類型值
*******************************************************************************/
bit key(void)
{
if(KEY==0)
{
delay(1);
if(KEY==1) return 0;
while(!KEY) _nop_();
LED=!LED;
return 1;
}
else return 0;
}
/******************************顯示控制函數(shù)**************************************
名稱:display_ctr()
功能:控制顯示輸出
參數(shù):P2口為數(shù)據(jù)口,*word1,*word2為待顯示字符串指針,adder1,adder2為兩行顯示起始地址
說明:word為待顯示字符串首地址;dder為顯示起始地址;str為起始顯示字符的順序位置;len_s為顯示長度
輸入:待顯示的數(shù)據(jù)的指針,顯示地址
輸出:無
*******************************************************************************/
void display_ctr(void)
{
loop:
if(flg_key)
{
lcd_w_cmd(0x01);//清除顯示DDRAM數(shù)據(jù)
if(flg_over==1){p=word;flg_over=0;page=48;}//顯示完畢從新開始顯示文章
if((page-48) < 9)page++;
else page=48;// 個位循環(huán)顯示頁碼
word_display(&page,0x80); //顯示頁碼
lcd_display(p,0x81,0,16); //1行
p+=16;
if(flg_over==1){flg_key=0;goto loop;}
lcd_display(p,0x90,0,16); //2行
p+=16;
if(flg_over==1){flg_key=0;goto loop;}
lcd_display(p,0x88,0,16); //3行
p+=16;
if(flg_over==1){flg_key=0;goto loop;}
lcd_display(p,0x98,0,16); //4行
p+=16;
if(flg_over==1){flg_key=0;goto loop;}
}
}
/*------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/
/******************************系統(tǒng)初始化函數(shù)**************************************
名稱:init_sys()
功能:定時方式2,8位重裝,波特率9600,串口方式3
輸入:無
輸出:無
***********************************************************************************/
void init_sys(void)
{
TMOD=0x20; //定時器1方式2,8位自動重裝,波特率發(fā)生器
TL1=0xfd;
TH1=0xfd; //波特率9600
SCON=0xd4;
PCON=0x00; //波特率未加倍
TR1=1;
}
/****************************串口接收一個字符函數(shù)*****************************
名稱:receive()
輸入:出口參數(shù)uchar *c
輸出:無
*****************************************************************/
void receive(uchar *c)
{
while( RI == 0 ); //判斷接收有沒有完成
RI=0;
*c=SBUF;
// _nop_();
}
/****************************串口接收一串字符函數(shù)*****************************
名稱:rece()
輸入:出口參數(shù)uchar *str
輸出:無
*****************************************************************/
void rec_str(uchar *str)
{
receive(str);
while(*str!=0)
{ receive(str); str++; }
_nop_();
}
/*------------------------------------------------------------------------
------------------------------------MAIN函數(shù)------------------------------
--------------------------------------------------------------------------*/
main()
{
init_sys();
lcd_init(); // 初始化LCD變量和顯示指令
delay(50);
while(1)
{
rec_str(word);
display_ctr();//顯示控制
flg_key=key();//鍵盤掃描
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -