|
- /*lcd12864只需三根线与单片机相连就可以实现串行通讯,lcd12864自带ASCII码库 以下是程序代码*/
- #include"reg52.h"
- #include"intrins.h"
- #define uint unsigned int
- #define uchar unsigned char
- #define x1 0x80
- #define x2 0x88
- #define y 0x80
- #define comm 0//0代表写指令
- #define dat 1//1代表写数据
- sbit LCD_cs =P2^7;
- sbit LCD_std =P2^6;
- sbit LCD_sclk =P2^5;
- void wr_lcd(uchar dat_comm,uchar content);
- void delay (uint us);
- void lcd_char(uchar x0,uchar y0,uchar k,uchar *chn);
- void init_lcd(void);
- void lcd_string(uchar x0,uchar y0,uchar k,uchar *chn);
- //------------------初始化 -----------------
- void init_lcd(void)
- {
- //rst=1;
- wr_lcd(comm,0x30);//30---基本指令动作
- wr_lcd(comm,0x01);//清屏,地址指针指向 00H
- wr_lcd(comm,0x06);//光标的移动方向
- wr_lcd(comm,0x0c);//开显示,关游标
- }
- void wr_lcd(uchar dat_comm,uchar content)
- {
- uchar a,i,j;
- delay (500);//此延时是必须的,以防在连续操作中 LCD 内部动作没有完成, 不响应后面的指令或数据
- a=content;
- LCD_cs=1;
- _nop_();
- LCD_sclk=0;
- _nop_();
- LCD_std=1;
- _nop_();
- for(i=0; i<5; i++)
- {
- LCD_sclk=1;
- _nop_();
- _nop_();
- _nop_();
- LCD_sclk=0;
- }
- LCD_std=0;
- _nop_();
- LCD_sclk=1;
- _nop_();
- _nop_();
- _nop_();
- LCD_sclk=0;
- _nop_();
- if(dat_comm) //如果 dat_comm为高则表示送的是显示数据
- LCD_std=1; //data
- else //如果 dat_comm为低则表示送的是控制指令
- LCD_std=0; //command
- LCD_sclk=1;
- _nop_();
- _nop_();
- _nop_();
- LCD_sclk=0;
- LCD_std=0;
- _nop_();
- LCD_sclk=1;
- _nop_();
- _nop_();
- _nop_();
- LCD_sclk=0;
- for(j=0; j<2; j++)
- {
- for(i=0; i<4; i++)
- {
- a=a<<1;
- LCD_std=CY;
- LCD_sclk=1;
- _nop_();
- _nop_();
- _nop_();
- LCD_sclk=0;
- }
- LCD_std=0;
- for(i=0; i<4; i++) {
- LCD_sclk=1;
- _nop_();
- _nop_();
- _nop_();
- LCD_sclk=0;
- }
- }
- }
- void delay (uint us)//delaytime
- {
- while(us--);
- }
- void delay1(uint ms)
- {
- uint i,j;
- for(i=0; i<ms; i++)
- for(j=0; j<15; j++)
- delay(1);
- }
- //-------在任意位置显示一串汉字或字符 ------
- //其中 一个汉字算两个个字节
- //X0为行, Y0为列, chn 为所要显示的汉字串 ,k 为字符个数
- void lcd_char(uchar x0,uchar y0,uchar k,uchar *chn)
- {
- uchar adr,i;
- switch(x0)
- {
- case 1:
- adr =0x80+y0;
- break; //在第 1行 y 列显示
- case 2:
- adr =0x90+y0;
- break; //在第 2行 y 列显示
- case 3:
- adr =0x88+y0;
- break; //在第 3行 y 列显示
- case 4:
- adr =0x98+y0;
- break; //在第 4行 y 列显示
- default:
- ;
- }
- wr_lcd(comm,adr);
- for(i=0; i<k; i++)
- wr_lcd(dat,chn[i]);
- }
- //------------------主程序 --------------------
- void main ()
- {
- uchar chn[3]= {0x32,0x31,0x33};
- // uchar chn[6]= {32,50,32,54,32,56};
- // uchar b[]= {"欢迎使用本程序 "};
- //
- // uchar b1[]= {"啦啦啦啦 "};
- // char b2[]= {"欢迎你 "};
- init_lcd();
- while (1)
- {
- // lcd_char(1,0,14,b);//显示汉字
- lcd_char(2,0,3,chn);//显示字符串
- // lcd_char(3,0,12,b1);
- // lcd_char(4,0,6,b2);
- delay1(8000);//此处延时是为了防止送显示过快,屏会闪动
- }
- }
复制代码
|
评分
-
查看全部评分
|