单片机
返回首页

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');


进入单片机查看更多内容>>
相关视频
  • RISC-V嵌入式系统开发

  • SOC系统级芯片设计实验

  • 云龙51单片机实训视频教程(王云,字幕版)

  • 2022 Digi-Key KOL 系列: 你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍

  • TI 新一代 C2000™ 微控制器:全方位助力伺服及马达驱动应用

  • MSP430电容触摸技术 - 防水Demo演示

精选电路图
  • CCD图像传感器在微光电视系统中的应用

  • 光控音效发生器电路

  • 非常简单的150W功放电路图

  • 分享一个电网倾角计电路

  • 电谐波图形均衡器示意图

  • 一种构建12V和230V双直流电源的简单方法

    相关电子头条文章