?? fyd12864串行.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 CS = P1^3;
sbit SID = P1^4;
sbit SCLK = P1^5;
sbit KEY= P3^5;
sbit LED= P1^5;
sbit bus= P1^6;
sbit reset=P2^7;
bit flg_over=0; //文章顯示完畢標志位,1為顯示完畢
bit flg_key=1;//按鍵發生標志位,1為按鍵發生
uchar page;//頁碼變量
uchar *p;//顯示文章首地址指針
uchar code word[] =
{"單片機的C 語言應用程序設計(第4 版)本書針對目前最通用的8051單片機和最流行的程序設計語言C 語言,結合KEIL公司8051單片機開發套件;介紹單片機的C 語言應用程序設計。全書共13章,既有單片機的基礎部分;51的基礎部分,本書可以作為高等學院相關專業培訓及全國電子設計競賽的教材。也可以作為從事單片機應用開發的工程技術人員的參考用書!謝謝使用!"};
/****************************延時函數***************************
名稱:delay()
輸入:uchar m
輸出:無
***************************************************************/
{ // 延時子程序
uchar i;
while(ms--)
{
i=50;
while(i--)
{
// _nop_();
// _nop_();
// _nop_();
// _nop_();
_nop_();
}
}
}
/******************************LCD狀態檢測函數**************************************
名稱:lcd_state()
功能:lcd狀態忙碌判斷
參數:P2口為數據口
輸入:無
輸出:返回狀態標志state位
************************************************************************************/
bit lcd_state(void)
{ // 測試LCD忙碌狀態
bit state;
RS = 0;//command
RW = 1;//read
E = 1;
_nop_();
_nop_();
_nop_();
_nop_();
state = (bit)(P0 & 0x80);
E = 0;
return state;
}
/******************************LCD指令寫入函數**************************************
名稱:lcd_w_cmd()
功能:寫指令到LCD數據口P2
參數:P2口為數據口
輸入:待寫指令
輸出:無
************************************************************************************/
void lcd_w_cmd(uchar cmd)
{ // 寫入指令數據到LCD
while(lcd_state());
RS = 0;//command
RW = 0;//write
P0 = cmd;
E = 1;
_nop_();
_nop_();
_nop_();
_nop_();
E = 0;
}
/******************************地址寫入函數**************************************
名稱:lcd_adder()
功能:lcd顯示地址設置
參數:P2口為數據口
輸入:一字節待設置的顯示地址長度0~15
輸出:無
************************************************************************************/
void lcd_adder(uchar adder)
{ //設定顯示位置
lcd_w_cmd(adder);
}
/******************************LCD數據寫入函數**************************************
名稱:lcd_w_date()
功能:寫數據到LCD數據口P2
參數:P2口為數據口
輸入:一字節待寫顯示數據
輸出:無
************************************************************************************/
void lcd_w_date(uchar dat)
{ //寫入字符顯示數據到LCD
while(lcd_state());
RS = 1;//date
RW = 0;//write
P0 = dat;
E = 1;
_nop_();
_nop_();
_nop_();
_nop_();
E = 0;
}
/******************************LCD初始化函數**************************************
名稱:lcd_init()
功能:初始化LCD模塊
參數:初始化指令
輸入:無
輸出:無
************************************************************************************/
void lcd_init(void)
{
EA=0;
bus=0;
delay(1);
bus=1;
delay(1);
reset=0;
_nop_();
_nop_();
_nop_();
_nop_();
reset=1;
_nop_();
page=48; //頁碼起始值設定
p=word; //全局指針變量P為待顯示字符串地址
lcd_w_cmd(0x30); //LCD初始化設定
delay(1);
lcd_w_cmd(0x0f); //
delay(1);
lcd_w_cmd(0x01); //
delay(100);
lcd_w_cmd(0x06); //清除LCD的顯示內容
delay(1);
}
/******************************LCD顯示函數**************************************
名稱:lcd_display()
功能:顯示words1[],words2[]字符串!
參數:P2口為數據口,*word1,*word2為待顯示字符串指針,adder1,adder2為兩行顯示起始地址
說明:word為待顯示字符串首地址;dder為顯示起始地址;str為起始顯示字符的順序位置;len_s為顯示長度
輸入:待顯示的數據的指針,顯示地址
輸出:無
*******************************************************************************/
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++;
}
}
}
/******************************顯示字符長度測量函數*****************************
名稱:words_len()
功能:測試顯示字符串長度!
參數:待測字符串地址為輸入參數
輸入:待測試字符串的指針
輸出:字符串長度
*******************************************************************************/
uchar words_len(uchar *s)
{
return(strlen(s));
}
/******************************顯示一個單一字符函數*****************************
名稱:word_display()
功能:顯示單一一個字符
參數:待測顯示字符地址為輸入參數
輸入:待顯示字符的地址
輸出:無
*******************************************************************************/
word_display(uchar *c,uchar adder_c)
{
lcd_adder(adder_c);
_nop_();
_nop_();
lcd_w_date(*c);
}
/*******************************鍵盤識別函數************************************
名稱:key()
功能:識別鍵盤動作
參數:識別鍵盤動作返回布爾類型值
輸入:無
輸出:BIT類型值
*******************************************************************************/
bit key(void)
{
if(KEY==0)
{
delay(1);
if(KEY==1) return 0;
while(!KEY) _nop_();
LED=!LED;
return 1;
}
else return 0;
}
/******************************顯示控制函數**************************************
名稱:display_ctr()
功能:控制顯示輸出
參數:P2口為數據口,*word1,*word2為待顯示字符串指針,adder1,adder2為兩行顯示起始地址
說明:word為待顯示字符串首地址;dder為顯示起始地址;str為起始顯示字符的順序位置;len_s為顯示長度
輸入:待顯示的數據的指針,顯示地址
輸出:無
*******************************************************************************/
void display_ctr(void)
{
loop:
if(flg_key)
{
lcd_w_cmd(0x01);//清除顯示DDRAM數據
if(flg_over==1){p=word;flg_over=0;page=48;}//顯示完畢從新開始顯示文章
if((page-48) < 9)page++;
else page=48;// 個位循環顯示頁碼
word_display(&page,0x80); //顯示頁碼
lcd_display(p,0x80,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;}
}
}
/*------------------------------------------------------------------------
------------------------------------MAIN函數------------------------------
--------------------------------------------------------------------------*/
main()
{
lcd_init(); // 初始化LCD變量和顯示指令
delay(50);
while(1)
{
display_ctr();//顯示控制
flg_key=key();//鍵盤掃描
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -