1602自定义字符
1602自带了常用的ascii字符库,为方便用户自定义,1602里面也有8个可自定义的字符。这8个字符是通过设定CGRAM地址来实现的,如下图。
DB7DB6已经固定;DB5DB4DB3为8个字符的地址位;DB2DB1DB0为字模的地址(字模有5列,需要3位表示)。如果字模要显示‘c’如下图。
地址:01000000 数据:00010000 图示: ○○○■○○○○
01000001 00000110 ○○○○○■■○
01000010 00001001 ○○○○■○○■
01000011 00001000 ○○○○■○○○
01000100 00001000 ○○○○■○○○
01000101 00001001 ○○○○■○○■
01000110 00000110 ○○○○○■■○
01000111 00000000 ○○○○○○○○
下面是一段用自定义字符显示进度条的程序(arduino pro mini)。
硬件连接图:
int LCD1602_RS=12; int LCD1602_RW=11; int LCD1602_EN=10; int DB[] = { 6, 7, 8, 9}; byte p1[8] = { 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10}; byte p2[8] = { 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18}; byte p3[8] = {0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c}; byte p4[8] = {0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e}; byte p5[8] = {0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f}; int num=0; void setup (void) { int i = 0; for (i=6; i <= 12; i++) { pinMode(i,OUTPUT); } delay(100); LCD_Command_Write(0x28);//4线 2行 5x7 delay(50); LCD_Command_Write(0x06); delay(50); LCD_Command_Write(0x0c); delay(50); LCD_Command_Write(0x80); delay(50); LCD_Command_Write(0x01); delay(50); } void loop (void) { LCD_Command_Write(0x01);//清屏 delay(50); for(int j=0;j<8;j++){LCD_Command_Write(0x40+j);LCD_Data_Write(p1[j]);} for(int j=0;j<8;j++){LCD_Command_Write(0x48+j);LCD_Data_Write(p2[j]);} for(int j=0;j<8;j++){LCD_Command_Write(0x50+j);LCD_Data_Write(p3[j]);} for(int j=0;j<8;j++){LCD_Command_Write(0x58+j);LCD_Data_Write(p4[j]);} for(int j=0;j<8;j++){LCD_Command_Write(0x60+j);LCD_Data_Write(p5[j]);} if(num==80)num=0; int b=0; int a; for(int i=0;i<=(num/5);i++) { int shi; int ge; int d; int dd; int bai; a = (num+1)*125; bai = a/10000; shi=(a-bai*10000)/1000; ge=(a-bai*10000-shi*1000)/100; d=(a-bai*10000-shi*1000-ge*100)/10; dd=a%10; if(bai)LCD_Write_Char(4,0,bai+48);//百位 LCD_Write_Char(5,0,shi+48);//十位 LCD_Write_Char(6,0,ge+48);//个位 LCD_Write_Char(7,0,0x2e);//. LCD_Write_Char(8,0,d+48);//小数点后一位 LCD_Write_Char(9,0,dd+48);//小数点后两位 LCD_Write_Char(10,0,0x25);//% if(i<(num/5))b=4; if(i==(num/5))b=num%5; LCD_Write_Char(i,1,b); } delay(1000); num++; } void LCD_Command_Write(int command)//写命令 { int i,temp; digitalWrite( LCD1602_RS,LOW); digitalWrite( LCD1602_RW,LOW); digitalWrite( LCD1602_EN,LOW); temp=command & 0xf0; for (i=DB[0]; i <= 9; i++) { digitalWrite(i,temp & 0x80); temp <<= 1; } digitalWrite( LCD1602_EN,HIGH); delayMicroseconds(1); digitalWrite( LCD1602_EN,LOW); temp=(command & 0x0f)<<4; for (i=DB[0]; i <= 9; i++) { digitalWrite(i,temp & 0x80); temp <<= 1; } digitalWrite( LCD1602_EN,HIGH); delayMicroseconds(1); digitalWrite( LCD1602_EN,LOW); } void LCD_Data_Write(int dat)//写数据 { int i=0,temp; digitalWrite( LCD1602_RS,HIGH); digitalWrite( LCD1602_RW,LOW); digitalWrite( LCD1602_EN,LOW); temp=dat & 0xf0; for (i=DB[0]; i <= 9; i++) { digitalWrite(i,temp & 0x80); temp <<= 1; } digitalWrite( LCD1602_EN,HIGH); delayMicroseconds(1); digitalWrite( LCD1602_EN,LOW); temp=(dat & 0x0f)<<4; for (i=DB[0]; i <= 9; i++) { digitalWrite(i,temp & 0x80); temp <<= 1; } digitalWrite( LCD1602_EN,HIGH); delayMicroseconds(1); digitalWrite( LCD1602_EN,LOW); } void LCD_SET_XY( int x, int y )//设置显示位置 { int address; if (y ==0) address = 0x80 + x; else address = 0xC0 + x; LCD_Command_Write(address); } void LCD_Write_Char( int x,int y,int dat)//写字符 { LCD_SET_XY( x, y ); LCD_Data_Write(dat); } void LCD_Write_String(int X,int Y,char *s)//写字符串 { LCD_SET_XY( X, Y ); //设置地址 while (*s) //写字符串 { LCD_Data_Write(*s); s ++; } }
效果图如下: