求个51单片机控制KNY12864M液晶屏的简单程序

2024-12-28 00:05:56
推荐回答(3个)
回答1:

#include
#include
#define uint unsigned int
#define uchar unsigned char
#define Nop() _nop_()

/*引脚端口定义*/
sbit LCD12864_RS_PORT = P1^7; /*LCD12864引脚定义*/
sbit LCD12864_RW_PORT = P1^6;
sbit LCD12864_E_PORT = P2^3;
#define LCD12864_DA_PORT P0

sbit led_en_port = P2^5; /*发光二极管寄存器LE引脚*/
sbit sled_en_port = P3^6; /*数码管寄存器LE引脚*/

uchar hanzi_buff1[]={" 液晶显示 "};

//////////////////以下是LCD12864驱动程序////////////////
void LCD12864_WaitIdle()
//LCD12864 忙 信号检测
{
LCD12864_DA_PORT = 0xff;
LCD12864_RS_PORT = 0;
LCD12864_RW_PORT = 1;
LCD12864_E_PORT = 1;
while((LCD12864_DA_PORT&0x80)==1); /*等待BF 不为1*/
LCD12864_E_PORT = 0;
}

void LCD12864_COM_Write( uchar com_da)
/* 检测忙信号写入命令字
com_da 为待写入的命令字*/
{
LCD12864_WaitIdle();
LCD12864_RS_PORT = 0;
LCD12864_RW_PORT = 0;
LCD12864_DA_PORT = com_da;
LCD12864_E_PORT = 1;
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
LCD12864_E_PORT = 0;
}

void LCD12864_NoWaitIdle_COM_Write(uchar com_da)
/* 不检测忙信号写入命令字
com_da 为待写入的命令字*/
{
LCD12864_RS_PORT = 0;
LCD12864_RW_PORT = 0;
LCD12864_DA_PORT = com_da;
LCD12864_E_PORT = 1;
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
LCD12864_E_PORT = 0;
}

void LCD12864_Data_Write(uchar da)
/* 数据写入
da 为待写入的8位数据*/
{
LCD12864_WaitIdle(); /*检测忙信号*/
LCD12864_RS_PORT = 1;
LCD12864_RW_PORT = 0;
LCD12864_DA_PORT = da;
LCD12864_E_PORT = 1;
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
LCD12864_E_PORT = 0;
}

void lcd_delay_ms(uchar x)

{
uchar j;
while(x--){
for(j=0;j<125;j++)
{;}
}
}

void LCD12864_Reset()
/*LCD12864初始化*/
{
lcd_delay_ms(100); /*适当延时待LCD自动复位完成*/
LCD12864_NoWaitIdle_COM_Write(0x30); /*使用8位并口通讯*/
lcd_delay_ms(10);
LCD12864_NoWaitIdle_COM_Write(0x30); /*使用8位并口通讯*/
lcd_delay_ms(10);
LCD12864_NoWaitIdle_COM_Write(0x0c); /*显示开及光标设置*/
lcd_delay_ms(10);
LCD12864_NoWaitIdle_COM_Write(0x01); /*显示清屏*/
lcd_delay_ms(30);
LCD12864_NoWaitIdle_COM_Write(0x06); /*DDRAM的地址计数器(AC)加1*/
lcd_delay_ms(30);
}

void LCD12864_HANZI_WRITE(uchar xpos,uchar ypos,uchar daH,uchar daL)
//ST7920 汉字字符写入
//参数说明: xpos 待写入的X位置
//ypos 待写入的Y位置
//daH 待写入的汉字的高八位 daL待写入的汉字的低八位
{
uchar xy_pos;
if((xpos>=8)||(ypos>=4) ) return; /*X位置超出显示范围退出*/
if(ypos==0) xy_pos = 0x80 + xpos;
else if(ypos==1) xy_pos = 0x90 + xpos; /*计算转换地址*/
else if(ypos==2) xy_pos = 0x88 + xpos;
else if(ypos==3) xy_pos = 0x98 + xpos;
LCD12864_COM_Write(xy_pos); /*写地址*/
lcd_delay_ms(1);
LCD12864_Data_Write(daH); /*写高八位数据*/
lcd_delay_ms(1);
LCD12864_Data_Write(daL); /*写低八位数据*/
lcd_delay_ms(1);
}

//////////////////以上是LCD12864驱动程序////////////////

void main()
{
uchar i;
LCD12864_Reset(); /*LCD初始化*/
LCD12864_DA_PORT = 0xff; /*释放P0端口*/
led_en_port = 0;/*关闭发光二极管显示*/
sled_en_port = 0;/*关闭数码管显示*/
while(1)
{
for (i=0;i<6;i++)
LCD12864_HANZI_WRITE(i,0,hanzi_buff1[i*2],hanzi_buff1[i*2+1]);
}
}

回答2:

/*带字库12864*/

#include
#define uchar unsigned char
#define uint unsigned int

#define LCD12864_DA_PORT P0 /*引脚端口定义*/

sbit LCD12864_RS=P2^0; /*LCD12864引脚定义*/
sbit LCD12864_RW=P2^1;
sbit LCD12864_E=P2^2;
sbit LCD12864_PSB=P2^3;

uchar code RD2[16]=" ";
uchar code RD3[16]=" ";

void delay_50us(uint t)
{
uint j;
for(;t>0;t--)
for (j=19;j>0;j--);
}

void delay_50ms(uint t)
{
uint j;
for(;t>0;t--)
for (j=6245;j>0;j--);
}

////////////////////////////////////////////// 液晶显示函数
void write_12864com(uchar com) // 液晶地址
{
LCD12864_RW=0;
LCD12864_RS=0;
delay_50us(1);
P0=com;
LCD12864_E=1;
delay_50us(10);
LCD12864_E=0;
delay_50us(2);
}
void write_12864dat(uchar dat) // 液晶数据
{
LCD12864_RW=0;
LCD12864_RS=1;
delay_50us(1);
P0=dat;
LCD12864_E=1;
delay_50us(10);
LCD12864_E=0;
delay_50us(2);
}
void initinal(void) // 液晶初始化
{
delay_50ms(2);
write_12864com(0x30);
delay_50us(4);
write_12864com(0x30);
delay_50us(4);
write_12864com(0x0f);
delay_50us(4);
write_12864com(0x01);
delay_50us(240);
write_12864com(0x06);
delay_50us(10);
}
void display_12864()
{
uchar i;
delay_50us(10);
write_12864com(0x0c); //关闭光标,开显示

write_12864com(0x90);
for(i=0;i<16;i++)
{
write_12864dat(RD2[i]); delay_50us(1);
}
delay_50us(10);
write_12864com(0x88);
for(i=0;i<16;i++)
{
write_12864dat(RD3[i]); delay_50us(1);
}
//delay(2000);

delay_50us(4);
write_12864com(0x01); //清屏
}

void main()
{
initinal();
while(1)
{
display_12864();
}
}

回答3:

你到OURAVR论坛里去找,哪里的资源很多,并且很多实际的高手,有问题他们会认真帮助你。到百度来找效果不大。