?? lcd3.c
字號:
//*******************************************************
//該程序實現用液晶顯示器LCD顯示已定義的字符串中的某一字符
//程序啟發:用查表法把數據送到LCD顯示
//作者:李錫堅
//完成時間:2007.07.24.21:43
//*******************************************************
/********************************************************
與前兩個程序LCD1.C,LCD2.C的不同處:
sbit LCD_RS=P3^4; //定義LCD的RS控制位
sbit LCD_RW=P3^5; //定義LCD的RW控制位
sbit LCD_DISPLAY_START=P1^0; //LCD開始顯示的指示燈
sbit LCD_E=P1^1; //定義LCD的E控制位
寫操作時用:LCD_E=1;
_nop_();
LCD_E=0;
代替
*********************************************************/
//****************************************
#include<reg52.h> //包含常用頭文件
#include<stdio.h>
#include<intrins.h>
#include<absacc.h>
#define uchar unsigned char //定義常用數據類型
int cnt;
void LCD_INIT(void); //LCD的初始化函數
void LCD_DISPLAY_STR(uchar *DATA);//在指定的位置顯示字符串
void LCD_CLR(uchar y); //清除LCD指定的行
void LCD_SEND_COMMAND(uchar COMMAND); //向LCD發送命令
void LCD_SEND_DATA(uchar DATA); //向LCD發送數據
void LCD_WAIT(void); //檢查LCD空閑
uchar LCD_GET_FLAG(void); //檢查LCD狀態
void DELAY(void); //延時
/*定義所要顯示的數據*/
char code DISPLAY[]="asdf ghjk";//{1,2,3,0x1f,0x04,0x1f,0x04,0x04};//"It goes without saying that this picture aims at revealing a current problem: what kind of attitude we will choose when facing difficulties and challenges. In this drawing, a football-player is prepared to kick a ball towards the net, where a goal-keeper keeps guard. However, in the player’s mind appears a scene in which the keeper becomes a giant covering the net completely, while the latter imagines that he turns out to be a dwarf standing below the huge net. Obviously, both of them lack courage and confidence in front of challenges.These two players represent those who often choose to magnify their enemies and dangers, and lose their confidence to fight against them. As a result, what they can achieve in the end is nothing but failure. This sad situation can be best illustrated in the fact that some people lose their chance of success in the entrance examination for the MA program. When preparing for the exam, they often feel depressed thinking that they are never well-prepared. In fact, they will soon realize that it is not as difficult as they thought before. In a word, they suffer from underestimating their abilities.In our life, what we need most is self-insurance and a proper view of challenges before us. Therefore, we should bear in mind that our competitors may not be as terrible as expected, and our painstaking efforts will pay off as long as we arm ourselves with courage and confidence. Only in this way can we overcome any difficulties and challenges.";
/*定義LCD控制字*/
#define LCD_MODE 0x3C /* 接口數據8位,顯示2行,字體為1號 */
#define LCD_NO_FLASH 0x0C /* 屏幕顯示開,無光標 */
#define LCD_HIDE 0x08 /* 屏幕顯示關 */
#define LCD_FLASH 0x0D /* 屏幕顯示開,并打開閃爍光標 */
#define LCD_SHIFT 0x07 /* 模塊數據輸入為增量方式,顯示內容移動 */
#define LCD_NO_SHIFT 0x06 /* 模塊數據輸入為增量方式,顯示光標移動 */
#define LCD_SH 0x14 /* 移動光標及整體顯示 */
#define LCD_LINE1 0x80 /*第一行DDRAM起始地址*/
#define LCD_LINE2 0xc0 /*第二行DDRAM起始地址*/
#define SEND_IN P0 /*XBYTE[0xff00] /*定義LCD的實際地址*/
sbit LCD_RS=P3^6; //定義LCD的RS控制位
sbit LCD_RW=P3^7; //定義LCD的RW控制位
sbit LCD_DISPLAY_START=P1^0; //LCD開始顯示的指示燈
sbit LCD_E=P1^4; //定義LCD的E控制位
int t=0; //中斷計數
//*************************************************
//LCD顯示字符串的主程序
//利用中斷間隔循環顯示
//
//*************************************************
main()
{
LCD_INIT(); //初始化LCD
do
{
LCD_DISPLAY_START=0; //開LCD顯示的指示燈
DELAY();
LCD_DISPLAY_START=1; //滅LCD顯示的指示燈
LCD_DISPLAY_STR(DISPLAY); //顯示字符串
}while(1);
}
//*************************************************
//函數功能:LCD初始化
//輸入變量:無
//輸出變量:無
//調用模塊:LCD_SEND_COMMAND(),LCD_CLR()
//*************************************************
void LCD_INIT(void)
{
LCD_SEND_COMMAND(LCD_MODE); //設置工作方式
LCD_SEND_COMMAND(LCD_NO_FLASH); //設置顯示方式
LCD_SEND_COMMAND(LCD_NO_SHIFT); //設置光標畫面滾動方式
LCD_SEND_COMMAND(LCD_SH); //設置輸入方式
LCD_CLR(1); //清除LCD第一行
LCD_CLR(2); //清除LCD第二行
}
//*************************************************
//函數功能:清除LCD指定行
//輸入變量:y
//輸出變量:無
//調用模塊:LCD_SEND_COMMAND(),LCD_SEND_DATA()
//*************************************************
void LCD_CLR(uchar y)
{
uchar i;
i=0;
if(y==1)
{
LCD_SEND_COMMAND(LCD_LINE1); //發送命令使LCD指向第一行
i=16;
}
if(y==2)
{
LCD_SEND_COMMAND(LCD_LINE2); //發送命令使LCD指向第二行
i=16;
}
if(i!=0)
{
do
{
LCD_SEND_DATA(' '); //讓LCD的相應位置顯示空格
}while(--i!=0);
}
}
//*************************************************
//函數功能:向LCD發送命令
//輸入變量:COMMAND
//輸出變量:無
//調用模塊:LCD_WAIT()
//*************************************************
void LCD_SEND_COMMAND(uchar COMMAND)
{
LCD_WAIT(); //等待空閑
LCD_RS=0; //命令方式
LCD_RW=0; //寫方式
LCD_E=1;
SEND_IN=COMMAND;//寫實際的命令到LCD
LCD_E=0;
}
//*************************************************
//函數功能:向LCD發送數據
//輸入變量:DATA
//輸出變量:無
//調用模塊:LCD_WAIT()
//*************************************************
void LCD_SEND_DATA(uchar DATA)
{
LCD_WAIT(); //等待空閑
LCD_RS=1; //數據方式
LCD_RW=0; //寫方式
LCD_E=1;
SEND_IN=DATA;//寫實際的數據到LCD
LCD_E=0;
}
//*************************************************
//函數功能:等待LCD空閑
//輸入變量:無
//輸出變量:無
//調用模塊:LCD_GET_FLAG()
//*************************************************
void LCD_WAIT(void)
{
uchar i;
i=1000; //定義等待時間,可以防止由于LCD損壞而使程序死循環
do
{
if((LCD_GET_FLAG()&0x80)==0) //判斷BF是否為0
{
break;
}
}while(--i!=0);
}
//*************************************************
//函數功能:檢查LCD狀態
//輸入變量:無
//輸出變量:LCD顯示的當前狀態
//調用模塊:無
//*************************************************
uchar LCD_GET_FLAG(void)
{
SEND_IN=0xff;
LCD_RS=0;
LCD_RW=1;
LCD_E=1;
_nop_();
_nop_();
return(SEND_IN);
}
//*************************************************
//函數功能:檢查LCD狀態
//輸入變量:無
//輸出變量:LCD顯示的當前狀態
//調用模塊:無
//*************************************************
void LCD_DISPLAY_STR(uchar *DATA)
{
int x=1,y=1,i=0;
// do
// {
if(y==1)
{
LCD_CLR(1);
LCD_SEND_COMMAND(LCD_LINE1);//發送顯示位置命令
//for(;x<(17)&&*DATA!='\0';x++)
//for(;x<(17)&&i<8;x++)
//{
LCD_SEND_DATA(DATA[7]); //發送數據
// }
/* if(*DATA!='\0') //判斷是否發送完畢
{
x=1;
y=2; //未完畢轉到第二行顯示
} */
DELAY();
/* }
if(y==2)
{
LCD_CLR(2);
LCD_SEND_COMMAND(LCD_LINE2);
for(;x<(17)&&*DATA!='\0';x++)
{
LCD_SEND_DATA(DATA[i++]);
}
if(*DATA!='\0') //判斷是否發送完畢
{
x=1;
y=1; //未完畢轉到第一行顯示
}
DELAY();
}
}while(*DATA!='\0'); */
}
}
//*************************************************
//函數功能:延時3秒
//輸入變量:無
//輸出變量:無
//調用模塊:無
//*************************************************
void DELAY(void)
{
TMOD=0x02;
TH0=0x06;
TL0=0x06;
TR0=1;
ET0=1;
EA=1;
while(t!=8000); //延時2秒
TR0=0;
ET0=0;
EA=0;
t=0;
}
//*****************************************
//
//定時器0的溢出中斷程序
//
//*****************************************
void timer0(void) interrupt 1 using 0
{
t++;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -