1. 求单片机简易数字钟的课程设计 :要求自制一个单片机最小系统,包括串口下载、复位电路,采用内部定时器计
下面的程序配合这个电路运行就是对的
这个是完整的程序,电路图如下
#include<reg52.h>
//定义共阳极字型码0123456789-
unsignedcharcodedispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsignedchartime[]={0,0,0,0};//用来储存分秒
unsignedchardate[]={0,0,0,0};//用来储存日时
unsignedcharyear[]={0,0,0,0};//用来储存年月
typedefstruct__SYSTEMTIME__
{
unsignedcharSecond;
unsignedcharMinute;
unsignedcharHour;
unsignedcharWeek;
unsignedcharDay;
unsignedcharMonth;
unsignedcharYear;
unsignedcharDateString[9];
unsignedcharTimeString[9];
}SYSTEMTIME; //定义的时间类型
SYSTEMTIMEtime1;
sbitDS1302_CLK=P1^6;//实时时钟时钟线引脚
sbitDS1302_IO=P1^7;//实时时钟数据线引脚
sbitDS1302_RST=P1^5;//实时时钟复位线引脚
sbitACC0=ACC^0;
sbitACC7=ACC^7;
sbitP10=P1^0;
sbitP11=P1^1;
sbitP12=P1^2;
sbitP13=P1^3;
sbitP14=P1^4;
//#defineAM(X) X
//#definePM(X) (X+12) //转成24小时制
#defineDS1302_SECOND 0x80//秒寄存器
#defineDS1302_MINUTE 0x82//分寄存器
#defineDS1302_HOUR 0x84
#defineDS1302_WEEK 0x8A
#defineDS1302_DAY 0x86
#defineDS1302_MONTH 0x88
#defineDS1302_YEAR 0x8C
#defineDS1302_RAM(X) (0xC0+(X)*2) //用于计算DS1302_RAM地址的宏
voidDS1302InputByte(unsignedchard) //实时时钟写入一字节(内部函数)
{
unsignedchari;
ACC=d;
for(i=8;i>0;i--)
{
DS1302_IO=ACC0; //相当于汇编中的RRC
DS1302_CLK=1;
DS1302_CLK=0;//发一个高跳变到低的脉冲
ACC=ACC>>1;
}
}
unsignedcharDS1302OutputByte(void) //实时时钟读取一字节(内部函数)
{
unsignedchari;
for(i=8;i>0;i--)
{
ACC=ACC>>1; //相当于汇编中的RRC
ACC7=DS1302_IO;
DS1302_CLK=1;
DS1302_CLK=0;//发一个高跳变到低的脉冲
}
return(ACC);
}
voidWrite1302(unsignedcharucAddr,unsignedcharucDa) //ucAddr:DS1302地址,ucData:要写的数据
{
DS1302_RST=0;
DS1302_CLK=0;
DS1302_RST=1;
DS1302InputByte(ucAddr); //地址,命令
DS1302InputByte(ucDa); //写1Byte数据
DS1302_CLK=1;
DS1302_RST=0;//RST0->1->0,CLK0->1
}
unsignedcharRead1302(unsignedcharucAddr) //读取DS1302某地址的数据
{
unsignedcharucData;
DS1302_RST=0;
DS1302_CLK=0;
DS1302_RST=1;//enable
DS1302InputByte(ucAddr|0x01);//地址,命令
ucData=DS1302OutputByte();//读1Byte数据
DS1302_CLK=1;//RST0->1->0,CLK0->1
DS1302_RST=0;
return(ucData);
}
voidDS1302_SetProtect(bitflag)//是否写保护
{
if(flag)
Write1302(0x8E,0x10);//WP=1,不能写入
else
Write1302(0x8E,0x00);//WP=0,可以写入
}
voidDS1302_SetTime(unsignedcharAddress,unsignedcharValue)//设置时间函数
{
DS1302_SetProtect(0);
Write1302(Address,((Value/10)<<4|(Value%10)));//高4位为十位,低4位为个位
}
voidDS1302_GetTime(SYSTEMTIME*Time)
{
unsignedcharReadValue;
ReadValue=Read1302(DS1302_SECOND);
Time->Second=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);//转换成10进制的秒
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&0xf0)>>4)*10+(ReadValue&0x0F);
}
voidInitial_DS1302(void)
{
unsignedcharSecond=Read1302(DS1302_SECOND);
if(Second&0x80)//如果第七为1(表明没有启动),则启动时钟
DS1302_SetTime(DS1302_SECOND,0);
}
voiddelay(unsignedchari)//延时子程序
{
unsignedcharj;
while((i--)!=0)
{
for(j=625;j>0;j--);
}
}
/*unsignedcharbutton_time(n,x,y)//时钟调整子程序
unsignedcharn,x,y;
{
if(P1^7==0)
{
delay(50);
if(P1^7==0)
{
n++;
if(n==x)
n=0;
while(P1^7==0);
}
}
if(P1^1==0)
{
delay(50);
if(P1^1==0)
{
if(n==0)
n=y;
else
n--;
while(P1^1==0);
}
}
returnn;
}
*/
/*unsignedcharbutton_date(n,x,y)//日期调整子程序
unsignedcharn,x,y;
{
if(P1^7==0)
{
delay(50);
if(P1^7==0)
{
n++;
if(n==x)
n=1;
while(P1^7==0);
}
}
if(P1^1==0)
{
delay(50);
if(P1^1==0)
{
if(n==1)
n=y;
else
n--;
while(P1^1==0);
}
}
returnn;
}*/
voiddisplay1(minute10,minute1,second10,second1) //显示第一页分秒子程序
//unsignedcharsecond10,second1,minute10,minute1;
{
P2=0x08;
P0=dispcode[second1];//显示秒的个位
delay(1);
P2=0x04;
P0=dispcode[second10]; //显示秒的十位
delay(1);
P2=0x02;
P0=dispcode[minute1]; //显示分的个位
delay(1);
P2=0x01;
P0=dispcode[minute10];//显示分的十位
delay(1);
}
voiddisplay2(data10,data1,hour10,hour1) //显示第二页天时子程序
//unsignedchardata10,data1,hour10,hour1;
{
P2=0xf8;
P0=dispcode[data1];//显示天的个位
delay(1);
P2=0xf4;
P0=dispcode[data10]; //显示天的十位
delay(1);
P2=0xf2;
P0=dispcode[hour1]; //显示时的个位
delay(1);
P2=0xf1;
P0=dispcode[hour10];//显示时的十位
delay(1);
}
voiddisplay3(year10,year1,month10,month1) //显示第三页年月子程序
//unsignedcharyear10,year1,month10,month1;
{
P2=0xf2;
P0=dispcode[month1];//显示月的个位
delay(1);
P2=0xf1;
P0=dispcode[month10]; //显示月的十位
delay(1);
P2=0xf8;
P0=dispcode[year1]; //显示月的个位
delay(1);
P2=0xf4;
P0=dispcode[year10];//显示月的十位
delay(1);
}
voidmain()
{
unsignedcharflag=0;
Initial_DS1302(); //初始化DS1302这个时钟芯片,
P10=0;//点亮测试灯
while(1)
{
DS1302_GetTime(&time1); //读取时间参数
time[3]=(time1.Second)%10; //把秒的个位数据存入time[3]
time[2]=(time1.Second)/10; //把秒的十位数据存入time[2]
time[1]=(time1.Minute)%10; //把分的个位数据存入time[1]
time[0]=(time1.Minute)/10; //把分的十位数据存入time[0]
date[3]=(time1.Day)%10;
date[2]=(time1.Day)/10;
date[1]=(time1.Hour)%10;
date[0]=(time1.Hour)/10;
year[3]=(time1.Year)%10;
year[2]=(time1.Year)/10;
year[1]=(time1.Month)%10;
year[0]=(time1.Month)/10;
//display1(time[0],time[1],time[2],time[3]);
if(P11==0)
{
delay(50);
if(P11==0)
{
flag++;
if(flag>2)//flag:1显示第二页日时;2显示第三页年月0:显示第一页分秒
{
flag=0;
}
}
while(P11==0);
}
/*if(P1^6==0) //如果按下TimeSet键一下,开始显示日期,再按一下进入日期跟时钟的调节模式
{
delay(50);
if(P1^6==0)
{
flag++;
if(flag>6)
{
flag=0;
}
}
while(P1^6==0);
}*/
switch(flag)
{
case0:display1(time[0],time[1],time[2],time[3]); //调用子函数display,把存入数组time的数据给显示出来
break;
case1:display2(date[0],date[1],date[2],date[3]); //调用子函数display,把存入数组date的数据给显示出来
break;
case2:display3(year[0],year[1],year[2],year[3]);
break;
/* case3:time1.Month=button_date(time1.Month,13,12); //调整月
DS1302_SetTime(0x88,time1.Month);
display(10,10,date[2],date[3]);
break;
case4:time1.Day=button_date(time1.Day,32,31); //调整日
DS1302_SetTime(0x86,time1.Day);
display(10,10,date[4],date[5]);
break;
case5:time1.Minute=button_time(time1.Minute,60,59); //调整分
DS1302_SetTime(0x82,time1.Minute);
display(time[2],time[3],10,10);
break;
case6:time1.Second=button_time(time1.Second,60,59); //调整秒
DS1302_SetTime(0x80,time1.Second);
display(10,10,time[4],time[5]);
break;*/
}
}
}
2. 单片机89c51的电子时钟课程设计
#include <reg52.h>
#include<stddef.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD1602_FLAG
#define LCD1602_PORT P0
sbit lcd1602_rs=P2^0;
sbit lcd1602_e=P2^2;
sbit lcd1602_rw=P2^1;
sbit lcd1602_busy=P0^7;
sbit key_ch=P3^5;
sbit key_add=P3^6;
sbit key_minus=P3^7;
uchar i,sec,min,h,date,month,flag;
uint year;
uchar *chgstr[7]={" ","sec","min","hour","date","min","year"};
uchar j,k,m,n,o,p;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar timestr[10],datestr[10];
void init();
void delay(uint);
void time_display();
void date_display();
void control();
void time();
/*
************************************
* 函数名称:lcd1602_CheckBusy()
* 函数功能:状态查询
************************************
*/
void lcd1602_CheckBusy()
{
do
{
lcd1602_busy=1;
lcd1602_rs=0;
lcd1602_rw=1;
lcd1602_e=0;
lcd1602_e=1;
}
while(lcd1602_busy);
}
/*
***************************************
* 函数名称: lcd1602_WriteCmd()
* 函数功能:写命令
* 入口参数:命令字
* 出口参数:无
***************************************
*/
void lcd1602_WriteCmd(const uchar cmd)
{
lcd1602_CheckBusy();
lcd1602_rs=0;
lcd1602_rw=0;
lcd1602_e=1;
LCD1602_PORT=cmd;
lcd1602_e=0;
}
/*
*******************************************
* 函数名称:lcd1602_WriteData()
* 函数功能:写数据
* 入口参数:c--待写数据
* 出口参数:无
*********************************************
*/
void lcd1602_WriteData(const uchar c)
{
lcd1602_CheckBusy();
lcd1602_rs=1;
lcd1602_rw=0;
lcd1602_e=1;
LCD1602_PORT=c;
lcd1602_e=0;
}
/*
***********************************************
* 函数名称:lcd1602_Init()
* 函数功能:初始化LCD
* 入口参数:无
* 出口参数:无
***********************************************
*/
void lcd1602_Init()
{
lcd1602_WriteCmd(0x38); //显示模式为8位2行5*7点阵
lcd1602_WriteCmd(0x0c); //display enable,flag enable,flash enable,
lcd1602_WriteCmd(0x06); //flag move to right,screen don't move
lcd1602_WriteCmd(0x01); //clear screen
}
/*
************************************************
* 函数名称:lcd1602_Display()
* 函数功能: 字符显示
* 入口参数:ptr--字符或字符串指针
* 出口参数:无
* 说 明:用户可通过以下方式来调用:
* 1)lcd1602_Display("Hello,world!");
* 2) INT8U 存储类型 txt[]="要显示的字符串";
* 或者 INT8U 存储类型 txt[]={'t','x','t',..,'