2、其次导入51单片机的头文件以及LCD1602的头文件。创建一个延时函数,可以传入想要具体延时的时长,其内部实现是由一个二重循环,两个循环的次数相乘积。
3、最后最后在主函数中首先执行LCD1602的初始化函数,首先创建一个无限循环,然后添加两个字符串,这里以两行显示百度经验的网址为例,再进行延时以及使用清屏函数进行刷新。
把这个十六进制数据0xDF,送到1602屏上就圆码桥行啦!给你个C程序橘猛模乎,什么都显示出来啦:#include<reg51.h>
// ==================================================================
//定义数据结构的简易写法
#define u8 unsigned char
#define u16 unsigned int
#define u32 unsigned long
#define s8 signed char
#define s16 signed int
#define s32 signed long
sbit rs = P3^4
sbit r_w = P3^6
sbit e = P3^3
idata u8 disp_buff[24]
u8 su_0=0x30
u8 su_1=0x31
u8 su_2=0x32
u8 su_3=0x33
u8 su_4=0x34
u8 su_5=0x35
u8 su_6=0x36
u8 su_7=0x37
u8 su_8=0x38
u8 su_9=0x39
u8 CHA=0x41
u8 CHB=0x42
u8 CHC=0x43
u8 CHD=0x44
u8 CHE=0x45
u8 CHF=0x46
u8 CHG=0x47
u8 CHH=0x48
u8 CHI=0x49
u8 CHJ=0x4A
u8 CHK=0x4B
u8 CHL=0x4C
u8 CHM=0x4D
u8 CHN=0x4E
u8 CHO=0x4F
u8 CHP=0x50
u8 CHQ=0x51
u8 CHR=0x52
u8 CHS=0x53
u8 CHT=0x54
u8 CHU=0x55
u8 CHV=0x56
u8 CHW=0x57
u8 CHX=0x58
u8 CHY=0x59
u8 CHZ=0x5A
u8 ch_a=0x61
u8 ch_b=0x62
u8 ch_c=0x63
u8 ch_d=0x64
u8 ch_e=0x65
u8 ch_f=0x66
u8 ch_g=0x67
u8 ch_h=0x68
u8 ch_i=0x69
u8 ch_j=0x6a
u8 ch_k=0x6b
u8 ch_l=0x6c
u8 ch_m=0x6d
u8 ch_n=0x6e
u8 ch_o=0x6f
u8 ch_p=0x70
u8 ch_q=0x71
u8 ch_r=0x72
u8 ch_s=0x73
u8 ch_t=0x74
u8 ch_u=0x75
u8 ch_v=0x76
u8 ch_w=0x77
u8 ch_x=0x78
u8 ch_y=0x79
u8 ch_z=0x7a
u8 blank=0x20
u8 point=0x2e
u8 uppoint=0xdf
//Here the MCU is STC1F32XE(1T MCU. If you used to be normal MCU,
//you should have changed the delay software. if not to change it,
//It is not work well ) WITH 22.1184MHZ.
//The dalay software as follow:
void delay1us()
{
u8 i=3//when Fosc=22.1184M 1T=0.045 here will delay 1.08us
while(--i)
}
void D20us() // 22.1184 MHz
{ u8 i
for(i=20i>0i--) //20US
delay1us()
}
void D250us(u8 Delay) // 22.1184 MHz
{ u8 i
for(Delay>0Delay--)
{ for(i=250i>0i--) //250US
delay1us()
}
}
void d1ms(u8 Delay) // Delay 1 mS Fm = 22.1184 MHz
{ u8 i
for(Delay>0Delay--)
{ for(i=50i>0i--) //50*20US=1000US=1MS
D20us()
}
}
//---------------------------------
u8 bf_and_ac () /*read busy and adrss counter*/
{
u8 x
r_w=0
rs=0
e=0
d1ms(10) //the delay time have tried OK!
r_w=1
e=1
d1ms(10)
x=P1
e=0
r_w=0
return(0)
}
//-------------------------
void w_code(u8 order) // write cmd
{
u8 x
x=bf_and_ac()
while((x&0x80)==0x80)
{
x=bf_and_ac()
}
r_w=0
e=1
P1=order
e=0
r_w=0
}
//----------------------------------------------
void w_data(u8 su) // write data
{
u8 x
x=bf_and_ac()
while((x&0x80)==0x80)
{
x=bf_and_ac()
}
r_w=0
rs=1
e=1
P1=su
e=0
r_w=0
}
//----------------------------
u8 r_data()
{
u8 x
x=bf_and_ac()
while((x&0x80)==0x80)
{
x=bf_and_ac()
}
r_w=0
rs=1
e=1
x=P1
e=0
r_w=0
return(x)
}
void w_ram(u8 adrss,u8 su,u8 length) //set positiondatadata length
{
u8 i
w_code(adrss)
for(i=0i<lengthi++)
w_data(su)
}
void DisplayOneChar(u8 x,u8 y,u8 su)
{
x=x&0x0f
y=y&0x01
if(y)x=x|0x40
x|=0x80
w_code(x)
w_data(su)
}
void DispLstChar(u8 x,u8 y,u8 slength,u8 *su)
{
u8 ListLength
ListLength=0
x=x&0x0f
y=y&0x01
while(ListLength<slength)
{
DisplayOneChar(x,y,su[ListLength])
ListLength++
x++
}
}
void init()
{
w_code(0x38)
d1ms(10)//dSP1ms(1)
w_code(0x01)
d1ms(10)//dSP1ms(1)
w_code(0x06)
d1ms(10)//dSP1ms(1)
w_code(0x0e)
}
void main()
{
char L0,L1,L2
init()
while(1)
{
disp_buff[0] = CHO
disp_buff[1] = CHK
disp_buff[2] = su_4
disp_buff[3] = su_0
disp_buff[4] = su_0
disp_buff[5] = uppoint
disp_buff[6] = blank
disp_buff[7] = CHS
disp_buff[8] = CHY
disp_buff[9] = CHS
disp_buff[10] = CHT
disp_buff[11] = CHE
disp_buff[12] = CHM
disp_buff[13] = blank
disp_buff[14] = CHO
disp_buff[15] = CHK
DispLstChar(0,0,16,disp_buff)
/*
w_ram(0x80,0XFF,0x01)
w_ram(0x81,0XFF,0x01)
w_ram(0x82,0XFF,0x01)
w_ram(0x83,0XFF,0x01)
w_ram(0x84,0xFF,0x01)
w_ram(0x85,0xFF,0x01)
w_ram(0x86,0xFF,0x01)
w_ram(0x87,0xFF,0x01)
w_ram(0xc0,0XFF,0x01)
w_ram(0xc1,0XFF,0x01)
w_ram(0xc2,0XFF,0x01)
w_ram(0xc3,0XFF,0x01)
w_ram(0xc4,0XFF,0x01)
w_ram(0xc5,0XFF,0x01)
w_ram(0xc6,0XFF,0x01)
w_ram(0xc7,0XFF,0x01)
w_ram(0x88,0XFF,0x01)
w_ram(0x89,0XFF,0x01)
w_ram(0x8A,0XFF,0x01)
w_ram(0x8B,0XFF,0x01)
w_ram(0x8C,0XFF,0x01)
w_ram(0x8D,0XFF,0x01)
w_ram(0x8E,0XFF,0x01)
w_ram(0x8F,0XFF,0x01)
w_ram(0xc8,0XFF,0x01)
w_ram(0xc9,0XFF,0x01)
w_ram(0xcA,0XFF,0x01)
w_ram(0xcB,0XFF,0x01)
w_ram(0xcC,0XFF,0x01)
w_ram(0xcD,0XFF,0x01)
w_ram(0xcE,0XFF,0x01)
w_ram(0xcF,0XFF,0x01)
d1ms(255)
w_ram(0x80,0X20,0x01)
w_ram(0x81,0X20,0x01)
w_ram(0x82,0X20,0x01)
w_ram(0x83,0X20,0x01)
w_ram(0x84,0x20,0x01)
w_ram(0x85,0x20,0x01)
w_ram(0x86,0x20,0x01)
w_ram(0x87,0x20,0x01)
w_ram(0xc0,0X20,0x01)
w_ram(0xc1,0X20,0x01)
w_ram(0xc2,0X20,0x01)
w_ram(0xc3,0X20,0x01)
w_ram(0xc4,0X20,0x01)
w_ram(0xc5,0X20,0x01)
w_ram(0xc6,0X20,0x01)
w_ram(0xc7,0X20,0x01)
w_ram(0x88,0X20,0x01)
w_ram(0x89,0X20,0x01)
w_ram(0x8A,0X20,0x01)
w_ram(0x8B,0X20,0x01)
w_ram(0x8C,0X20,0x01)
w_ram(0x8D,0X20,0x01)
w_ram(0x8E,0X20,0x01)
w_ram(0x8F,0X20,0x01)
w_ram(0xc8,0X20,0x01)
w_ram(0xc9,0X20,0x01)
w_ram(0xcA,0X20,0x01)
w_ram(0xcB,0X20,0x01)
w_ram(0xcC,0X20,0x01)
w_ram(0xcD,0X20,0x01)
w_ram(0xcE,0X20,0x01)
w_ram(0xcF,0X20,0x01)
d1ms(255)
L0=0x20L2=0x80
for(L1=0L1<0x10L1++)
{
w_ram(L2+L1,L0+L1,0x01)
}
L0=0x30L2=0xc0
for(L1=0L1<0x10L1++)
{
w_ram(L2+L1,L0+L1,0x01)
}
d1ms(255)
L0=0x40L2=0x80
for(L1=0L1<0x10L1++)
{
w_ram(L2+L1,L0+L1,0x01)
}
L0=0x50L2=0xc0
for(L1=0L1<0x10L1++)
{
w_ram(L2+L1,L0+L1,0x01)
}
d1ms(255)
L0=0x60L2=0x80
for(L1=0L1<0x10L1++)
{
w_ram(L2+L1,L0+L1,0x01)
}
L0=0x70L2=0xc0
for(L1=0L1<0x10L1++)
{
w_ram(L2+L1,L0+L1,0x01)
}
d1ms(255)
L0=0xa0L2=0x80
for(L1=0L1<0x10L1++)
{
w_ram(L2+L1,L0+L1,0x01)
}
L0=0xb0L2=0xc0
for(L1=0L1<0x10L1++)
{
w_ram(L2+L1,L0+L1,0x01)
}
d1ms(255)
L0=0xc0L2=0x80
for(L1=0L1<0x10L1++)
{
w_ram(L2+L1,L0+L1,0x01)
}
*/
L0=0xd0L2=0xc0
for(L1=0L1<0x10L1++)
{
w_ram(L2+L1,L0+L1,0x01)
}
d1ms(255)
L0=0xd0L2=0xc0
for(L1=0L1<0x10L1++)
{
w_ram(L2+L1,0xdf,0x01)
}
L0=0xe0L2=0x80
for(L1=0L1<0x10L1++)
{
w_ram(L2+L1,L0+L1,0x01)
}
L0=0xf0L2=0xc0
for(L1=0L1<0x10L1++)
{
w_ram(L2+L1,L0+L1,0x01)
}
d1ms(255)
}
}
* 1、LCD第一行显示: www.saxmcu.com* LCD第二行显示: SP-28A--DIY
* 2、先将两行字符写入DDRAM中,然后从右至左逐字移出显毕仿示。
* 3、在指定位置停留1.6秒后,闪烁2次,清屏。
* 4、反复循环上述显示方手察纤式。
*******************************************************************
LCD_RS EQU P2.0
LCD_RW EQU P2.1
LCD_EN EQU P2.2
*******************************************************************
ORG 0000H
AJMP MAIN
ORG 0030H
*******************************************************************
MAIN:
MOV SP,#60H
ACALL LCD_INIT LCD初始化
MAIN1:
ACALL CLR_LCD 清LCD
MOV A,#90H在第一行第17列的位置
ACALL LCD_CMD
MOV DPTR,#LINE1 第一行字符串的起始地址没源送入DPTR。
ACALL WRITE
MOV A,#0D0H 在第二行第17列的位置
ACALL LCD_CMD
MOV DPTR,#LINE2 第二行字符串的起始地址送入DPTR。
ACALL WRITE
MOV R3,#10H 向左移动16格
LOOPA:
MOV A,#18H 字符同时左移一格
ACALL LCD_CMD
MOV R5,#03H 延时375MS
ACALL DELAY125MS
DJNZ R3,LOOPA
ACALL DELAY4延时1.6s
ACALL DELAY4
MOV R4,#02H设置闪烁次数
ACALL FLASH 开始闪烁
AJMP MAIN1
LINE1:
DB "www.saxmcu.com",00H
LINE2:
DB "SP-28A--DIY",00H
***************************************************************
LCD初始化设定子程序
***************************************************************
LCD_INIT:
ACALL DELAY5MS延时15MS
ACALL DELAY5MS等待LCD电源稳定
ACALL DELAY5MS
MOV A,#38H 16*2显示,5*7点阵,8位数据
ACALL LCD_CMD_NC 不进行LCD忙检测
ACALL DELAY5MS
MOV A,#38H 16*2显示,5*7点阵,8位数据
ACALL LCD_CMD_NC 不进行LCD忙检测
ACALL DELAY5MS
MOV A,#38H 16*2显示,5*7点阵,8位数据
ACALL LCD_CMD_NC 不进行LCD忙检测
ACALL DELAY5MS
MOV A,#08H 显示关
ACALL LCD_CMD进行LCD忙检测
MOV A,#01H 清除屏幕
ACALL LCD_CMD进行LCD忙检测
MOV A,#0CH显示开,关光标
ACALL LCD_CMD 进行LCD忙检测
RET
***************************************************************
清LCD子程序
***************************************************************
CLR_LCD:
MOV A,#01H清除屏幕
ACALL LCD_CMD 进行LCD忙检测
RET
***************************************************************
写指令数据到LCD
RS=L,RW=L,D0-D7=指令码,E=高脉冲
***************************************************************
LCD_CMD:
CALL CHECKBUSY
LCD_CMD_NC:
CLR LCD_RS
CLR LCD_RW
MOV P0,A
SETB LCD_EN
NOP
NOP
NOP
NOP
CLR LCD_EN
RET
***************************************************************
写显示数据到LCD
RS=H,RW=L,D0-D7=数据,E=高脉冲
***************************************************************
LCD_WDATA:
ACALL CHECKBUSY
SETB LCD_RS
CLR LCD_RW
MOV P0,A
SETB LCD_EN
NOP
NOP
NOP
NOP
CLR LCD_EN
RET
***************************************************************
检测LCD控制器忙状态
读数据
RS=L,RW=H,E=H,输出:D0-D7=数据
P0.7=1,LCD忙,等待。P0.7=0,LCD闲,可以进行读写 *** 作。
***************************************************************
CHECKBUSY:
PUSH ACC
MOV P0,#0FFH
CLR LCD_RS
SETB LCD_RW
SETB LCD_EN
BUSYLOOP:
NOP
JB P0.7,BUSYLOOP
CLR LCD_EN
POP ACC
RET
***************************************************************
发送字符串子程序
***************************************************************
WRITE:
PUSH ACC
WRITE1:
CLR A
MOVC A,@A+DPTR
JZ WRITE2
INC DPTR
ACALL LCD_WDATA
JMP WRITE1
WRITE2:
POP ACC
RET
***************************************************************
闪烁子程序
***************************************************************
FLASH:
MOV A,#08H关闭显示
ACALL LCD_CMD
ACALL DELAY4
MOV A,#0CH开显示,关闭光标
ACALL LCD_CMD
ACALL DELAY4
DJNZ R4,FLASH
RET
***************************************************************
延时5MS子程序
LCD初始化使用
***************************************************************
DELAY5MS:
MOV R6,#10
DL1:
MOV R7,#249
DL2:
DJNZ R7,DL2
DJNZ R6,DL1
RET
***************************************************************
延时125MS子程序
字符串移动时使用
***************************************************************
DELAY125MS:
DL3:
MOV R6,#250
DL4:
MOV R7,#249
DL5:
DJNZ R7,DL5
DJNZ R6,DL4
DJNZ R5,DL3
RET
***************************************************************
延时800MS子程序
闪烁时使用
***************************************************************
DELAY4:
MOV R5,#40
DL6:
MOV R6,#100
DL7:
MOV R7,#100
DL8:
DJNZ R7,DL8
DJNZ R6,DL7
DJNZ R5,DL6
RET
***************************************************************
END
可以吧?
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)