PIC18F452 1602 custom characters

Publisher:熙风细雨Latest update time:2017-02-16 Source: eefocusKeywords:PIC18F452 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Display custom characters

  The steps are as follows: 
    1. Write the custom characters into CGRAM first; 2. Send the custom characters in CGRAM to DDRAM for display. 
  It is very simple: look 
  at the CGROM character code table of LCD1602, you can find that the content from 00000000B~00000111B (00H~07H) address is not defined, it is left for the user to define. The user can define the content in CGRAM of LCD1602 first, and then call the custom characters in the same way as calling CGROM characters (here is a tip, up to 8 custom characters can be written), so how to set the content in CGRAM?

  First, we need to extract the "font" corresponding to the 5X8 dot matrix of the character to be written. We can extract it through related software or manually. To put it simply, the dots that are displayed in a row of the dot matrix are represented by 1, and the dots that are not displayed are represented by 0, so as to form the font data corresponding to the row. 
    To set the content of CGRAM, you need to set it line by line. Each row corresponds to a CGRAM. 5X8 dot matrix, 5 dots per row, a total of 8 rows, so the font data of 8 rows should be written into CGRAM. After writing, you can call it like
calling  CGROM characters.
    Define the content of a row in two steps:  
     1. Set the row address (CGRAM address):  
        The commands used are as follows:  
          RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0  
           0 0 0 1 DATA  
Among them: DB5, DB4, DB3 are character numbers, that is, the character addresses you will use when you want to display the character in the future. 
        DB2, DB1, DB0 are row numbers. 
      2. Set the CGRAM data (content) command code as follows:  
          RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0  
          1 0 DATA 
Among them: DB4, DB3, DB2, DB1, DB0 correspond to the font data of 5 dots per line. 
         DB7, DB6, DB5 can be any data, generally "000".

  1 #include //Call the header file, you can go to the PICC18 software to find PIC18FXX2.H

  2 __CONFIG(1,XT); //Crystal oscillator is external 4M

  3 __CONFIG(2,WDTDIS); //watchdog off

  4 __CONFIG(4,LVPDIS); //Disable low voltage programming

  5 

  6 #define RSPIN RB5 //Data or Instrument Select

  7 #define RWPIN RB4 //Write or Read

  8 #define EPIN RB3 //6800 mode Enable single

  9 unsigned char table[]="LCD check ok"; //The content to be displayed is put into the array table1 

 10 

 11 //This function defines eight characters to be written into eight addresses of CGRAM respectively 

 12 unsigned char pic[]={ 

 13 0x03,0x07,0x0f,0x1f,0x1f,0x1f,0x1f,0x1f, 

 14 0x18,0x1E,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f, 

 15 0x07,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f, 

 16 0x10,0x18,0x1c,0x1E,0x1E,0x1E,0x1E,0x1E, 

 17 0x0f,0x07,0x03,0x01,0x00,0x00,0x00,0x00, 

 18 0x1f,0x1f,0x1f,0x1f,0x1f,0x0f,0x07,0x01, 

 19 0x1f,0x1f,0x1f,0x1f,0x1f,0x1c,0x18,0x00, 

 20 0x1c,0x18,0x10,0x00,0x00,0x00,0x00,0x00

 twenty one 

 twenty two }; 

 twenty three ///---------------------------------------

 24 //Name: Delay function         

 25 //----------------------------------------

 26 void delay(unsigned int t)

 27 {

 28 unsigned int i,j;

 29 for(i=0;i

 30 {

 31 for(j=0;j<10;j++);    

 32 }

 33 }

 34 

 35 //------------------------------------------

 36 //Name: 1602 busy detection function         

 37 //---------------------------------------- 

 38 void lcd_wait_busy(void)

 39 {  

 40 TRISD7=1; //Prepare for reading status

 41 RSPIN=0; //Select instruction register

 42 RWPIN=1; //Select read

 43 EPIN=1; //Enable line level change

 44 while(RD7==1); //Read busy status, exit when not busy

 45 EPIN=0; //Restore the enable line level

 46 TRISD7=0;

 47 }

 48 //------------------------------------------

 49 //Name: 1602 write command function         

 50 //----------------------------------------

 51 void lcd_write_com(unsigned char combuf)

 52 {  

 53 RSPIN=0; //Select instruction register

 54 RWPIN=0; //Select write

 55 PORTD=combuf; //Send the command word into RD     

 56 EPIN=1; //Enable line level change, command sent to 1602 8-bit data port

 57 asm("NOP");

 58 EPIN=0; //Restore the enable line level

 59 }

 60 //------------------------------------------

 61 //Name: 1602 write command function (with busy detection)         

 62 //----------------------------------------

 63 void lcd_write_com_busy(unsigned char combuf)

 64 {  

 65 lcd_wait_busy(); //Call busy detection function

 66 lcd_write_com(combuf); //Call write command function

 67 }

 68 //------------------------------------------

 69 //Name: 1602 write data function (with busy detection)         

 70 //----------------------------------------

 71 void lcd_write_data(unsigned char databuf)

 72 {  

 73 lcd_wait_busy(); //Call busy detection function

 74 RSPIN=1; //Select data register

 75 RWPIN=0; //Select write

 76 PORTD = databuf; // send the data word to P2

 77 EPIN = 1; // Enable line level change, command sent to 1602 8-bit data port

 78 asm("NOP");

 79 EPIN=0; //Restore the enable line level

 80 }

 81 //------------------------------------------

 82 //Name: 1602 display address write function         

 83 //----------------------------------------

 84 void lcd_write_address(unsigned char x, unsigned char y)

 85 {  

 86 x&=0x0f; //Column address is limited to 0-15

 87 y&=0x01; //Row address is limited to 0-1

 88 if(y==0x00)

 89 lcd_write_com_busy(x|0x80); //write the column address of the first row

 90 else

 91 lcd_write_com_busy((x+0x40)|0x80); //Write the column address of the second row

 92 }

 93 //------------------------------------------

 94 //Name: 1602 initialization function         

 95 //----------------------------------------

 96 void lcdreset(void)

 97 {  

 98 lcd_write_com_busy(0x38); //8-bit data, double column, 5*7 font

 99 lcd_write_com_busy(0x08); //Display function off, no cursor

100 lcd_write_com_busy(0x01); // clear screen command

101 lcd_write_com_busy(0x06); //After writing new data, the cursor moves right but the display screen does not move

102 lcd_write_com_busy(0x0c); //Display function on, no cursor,

103 }      

104 //------------------------------------------

105 //Name: Specify address write function         

106 //----------------------------------------    

107 void lcd_write_char(unsigned char x, unsigned char y, unsigned char buf)

108 {          

109 lcd_write_address(x,y); //write address            

110 lcd_write_data(buf); //Write display data    

111 } 

112 

113 //Display string, cannot be applied accurately yet

114 void lcd1602_write_character(unsigned char add,unsigned char *p) 

115 { 

116 lcd_write_com_busy(add); 

117 while (*p!='\0') 

118 { 

119 lcd_write_data(*p++);  

120 } 

121 } 

122 

123 //Write custom characters into CGRAM 

124 //add is the address of CGRAM (0~7) 

125 //*pic_num points to the first address of an 8-bit group 

126 void lcd1602_write_pic(unsigned char add,unsigned char pic_num) 

127 { 

128 unsigned char i; 

129 add=add<<3; 

130 for(i=0;i<8;i++) 

131 { 

132 lcd_write_com_busy(0x40|add+i); //Set the row address, DB6=1, so the address must be added with 0X40;

133 lcd_write_data(pic_num); 

134 } 

135 }

136 

137 //------------------------------------------

138 //Name: Main function         

139 //----------------------------------------  

140 void main(void)

141 {     

142 char i,m;    

143 ADCON1=0X06; //All IO are digital ports, analog input is prohibited

144 TRISB=0B11000111; //RB3-5 is set as output

145 TRISD=0B00000000; //RD is set as output             

146 lcdreset(); //Reset 1602                            

147 lcd_write_char(6,0,0x31); //1

148 lcd_write_char(7,0,0x36); //6

149 lcd_write_char(8,0,0x30); //0

150 lcd_write_char(9,0,0x32); //2

151 lcd_write_char(10,0,0x35); //5

152 lcd_write_char(11,0,'g'); //g

153 lcd_write_char(12,0,'o'); //o

154 lcd_write_char(13,0,'o'); //o

155 lcd_write_char(14,0,'d'); //d

156 lcd_write_com_busy(0x80+0x44); //8. Reset the display address to 0xc0, which is the 0th position in the bottom row

157 for(m=0;m<12;m++) //Write the data in table1[] into 1602 for display 

158 {

159 lcd_write_data(table[m]);

160 }    

161 //Write eight custom characters to CGRAM

162 lcd_write_com_busy(0x40); //7. Set CGRAM address to 0100; 0x06     

163 for(i=0;i<64;i++) //Write the heart-shaped code into CGRAM 

164 lcd_write_data(pic[i]);//lcd1602_write_pic(i,pic[i]);

165 //Display eight custom characters

166 lcd_write_com_busy(0x80); //Set the upper display position 

167 for(i=0;i<0x04;i++)//Display the upper part of the heart-shaped pattern 

168 lcd_write_data(i);

169 lcd_write_com_busy(0xc0); ////Transfer the display coordinates to the corresponding positions in the lower and upper rows 

170 for(i=4;i<0x08;i++)

171 lcd_write_data(i);

172 while(1)

173 {        

174     

175 }  

176 }

This is the third step to draw the custom characters using 4-wire data transmission 
. When the refresh rate of the LCD screen is not high, we can use another 4-wire data transmission of 1602 to reduce the number of io ports used. I hope you like it. Since I didn't find any relevant information, I just posted the program here for your reference. 
#include 
#include 
sbit lcd1602_rs=P0^7; 
sbit lcd1602_rw=P0^6; 
sbit lcd1602_e=P0^5; 
/*#define lcd1602_io_writ lcd1602_rw=0; 
#define lcd1602_io_read lcd1602_rw=1;

 #define lcd1602_io_com lcd1602_rs=0; 
#define lcd1602_io_dat lcd1602_rs=1; 
#define lcd1602_io_able lcd1602_e=0; #define lcd1602_io_unable lcd1602_e=1 

*/ 
sbit lcd1602_dat1= P0^0; 
sbit lcd1602_dat2=P0^1; 
sbit lcd1602_dat3=P0^2 ; 
sbit lcd1602_dat4=P0^3; 
//unsigned char code table[]="ball hahhahhaha"; 
//unsigned char code table1[]="THE BEST!"; 
void delay(unsigned int z) 

  unsigned int a,b ; 
  for(a=z;a>0;a--) 
    for(b=114;b>0;b--); 

void lcd1602_write(bit cd,unsigned char dat)//cd=0, command; c

  int i; 
  unsigned char j; 
  lcd1602_rs=cd; 
  for(j=0;j<2;j++) 
  { 
  lcd1602_e=1; 
  for(i=0;i<200;i++) 
    _nop_(); 
  lcd1602_dat4=dat&0x80; 
  lcd1602_dat3= (dat<<1)&0x80; 
  lcd1602_dat2=(dat<<2)&0x80; 
  lcd1602_dat1=(dat<<3)&0x80; 
  lcd1602_e=0; 
  //lcd1602_e=1; 
    dat<<=4; 
  
  } 

 
/*unsigned char lcd1602_read_dat(unsigned char add) 

  unsigned char dat; 
return dat; 
}*/ 
void main() 

lcd1602_rw=0; 
lcd1602_e=0; 
lcd1602_write( 0,0x28); 
lcd1602_write(0,0x0f); 
lcd1602_write(0,0x06); 
lcd1602_write(0,0x01); 
lcd1602_write(0,0x80); 
lcd1602_write(1,'q'); 
while(1); 
}


Keywords:PIC18F452 Reference address:PIC18F452 1602 custom characters

Previous article:PIC18FXX2 INT0 interrupt
Next article:PIC18F4521602 simple display rules

Recommended ReadingLatest update time:2024-11-15 10:31

51 MCU study notes: merge 1602 and 12864 LCD strip interface
  Today I successfully merged the 1602 and 12864 LCD strip interfaces! Share the code   The two pictures above are the 1602 and 12864 LCD socket interfaces, which are usually found on MCU development boards. A careful observation revealed that their sockets are mostly the same.   For the contrast adjustment of the t
[Microcontroller]
51 MCU study notes: merge 1602 and 12864 LCD strip interface
How to customize characters for LCD1602
   LCD1602 contains eight custom character spaces. Users can set custom characters as needed, but the pixels of the characters are usually 5X7. If you need characters with larger pixels, you can only use multiple 5X7 characters. Each custom character has 8 bytes, the last one is 0x00; the upper 3 bits of each byte are
[Microcontroller]
[51 MCU] The functions of 1602 CGRAM, CGROM and DDRAM
CGRAM: A space that allows users to create their own font areas. As can be seen from the CGROM table, on the far left of the table is a column that allows users to customize CGRAM. From top to bottom, there are 16, but in fact only 8 bytes are available. Its character code is the 8 addresses of 00000000-00000111. The
[Microcontroller]
[51 MCU] The functions of 1602 CGRAM, CGROM and DDRAM
PIC MCU 16F877+LCD1602+DS1302 Perpetual Calendar
This perpetual calendar has the functions of adjusting time, date, year and week. It can also display time, date, year, month, week and temperature through LCD1602.
[Microcontroller]
PIC MCU 16F877+LCD1602+DS1302 Perpetual Calendar
LCD1602 font table
Note: The characters in the left half of the table correspond to their ASCII codes, so when writing code, you can directly write "A" instead of "0x41".
[Microcontroller]
LCD1602 font table
51 single-chip multi-channel temperature measurement alarm system (AT89C51, multiple DS18B20, LCD1602)
1. Introduction This system is mainly composed of AT89C51, multiple DS18B20 temperature modules and LCD1602. The general principle is that multiple DS18B20 collect temperature data in sequence and transmit it to P2.7 of AT89C51. Finally, the current real-time temperature is displayed through LCD1602. The upper and low
[Microcontroller]
51 single-chip multi-channel temperature measurement alarm system (AT89C51, multiple DS18B20, LCD1602)
Introduction to 51 MCU——LCD1602
1. Introduction to LCD1602 LCD (short for Liquid Crystal Display) liquid crystal display. The structure of LCD is to place a liquid crystal box between two parallel glass substrates, set TFT (thin film transistor) on the lower substrate glass, and set color filter on the upper substrate glass. The signal and voltage
[Microcontroller]
Introduction to 51 MCU——LCD1602
1602LCD digital electronic clock C language
1602LCD digital electronic clock basic design ideas:   1. 1602 LCD display:     First line: Year, Month, Day 2012-01-22    Second line: hours, minutes, seconds 23:59:00    2. Independent button functions:     Key1 restores the actual time before the MCU adjusts the time. Since timer T0 is paused during the
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号