导航:首页 > 操作系统 > 单片机课程设计下载

单片机课程设计下载

发布时间:2023-03-18 03:54:41

单片机课程设计

刚给一个兄弟做的

KEYVAL EQU30H

KEYTM EQU31H

KEYSCAN EQU32H

DAT EQU33H

SCANLED EQU39H

CLK EQU77H

SEC EQU78H

MIN EQU79H

HOUR EQU7AH

PAUSE BIT00H

DOT BIT01H

ORG0000H

LJMPMAIN

ORG000BH

LJMPT0ISR ;50ms定时

ORG001BH

LJMPT1ISR ;扫描显示

ORG0030H

MAIN:

MOVSP,#5FH

MOVTMOD,#11H

MOVTH0,#03CH

MOVTL0,#0B0H

MOVTH1,#0ECH

MOVTL1,#078H

MOVKEYVAL,#0

MOVSCANLED,#0

MOV33H,#10H

MOV34H,#10H

MOV35H,#10H

MOV36H,#10H

MOV37H,#10H

MOV38H,#10H

MOVSEC,#0

MOVMIN,#0

MOVHOUR,#0

MOVCLK,#0

CLRPAUSE

SETBEA

SETBET1

SETBTR1

LOOP:

LCALLKEYSEL

MOVA,KEYVAL

CJNEA,#0FFH,LOOP1

SJMPLOOP

LOOP1:

CJNEA,#10,LOOP2 ;“ON”启动

SETBTR0

SETBET0

SETBPAUSE

SJMPLOOP

LOOP2:

CJNEA,#11,LOOP3 ;“=”清零

MOVSEC,#0

MOVMIN,#0

MOVHOUR,#0

LCALLDISCHG

SJMPLOOP

LOOP3:

CJNEA,#15,LOOP4 ;“+”暂停

CLRTR0

CLRET0

CLRPAUSE

SJMPLOOP

LOOP4:

CJNEA,#14,LOOP5 ;“-”清显示暂停

MOV33H,#10H

MOV34H,#10H

MOV35H,#10H

MOV36H,#10H

MOV37H,#10H

MOV38H,#10H

CLRTR0

CLRET0

CLRPAUSE

SJMPLOOP

LOOP5:

CJNEA,#10,LOOP6 ;数字键

LOOP6:

JCLOOP7

LJMPLOOP

LOOP7:

JNBPAUSE,LOOP8 ;暂停状态可以输入数字键

LJMPLOOP

LOOP8:

MOV33H,34H

MOV34H,35H

MOV35H,36H

MOV36H,37H

MOV37H,38H

MOV38H,KEYVAL

MOVA,34H

SWAPA

ORLA,33H

LCALLBCDH

MOVHOUR,A

MOVA,36H

SWAPA

ORLA,35H

LCALLBCDH

MOVMIN,A

MOVA,38H

SWAPA

ORLA,37H

LCALLBCDH

MOVSEC,A

LJMPLOOP

;------------------

;BCD转换为十六进制

BCDH:

MOVB,#10H

DIVAB

MOVR7,B

MOVB,#10

MULAB

ADDA,R7

RET

;------------------

;十六进制转换为BCD

HBCD:

MOVB,#10

DIVAB

SWAPA

ORLA,B

RET

;------------------

KEYSEL:

MOV KEYVAL,#0

MOV KEYSCAN,#0EFH

LCALL GETKEY

MOV A,KEYTM

JZ KEYS1

MOV KEYVAL,A

SJMP KEYRTN

KEYS1:

MOV KEYSCAN,#0DFH

LCALL GETKEY

MOV A,KEYTM

JZ KEYS2

CLR C

ADD A,#4

MOV KEYVAL,A

SJMP KEYRTN

KEYS2:

MOV KEYSCAN,#0BFH

LCALL GETKEY

MOV A,KEYTM

JZ KEYS3

CLR C

ADD A,#8

MOV KEYVAL,A

SJMP KEYRTN

KEYS3:

MOV KEYSCAN,#7FH

LCALL GETKEY

MOV A,KEYTM

JZ KEYRTN

CLR C

ADD A,#12

MOV KEYVAL,A

KEYRTN:

LCALLCHGKEY

RET

;--------------------

GETKEY:

MOVKEYTM,#0

MOV A,KEYSCAN

MOV P3,A

NOP

MOV A,P3

ANL A,#0FH

XRL A,#0FH

JZ NOKEY

MOVR2,#10

LCALLDELAY

MOV A,P3

ANL A,#0FH

XRL A,#0FH

JZ NOKEY

MOV A,P3

ANL A,#0FH

MOV R7,A

SF:

MOV A,P3

ANL A,#0FH

XRL A,#0FH

JNZ SF

MOV A,R7

CJNE A,#0EH,NK1

MOV KEYTM,#1

SJMP NOKEY

NK1:

CJNE A,#0DH,NK2

MOV KEYTM,#2

SJMP NOKEY

NK2:

CJNE A,#0BH,NK3

MOV KEYTM,#3

SJMP NOKEY

NK3:

CJNE A,#07H,NOKEY

MOV KEYTM,#4

NOKEY: RET

;--------------------

DELAY:

MOVR3,#50

DELAY1:

MOVR4,#100

DJNZR4,$

DJNZR3,DELAY1

DJNZR2,DELAY

RET

;--------------------

T0ISR:

PUSHACC

CLRTR0

MOVTH0,#3CH

MOVTL0,#0B0H

SETBTR0

INCCLK

MOVA,CLK

CJNEA,#20,T0ISRE

MOVCLK,#0

INCSEC

MOVA,SEC

CJNEA,#60,T0ISRE

MOVSEC,#0

INCMIN

MOVA,MIN

CJNEA,#60,T0ISRE

MOVMIN,#0

INCHOUR

MOVA,HOUR

CJNEA,#24,T0ISRE

MOVSEC,#0

MOVMIN,#0

MOVHOUR,#0

T0ISRE:

LCALLDISCHG

POPACC

RETI

;--------------------

DISCHG:

MOVA,HOUR

LCALLHBCD

PUSHACC

ANLA,#0FH

MOV34H,A

POPACC

ANLA,#0F0H

SWAPA

MOV33H,A

MOVA,MIN

LCALLHBCD

PUSHACC

ANLA,#0FH

MOV36H,A

POPACC

ANLA,#0F0H

SWAPA

MOV35H,A

MOVA,SEC

LCALLHBCD

PUSHACC

ANLA,#0FH

MOV38H,A

POPACC

ANLA,#0F0H

SWAPA

MOV37H,A

RET

;--------------------

T1ISR:

PUSHACC

CLRTR1

MOVTH1,#0ECH

MOVTL1,#78H

SETBTR1

MOVDPTR,#LEDTAB

T100:

MOVR0,#DAT

MOVA,SCANLED

ADDA,R0

MOVR0,A

MOVA,SCANLED

JNZT101

MOVP2,#01H

CLRDOT

SJMPT1DIS

T101:

DECA

JNZT102

MOVP2,#02H

SETBDOT

SJMPT1DIS

T102:

DECA

JNZT103

MOVP2,#04H

CLRDOT

SJMPT1DIS

T103:

DECA

JNZT104

MOVP2,#08H

SETBDOT

SJMPT1DIS

T104:

DECA

JNZT105

MOVP2,#10H

CLRDOT

SJMPT1DIS

T105:

MOVP2,#20H

CLRDOT

T1DIS:

MOVA,@R0

MOVCA,@A+DPTR

JNBDOT,T1DIS1

ORLA,#01H

T1DIS1:

CPLA

MOVP0,A

INCSCANLED

MOVA,SCANLED

CJNEA,#6,T1END

MOVSCANLED,#0

T1END:

POPACC

RETI

;--------------------

CHGKEY:

MOVA,KEYVAL

JZKV16

DECA

JNZKV01

MOVKEYVAL,#7

RET

KV01:

DECA

JNZKV02

MOVKEYVAL,#4

RET

KV02:

DECA

JNZKV03

MOVKEYVAL,#1

RET

KV03:

DECA

JNZKV04

MOVKEYVAL,#10

RET

KV04:

DECA

JNZKV05

MOVKEYVAL,#8

RET

KV05:

DECA

JNZKV06

MOVKEYVAL,#5

RET

KV06:

DECA

JNZKV07

MOVKEYVAL,#2

RET

KV07:

DECA

JNZKV08

MOVKEYVAL,#0

RET

KV08:

DECA

JNZKV09

MOVKEYVAL,#9

RET

KV09:

DECA

JNZKV10

MOVKEYVAL,#6

RET

KV10:

DECA

JNZKV11

MOVKEYVAL,#3

RET

KV11:

DECA

JNZKV12

MOVKEYVAL,#11

RET

KV12:

DECA

JNZKV13

MOVKEYVAL,#12

RET

KV13:

DECA

JNZKV14

MOVKEYVAL,#13

RET

KV14:

DECA

JNZKV15

MOVKEYVAL,#14

RET

KV15:

DECA

JNZKV16

MOVKEYVAL,#15

RET

KV16:

MOVKEYVAL,#0FFH

RET

;--------------------

LEDTAB: DB0FCH ;"0" 00H

DB60H ;"1" 01H

DB0DAH ;"2" 02H

DB0F2H ;"3" 03H

DB66H ;"4" 04H

DB0B6H ;"5" 05H

DB0BEH ;"6" 06H

DB0E0H ;"7" 07H

DB0FEH ;"8" 08H

DB0F6H ;"9" 09H

DB0EEH ;"A" 0AH

DB3EH ;"B" 0BH

DB9CH ;"C" 0CH

DB7AH ;"D" 0DH

DB9EH ;"E" 0EH

DB8EH ;"F" 0FH

DB00H ;"" 10H

;--------------------

⑵ 求单片机简易数字钟的课程设计 :要求自制一个单片机最小系统,包括串口下载、复位电路,采用内部定时器计

下面的程序配合这个电路运行就是对的

这个是完整的程序,电路图如下

#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;*/

}

}

}

⑶ 单片机课程设计(数字时钟) 麻烦大家不要发链接 直接帮我把程序写过来。

哈哈有个1602显示的不过程序太长贴不上给你个数码管的吧不行再联系

1302.c

#include<DS1302.h>

#include<key.h>

ucharbit_ser[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};

ucharseven_seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

/***********************时间显示*****************/

voidtimer0_init(void) //T0初始化函数,用于时间的动态显示

{

TMOD=0x21;

TL0=(65536-5000)%256;

TH0=(65536-5000)/256;

EA=1;

ET0=1;

TR0=1;

}

voidtimer0_isr(void)interrupt1 //T0中断处理函数

{

charflag; //flag用于表示调整时闪烁的亮或灭

TR0=0;

TL0=(65536-5000)%256;

TH0=(65536-5000)/256;

TR0=1;

flag=x/100*0xff; //设置闪烁标志,如果x大于100则flag为0xff,小于100则为0x00

x++;

if(x>200)

x=0;

switch(i)

{

case0:

P2=bit_ser[0];

if(setflag==3) //根据setflag的值判断当前位是否需要闪烁

P0=flag|seven_seg[dis_buffer[0]];

else

P0=seven_seg[dis_buffer[0]];

break;

case1:

P2=bit_ser[1];

if(setflag==3)

P0=flag|seven_seg[dis_buffer[1]];

else

P0=seven_seg[dis_buffer[1]];

break;

case2:

P2=bit_ser[2];

if(setflag==2)

P0=flag|seven_seg[dis_buffer[2]];

else

P0=seven_seg[dis_buffer[2]];

break;

case3:

P2=bit_ser[3];

if(setflag==2)

P0=flag|seven_seg[dis_buffer[3]];

else

P0=seven_seg[dis_buffer[3]];

break;

case4:

P2=bit_ser[4];

if(setflag==1)

P0=flag|seven_seg[dis_buffer[4]];

else

P0=seven_seg[dis_buffer[4]];

break;

case5:

P2=bit_ser[5];

if(setflag==1)

P0=flag|seven_seg[dis_buffer[5]];

else

P0=seven_seg[dis_buffer[5]];

break;

}

i++;

if(i>=6)

{

i=0;

if(j==10)

{

j=0;

if(setflag==0)

DS1302_GetTime(&Time); //如果setflag是0,就从1302中读出时间,因为setflag不是0时,说明处于调整状态,不需要读时间

dis_buffer[5]=Time.Second%10; //把当前时间放入显示缓冲区

dis_buffer[4]=Time.Second/10;

dis_buffer[3]=Time.Minute%10;

dis_buffer[2]=Time.Minute/10;

dis_buffer[1]=Time.Hour%10;

dis_buffer[0]=Time.Hour/10;

}

j++;

}

}

voidmain()

{

Initial_DS1302(Time);

timer0_init();

while(1)

{

set_down();

timer_down();

up_down();

down_down();

beepflag_down();

if(setflag==0&&Time.Hour==romhour&&Time.Minute==romminute&&Beepflag==1) //判断蜂鸣器是否要响

Beep=!Beep;

}

}

//key.c

#include<reg51.h>

#defineucharunsignedchar

#defineuintunsignedint

uchari=0,j=0,x=0,setflag,flag_set,flag_timer;//setflag用来表示调整的位置,flag_set和flag_timer分别表示当前处于调整状态还是定时状态

SYSTEMTIMETime={0,20,15,3,30,6,10}; //系统时间的初始值2010年6月30日星期三,15时20分0秒

chardis_buffer[6]; //存放显示数据的缓冲区

sbitBeep_flag=P3^2; //蜂鸣器的接口

sbitkey_timer=P3^4; //定时按钮

sbitkey_set=P3^5; //调整按钮

sbitkey_up=P3^6; //增加按钮

sbitkey_down=P3^7; //减小按钮

charromhour,romminute,romsec; //分别存放定时的时,分,秒

bitBeepflag; //标记闹钟是否开启

//延时函数

voiddelays(ucharx)

{

while(x)x--;

}

//设置键的处理函数

voidset()

{

setflag++;

flag_set=1;

if(setflag>=4)

{

setflag=0;

flag_set=0;

Initial_DS1302(Time);

}

}

//定时间的处理函数

voidtimer()

{

setflag++;

flag_timer=1;

if(setflag==1)

{

Time.Hour=romhour;

Time.Minute=romminute;

Time.Second=romsec;

}

elseif(setflag>=4)

{

setflag=0;

flag_timer=0;

romhour=Time.Hour;

romminute=Time.Minute;

romsec=Time.Second;

}

}

//增加键的处理函数

voip()

{

switch(setflag)

{

case0:

break;

case1:

Time.Second++;

if(Time.Second>=60)

Time.Second=0;

break;

case2:

Time.Minute++;

if(Time.Minute>=60)

Time.Minute=0;

break;

case3:

Time.Hour++;

if(Time.Hour>=24)

Time.Hour=0;

break;

}

}

//减小键的处理函数

voiddown()

{

switch(setflag)

{

case0:

break;

case1:

Time.Second--;

if(Time.Second<0)

Time.Second=59;

break;

case2:

Time.Minute--;

if(Time.Minute<0)

Time.Minute=59;

break;

case3:

Time.Hour--;

if(Time.Hour<0)

Time.Hour=23;

break;

}

}

//设置键的扫描函数

voidset_down()

{

if(key_set==0&&flag_timer==0)

{

delays(100);

if(key_set==0)

{

set();

}

while(!key_set);

}

}

//定时键的扫描函数

voidtimer_down()

{

if(key_timer==0&&flag_set==0)

{

delays(100);

if(key_timer==0)

{

timer();

}

while(!key_timer);

}

}

//增加键的扫描函数

voip_down()

{

if(key_up==0&&setflag!=0)

{

delays(100);

if(key_up==0)

{

up();

while(!key_up);

}

}

}

//减少键的处理函数

voiddown_down()

{

if(key_down==0&&setflag!=0)

{

delays(100);

if(key_down==0)

{

down();

while(!key_down);

}

}

}

//定时开关的扫描处理函数

voidbeepflag_down()

{

if(Beep_flag==0)

{

delays(100);

{

Beepflag=!Beepflag;

while(!Beep_flag);

}

}

}

//ds1302.h

#ifndef_REAL_TIMER_DS1302

#define_REAL_TIMER_DS1302

#include<REG51.h>

sbitDS1302_CLK=P1^1;//实时时钟时钟线引脚

sbitDS1302_IO=P1^2;//实时时钟数据线引脚

sbitDS1302_RST=P1^3;//实时时钟复位线引脚

sbitACC0=ACC^0;

sbitACC7=ACC^7;

sbitBeep=P2^7;

typedefstruct__SYSTEMTIME__

{ charSecond;

charMinute;

charHour;

charWeek;

charDay;

charMonth;

charYear;

}SYSTEMTIME; //定义的时间类型

#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,0x80);//WP=1,不能写入

else

Write1302(0x8E,0x00);//WP=0,可以写入

}

voidDS1302_SetTime(unsignedcharAddress,unsignedcharValue)//设置时间函数

{ DS1302_SetProtect(0);

Write1302(Address,((Value/10)<<4|(Value%10)));//高4位为十位,低4位为个位

DS1302_SetProtect(1);

}

//获取时间函数,从DS1302内读取时间然后存入Time内

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&0x70)>>4)*10+(ReadValue&0x0F);

}

//利用STime初始化DS1302

voidInitial_DS1302(SYSTEMTIMESTime)

{ unsignedcharSecond=Read1302(DS1302_SECOND);

if(Second&0x80) DS1302_SetTime(DS1302_SECOND,0);//如果第七为1(表明没有启动),则启动时钟

DS1302_SetTime(DS1302_SECOND,STime.Second); //设定起始时间

DS1302_SetTime(DS1302_MINUTE,STime.Minute);

DS1302_SetTime(DS1302_HOUR,STime.Hour);

DS1302_SetTime(DS1302_DAY,STime.Day);

DS1302_SetTime(DS1302_MONTH,STime.Month);

DS1302_SetTime(DS1302_YEAR,STime.Year);

DS1302_SetTime(DS1302_WEEK,STime.Week);

}

#endif

阅读全文

与单片机课程设计下载相关的资料

热点内容
淮南程序员接私活项目 浏览:480
怎样加密自己的密码 浏览:521
安卓怎么关权限保护隐私 浏览:390
海牛微视app怎么用 浏览:70
单片机怎样选变压器 浏览:829
癌症pdf 浏览:725
云服务器镜像批量部署环境 浏览:683
安卓手机浏览器能访问什么网站 浏览:254
找不到网站的服务器ip地址该如何解决 浏览:743
算法十个数降序排列 浏览:95
基于单片机的老年人健康监测系统 浏览:706
python入门经典pdf下载 浏览:17
东芝变频2p空调压缩机 浏览:227
自家wifi怎么能加密 浏览:644
红米k40加密门禁卡 浏览:847
什么样的源码好看 浏览:156
手机主服务器有什么用 浏览:612
程序编写命令 浏览:597
android发送心跳包 浏览:385
指标源码和原理 浏览:700