PIC16 单片机 LCD1602 驱动 程序 PIC16F877A PIC16F887
2022-01-04 来源:eefocus
/* LCD显示相关 */
#define DATA 1 /* LCD写数据时为1 */
#define COM 0 /* LCD写命令时为0 */
#define LINE1 0b10000000
#define LINE2 0b11000000
#define LCD_E RD6
#define LCD_RW RD5
#define LCD_RS RD4
void LCD_WRITE_4( unsigned char R1, unsigned char FLAG );
void LCD_WRITE( unsigned char R1, unsigned char FLAG );
unsigned char LCD_READ( void );
void LCD_BUSY( void );
void LcdWriteChar( unsigned char x, unsigned char y, char s );
void lcd_dispaly_str( unsigned char x, unsigned char y, char *s );
void LCD_CSH( void );
/* LCD写4位命令 */
void LCD_WRITE_4( unsigned char R1, unsigned char FLAG )
{
LCD_RW = 0; NOP();
LCD_RS = FLAG;
PORTD &= 0xF0;
LCD_E = 1; NOP();
PORTD |= R1;
NOP(); NOP();
LCD_E = 0; NOP();
LCD_RS = 0; NOP();
PORTD &= 0xF0;
}
/* LCD写命令 */
void LCD_WRITE( unsigned char R1, unsigned char FLAG )
{
unsigned char R2;
LCD_BUSY();
LCD_RW = 0; NOP();
LCD_RS = FLAG;
R2 = (R1 >> 4) & 0x0F;
PORTD &= 0xF0; NOP();
LCD_E = 1;
PORTD |= R2;
NOP(); NOP();
LCD_E = 0;
R2 = R1 & 0x0F;
PORTD &= 0xF0; NOP();
LCD_E = 1; NOP();
PORTD |= R2;
NOP(); NOP();
LCD_E = 0; NOP();
LCD_RS = 0; NOP();
PORTD &= 0xF0;
}
/* 读LCD状态 */
unsigned char LCD_READ( void )
{
unsigned char R1;
TRISD = 0x0F;
LCD_RS = 0; NOP();
LCD_RW = 1;
NOP(); NOP();
LCD_E = 1;
NOP(); NOP();
NOP(); NOP();
R1 = 0;
R1 = (PORTD << 4) & 0xF0;
LCD_E = 0;
NOP(); NOP();
NOP(); NOP();
LCD_E = 1;
NOP(); NOP();
R1 |= (PORTD & 0x0F);
LCD_E = 0; NOP();
LCD_RW = 0;
TRISD = 0x00;
return(R1);
}
/* 检测LCD是否忙 */
void LCD_BUSY( void )
{
unsigned char R1;
do
{
R1 = LCD_READ();
}
while ( (R1 & 0x80) == 0x80 );
}
/* 写字符 */
void LcdWriteChar( unsigned char x, unsigned char y, char s )
{
if ( y == 0 )
{
LCD_WRITE( 0x80 + x, COM );
}else {
LCD_WRITE( 0xC0 + x, COM );
}
LCD_WRITE( s, DATA );
}
/* 写字符串 */
void lcd_dispaly_str( unsigned char x, unsigned char y, char *s )
{
if ( y == 0 )
{
LCD_WRITE( 0x80 + x, COM );
}else {
LCD_WRITE( 0xC0 + x, COM );
}
while ( *s )
{
LCD_WRITE( *s, DATA );
s++;
}
}
/* LCD初始化 */
void LCD_CSH( void )
{
TRISD = 0x00; /* RD方向输出 */
__delay_ms( 20 );
LCD_WRITE_4( 0b0011, COM );
__delay_ms( 5 );
LCD_WRITE_4( 0b0011, COM );
__delay_ms( 10 );
LCD_WRITE_4( 0b0011, COM );
__delay_ms( 10 );
LCD_WRITE_4( 0b0010, COM );
LCD_BUSY();
LCD_WRITE( 0b00101000, COM );
LCD_WRITE( 0b00001100, COM );
LCD_WRITE( 0b00000001, COM );
__delay_ms( 2 );
LCD_WRITE( 0b00000110, COM );
}
/* 游标闪烁 */
void SetCurPosition( unsigned char x, unsigned char y )
{
unsigned char temp;
temp = x & 0x0f;
y &= 0x01;
if ( y )
temp |= 0x40; /* 如果在第2行 */
temp |= 0x80;
LCD_WRITE( temp, COM );
LCD_WRITE( 0x0f, COM );
}
/* 关游标 */
void LCD_DELET_CUR_P( void )
{
LCD_WRITE( 0x0c, COM );
}
使用:
LCD_CSH();
lcd_dispaly_str(0,0,'dasdasas');
- Microchip推出集成微型FPGA的PIC16 微控制器,售价不到 50 美分
- Microchip 发布PIC16F13145系列MCU,促进可定制逻辑的新发展
- A FLASH Bootloader for PIC16 and PIC18 Devices--硬译(一)
- pic16多路adc采样
- PIC16 PCF8591
- A FLASH Bootloader for PIC16 and PIC18 Devices--硬译(五)
- PIC16 MPLAB项目-安装CCS编译器
- PIC16模拟串口以及采样正弦波
- PIC16位单片机——彻底弄懂时钟
- 英飞凌与西门子将嵌入式汽车软件平台与微控制器结合 为下一代SDV提供所需功能