微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > PIC16F877A例程---LCD1602

PIC16F877A例程---LCD1602

时间:11-24 来源:互联网 点击:
#include //调用头文件

#include

__CONFIG(0x3F32); //芯片配置字

//__CONFIG(HS&UNPROTECT&PWRTEN&BORDIS&WDTDIS);//芯片配置字

//__CONFIG(0x3FFD&0x3FFF&0x3FF7&0x3FBF&0x3FFB);//芯片配置字

#define uchar unsigned char

#define uint unsigned int

#define ulint unsigned long int

#define LCD_DATA_PORT PORTD //定义LCD的D0-D7所在端口

#define LCD_DATA_POUT TRISD=0x00 //定义LCD数据端口为输出

#define LCD_DATA_PIN TRISD=0xFF //定义LCD数据端口为输入(在读LCD状态时用到)

#define LCD_Control_IN TRISB|=0XFF

#define LCD_Control_OUT TRISB&=0X00

#define LCD_EN_1 RB0=1

#define LCD_EN_0 RB0=0

//LCD读写控制引脚

#define LCD_RW_1 RB1=1

#define LCD_RW_0 RB1=0

//LCD指令或数据选择引脚

#define LCD_RS_1 RB2=1

#define LCD_RS_0 RB2=0

void nus_delay(uint nus);

void nms_delay(uint nms);

void mcu_io_init(void);

void lcd_init(void);

void LCD_en_write(void);

void wait_LCD_Ready(void);

void set_LCD_xy(uchar x, uchar y );

void LCD_write_onechar(uchar COMM,uchar DAT);

void LCD_write_string(uchar X,uchar Y,uchar *string);

void main(void)

{

uchar START_DISPLAY1[]={"mcu.2000"};

uchar START_DISPLAY2[]={"TEL: 12345678901"};

mcu_io_init();

lcd_init();

nms_delay(10);

LCD_write_string(0,0,START_DISPLAY1);

LCD_write_string(0,1,START_DISPLAY2);

while(1);

}

void mcu_io_init(void)

{

LCD_DATA_PORT=0xff;

LCD_DATA_POUT; //LCD数据口设为输出

LCD_EN_1;

LCD_RW_1;

LCD_RS_1;

LCD_Control_IN;

}

void lcd_init(void)

{

nms_delay(15);

LCD_DATA_POUT;

LCD_Control_OUT;

LCD_write_onechar(0x38,0);

nms_delay(1);

LCD_write_onechar(0x38,0);

nms_delay(1);

LCD_write_onechar(0x38,0);

nms_delay(1);

LCD_write_onechar(0x38,0);

LCD_write_onechar(0x08,0);

LCD_write_onechar(0x01,0);

LCD_write_onechar(0x06,0);

nms_delay(1);;

LCD_write_onechar(0x0C,0);

}

void LCD_en_write(void)

{

LCD_EN_1;

nus_delay(150); //E脉冲周期最小150ns

LCD_EN_0;

}

void wait_LCD_Ready(void)

{

uchar temp1;

LCD_DATA_PORT=0XFF;

LCD_DATA_PIN; //D7设为输入,准备判断LCD忙标志

//1为忙 0为空闲

LCD_RS_0;

LCD_RW_1; //读

LCD_EN_1;

nus_delay(2);

while((temp1=PORTD & 0X80));

LCD_EN_0;

LCD_RS_1;

LCD_DATA_POUT;

}

void LCD_write_onechar(uchar COMM,uchar DAT)

{

wait_LCD_Ready(); //等待LCD空闲

LCD_RW_0; //写

//写数据

if(COMM==0)

{

LCD_RS_1; //RS高电平向LCD写数据

LCD_DATA_PORT=DAT;

}

//写命令

else

{

LCD_RS_0;

LCD_DATA_PORT=COMM;

}

LCD_en_write();

nus_delay(2);

LCD_RW_1;

LCD_RS_1;

}

void LCD_write_string(uchar X,uchar Y,uchar *string)

{

set_LCD_xy( X, Y ); //设置LCD显示坐标

while (*string)

{

LCD_write_onechar(0,*string );

string ++; //指向下一显示字符地址

}

}

void set_LCD_xy(uchar x, uchar y )

{

uchar DIS_address;

if (y == 0)

DIS_address = 0x80 + x; //第一行X列

else

DIS_address = 0xc0 + x;

LCD_write_onechar( DIS_address, 0 ); //第二行X列

}

void nus_delay(uint nus)

{

int j=10;

while(nus--)

{

while(j--);

j=10;

}

}

void nms_delay(uint nms)

{

while(nms--)

nus_delay(1000);

}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top