📄 1602显示._c
字号:
#include <iom16v.h>
#include <macros.h>
//定义MCU与LCD的接口
#define LCD_EN_PORT PORTC
#define LCD_RW_PORT PORTC
#define LCD_RS_PORT PORTC
#define LCD_DATA_PORT PORTA
#define LCD_DATA_DDR DDRA
#define LCD_DATA_PIN PINA
#define LCD_EN 0x80 //portC7 out
#define LCD_RW 0x40 //portC6 out/in
#define LCD_RS 0x20 //portC5 out
#define LCD_DATA 0xf0 //porta4/5/6/7 out
/*-----------------------------------------------------------------------
延时函数
系统时钟:8M
-----------------------------------------------------------------------*/
void delay_1us(void) //1us延时函数
{
asm("nop");
}
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1ms();
}
/*--------------------------------------------------------------------------------------------------
Public function prototypes
--------------------------------------------------------------------------------------------------*/
void LCD_init(void);
void LCD_en_write(void);
void LCD_write_char(unsigned command,unsigned data);
void LCD_wait_Ready(void);
void LCD_set_xy(unsigned char x, unsigned char y);
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);
void delay_nus(unsigned int n);
void delay_nms(unsigned int n);
/**********************************************************
光标命令
LCD_write_char(0x0e,0); //光标开
LCD_write_char(0x0d,0); //光标所在字符闪烁
LCD_write_char(0x0c,0); //光标关
**********************************************************/
/**********************************************************
TC1602B LCD DISPLAY
LCD_write函数功能:当command=0时,向LCD写入数据,否则向LCD写
入命令
LCD第一行显示寄存器地址:0X80-0X8F
LCD第二行显示寄存器地址:0XC0-0XCF
**********************************************************/
void LCD_init(void)
{
DDRA=0XFF; //设置PA输出
PORTA=0XFF; //全部加上上拉电阻
DDRC=0XFF; //设置PC为输出
PORTC=0XFF; //全部加上上拉电阻
delay_nms(15);
LCD_write_char(0x28,0); //4bit test
LCD_write_char(0x0c,0); //显示开
LCD_write_char(0x01,0); //显示清屏
LCD_write_char(0x06,0); //显示光标移动设置
}
void LCD_en_write(void) //EN端产生一个高电平脉冲,写LCD
{
LCD_EN_PORT |= LCD_EN;
delay_nus(1);
LCD_EN_PORT &= ~LCD_EN;
}
/*-----------------------------------------------------------------------
LCD_write_char : 英文字符串显示函数
输入参数:*s :英文字符串指针;
X、Y : 显示字符串的位置,X:0-15,Y:0-1
LCD第一行显示寄存器地址:0X80-0X8F
LCD第一行显示寄存器地址:0XC0-0XCF
-----------------------------------------------------------------------*/
void LCD_write_char(unsigned command,unsigned data)
{
unsigned command_temp,data_temp;
command_temp = command;
data_temp = data;
LCD_wait_Ready();
LCD_RW_PORT &= ~LCD_RW; //RW=0
if (command == 0)
{
LCD_RS_PORT |= LCD_RS; //RS=1
LCD_DATA_PORT &= 0X0F;
LCD_DATA_PORT|= data_temp&0xf0; //send high 4bit gai f
}
else
{
LCD_RS_PORT &= ~LCD_RS; //RS=0
LCD_DATA_PORT &= 0X0F;
LCD_DATA_PORT |= command_temp&0xf0;//send high 4bit gai f
}
LCD_en_write();
command_temp=command_temp << 4; //send low 4bit
data_temp=data_temp << 4;
LCD_DATA_PORT &= 0X0F;
if (command==0)
LCD_DATA_PORT |= data_temp&0xf0;
else
LCD_DATA_PORT |= command_temp&0xf0;
LCD_en_write();
LCD_RW_PORT |= LCD_RW;
LCD_RS_PORT ^= LCD_RS;
}
void LCD_wait_Ready(void) //等待LCD空闲
{
LCD_DATA_DDR &= ~0x80; //PA7 I/O口方向设置为输入
LCD_RW_PORT |= LCD_RW; //RW=1
LCD_RS_PORT &= ~LCD_RS; //RS=0
LCD_EN_PORT |= LCD_EN; //EN=1
while (!( LCD_DATA_PIN&0x80 ) == 0); //RW=1,读PA7,为0表示空闲;
LCD_EN_PORT &= ~LCD_EN; //EN=0
LCD_DATA_DDR |= 0xf0;
}
/*-----------------------------------------------------------------------
LCD_set_xy : 设置LCD显示的起始位置
输入参数:x、y : 显示字符串的位置,X:0-15,Y:0-1
LCD第一行显示寄存器地址:0X80-0X8F
LCD第一行显示寄存器地址:0XC0-0XCF
-----------------------------------------------------------------------*/
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == 0) address = 0x80 + x;
else
address = 0xc0 + x;
LCD_write_char( address, 0 );
}
/*-----------------------------------------------------------------------
LCD_write_string : 英文字符串显示函数
LCD_write_dan_data:显示0~10内的单个十进制数
LCD_write_chang_dat:显示0~4294967295内的任意一个数
输入参数:*s :英文字符串指针;
X、Y : 显示字符串的位置
-----------------------------------------------------------------------*/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_set_xy( X, Y ); ///显示字符串
while (*s)
{
LCD_write_char( 0, *s );
s ++;
}
}
void LCD_write_dan_data(unsigned char X,unsigned char Y,unsigned char z)
{
unsigned char i; //显示一个字节数字
LCD_set_xy( X, Y );
i=z+'0';
LCD_write_char(0,i);
}
void LCD_write_chang_data(unsigned char X,unsigned char Y, unsigned long z)
{
unsigned char i; //显示一个长字节数字(0~4294967295)
LCD_set_xy( X, Y ); //注:现在显的数是个位开始的
while(z)
{
i=z%10+'0';
z=z/10;
LCD_write_char(0,i);
}
}
/*************************************************************
使用方法:
先LCD_init();
再用上面要显的数
*************************************************************/
void main()
{
LCD_init();
LCD_write_chang_data(0,0,1234567890);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -