导航:首页 > 编程语言 > lcd1602数字时钟程序

lcd1602数字时钟程序

发布时间:2024-08-28 11:05:43

A. 跪求:液晶LCD1602显示时钟的C程序

顶层文件 万年历.C #include<reg51.h> #include "LCD1602.h" #include "DS1302.h" #define uchar unsigned char #define uint unsigned int sbit speaker=P2^4; bit key_flag1=0,key_flag2=0; SYSTEMTIME adjusted; uchar sec_add=0,min_add=0,hou_add=0,day_add=0,mon_add=0,yea_add=0; uchar data_alarm[7]={0}; /************键盘控制******************************/ int key_scan() //扫描是否有键按下 { int i=0; uint temp; P1=0xf0; temp=P1; if(temp!=0xf0) i=1; else i=0; return i; } uchar key_value() //确定按键的值 { uint m=0,n=0,temp; uchar value; uchar v[4][3]={'2','1','0','5','4','3','8','7','6','b','a','9'} ; P1=0xfe; temp=P1; if(temp!=0xfe)m=0; P1=0xfd;temp=P1 ;if(temp!=0xfd)m=1; P1=0xfb;temp=P1 ;if(temp!=0xfb)m=2; P1=0xf7;temp=P1 ;if(temp!=0xf7)m=3; P1=0xef;temp=P1 ;if(temp!=0xef)n=0; P1=0xdf;temp=P1 ;if(temp!=0xdf)n=1; P1=0xbf;temp=P1 ;if(temp!=0xbf)n=2; value=v[m][n]; return value; } /***************************设置闹铃函数*******************************/ void naoling(void) { uchar i=0,l=0,j; init1602(); while(key_flag2&&i<12) if(key_scan()){j=key_value();write_data(j);if(i%2==0)data_alarm[l]=(j-'0')*10;else {data_alarm[l]+=(j-'0');l++;}i++;delay(600);} write_com(0x01); } uchar according(void) { uchar k; if(data_alarm[0]==adjusted.Year&&data_alarm[1]==adjusted.Month&&data_alarm[2]==adjusted.Day&&data_alarm[3]==adjusted.Hour&&data_alarm[4]==adjusted.Minute&&data_alarm[5]==adjusted.Second) k=1; else k=0; return k; } void speak(void) {uint i=50; while(i) {speaker=0; delay(1); speaker=1; delay(1); i--; } } void alarm(void) {uint i=10; while(i) { speak(); delay(10); i--; } } /**************************修改时间操作********************************/ void reset(void) { sec_add=0; min_add=0; hou_add=0; day_add=0; mon_add=0; yea_add=0 ; } void adjust(void) { if(key_scan()&&key_flag1) switch(key_value()) {case '0':sec_add++;break; case '1':min_add++;break; case '2':hou_add++;break; case '3':day_add++;break; case '4':mon_add++;break; case '5':yea_add++;break; case 'b':reset();break; default: break; } adjusted.Second+=sec_add; adjusted.Minute+=min_add; adjusted.Hour+=hou_add; adjusted.Day+=day_add; adjusted.Month+=mon_add; adjusted.Year+=yea_add; if(adjusted.Second>59) adjusted.Second=adjusted.Second%60; if(adjusted.Minute>59) adjusted.Minute=adjusted.Minute%60; if(adjusted.Hour>23) adjusted.Hour=adjusted.Hour%24; if(adjusted.Day>31) adjusted.Day=adjusted.Day%31; if(adjusted.Month>12) adjusted.Month=adjusted.Month%12; if(adjusted.Year>100) adjusted.Year=adjusted.Year%100; } /**************************中断处理函数*********************************/ void changing(void) interrupt 0 using 0 //需要修改时间和日期,或者停止修改 { if(key_flag1)key_flag1=0; else key_flag1=1; } void alarming(void) interrupt 3 using 0 //需要设置闹铃或者停止设置 { if(key_flag2)key_flag2=0; else key_flag2=1; } /********************************主函数***********************************/ main() {uint i; uchar *l; uchar p1[]="D:",p2[]="T:"; SYSTEMTIME T; EA=1; EX0=1; IT0=1; EA=1; EX1=1; IT1=1; init1602(); Initial_DS1302() ; while(1) { write_com(0x80); write_string(p1,2); write_com(0xc0); write_string(p2,2); DS1302_GetTime(&T) ; adjusted.Second=T.Second; adjusted.Minute=T.Minute; adjusted.Hour=T.Hour; adjusted.Week=T.Week; adjusted.Day=T.Day; adjusted.Month=T.Month; adjusted.Year=T.Year; for(i=0;i<9;i++) { adjusted.DateString[i]=T.DateString[i]; adjusted.TimeString[i]=T.TimeString[i]; } adjust(); if(key_flag2)naoling(); if(according())alarm(); DateToStr(&adjusted); TimeToStr(&adjusted); write_com(0x82); write_string(adjusted.DateString,8); write_com(0xc2); write_string(adjusted.TimeString,8); delay(10); } (二)头文件1 显示模块 LCD1602.H #ifndef LCD_CHAR_1602_2009_5_9 #define LCD_CHAR_1602_2009_5_9 #define uchar unsigned char #define uint unsigned int sbit lcdrs = P2^0; sbit lcdrw = P2^1; sbit lcden = P2^2; void delay(uint z) // 延时 { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void write_com(uchar com) // 写入指令数据到 lcd { lcdrw=0; lcdrs=0; P0=com; delay(5); lcden=1; delay(5); lcden=0; } void write_data(uchar date) // 写入字符显示数据到 lcd { lcdrw=0; lcdrs=1; P0=date; delay(5); lcden=1; delay(5); lcden=0; } void init1602() // 初始化设定 { lcdrw=0; lcden=0; write_com(0x3C); write_com(0x0c); write_com(0x06); write_com(0x01); write_com(0x80); } void write_string(uchar *pp,uint n) { int i; for(i=0;i<n;i++) write_data(pp[i]); } #endif (三)头文件2 时钟模块 DS1302.H #ifndef _REAL_TIMER_DS1302_2009_5_20_ #define _REAL_TIMER_DS1302_2003_5_20_ sbit DS1302_CLK = P2^6; //实时时钟时钟线引脚 sbit DS1302_IO = P2^7; //实时时钟数据线引脚 sbit DS1302_RST = P2^5; //实时时钟复位线引脚 sbit ACC0 = ACC^0; sbit ACC7 = ACC^7; typedef struct SYSTEM_TIME { unsigned char Second; unsigned char Minute; unsigned char Hour; unsigned char Week; unsigned char Day; unsigned char Month; unsigned char Year; unsigned char DateString[9]; //用这两个字符串来放置读取的时间 unsigned char TimeString[9]; }SYSTEMTIME; //定义的时间类型 #define AM(X) X #define PM(X) (X+12) // 转成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 #define DS1302_RAM(X) (0xC0+(X)*2) //用于计算 DS1302_RAM 地址的宏 /******内部指令**********/ void DS1302InputByte(unsigned char d) //实时时钟写入一字节(内部函数) { unsigned char i; ACC = d; for(i=8; i>0; i--) { DS1302_IO = ACC0; DS1302_CLK = 1; DS1302_CLK = 0; ACC = ACC >> 1; //因为在前面已经定义了ACC0 = ACC^0;以便再次利用DS1302_IO = ACC0; } } unsigned char DS1302OutputByte(void) //实时时钟读取一字节(内部函数) { unsigned char i; for(i=8; i>0; i--) { ACC = ACC >>1; ACC7 = DS1302_IO; DS1302_CLK = 1; DS1302_CLK = 0; } return(ACC); } /********************************/ void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要写的数据 { DS1302_RST = 0; DS1302_CLK = 0; DS1302_RST = 1; DS1302InputByte(ucAddr); // 地址,命令 DS1302InputByte(ucDa); // 写1Byte数据 DS1302_CLK = 1; DS1302_RST = 0; } unsigned char Read1302(unsigned char ucAddr) //读取DS1302某地址的数据 { unsigned char ucData; DS1302_RST = 0; DS1302_CLK = 0; DS1302_RST = 1; DS1302InputByte(ucAddr|0x01); // 地址,命令 ucData = DS1302OutputByte(); // 读1Byte数据 DS1302_CLK = 1; DS1302_RST = 0; return(ucData); } void DS1302_SetProtect(bit flag) //是否写保护 { if(flag) Write1302(0x8E,0x10); else Write1302(0x8E,0x00); } void DS1302_SetTime(unsigned char Address, unsigned char Value) // 设置时间函数 { DS1302_SetProtect(0); Write1302(Address, ((Value/10)<<4 | (Value%10))); //将十进制数转换为BCD码 } //在DS1302中的与日历、时钟相关的寄存器存放的数据必须为BCD码形式 void DS1302_GetTime(SYSTEMTIME *Time) { unsigned char ReadValue; ReadValue = Read1302(DS1302_SECOND); Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); //将BCD码转换为十进制数 ReadValue = Read1302(DS1302_MINUTE); Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_HOUR); Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_DAY); Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_WEEK); Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_MONTH); Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_YEAR); Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); } unsigned char *DataToBCD(SYSTEMTIME *Time) { unsigned char D[8]; D[0]=Time->Second/10<<4+Time->Second%10; D[1]=Time->Minute/10<<4+Time->Minute%10; D[2]=Time->Hour/10<<4+Time->Hour%10; D[3]=Time->Day/10<<4+Time->Day%10; D[4]=Time->Month/10<<4+Time->Month%10; D[5]=Time->Week/10<<4+Time->Week%10; D[6]=Time->Year/10<<4+Time->Year%10; return D; } void DateToStr(SYSTEMTIME *Time) { //将十进制数转换为液晶显示的ASCII值 Time->DateString[0] = Time->Year/10 + '0'; Time->DateString[1] = Time->Year%10 + '0'; Time->DateString[2] = '-'; Time->DateString[3] = Time->Month/10 + '0'; Time->DateString[4] = Time->Month%10 + '0'; Time->DateString[5] = '-'; Time->DateString[6] = Time->Day/10 + '0'; Time->DateString[7] = Time->Day%10 + '0'; Time->DateString[8] = '\0'; } void TimeToStr(SYSTEMTIME *Time) { //将十进制数转换为液晶显示的ASCII值 Time->TimeString[0] = Time->Hour/10 + '0'; Time->TimeString[1] = Time->Hour%10 + '0'; Time->TimeString[2] = ':'; Time->TimeString[3] = Time->Minute/10 + '0'; Time->TimeString[4] = Time->Minute%10 + '0'; Time->TimeString[5] = ':'; Time->TimeString[6] = Time->Second/10 + '0'; Time->TimeString[7] = Time->Second%10 + '0'; Time->DateString[8] = '\0'; } void Initial_DS1302(void) { unsigned char Second; Second=Read1302(DS1302_SECOND); if(Second&0x80) //初始化时间 DS1302_SetTime(DS1302_SECOND,0); } void DS1302_TimeStop(bit flag) // 是否将时钟停止 { unsigned char Data; Data=Read1302(DS1302_SECOND); DS1302_SetProtect(0); if(flag) Write1302(DS1302_SECOND, Data|0x80); else Write1302(DS1302_SECOND, Data&0x7F); } #endif zftzbpujjj1134358763,jaiezwwysu6517560870,2012-2-15 11:08:28

B. 跪求 51单片机+12864液晶+1302时钟制成的万年历c程序

顶层文件 万年历.C
#include<reg51.h>
#include "LCD1602.h"
#include "DS1302.h"
#define uchar unsigned char
#define uint unsigned int
sbit speaker=P2^4;
bit key_flag1=0,key_flag2=0;
SYSTEMTIME adjusted;
uchar sec_add=0,min_add=0,hou_add=0,day_add=0,mon_add=0,yea_add=0;
uchar data_alarm[7]={0};
/************键盘控制******************************/
int key_scan() //扫描是否有键按下
{ int i=0;
uint temp;
P1=0xf0;
temp=P1;
if(temp!=0xf0)
i=1;
else
i=0;
return i;
}
uchar key_value() //确定按键的值
{
uint m=0,n=0,temp;
uchar value;
uchar v[4][3]={'2','1','0','5','4','3','8','7','6','b','a','9'} ;
P1=0xfe; temp=P1; if(temp!=0xfe)m=0;
P1=0xfd;temp=P1 ;if(temp!=0xfd)m=1;
P1=0xfb;temp=P1 ;if(temp!=0xfb)m=2;
P1=0xf7;temp=P1 ;if(temp!=0xf7)m=3;
P1=0xef;temp=P1 ;if(temp!=0xef)n=0;
P1=0xdf;temp=P1 ;if(temp!=0xdf)n=1;
P1=0xbf;temp=P1 ;if(temp!=0xbf)n=2;
value=v[m][n];
return value;
}
/***************************设置闹铃函数*******************************/
void naoling(void)
{
uchar i=0,l=0,j;
init1602();
while(key_flag2&&i<12)
if(key_scan()){j=key_value();write_data(j);if(i%2==0)data_alarm[l]=(j-'0')*10;else {data_alarm[l]+=(j-'0');l++;}i++;delay(600);}
write_com(0x01);
}
uchar according(void)
{ uchar k;
if(data_alarm[0]==adjusted.Year&&data_alarm[1]==adjusted.Month&&data_alarm[2]==adjusted.Day&&data_alarm[3]==adjusted.Hour&&data_alarm[4]==adjusted.Minute&&data_alarm[5]==adjusted.Second)
k=1;
else k=0;
return k;
}
void speak(void)
{uint i=50;
while(i)
{speaker=0;
delay(1);
speaker=1;
delay(1);
i--;
}
}
void alarm(void)
{uint i=10;
while(i)
{
speak();
delay(10);
i--;
}
}
/**************************修改时间操作********************************/
void reset(void)
{
sec_add=0;
min_add=0;
hou_add=0;
day_add=0;
mon_add=0;
yea_add=0 ;
}
void adjust(void)
{

if(key_scan()&&key_flag1)
switch(key_value())
{case '0':sec_add++;break;
case '1':min_add++;break;
case '2':hou_add++;break;
case '3':day_add++;break;
case '4':mon_add++;break;
case '5':yea_add++;break;
case 'b':reset();break;
default: break;
}
adjusted.Second+=sec_add;
adjusted.Minute+=min_add;
adjusted.Hour+=hou_add;
adjusted.Day+=day_add;
adjusted.Month+=mon_add;
adjusted.Year+=yea_add;
if(adjusted.Second>59) adjusted.Second=adjusted.Second%60;

if(adjusted.Minute>59) adjusted.Minute=adjusted.Minute%60;

if(adjusted.Hour>23) adjusted.Hour=adjusted.Hour%24;

if(adjusted.Day>31) adjusted.Day=adjusted.Day%31;

if(adjusted.Month>12) adjusted.Month=adjusted.Month%12;

if(adjusted.Year>100) adjusted.Year=adjusted.Year%100;

}

/**************************中断处理函数*********************************/
void changing(void) interrupt 0 using 0 //需要修改时间和日期,或者停止修改
{
if(key_flag1)key_flag1=0;
else key_flag1=1;
}
void alarming(void) interrupt 3 using 0 //需要设置闹铃或者停止设置
{
if(key_flag2)key_flag2=0;
else key_flag2=1;
}
/********************************主函数***********************************/
main()
{uint i;
uchar *l;
uchar p1[]="D:",p2[]="T:";
SYSTEMTIME T;
EA=1;
EX0=1;
IT0=1;
EA=1;
EX1=1;
IT1=1;
init1602();
Initial_DS1302() ;

while(1)
{ write_com(0x80);
write_string(p1,2);
write_com(0xc0);
write_string(p2,2);
DS1302_GetTime(&T) ;
adjusted.Second=T.Second;
adjusted.Minute=T.Minute;
adjusted.Hour=T.Hour;
adjusted.Week=T.Week;
adjusted.Day=T.Day;
adjusted.Month=T.Month;
adjusted.Year=T.Year;
for(i=0;i<9;i++)
{
adjusted.DateString[i]=T.DateString[i];
adjusted.TimeString[i]=T.TimeString[i];
}
adjust();
if(key_flag2)naoling();
if(according())alarm();
DateToStr(&adjusted);
TimeToStr(&adjusted);
write_com(0x82);
write_string(adjusted.DateString,8);
write_com(0xc2);
write_string(adjusted.TimeString,8);
delay(10);
}
(二)头文件1 显示模块 LCD1602.H
#ifndef LCD_CHAR_1602_2009_5_9
#define LCD_CHAR_1602_2009_5_9
#define uchar unsigned char
#define uint unsigned int
sbit lcdrs = P2^0;
sbit lcdrw = P2^1;
sbit lcden = P2^2;
void delay(uint z) // 延时
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com) // 写入指令数据到 lcd
{
lcdrw=0;
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}

void write_data(uchar date) // 写入字符显示数据到 lcd
{
lcdrw=0;
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init1602() // 初始化设定
{
lcdrw=0;
lcden=0;
write_com(0x3C);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
void write_string(uchar *pp,uint n)
{
int i;
for(i=0;i<n;i++)
write_data(pp[i]);
}
#endif
(三)头文件2 时钟模块 DS1302.H
#ifndef _REAL_TIMER_DS1302_2009_5_20_
#define _REAL_TIMER_DS1302_2003_5_20_

sbit DS1302_CLK = P2^6; //实时时钟时钟线引脚
sbit DS1302_IO = P2^7; //实时时钟数据线引脚
sbit DS1302_RST = P2^5; //实时时钟复位线引脚
sbit ACC0 = ACC^0;
sbit ACC7 = ACC^7;

typedef struct SYSTEM_TIME
{
unsigned char Second;
unsigned char Minute;
unsigned char Hour;
unsigned char Week;
unsigned char Day;
unsigned char Month;
unsigned char Year;
unsigned char DateString[9]; //用这两个字符串来放置读取的时间
unsigned char TimeString[9];
}SYSTEMTIME; //定义的时间类型

#define AM(X) X
#define PM(X) (X+12) // 转成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
#define DS1302_RAM(X) (0xC0+(X)*2) //用于计算 DS1302_RAM 地址的宏

/******内部指令**********/
void DS1302InputByte(unsigned char d) //实时时钟写入一字节(内部函数)
{
unsigned char i;
ACC = d;
for(i=8; i>0; i--)
{
DS1302_IO = ACC0;
DS1302_CLK = 1;
DS1302_CLK = 0;
ACC = ACC >> 1; //因为在前面已经定义了ACC0 = ACC^0;以便再次利用DS1302_IO = ACC0;
}
}

unsigned char DS1302OutputByte(void) //实时时钟读取一字节(内部函数)
{
unsigned char i;
for(i=8; i>0; i--)
{
ACC = ACC >>1;
ACC7 = DS1302_IO;
DS1302_CLK = 1;
DS1302_CLK = 0;
}
return(ACC);
}
/********************************/

void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要写的数据
{
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr); // 地址,命令
DS1302InputByte(ucDa); // 写1Byte数据
DS1302_CLK = 1;
DS1302_RST = 0;
}

unsigned char Read1302(unsigned char ucAddr) //读取DS1302某地址的数据
{
unsigned char ucData;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr|0x01); // 地址,命令
ucData = DS1302OutputByte(); // 读1Byte数据
DS1302_CLK = 1;
DS1302_RST = 0;
return(ucData);
}

void DS1302_SetProtect(bit flag) //是否写保护
{
if(flag)
Write1302(0x8E,0x10);
else
Write1302(0x8E,0x00);
}

void DS1302_SetTime(unsigned char Address, unsigned char Value) // 设置时间函数
{
DS1302_SetProtect(0);
Write1302(Address, ((Value/10)<<4 | (Value%10))); //将十进制数转换为BCD码
} //在DS1302中的与日历、时钟相关的寄存器存放的数据必须为BCD码形式

void DS1302_GetTime(SYSTEMTIME *Time)
{
unsigned char ReadValue;
ReadValue = Read1302(DS1302_SECOND);
Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); //将BCD码转换为十进制数
ReadValue = Read1302(DS1302_MINUTE);
Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_HOUR);
Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_DAY);
Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_WEEK);
Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_MONTH);
Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_YEAR);
Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
}
unsigned char *DataToBCD(SYSTEMTIME *Time)
{
unsigned char D[8];
D[0]=Time->Second/10<<4+Time->Second%10;
D[1]=Time->Minute/10<<4+Time->Minute%10;
D[2]=Time->Hour/10<<4+Time->Hour%10;
D[3]=Time->Day/10<<4+Time->Day%10;
D[4]=Time->Month/10<<4+Time->Month%10;
D[5]=Time->Week/10<<4+Time->Week%10;
D[6]=Time->Year/10<<4+Time->Year%10;
return D;
}
void DateToStr(SYSTEMTIME *Time)
{
//将十进制数转换为液晶显示的ASCII值
Time->DateString[0] = Time->Year/10 + '0';
Time->DateString[1] = Time->Year%10 + '0';
Time->DateString[2] = '-';
Time->DateString[3] = Time->Month/10 + '0';
Time->DateString[4] = Time->Month%10 + '0';
Time->DateString[5] = '-';
Time->DateString[6] = Time->Day/10 + '0';
Time->DateString[7] = Time->Day%10 + '0';
Time->DateString[8] = '\0';
}

void TimeToStr(SYSTEMTIME *Time)
{
//将十进制数转换为液晶显示的ASCII值
Time->TimeString[0] = Time->Hour/10 + '0';
Time->TimeString[1] = Time->Hour%10 + '0';
Time->TimeString[2] = ':';
Time->TimeString[3] = Time->Minute/10 + '0';
Time->TimeString[4] = Time->Minute%10 + '0';
Time->TimeString[5] = ':';
Time->TimeString[6] = Time->Second/10 + '0';
Time->TimeString[7] = Time->Second%10 + '0';
Time->DateString[8] = '\0';
}
void Initial_DS1302(void)
{
unsigned char Second;
Second=Read1302(DS1302_SECOND);
if(Second&0x80) //初始化时间
DS1302_SetTime(DS1302_SECOND,0);
}
void DS1302_TimeStop(bit flag) // 是否将时钟停止
{
unsigned char Data;
Data=Read1302(DS1302_SECOND);
DS1302_SetProtect(0);
if(flag)
Write1302(DS1302_SECOND, Data|0x80);
else
Write1302(DS1302_SECOND, Data&0x7F);
}
#endif

C. 单片机数字时钟程序及电路图

#include <reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit LCD_RS=P2^0;
sbit LCD_RW=P2^1;
sbit LCD_EN=P2^2;
sbit SPK=P3^0;
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
sbit key4=P1^3;
uchar Display_Buffer[16]=" ";
uchar Count,hour=12,min=12,sec=12;
bit flag,H_or_M;
uchar display[]="This is colck";
void _delay_ms(uint x)
{
uchar i;
while(x--)
{
for(i=0;i<120;i++);
}
}
/**********************************************************************
*函数名称:LCD_Write_Command
*输 入:uchar cmd
*输 出:无
*功 能:向LCD1602写指令
**********************************************************************/
void LCD_Write_Command(uchar cmd)
{
LCD_RS=0;//根据规定,RS和R/W同时为低电平时,可以写入指令
LCD_RW=0;
LCD_EN=0;//EN置低电平
P0=cmd;//将数据送入P2口,即写入指令或地址
LCD_EN=1;//EN置高电平
_delay_ms(1);//稍延时,给硬件反应时间
LCD_EN=0;//当EN由高电平跳变成低电平时,液晶模块开始执行命令
}
/**********************************************************************
*函数名称:LCD_Write_Data
*输 入:uchar dat
*输 出:无
*功 能:向LCD1602写数据
**********************************************************************/
void LCD_Write_Data(uchar dat)
{
LCD_RS=1;//RS为高电平,RW为低电平时,可以写入数据
LCD_RW=0;
LCD_EN=0;//EN置低电平
P0=dat;//将数据送入P0口,即将数据写入液晶模块
LCD_EN=1;//EN置高电平
_delay_ms(1);//稍延时,给硬件反应时间
LCD_EN=0;//当EN由高电平跳变成低电平时,液晶模块开始执行命令
}
/**********************************************************************
*函数名称:Init_LCD
*输 入:无
*输 出:无
*功 能:初始化LCD1602
**********************************************************************/
void Init_LCD(void)
{
LCD_Write_Command(0x38);//显示模式设置:16×2显示,5×7点阵,8位数据接口
_delay_ms(1);
LCD_Write_Command(0x06);//显示模式设置:光标右移,字符不移
_delay_ms(1);
LCD_Write_Command(0x0c);//显示模式设置:显示开,无光标,光标不闪烁
_delay_ms(1);
LCD_Write_Command(0x01);//清屏幕指令,将以前的显示内容清除
_delay_ms(1);
}
/**********************************************************************
*函数名称:LCD_POS
*输 入:uchar pos
*输 出:无
*功 能:字符显示位置
**********************************************************************/
void LCD_POS(uchar pos)
{
LCD_Write_Command(0x80|pos);//显示位置的确定方法规定为"80H+地址码x"
}
/**********************************************************************
*函数名称:Show_String
*输 入:uchar *str
*输 出:无
*功 能:LCD1602显示字符串处理函数
**********************************************************************/
void Show_String(uchar *str)
{
while(*str!='\0')//只要没有写到结束标志,就继续写
LCD_Write_Data(*str++);//将字符常量写入LCD,并指向下一个字符
}
void song(void)
{
uchar i,j,k;
for(i=0;i<200;i++)
{
SPK=~SPK;
for(k=0;k<50;k++);
for(j=0;j<50;j++);
}
}
/////lcd1602液晶显示//////
void Display_Time(uchar Hour,uchar Min,uchar Sec)
{
Display_Buffer[0]=Hour/10+'0';//小时十位
Display_Buffer[1]=Hour%10+'0';//小时个位
Display_Buffer[2]=':';//显示:号
Display_Buffer[3]=Min/10+'0';//显示分钟十位
Display_Buffer[4]=Min%10+'0';//显示分钟个位
Display_Buffer[5]=':';//显示:号
Display_Buffer[6]=Sec/10+'0';//显示秒十位
Display_Buffer[7]=Sec%10+'0';//显示秒个位
LCD_POS(0x40);//显示在液晶第1行第0列
Show_String(Display_Buffer);
}
/////按键处理//////
void Set_time(void)
{
flag=0;
if(key1==0||key2==0||key3==0)//若key1,key2,key3其中有键按下,则为真
{
TR0=0;//定时器0停止
flag=1;//按键标志位为真
}
while(flag)//判断那一个按键按下
{
if(key1==0)//判断key1是否按下
{
while(!key1);//等待释放
H_or_M=!H_or_M;//选择是修改时还是分钟
}
else if(key2==0)//判断key2是否被按下
{
while(!key2);//等待释放
if(H_or_M)//若H_OR_M为真,则修改小时
{
if(++hour==24)//若小时=24,则小时=0
hour=0;//小时加1
}
else//否则修改分钟
{
if(++min==60)//若分钟等于60,则分钟等于0
min=0;//分钟加1
}
}
else if(key3==0)//判断key3是否被按下
{
while(!key3);//等待释放
if(H_or_M)//若H_OR_M为真,则修改小时
{
if(--hour==0xff) //小时减1,若小时等于-1,则小时等于23
hour=23;
}
else//否则修改分钟
{
if(--min==0xff)//分钟减1,若分钟等于-1,则分钟等于59
min=59;
}
}
else if(key4==0)//判断key4是否被按下
{
while(!key4);//等待释放
flag=0;//按键标志清零
TR0=1;//定时器0启动
}
Display_Time(hour,min,sec);//传H,M,S值显示到液晶
}
}
void timer0_init(void)//定时器0初始化
{
TMOD=0x01;//定时器0,方式1
ET0=1;//
EA=1;//总中断允许
TR0=1;//启动定时器0
TH0=(65535-50000)/256;//装高8位初值
TL0=(65535-50000)%256;//装低8位初值
}
void main(void)//主函数
{
//P0=0xff;
P1=0xff;//端口初始化
P3=0xff;
Init_LCD();//液晶初始化
timer0_init();//定时器0初始化
LCD_POS(0);//显示在液晶第1行第0列
Show_String(display);
while(1)//无限循环
{
Display_Time(hour,min,sec);//液晶显示时间
//_delay_ms(500);//延时
Set_time();//时间调整
}
}
void timer0() interrupt 1//定时器0中断
{
TH0=(65535-50000)/256;//重装定时器0高8位
TL0=(65535-50000)%256;//重装定时器0低8位
Count++;//计数加1
if(Count==20)//如果COUNT等于20
{
Count=0;//计数清零
sec++;//秒加1
if(sec==60)//判断秒是否等于60
{
sec=0;//秒清零
min++;//分钟加1
if(min==60)//判断分钟是否等于60
{
min=0;//若等于60,则清零分钟
hour++;//小时加1
if(hour==24)//判断小时是否等于24
{
hour=0;//若小时等于24,则清零小时,分钟,秒
min=0;
sec=0;
}
song();
}
}
}
}

阅读全文

与lcd1602数字时钟程序相关的资料

热点内容
word文件如何批量插入盖章 浏览:548
jscheckbox 浏览:338
pics规则文件 浏览:644
如何从数据中找出问题和机会 浏览:668
写作投稿在哪个网站好 浏览:895
绝代双骄版本 浏览:380
手机app在哪里找的对象靠谱吗 浏览:919
win10文件默认显示ck方式 浏览:829
如何更改桌面文件图标 浏览:418
word2010如何去掉背景 浏览:632
adp文件如何打开 浏览:531
ug编程怎么导出零件 浏览:586
asp在线文件管理系统 浏览:468
tks文件如何分解 浏览:132
java7tmd32位 浏览:49
网络公司关键词 浏览:925
vivo手机的便签文件夹是哪个 浏览:672
win10升级助手未激活 浏览:530
浏览器保存密码在哪个文件 浏览:691
sitemap代码 浏览:108

友情链接