没有跟着学校的走反正是之后都要碰到单片机就在B站江科大学的这一手单片机真的是爽翻天属于是 赛高泥嗨铁鸭子哒!!!!!!!!强烈推荐去看,然后这个电子钟是我两天两夜0基础赶出来功能还是有些不完美希望各位大佬进行指证多谢多谢,那就让我们步入正题吧!
首先是proteus 8的仿真电路图
就是用了一个LCD1602液晶显示和一个DS1302的时钟控制芯片来进行操作的
然后是要烧进去的代码啦
?主函数
#include <REGX52.H>
#include "LCD1602.h"
#include "DS1302.h"
unsigned char num,chose,set_time;
void Delay(unsigned int a)
{
uc i, j;
while(a--){
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
unsigned char button()
{
unsigned char num=0;
if(P1_1==0){
Delay(20);
while(P1_1==0);
Delay(20);
num=1;
}
if(P1_3==0){
Delay(20);
while(P1_3==0);
Delay(20);
num=2;
}
if(P1_5==0){
Delay(20);
while(P1_5==0);
Delay(20);
num=3;
}
if(P1_7==0){
Delay(20);
while(P1_7==0);
Delay(20);
num=4;
}
return num;
}
void ShowTime(){
DS1302_ReadTime();
LCD_ShowNum(1,6,time[0],2);
LCD_ShowNum(1,9,time[1],2);
LCD_ShowNum(1,12,time[2],2);
LCD_ShowNum(2,5,time[3],2);
LCD_ShowNum(2,8,time[4],2);
LCD_ShowNum(2,11,time[5],2);
}
void SetTime()
{
if(num==2)
{
set_time++;
if(set_time>5)
{
set_time=0;
}
LCD_ShowNum(2,14,set_time,1);
}
if(num==3)
{
time[set_time]++;
if(time[0]>99)
{
time[0]=0;
}
if(time[1]>12)
{
time[1]=1;
}
if( time[1]==1 || time[1]==3 || time[1]==5 || time[1]==7 || time[1]==8 || time[1]==10 || time[1]==12)
{
if(time[2]>31)
{
time[2]=1;
}
}
else if(time[1]==4 || time[1]==6 || time[1]==9 || time[1]==11)
{
if(time[2]>30)
{
time[2]=1;
}
}
else if(time[1]==2)
{
if(time[0]%4==0)
{
if(time[2]>29)
{
time[2]=1;
}
}
else
{
if(time[2]>28)
{
time[2]=1;
}
}
}
if(time[3]>23)
{
time[3]=0;
}
if(time[4]>59)
{
time[4]=0;
}
if(time[5]>59)
{
time[5]=0;
}
}
if(num==4)
{
time[set_time]--;
if(time[0]<0)
{
time[0]=99;
}
if(time[1]<1)
{
time[1]=12;
}
if( time[1]==1 || time[1]==3 || time[1]==5 || time[1]==7 || time[1]==8 || time[1]==10 || time[1]==12)
{
if(time[2]<1)
{
time[2]=31;
}
if(time[2]>31)
{
time[2]=1;
}
}
else if(time[1]==4 || time[1]==6 || time[1]==9 || time[1]==11)
{
if(time[2]<1)
{
time[2]=30;
}
if(time[2]>30)
{
time[2]=1;
}
}
else if(time[1]==2)
{
if(time[0]%4==0)
{
if(time[2]<1)
{
time[2]=29;
}
if(time[2]>29)
{
time[2]=1;
}
}
else
{
if(time[2]<1)
{
time[2]=28;
}
if(time[2]>28)
{
time[2]=1;
}
}
}
if(time[3]<0)
{
time[3]=23;
}
if(time[4]<0)
{
time[4]=59;
}
if(time[5]<0)
{
time[5]=59;
}
}
LCD_ShowNum(1,6,time[0],2);
LCD_ShowNum(1,9,time[1],2);
LCD_ShowNum(1,12,time[2],2);
LCD_ShowNum(2,5,time[3],2);
LCD_ShowNum(2,8,time[4],2);
LCD_ShowNum(2,11,time[5],2);
}
void main()
{
LCD_Init();
DS1302_Init();
LCD_ShowString(1,4,"20 - - ");
LCD_ShowString(2,3," : : ");
DS1302_SetTime();
while(1)
{
num=button();
if(num==1)
{
if(chose==0)
{
chose=1;
}
else if(chose==1)
{
chose=0;
DS1302_SetTime();
}
}
switch(chose)
{
case 0:
ShowTime();
break;
case 1:
SetTime();
break;
}
}
}
然后是LCD1602的函数部分
#include <REGX52.H>
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_EN=P2^7;
#define LCD_DataPort P0
typedef unsigned char uc;
void LCD_Delay()
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
void LCD_WriteCommand(uc Command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DataPort=Command;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
void LCD_WriteData(uc Data)
{
LCD_RS=1;
LCD_RW=0;
LCD_DataPort=Data;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
void LCD_SetCursor(uc Line, uc Column)
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1));
}
else if(Line==2)
{
LCD_WriteCommand(0x80|(Column-1+0x40));
}
}
void LCD_Init()
{
LCD_WriteCommand(0x38);
LCD_WriteCommand(0x0c);
LCD_WriteCommand(0x06);
LCD_WriteCommand(0x01);
}
void LCD_ShowChar(uc Line, uc Column,char Char)
{
LCD_SetCursor(Line,Column);
LCD_WriteData(Char);
}
void LCD_ShowString(uc Line, uc Column,char *String)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
LCD_WriteData(String[i]);
}
}
int LCD_Pow(int X,int Y)
{
uc i;
int pow=1;
for(i=0;i<Y;i++)
{
pow*=X;
}
return pow;
}
void LCD_ShowNum(uc Line, uc Column,unsigned int Number, uc Length)
{
uc i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
}
}
DS1302的函数部分
#include <REGX52.H>
sbit DS1302_SCLK=P3^5;
sbit DS1302_IO=P3^6;
sbit DS1302_CE=P3^4;
#define SECOND 0x80
#define MINUTE 0x82
#define HOUR 0x84
#define DATE 0x86
#define MONTH 0x88
#define DAY 0x8A
#define YEAR 0x8C
#define WP 0x8E
typedef unsigned char uc;
uc time[]={21,12,30,14,14,14,4};
void DS1302_Init()
{
DS1302_CE=0;
DS1302_SCLK=0;
}
void DS1302_WriteByte(uc Command,Data)
{
uc i;
DS1302_CE=1;
for(i=0;i<8;i++)
{
DS1302_IO=Command&(0x01<<i);
DS1302_SCLK=1;
DS1302_SCLK=0;
}
for(i=0;i<8;i++)
{
DS1302_IO=Data&(0x01<<i);
DS1302_SCLK=1;
DS1302_SCLK=0;
}
DS1302_CE=0;
}
uc DS1302_ReadByte(uc Command)
{
uc i,Data=0x00;
Command|=0x01;
DS1302_CE=1;
for(i=0;i<8;i++)
{
DS1302_IO=Command&(0x01<<i);
DS1302_SCLK=0;
DS1302_SCLK=1;
}
for(i=0;i<8;i++)
{
DS1302_SCLK=1;
DS1302_SCLK=0;
if(DS1302_IO){Data|=(0x01<<i);}
}
DS1302_CE=0;
DS1302_IO=0;
return Data;
}
void DS1302_SetTime()
{
DS1302_WriteByte(WP,0x00);
DS1302_WriteByte(YEAR,time[0]/10*16+time[0]%10);
DS1302_WriteByte(MONTH,time[1]/10*16+time[1]%10);
DS1302_WriteByte(DATE,time[2]/10*16+time[2]%10);
DS1302_WriteByte(HOUR,time[3]/10*16+time[3]%10);
DS1302_WriteByte(MINUTE,time[4]/10*16+time[4]%10);
DS1302_WriteByte(SECOND,time[5]/10*16+time[5]%10);
DS1302_WriteByte(DAY,time[6]/10*16+time[6]%10);
DS1302_WriteByte(WP,0x80);
}
void DS1302_ReadTime()
{
uc temp;
temp=DS1302_ReadByte(YEAR);
time[0]=temp/16*10+temp%16;
temp=DS1302_ReadByte(MONTH);
time[1]=temp/16*10+temp%16;
temp=DS1302_ReadByte(DATE);
time[2]=temp/16*10+temp%16;
temp=DS1302_ReadByte(HOUR);
time[3]=temp/16*10+temp%16;
temp=DS1302_ReadByte(MINUTE);
time[4]=temp/16*10+temp%16;
temp=DS1302_ReadByte(SECOND);
time[5]=temp/16*10+temp%16;
temp=DS1302_ReadByte(DAY);
time[6]=temp/16*10+temp%16;
}
两个函数的头文件部分
typedef unsigned char uc;
void LCD_Init();
void LCD_ShowChar(uc Line, uc Column,char Char);
void LCD_ShowString(uc Line, uc Column,char *String);
void LCD_ShowNum(uc Line, uc Column,unsigned int Number, uc Length);
extern uc time[];
void DS1302_Init(void);
void DS1302_WriteByte(uc Command,Data);
unsigned char DS1302_ReadByte(uc Command);
void DS1302_SetTime(void);
void DS1302_ReadTime(void);
毕竟模块化操作的话更利于单片机的学习要是想要用这个芯片操作起来就是十分的便利了
|