顯示實時時鐘、溫度檢測并使用LCD1602顯示相關(guān)信息 ,能模式切換、加減時間。
仿真圖:
#include <REG51.H>
#include <intrins.h>
//#include "LCD1602.h"
//#include "DS1302.h"
#define uint unsigned int
#define uchar unsigned char
sbit ?DS1302_CLK = P1^7; ? ? ? ? ? ? ?//實時時鐘時鐘線引腳
sbit ?DS1302_IO ?= P1^6; ? ? ? ? ? ? ?//實時時鐘數(shù)據(jù)線引腳
sbit ?DS1302_RST = P1^5; ? ? ? ? ? ? ?//實時時鐘復(fù)位線引腳
sbit ?wireless_1 = P3^0;
sbit ?wireless_2 = P3^1;
sbit ?wireless_3 = P3^2;
sbit ?wireless_4 = P3^3;
sbit ?ACC0 = ACC^0;
sbit ?ACC7 = ACC^7;
char hide_sec,hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year; ?//秒,分,時到日,月,年位閃的計數(shù)
sbit Set = P2^0; ? ? ? //模式切換鍵
sbit Up = P2^1; ? ? ? ?//加法按鈕
sbit Down = P2^2; ? ? ?//減法按鈕
sbit out = P2^3; ? ? ? //立刻跳出調(diào)整模式按鈕
sbit DQ = P1^0; ? ? ? ?//溫度傳送數(shù)據(jù)IO口
char done,count,temp,flag,up_flag,down_flag;
uchar temp_value; ? ? ?//溫度值
uchar TempBuffer[5],week_value[2];
void show_time(); ? //液晶顯示程序
/***********1602液晶顯示部分子程序****************/
//Port Definitions**********************************************************
sbit LcdRs?? ??? ?= P2^5;
sbit LcdRw?? ??? ?= P2^6;
sbit LcdEn ??? ?= P2^7;
sfr ?DBPort ?? ?= 0x80;?? ??? ?//P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.數(shù)據(jù)端口
//內(nèi)部等待函數(shù)**************************************************************************
unsigned char LCD_Wait(void)
{
LcdRs=0;
LcdRw=1;?? ?_nop_();
LcdEn=1;?? ?_nop_();
LcdEn=0;
return DBPort;
}
//向LCD寫入命令或數(shù)據(jù)************************************************************
#define LCD_COMMAND?? ??? ??? ?0 ? ? ?// Command
#define LCD_DATA?? ??? ??? ?1 ? ? ?// Data
#define LCD_CLEAR_SCREEN?? ?0x01 ? ? ?// 清屏
#define LCD_HOMING ??? ??? ?0x02 ? ? ?// 光標(biāo)返回原點
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();
}
//設(shè)置顯示模式************************************************************
#define LCD_SHOW?? ??? ??? ?0x04 ? ?//顯示開
#define LCD_HIDE?? ??? ??? ?0x00 ? ?//顯示關(guān)
#define LCD_CURSOR?? ??? ??? ?0x02 ?? ?//顯示光標(biāo)
#define LCD_NO_CURSOR?? ??? ?0x00 ? ?//無光標(biāo)
#define LCD_FLASH?? ??? ??? ?0x01 ? ?//光標(biāo)閃動
#define LCD_NO_FLASH?? ??? ?0x00 ? ?//光標(biāo)不閃動
void LCD_SetDisplay(unsigned char DisplayMode)
{
LCD_Write(LCD_COMMAND, 0x08|DisplayMode);
}
//設(shè)置輸入模式************************************************************
#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);
}
//初始化LCD************************************************************
void LCD_Initial()
{
LcdEn=0;
LCD_Write(LCD_COMMAND,0x38); ? ? ? ? ? //8位數(shù)據(jù)端口,2行顯示,5*7點陣
LCD_Write(LCD_COMMAND,0x38);
LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); ? ?//開啟顯示, 無光標(biāo)
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(y==0)
LCD_Write(LCD_COMMAND,0x80|x);
if(y==1)
LCD_Write(LCD_COMMAND,0x80|(x-0x40));
}
//將字符輸出到液晶顯示
void Print(unsigned char *str)
{
while(*str!='?')
{
LCD_Write(LCD_DATA,*str);
str++;
}
}
/***********DS1302時鐘部分子程序******************/
typedef struct __SYSTEMTIME__
{
unsigned char Second;
unsigned char Minute;
unsigned char Hour;
unsigned char Week;
unsigned char Day;
unsigned char Month;
unsigned char ?Year;
unsigned char DateString[11];
unsigned char TimeString[9];
}SYSTEMTIME;?? ?//定義的時間類型
SYSTEMTIME CurrentTime;
#define AM(X)?? ?X
#define PM(X)?? ?(X+12) ? ? ? ? ? ??? ? ?// 轉(zhuǎn)成24小時制
#define DS1302_SECOND?? ?0x80 ? ? ? ? ?//時鐘芯片的寄存器位置,存放時間
#define DS1302_MINUTE?? ?0x82
#define DS1302_HOUR?? ??? ?0x84
#define DS1302_WEEK?? ??? ?0x8A
#define DS1302_DAY?? ??? ?0x86
#define DS1302_MONTH?? ?0x88
#define DS1302_YEAR?? ??? ?0x8C
void DS1302InputByte(unsigned char d) ?? ?//實時時鐘寫入一字節(jié)(內(nèi)部函數(shù))
{
unsigned char i;
ACC = d;
for(i=8; i>0; i--)
{
DS1302_IO = ACC0; ? ? ? ? ? ?? ?//相當(dāng)于匯編中的 RRC
DS1302_CLK = 1;
DS1302_CLK = 0;
ACC = ACC >> 1;
}
}
unsigned char DS1302OutputByte(void) ?? ?//實時時鐘讀取一字節(jié)(內(nèi)部函數(shù))
{
unsigned char i;
for(i=8; i>0; i--)
{
ACC = ACC >>1; ? ? ? ? ?? ??? ??? ?//相當(dāng)于匯編中的 RRC
ACC7 = DS1302_IO;
DS1302_CLK = 1;
DS1302_CLK = 0;
}
return(ACC);
}
void Write1302(unsigned char ucAddr, unsigned char ucDa)?? ?//ucAddr: DS1302地址, ucData: 要寫的數(shù)據(jù)
{
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr); ? ? ? ?? ?// 地址,命令
DS1302InputByte(ucDa); ? ? ? ?? ?// 寫1Byte數(shù)據(jù)
DS1302_CLK = 1;
DS1302_RST = 0;
}
unsigned char Read1302(unsigned char ucAddr)?? ?//讀取DS1302某地址的數(shù)據(jù)
{
unsigned char ucData;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr|0x01); ? ? ? ?// 地址,命令
ucData = DS1302OutputByte(); ? ? ? ? // 讀1Byte數(shù)據(jù)
DS1302_CLK = 1;
DS1302_RST = 0;
return(ucData);
}