|
5#
發(fā)表于 2022-7-16 08:02:24
|
只看該作者
肯定可以的:比如
程序動態(tài)顯示字符 顯示光標(biāo)和光標(biāo)閃爍打開效果
------------------------------------------------*/
#include<reg52.h> //包含頭文件,義
#include<intrins.h>
sbit RS = P2^4; //定義端口
sbit RW = P2^5;
sbit EN = P2^6;
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
#define DataPort P0
/*------------------------------------------------
uS延時(shí)函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值
unsigned char 是定義無符號字符變量,其值的范圍是
0~255 這里使用晶振12M,精確延時(shí)請使用匯編,大致延時(shí)
長度如下 T=tx2+5 uS
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
mS延時(shí)函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值
unsigned char 是定義無符號字符變量,其值的范圍是
0~255 這里使用晶振12M,精確延時(shí)請使用匯編
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延時(shí)1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
/*------------------------------------------------
判忙函數(shù)
------------------------------------------------*/
bit LCD_Check_Busy(void)
{
DataPort= 0xFF;
RS_CLR;
RW_SET;
EN_CLR;
_nop_();
EN_SET;
return (bit)(DataPort & 0x80);
}
/*------------------------------------------------
寫入命令函數(shù)
------------------------------------------------*/
void LCD_Write_Com(unsigned char com)
{
while(LCD_Check_Busy()); //忙則等待
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
_nop_();
EN_CLR;
}
/*------------------------------------------------
寫入數(shù)據(jù)函數(shù)
------------------------------------------------*/
void LCD_Write_Data(unsigned char Data)
{
while(LCD_Check_Busy()); //忙則等待
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
_nop_();
EN_CLR;
}
/*------------------------------------------------
清屏函數(shù)
------------------------------------------------*/
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
DelayMs(5);
}
/*------------------------------------------------
寫入字符串函數(shù)
------------------------------------------------*/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
while (*s)
{
LCD_Write_Data( *s);
s ++;
}
}
/*------------------------------------------------
寫入字符函數(shù)
------------------------------------------------*/
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
LCD_Write_Data( Data);
}
/*------------------------------------------------
初始化函數(shù)
------------------------------------------------*/
void LCD_Init(void)
{
LCD_Write_Com(0x38); /*顯示模式設(shè)置*/
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
LCD_Write_Com(0x08); /*顯示關(guān)閉*/
LCD_Write_Com(0x01); /*顯示清屏*/
LCD_Write_Com(0x06); /*顯示光標(biāo)移動設(shè)置*/
DelayMs(5);
LCD_Write_Com(0x0C); /*顯示開及光標(biāo)設(shè)置*/
}
/*------------------------------------------------
主函數(shù)
------------------------------------------------*/
void main(void)
{
unsigned char i;
unsigned char *pa,*pb;
LCD_Init();
LCD_Write_Com(0x0f);//光標(biāo)開,光標(biāo)閃爍開
while (1)
{
pa = "Welcome to";
pb = "www.doflye.net";
LCD_Clear();
LCD_Write_String(2,0,pa); //顯示2行信息
LCD_Write_String(1,1,pb);
for(i=0;i<8;i++) //延時(shí)2s左右
DelayMs(250);
LCD_Clear(); //清屏顯示其他信息
i = 2;
while (*pa) //循環(huán)輸入方式顯示2行信息
{
LCD_Write_Char(i,0,*pa);
i ++;
pa ++;
DelayMs(250);
}
i=1;
while (*pb)
{
LCD_Write_Char(i,1,*pb);
i ++;
pb ++;
DelayMs(250);
}
DelayMs(250);
}
} |
|