本文主要是介绍单片机==LCD1602之简易万年历,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
LCD1602可以简单的显示一些字母数字,但是对于汉字来说,显示起来还是不太好,不怎么好显示,毕竟像素比较低,这次用LCD1602显示了一下简易的万年历。
效果图:
程序:
/*******lcd.h*******/
#ifndef _LCD_H_
#define _LCD_H_#include <reg51.h>
#include <intrins.h>sbit RS = P2^6;
sbit RW = P2^5;
sbit EN = P2^7;#define RS_CLR RS = 0
#define RS_SET RS = 1
#define RW_CLR RW = 0
#define RW_SET RW = 1
#define EN_CLR EN = 0
#define EN_SET EN = 1
#define dataport P0
#define uchar unsigned charextern void DelayUs2x(uchar t);
extern void DelayMs(uchar t);
extern void LCD_Write_COM(uchar com); //写指令
extern void LCD_Write_Data(uchar Data);
extern void LCD_Clear();
extern void LCD_Write_String(uchar x, uchar y, uchar* s);
extern void LCD_Write_Char(uchar x, uchar y, uchar c);
extern void LCD_Init(); #endif
/*****lcd.c********/
#include "lcd.h"void DelayUs2x(uchar t)
{while(--t);
}void DelayMs(uchar t)
{while(t--){DelayUs2x(245);DelayUs2x(245);}
}/* bit LCD_Check_busy(void)
{dataport = 0xff;RS_CLR;RW_SET;EN_CLR;_nop_();EN_SET;return (bit)(dataport&0x80);
} *///写指令void LCD_Write_COM(uchar com)
{DelayMs(5);RS_CLR;RW_CLR;EN_SET;dataport = com;_nop_();EN_CLR;
}//写数据
void LCD_Write_Data(uchar Data)
{DelayMs(5);RS_SET;RW_CLR;EN_SET;dataport = Data;_nop_();EN_CLR;
}//清屏
void LCD_Clear()
{LCD_Write_COM(0x01);DelayMs(5);
}//输入字符,x为横坐标,y为纵坐标
void LCD_Write_String(uchar x, uchar y, uchar* s)
{if(y == 0){LCD_Write_COM(0x80+x);}else{LCD_Write_COM(0xC0+x);}while(*s){LCD_Write_Data(*s);s++;}
}//输入字符,x为横坐标,y为纵坐标
void LCD_Write_Char(uchar x, uchar y, uchar c)
{if(y == 0){LCD_Write_COM(0x80+x);}else{LCD_Write_COM(0xC0+x);}LCD_Write_Data(c);
}void LCD_Init() //LCD初始化子程序
{EN_CLR;LCD_Write_COM(0x38); //设置显示模式DelayMs(5);LCD_Write_COM(0x38); //设置显示模式DelayMs(5);LCD_Write_COM(0x38); //设置显示模式DelayMs(5);LCD_Write_COM(0x38); //设置显示模式DelayMs(5);LCD_Write_COM(0x0c); //开显示不显示光标,光标闪烁LCD_Write_COM(0x06); //写一个指针加一LCD_Write_COM(0x01); //清屏LCD_Write_COM(0x80); //设置数据指针起点
}
/*****main.c*******/
#include "lcd.h"uchar hour = 21, min = 43, sec = 0, count, date = 28, monse = 4;
unsigned int year = 2017;void init_T0()
{EA = 1;ET0 = 1;TMOD = 0X01;TH0 = 0X3C;TL0 = 0XB0;TR0 = 1;
}void main()
{init_T0();LCD_Init();LCD_Clear();while(1){ LCD_Write_String(0, 0, "Date:");LCD_Write_Char(5, 0, year/1000+48);LCD_Write_Char(6, 0, year%1000/100+48);LCD_Write_Char(7, 0, year%100/10+48);LCD_Write_Char(8, 0, year%10+48);LCD_Write_Char(9, 0, '-'); LCD_Write_Char(10, 0, monse/10+48);LCD_Write_Char(11, 0, monse%10+48);LCD_Write_Char(12, 0, '-'); LCD_Write_Char(13, 0, date/10+48);LCD_Write_Char(14, 0, date%10+48); LCD_Write_String(0, 1, "Time:");LCD_Write_Char(5, 1, hour/10+48);LCD_Write_Char(6, 1, hour%10+48);LCD_Write_Char(7, 1, '-');LCD_Write_Char(8, 1, min/10+48);LCD_Write_Char(9, 1, min%10+48);LCD_Write_Char(0xa, 1, '-');LCD_Write_Char(0xb, 1, sec/10+48);LCD_Write_Char(0xc, 1, sec%10+48);}
}void inter() interrupt 1
{TH0 = 0X3C;TL0 = 0XB0;count++;if(count >= 20){count = 0;sec++;}if(sec >= 60){sec = 0;min++;}if(min >= 60){min = 0;hour++;}if(hour >= 24){hour = 0;date++;}if(date > 31){date = 0;monse++;}if(monse > 13){monse = 0;year++;}}
这篇关于单片机==LCD1602之简易万年历的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!