// Compiler: ICC-AVR v6.31A
// Target chip: M16
// Clock: 8.0000 Mhz
/*-------------------------------------------------------------
LCD pin Definition
1---GND
2---VCC
3---VO
4---RS
5---RW
6---EN
7 to 14--D0-D7
15 -- background light +
16 -- background light-
-----------------------------------------------------------------*/
# Include <iom16v. h>
# Include <macros. h>
/*---------------------------------------------------------------
The following figure shows the connection information between the AVR and LCD.
PA2-> RS
PA3-> EN
Location-> RW
Pa4-> D4
Pa5-> D5
IPv6> D6
Pa7-> D7
To use this driver, change the following configuration information.
-----------------------------------------------------------------*/
# Define LCD _en_port Porta // The following two must be set to the same port
# Define LCD _en_ddr ddra
# Define LCD _rs_port Porta // The following two must be set to the same port
# Define LCD _rs_ddr ddra
# Define LCD _data_port Porta // The following three must be set to the same port
# Define LCD _data_ddr ddra // The value must be 4 characters in height.
# Define LCD _data_pin Pina
# Define LCD _RS (1 <PA2) // 0x04 portA2 out
# Define LCD _EN (1 <PA3) // 0x08 portA3 out
# Define LCD _DATA (1 <PA4) | (1 <PA5) | (1 <PA66) | (1 <PA7 )) // 0xf0 portA4/5/6/7 out
/*------------------------------------------------------------------------------
Function Description
------------------------------------------------------------------------------*/
Void LCD _init (void );
Void LCD _en_write (void );
Void LCD _write_command (unsigned char command );
Void LCD _write_data (unsigned char data );
Void LCD _set_xy (unsigned char x, unsigned char y );
Void LCD _write_string (unsigned char X, unsigned char Y, unsigned char * s );
Void LCD _write_char (unsigned char X, unsigned char Y, unsigned char data );
Void delay_nus (unsigned int n );
Void delay_nms (unsigned int n );
========================================================== ==============================================
# Include "LCD. h"
Void LCD _init (void) // LCD Initialization
{
LCD _DATA_DDR | = LCD _DATA; // The data port direction is output.
LCD _EN_DDR | = LCD _EN; // set the EN direction to output.
LCD _RS_DDR | = LCD _RS; // set the RS direction to output.
LCD _write_command (0x28 );
LCD _en_write ();
Delay_nus (40 );
LCD _write_command (0x28); // 4-Digit Display
LCD _write_command (0x0c); // display on
LCD _write_command (0x01); // clear the screen
Delay_nms (2 );
}
Void LCD _en_write (void) // enables LCD
{
LCD _EN_PORT | = LCD _EN;
Delay_nus (1 );
LCD _EN_PORT & = ~ LCD _EN;
}
Void LCD _write_command (unsigned char command) // write command
{
Delay_nus (16 );
LCD _RS_PORT & = ~ LCD _RS; // RS = 0
LCD _DATA_PORT & = 0X0f; // four digits for HD
LCD _DATA_PORT | = command & 0xf0; // write the last four digits
LCD _en_write ();
Command = command <4; // four-digit low to four-digit high
LCD _DATA_PORT & = 0x0f; // four digits for HD
LCD _DATA_PORT | = command & 0xf0; // write four lower digits
LCD _en_write ();
}
Void LCD _write_data (unsigned char data) // write data
{
Delay_nus (16 );
LCD _RS_PORT | = LCD _RS; // RS = 1
LCD _DATA_PORT & = 0X0f; // four digits for HD
LCD _DATA_PORT | = data & 0xf0; // write the last four digits
LCD _en_write ();
Data = data <4; // four-digit low to four-digit high
LCD _DATA_PORT & = 0X0f; // four digits for HD
LCD _DATA_PORT | = data & 0xf0; // write four low
LCD _en_write ();
}
Void LCD _set_xy (unsigned char x, unsigned char y) // write address Function
{
Unsigned char address;
If (y = 0) address = 0x80 + x;
Else address = 0xc0 + x;
LCD _write_command (address );
}
Void LCD _write_string (unsigned char X, unsigned char Y, unsigned char * s) // column x = 0 ~ 15, row y = 0, 1
{
LCD _set_xy (X, Y); // write address
While (* s) // write the display characters
{
LCD _write_data (* s );
S ++;
}
}
Void LCD _write_char (unsigned char X, unsigned char Y, unsigned char data) // column x = 0 ~ 15, row y = 0, 1
{
LCD _set_xy (X, Y); // write address
LCD _write_data (data );
}
========================================================== ====================================
Void delay_1us (void) // 1us latency Function
{
Asm ("nop ");
}
Void delay_nus (unsigned int n) // N us latency Function
{
Unsigned int I = 0;
For (I = 0; I <n; I ++)
Delay_1us ();
}
Void delay_1ms (void) // 1 ms latency Function
{
Unsigned int I;
For (I = 0; I <1140; I ++ );
}
Void delay_nms (unsigned int N) // latency function of N Ms
{
Unsigned int I = 0;
For (I = 0; I <n; I ++)
Delay_1ms ();
}
========================================================== ======================================
# Include "LCD. H"
Void init_devices (void)
{
CLI (); // disable all interrupts
LCD _init ();
Mcucr = 0x00;
GRN = 0x00;
TIMSK = 0x00; // timer interrupt sources
SEI (); // re-enable interrupts
}
Void main (void)
{
Init_devices ();
LCD _write_string (1, 0, "Hi !! ");
For (;;)
{
LCD _write_string (0, 1, "archeng504 ");
LCD _write_char (6, 0, '8 ');
}
}
======= The following are the programs that display custom characters ================
You can also customize the 8-character pattern in 1602
Pattern specification 5*8
Below is an up arrow
0 0 0 | 0 0 1 0 0 // 0x04
|
0 0 0 | 0 1 1 1 0 // 0x0e
|
0 0 0 | 1 0 1 0 1 // 0x15
|
0 0 0 | 0 0 1 0 0 // 0x04
|
0 0 0 | 0 0 1 0 0 // 0x04
|
0 0 0 | 0 0 1 0 0 // 0x04
|
0 0 0 | 0 0 1 0 0 // 0x04
|
0 0 0 | 0 0 0 0 0 // 0x00
Unsigned char data [8] = {0x04, 0x0e, 0x15, 0x04,0x04,0x04,0x04,0x00}; // optional
Unsigned char data1 [8] = {0x04,0x04,0x04,0x04,0x15, 0x0e, 0x04,0x00 }; //
Void my_signs (unsigned char ascii, unsigned char * z) // 0x00
{
Unsigned char address = 0, I, temp = 0; // ascii is the ascii code value (0x00
Temp = (ascii & 0x07) <3; // optional between 0x007)
For (I = 0; I <8; I ++)
{
Address = 0x40 + temp + I;
LCD _write_command (address );
Delay_nus (1 );
LCD _write_data (* (z ++ ));
Delay_nus (1 );
}
}
// Create a pattern
Void LCD _init (void) // LCD Initialization
{
LCD _DATA_DDR | = LCD _DATA; // The data is output.
LCD _EN_DDR | = LCD _EN; // sets the RS. EN direction.
LCD _RS_DDR | = LCD _RS;
LCD _write_command (0x28); // 4-Digit Display
LCD _en_write ();
Delay_nms (15 );
LCD _write_command (0x28); // 4-Digit Display
LCD _write_command (0x0c); // display on
My_signs (0x00, data); // creating // create a pattern
My_signs (0x01, data1); // returns
LCD _write_command (0x01); // clear the screen
}
Display custom patterns
LCD _write_char (9, 0, 0x00); // bytes
LCD _write_char (9, 0, 0x01); // returns <