|
#include<reg51.h>
#include<intrins.h>
typedef unsigned char U8; /* defined for unsigned 8-bits integer variable 無符號8位整型變量 */
typedef signed char S8; /* defined for signed 8-bits integer variable 有符號8位整型變量 */
typedef unsigned int U16; /* defined for unsigned 16-bits integer variable 無符號16位整型變量 */
typedef signed int S16; /* defined for signed 16-bits integer variable 有符號16位整型變量 */
typedef unsigned long U32; /* defined for unsigned 32-bits integer variable 無符號32位整型變量 */
typedef signed long S32; /* defined for signed 32-bits integer variable 有符號32位整型變量 */
typedef float F32; /* single precision floating point variable (32bits) 單精度浮點數(shù)(32位長度) */
typedef double F64; /* double precision floating point variable (64bits) 雙精度浮點數(shù)(64位長度) */
sbit LCD_RS = P0^7;
sbit LCD_RW = P0^6;
sbit LCD_EN = P0^5;
sbit LED = P1^0;
#define Data P2
#define Data_0_time 4
//獨立按鍵接口 可自行修改
/*****************
函數(shù)名稱:delay();
函數(shù)功能:粗略延時
******************/
void delay1(U16 i)
{
U16 j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void DelayUs2x(U8 t)
{
while(--t);
}
void DelayMs(U8 t)
{
while(t--)
{
//大致延時1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
void Delay(U16 j)
{
U8 i;
for(;j>0;j--)
{
for(i=0;i<27;i++);
}
}
void Delay_10us(void)
{
U8 i;
i--;
i--;
i--;
i--;
i--;
i--;
}
/*****************
函數(shù)名稱:bit lcd_busy()
函數(shù)功能:檢查LCD忙狀態(tài)
輸入值 :無
返回值 :
******************/
bit lcd_busy()
{
bit result;
LCD_RS = 0;
LCD_EN = 1;
delay1(1);
result = (bit)(Data&0x80);
LCD_EN = 0;
return(result);
}
/*****************
函數(shù)名稱:void LCD1602_WriteCmd(unsigned char cmd)
函數(shù)功能:寫指令數(shù)據(jù)到LCD
輸入值 :指令
返回值 :無
******************/
void LCD1602_WriteCmd(unsigned char cmd)
{
LCD_RS = 0;
LCD_EN = 0;
delay1(1);
Data = cmd;
delay1(5);
LCD_EN = 1;
delay1(5);
LCD_EN = 0;
delay1(10);
}
/*****************
函數(shù)名稱:void LCD1602_WriteData(unsigned char dat)
函數(shù)功能:寫顯示數(shù)據(jù)到LCD
輸入值 :數(shù)據(jù)
返回值 :無
******************/
void LCD1602_WriteData(U8 dat)
{
LCD_RS = 1;
LCD_EN = 0;
Data = dat;
delay1(1);
LCD_EN = 1;
delay1(1);
LCD_EN = 0;
delay1(10);
}
/*****************
函數(shù)名稱:void lcd_init(void)
函數(shù)功能:LCD1602初始化函數(shù)
輸入值 :無
返回值 :無
******************/
void lcd_init(void)
{
LCD_RW = 0;
delay1(15);
LCD1602_WriteCmd(0x01); //清除LCD的顯示內(nèi)容
LCD1602_WriteCmd(0x38); //16*2顯示,5*7點陣,8位數(shù)據(jù)
delay1(5);
LCD1602_WriteCmd(0x38);
delay1(5);
LCD1602_WriteCmd(0x38);
delay1(5);
LCD1602_WriteCmd(0x0c); //開顯示,不顯示光標(biāo)
delay1(5);
LCD1602_WriteCmd(0x01); //清除LCD的顯示內(nèi)容
delay1(5);
}
/*****************
函數(shù)名稱:void LCD1602_WriteChar(unsigned char x,unsigned char y,unsigned char Dat)
函數(shù)功能:寫單個字符到指定坐標(biāo)
輸入值 :x:行 (可取0~1) y:列 (可取0~15) Dat:為要顯示的數(shù)據(jù)
返回值 :無
******************/
void LCD1602_WriteChar(U8 x,U8 y,U8 Dat)
{
if(x==0)
{
LCD1602_WriteCmd(0x80+y);
LCD1602_WriteData(Dat);
delay1(5);
}
if(x != 0)
{
LCD1602_WriteCmd(0x80+0x40+y);
LCD1602_WriteData(Dat);
delay1(5);
}
}
/*****************
函數(shù)名稱:void LCD1602_WriteChar(unsigned char x,unsigned char y,unsigned char Dat)
函數(shù)功能:寫字符串到指定坐標(biāo) (x,y)為首個字符的顯示位置
輸入值 :x:行 (可取0~1) y:列 (可取0~15) Dat:為要顯示的數(shù)據(jù)
返回值 :無
******************/
void LCD1602_WriteString(U8 x,U8 y,U8 *String)
{
U8 i;
for(i=0;String[i]!='\0';i++)
{
LCD1602_WriteChar(x,y++,String[i]);
}
}
void init(void)
{
TMOD = 0x20; //定時器T1使用工作方式2
TH1 = 253; // 設(shè)置初值
TL1 = 253;
TR1 = 1; // 開始計時
SCON = 0x50; //工作方式1,波特率9600bps,允許接收
ES = 1;
EA = 1; // 打開所以中斷
}
void main(void)
{
int a;
lcd_init();
init();
while (1)
{
LCD1602_WriteString(0,0,"GangBanHou:");
LCD1602_WriteString(1,3,"mm");
LCD1602_WriteChar(1,0,'f');
a++;
DelayMs(500);
LCD1602_WriteChar(1,1,a+'0');
}
}
|
-
-
1602顯示程序.zip
2016-4-28 11:35 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
39.58 KB, 下載次數(shù): 6, 下載積分: 黑幣 -5
1602顯示程序
評分
-
查看全部評分
|