头文件:
- #ifndef __LCD1602_H__
- #define __LCD1602_H__
- #include
- #include
- #include
- #include
- #include
- #define uchar unsigned char
- #define uint unsigned int
- ***it RS = P2^6;
- ***it RW = P2^5;
- ***it EN = P2^7;
- void delay(uint t);
- void busy_wait();
- void write_LCD_command(uchar cmd);
- void write_LCD_data(uchar dat);
- void initialize_LCD();
- void LCD_showstring(uchar r,uchar c,uchar *str);
- #endif
主程序:
- #include "LCD1602.h"
- void delay50us(uint x)
- {
- uint i;
- for(;x>0;x--)
- for(i=19;i>0;i--);
- }
- void delay(uint t)
- {
- uint i;
- while(t--)
- {
- for(i=0;i<120;i++) ;
- }
- }
- void busy_wait()
- {
- uint LCD_status;
- do
- {
- P0 = 0XFF;
- EN = 0;
- RS = 0;
- RW = 1;
- EN = 1;
- LCD_status = P0;
- EN = 0;
- }while(LCD_status&0x80) ;
- }
- void write_LCD_command(uchar cmd)
- {
- busy_wait();
- EN = 0;
- RS = 0;
- RW = 0;
- P0 = cmd;
- _nop_();
- _nop_();
- _nop_();
- EN = 1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- EN = 0;
- }
- void write_LCD_data(uchar dat)
- {
- busy_wait();
- EN = 0;
- RS = 1;
- RW = 0;
- P0 = dat;
- EN = 1;
- _nop_();
- EN = 0;
- }
- void initialize_LCD()
- {
- write_LCD_command(0x38); delay(1);
- write_LCD_command(0x01); delay(1);
- write_LCD_command(0x06); delay(1);
- write_LCD_command(0x0c); delay(1);
- }
- void LCD_showstring(uchar r,uchar c,uchar *str)
- {
- uchar i = 0;
- code uint DDRAM[] = {0X80,0Xc0};
- write_LCD_command(DDRAM[r]|c);
- for(i = 0; str&&i<16;i++)
- {
- write_LCD_data(str) ;
- }
- for(;i<16;i++)
- write_LCD_data(' ') ;
- }
- #include "LCD1602.h"
- ***it k1 = P0^6;
- ***it k2 = P0^7;
- ***it k3 = P0^5;
- uchar msg[]="happy everyday";
- uchar table1[]= "studing";
- void H_scroll_display() //字符滚动输出
- {
- uint i;
- write_LCD_command(0X0C);
- LCD_showstring(0,0,table1);
- delay(500);
- loop1:
- for(i=0;i<=13;i++)
- {
- LCD_showstring(1,0,msg+i);
- delay(500);
- if(k1 == 1)
- {
- return;
- }
- }
- delay(1000);
- goto loop1;
- }
- void show_all_chars() //显示LCD1602 字符编码表中的所有的字符
- {
- uint i,j=0;
- write_LCD_command(0X0C);
- LCD_showstring(0,0,"studing");
- LCD_showstring(1,0," ");
- loop2:
- write_LCD_command(0Xc0);
- for(i=0x20;i!=0x00;i++)
- {
- if(i>=0x80&&i<=0x9f)
- continue;
- if((++j)%16 == 0)
- {
- delay(500);
- LCD_showstring(1,0," ");
- write_LCD_command(0X0C);
- j=0;
- write_LCD_command(0Xc0);
- }
- write_LCD_data(i) ;
- delay(20);
- if(k2 == 0)
- {
- return;
- }
- }
- delay(500);
- goto loop2;
- }
- uchar CGRAM1[][8]=
- {
- {0x80,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x00}, // 年
- {0x0f,0x09,0x0f,0x09,0x0f,0x09,0x13,0x00}, // 月
- {0x0f,0x09,0x09,0x0f,0x09,0x09,0x0f,0x00} // 日
- };
- void write_CGRAM(uchar g[][8],uchar n) //自定义的字符显示
- {
- uchar i,j;
- write_LCD_command(0X40);
- for(i=0;i
- for(j=0;j<8;j++)
- write_LCD_data(g[j]) ;
- }
- void display_chars()
- {
- uchar i;
- LCD_showstring(1,0," ");
- write_CGRAM(CGRAM1,3) ;
- write_LCD_command(0Xc0);
- loop3:
- for(i=0;i<3;i++)
- {
- write_LCD_data(i) ;
- write_LCD_data(0x3f) ;
- delay(100);
- }
- if(k3 == 0)
- {
- return;
- }
- delay(500);
- goto loop3
- }
- void main()
- {
- initialize_LCD();
- write_LCD_command(0X80);
- LCD_showstring(0,0,"hello");
- while(1)
- {
- P0 = 0XFF;
- if(k1 == 0) H_scroll_display();
- if(k2 == 0) show_all_chars();
- if(k3 == 0) display_chars();
- }
- }
|