LCD1602的驱动程序的代码编写

控制/MCU

1878人已加入

描述

#include

#include "./delay/delay.h"

#define LCDPORT P0

#define LCD_WRITE_DATA 1

#define LCD_WRITE_COM 0

sbit RS = P2^4;

sbit RW = P2^5;

sbit E = P2^6;

unsigned char flag = 1;

unsigned char shi = 23, fen = 59, miao = 50;

void lcd1602_write(unsigned char byte, unsigned char flag)

{

if(flag)

{

RS = 1; //选择输入的数据为数据

}

else

{

RS = 0; //选择输入的数据为命令

}

RW = 0; //写

E = 1; //选中lcd

LCDPORT = byte;

delay_ms(5);

E = 0; //失能LCD 高电平有效

}

void lcd_init()

{

delay_ms(15);

lcd1602_write(0x38,LCD_WRITE_COM); //设置8位数据接口,两行指令,5*8点阵字符

delay_ms(5);

lcd1602_write(0x38,LCD_WRITE_COM);

delay_ms(5);

lcd1602_write(0x38,LCD_WRITE_COM);

delay_ms(5);

lcd1602_write(0x38,LCD_WRITE_COM);

delay_ms(5);

lcd1602_write(0x08,LCD_WRITE_COM); //关闭显示,关闭光标和闪烁

delay_ms(5);

lcd1602_write(0x01,LCD_WRITE_COM); //清屏

delay_ms(5);

lcd1602_write(0x06,LCD_WRITE_COM); //设置指针方式,画面不平移

delay_ms(5);

lcd1602_write(0x0c,LCD_WRITE_COM); //设置指针方式,画面不平移

delay_ms(5);

}

void lcd_dis_char(unsigned char x, unsigned char y, unsigned char *s)

{

if(0 == x)

{

lcd1602_write(y + 0x80,LCD_WRITE_COM);

}

else if(1 == x)

{

lcd1602_write(y + 0xc0,LCD_WRITE_COM);

}

while(*s)

{

lcd1602_write(*s, LCD_WRITE_DATA);

s++;

}

}

void time0_init()

{

EA = 1;

TMOD |= 0x01;

TH0 = (65536 - 20000) / 255;

TL0 = (65536 - 20000) % 255;

ET0 = 1;

TR0 = 1;

}

void time0_isr() interrupt 1

{

static unsigned char i = 0;

TH0 = (65536 - 20000) / 255;

TL0 = (65536 - 20000) % 255;

i++;

if(50 == i)

{

i = 0;

miao++;

if(60 == miao)

{

miao = 0;

fen++;

if(60 == fen)

{

fen = 0;

shi++;

if(24 == shi)

{

shi = 0;

}

}

}

}

}

void lcd_diaplay_time() //时间的显示函数

{

lcd1602_write(0x80 + 7, LCD_WRITE_COM);

lcd1602_write((shi / 10) + 0x30,LCD_WRITE_DATA);

lcd1602_write((shi % 10) + 0x30,LCD_WRITE_DATA);

lcd1602_write(':',LCD_WRITE_DATA);

lcd1602_write((fen / 10) + 0x30,LCD_WRITE_DATA);

lcd1602_write((fen % 10) + 0x30,LCD_WRITE_DATA);

lcd1602_write(':',LCD_WRITE_DATA);

lcd1602_write((miao / 10) + 0x30,LCD_WRITE_DATA);

lcd1602_write((miao % 10) + 0x30,LCD_WRITE_DATA);

}

void lcd_dis_hex() //显示字符或数字

{

unsigned char temp = 0x2b;

lcd1602_write(0x80,LCD_WRITE_COM);

lcd1602_write((temp / 16) + 0x30,LCD_WRITE_DATA);

lcd1602_write((temp % 16) + 0x37,LCD_WRITE_DATA);

}

void main()

{

time0_init();

lcd_init();

lcd_dis_hex();

lcd_dis_char(0,2,"time:");

while(1)

{

lcd_diaplay_time();

}

}

#include

#include "./delay/delay.h"

#define LCDPORT P0

#define LCD_WRITE_DATA 1

#define LCD_WRITE_COM 0

sbit RS = P2^4;

sbit RW = P2^5;

sbit E = P2^6;

unsigned char flag = 1;

unsigned char shi = 23, fen = 59, miao = 50;

void lcd1602_write(unsigned char byte, unsigned char flag)

{

if(flag)

{

RS = 1; //选择输入的数据为数据

}

else

{

RS = 0; //选择输入的数据为命令

}

RW = 0; //写

E = 1; //选中lcd

LCDPORT = byte;

delay_ms(5);

E = 0; //失能LCD 高电平有效

}

void lcd_init()

{

delay_ms(15);

lcd1602_write(0x38,LCD_WRITE_COM); //设置8位数据接口,两行指令,5*8点阵字符

delay_ms(5);

lcd1602_write(0x38,LCD_WRITE_COM);

delay_ms(5);

lcd1602_write(0x38,LCD_WRITE_COM);

delay_ms(5);

lcd1602_write(0x38,LCD_WRITE_COM);

delay_ms(5);

lcd1602_write(0x08,LCD_WRITE_COM); //关闭显示,关闭光标和闪烁

delay_ms(5);

lcd1602_write(0x01,LCD_WRITE_COM); //清屏

delay_ms(5);

lcd1602_write(0x06,LCD_WRITE_COM); //设置指针方式,画面不平移

delay_ms(5);

lcd1602_write(0x0c,LCD_WRITE_COM); //设置指针方式,画面不平移

delay_ms(5);

}

void lcd_dis_char(unsigned char x, unsigned char y, unsigned char *s)

{

if(0 == x)

{

lcd1602_write(y + 0x80,LCD_WRITE_COM);

}

else if(1 == x)

{

lcd1602_write(y + 0xc0,LCD_WRITE_COM);

}

while(*s)

{

lcd1602_write(*s, LCD_WRITE_DATA);

s++;

}

}

void time0_init()

{

EA = 1;

TMOD |= 0x01;

TH0 = (65536 - 20000) / 255;

TL0 = (65536 - 20000) % 255;

ET0 = 1;

TR0 = 1;

}

void time0_isr() interrupt 1

{

static unsigned char i = 0;

TH0 = (65536 - 20000) / 255;

TL0 = (65536 - 20000) % 255;

i++;

if(50 == i)

{

i = 0;

miao++;

if(60 == miao)

{

miao = 0;

fen++;

if(60 == fen)

{

fen = 0;

shi++;

if(24 == shi)

{

shi = 0;

}

}

}

}

}

void lcd_diaplay_time() //时间的显示函数

{

lcd1602_write(0x80 + 7, LCD_WRITE_COM);

lcd1602_write((shi / 10) + 0x30,LCD_WRITE_DATA);

lcd1602_write((shi % 10) + 0x30,LCD_WRITE_DATA);

lcd1602_write(':',LCD_WRITE_DATA);

lcd1602_write((fen / 10) + 0x30,LCD_WRITE_DATA);

lcd1602_write((fen % 10) + 0x30,LCD_WRITE_DATA);

lcd1602_write(':',LCD_WRITE_DATA);

lcd1602_write((miao / 10) + 0x30,LCD_WRITE_DATA);

lcd1602_write((miao % 10) + 0x30,LCD_WRITE_DATA);

}

void lcd_dis_hex() //显示字符或数字

{

unsigned char temp = 0x2b;

lcd1602_write(0x80,LCD_WRITE_COM);

lcd1602_write((temp / 16) + 0x30,LCD_WRITE_DATA);

lcd1602_write((temp % 16) + 0x37,LCD_WRITE_DATA);

}

void main()

{

time0_init();

lcd_init();

lcd_dis_hex();

lcd_dis_char(0,2,"time:");

while(1)

{

lcd_diaplay_time();

}

}

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
评论(0)
发评论
1368979359 2020-06-17
0 回复 举报
全是错误 收起回复

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分