单片机驱动 DS1302时钟+1602液晶 PROTEUS 和51单片机教程
程序参考C语言代码如下:
#include
#include “LCD1602.h”
#include “DS1302.h”
void Delay1ms(unsigned int count)
{
unsigned int i,j;
for(i=0;i
}
main()
{
SYSTEMTIME CurrentTime;
LCD_Initial();
Initial_DS1302();
GotoXY(0,0);
Print(" www.ourhc.cn");
GotoXY(0,1);
Print("Time: ");
while(1)
{
DS1302_GetTime(&CurrentTime);
TimeToStr(&CurrentTime);
GotoXY(6,1);
Print(CurrentTime.TimeString);
Delay1ms(400);
}
}
/**************************************************************************
THE 1602 CHAR LCD LIB
COPYRIGHT (c) 2005 BY JJJ.
-- ALL RIGHTS RESERVED --
File Name: LCD.h
Author: Jiang Jian Jun
Created: 2005/4/3
Modified: NO
Revision: 1.0
***************************************************************************/
#ifndef LCD_CHAR_1602_2005_4_9
#define LCD_CHAR_1602_2005_4_9
#include
//Port Definitions**********************************************************
sbit LcdRs = P2^0;
sbit LcdRw = P2^1;
sbit LcdEn = P2^2;
sfr DBPort = 0x80; //P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.数据端口
//内部等待函数**************************************************************************
unsigned char LCD_Wait(void)
{
LcdRs=0;
LcdRw=1; nop();
LcdEn=1; nop();
//while(DBPort&0x80);//在用Proteus仿真时,注意用屏蔽此语句,在调用GotoXY()时,会进入死循环,
//可能在写该控制字时,该模块没有返回写入完备命令,即DBPort&0x80==0x80
//实际硬件时打开此语句
LcdEn=0;
return DBPort;
}
//向LCD写入命令或数据************************************************************
#define LCD_COMMAND 0 // Command
#define LCD_DATA 1 // Data
#define LCD_CLEAR_SCREEN 0x01 // 清屏
#define LCD_HOMING 0x02 // 光标返回原点
void LCD_Write(bit style, unsigned char input)
{
LcdEn=0;
LcdRs=style;
LcdRw=0; nop();
DBPort=input; nop();//注意顺序
LcdEn=1; nop();//注意顺序
LcdEn=0; nop();
LCD_Wait();
}
//设置显示模式************************************************************
#define LCD_SHOW 0x04 //显示开
#define LCD_HIDE 0x00 //显示关
#define LCD_CURSOR 0x02 //显示光标
#define LCD_NO_CURSOR 0x00 //无光标
#define LCD_FLASH 0x01 //光标闪动
#define LCD_NO_FLASH 0x00 //光标不闪动
void LCD_SetDisplay(unsigned char DisplayMode)
{
LCD_Write(LCD_COMMAND, 0x08|DisplayMode);
}
//设置输入模式************************************************************
#define LCD_AC_UP 0x02
#define LCD_AC_DOWN 0x00 // default
#define LCD_MOVE 0x01 // 画面可平移
#define LCD_NO_MOVE 0x00 //default
void LCD_SetInput(unsigned char InputMode)
{
LCD_Write(LCD_COMMAND, 0x04|InputMode);
}
//移动光标或屏幕************************************************************
/*
#define LCD_CURSOR 0x02
#define LCD_SCREEN 0x08
#define LCD_LEFT 0x00
#define LCD_RIGHT 0x04
void LCD_Move(unsigned char object, unsigned char direction)
{
if(objectLCD_CURSOR)
LCD_Write(LCD_COMMAND,0x10|direction);
if(objectLCD_SCREEN)
LCD_Write(LCD_COMMAND,0x18|direction);
}
/
//初始化LCD***********************************************************
void LCD_Initial()
{
LcdEn=0;
LCD_Write(LCD_COMMAND,0x38); //8位数据端口,2行显示,5*7点阵
LCD_Write(LCD_COMMAND,0x38);
LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); //开启显示, 无光标
LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏
LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); //AC递增, 画面不动
}
//************************************************************************
void GotoXY(unsigned char x, unsigned char y)
{
if(y0)
LCD_Write(LCD_COMMAND,0x80|x);
if(y1)
LCD_Write(LCD_COMMAND,0x80|(x-0x40));
}
void Print(unsigned char str)
{
while(str!=’\0’)
{
LCD_Write(LCD_DATA,str);
str++;
}
}
/
void LCD_LoadChar(unsigned char user[8], unsigned char place)
{
unsigned char i;
LCD_Write(LCD_COMMAND,0x40|(place8));
for(i=0; i<8; i++)
LCD_Write(LCD_DATA,user[i]);
}
/
//**********************************************************************
#endif
最后,如果有什么意见或者建议欢迎您留言给我,让我们共同学习一起进步,
如果需要 程序完整源代码和 设计文件,请在下方留言或者私信我,看到后会第一时间回复。
谢谢!