c语言闹钟
⑴ 如何拿单片机用C语言编程做一个闹钟
下面的程序就是用AT89C2051做的数字式时钟,数码管显示,具有闹钟功能,整点报时,几点蜂鸣器响几下,晚上九点到第二天早上六点整点不报时。给你参考一下吧。
#include<AT89X051.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit sda=P3^1;
sbit scl=P3^0;
sbit key1=P1^7;
sbit key2=P1^6;
sbit key3=P1^5;
sbit kz=P3^5;
sbit bs=P3^4;
char shi=12,fen=12,ke1,mm,mi,nh,nf,mn,mms,baos,a;
bit ms,nt,b;
uchar code SEG8[]={ 0x50,0x5F,0x94,0x15,0x1B,
0x31,0x30,0x57,0x10,0x11,
0x40,0x4F,0x84,0x05,0x0B,
0x21,0x20,0x47,0x00,0x01,
0x21,0xa2,0x52,0xff};
//0-9,0.-9.;s.,f.,n 暗
void delay1ms(uchar ms)
{
uchar i;
while(ms--)
{
for(i = 0; i< 250; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
void display(char numh,char numf)
{
uchar qian,,sh,ge;
if(ke1==1)
qian=20;
else if(ke1==2)
qian=21;
else if((ke1==3)||(ke1==4))
qian=22;
else if(ke1==0)
qian=numh/10;
if((ke1==1)||(ke1==2))
=23;
else if(ke1==3)
=20;
else if(ke1==4)
=21;
else if(ke1==0)
{
=numh%10;
if(ms)
=+10;
}
if((ke1==1)||(ke1==3))
{sh=numh/10;ge=numh%10;}
else if((ke1==2)||(ke1==4)||(ke1==0))
{sh=numf/10;ge=numf%10;}
P1=SEG8[qian];
P3_3=0;delay1ms(2);P3_3=1;
P1=SEG8[];
P3_7=0;delay1ms(2);P3_7=1;
P1=SEG8[sh];
P3_2=0;delay1ms(2);P3_2=1;
P1=SEG8[ge];
P3_0=0;delay1ms(2);P3_0=1;
}
///////////x24c02//////////////////
void delay24()
{ ;; }
void init24c02() //初始化
{
sda=1;
delay24();
scl=1;
delay24();
}
void start2() //开始信号
{
sda=1;
delay24();
scl=1;
delay24();
sda=0;
delay24();
}
void stop() //停止
{
sda=0;
delay24();
scl=1;
delay24();
sda=1;
delay24();
}
void respons() //应答
{
uchar i;
scl=1;
delay24();
while((sda==1)&&(i<250))i++;
scl=0;
delay24();
}
void write_byte(char date) // 写数据子函数
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay24();
sda=CY;
delay24();
scl=1;
delay24();
}
scl=0;
delay24();
sda=1;
delay24();
}
uchar read_byte() // 读数据子函数
{
uchar i,k;
scl=0;
delay24();
sda=1;
delay24();
for(i=0;i<8;i++)
{
scl=1;
delay24();
k=(k<<1)|sda;
scl=0;
delay24();
}
return k;
}
///////写数据函数///////////////////
void write_add(uchar address,char date)
{
start2();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
char read_add(uchar address) //读数据函数
{
uchar date;
start2();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start2();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
void keyf(char *con,char up,char don)
{
uint i;
for(i=0;i<800;i++)
{
if((ke1==1)||(ke1==2))
display(shi,fen);
else if((ke1==3)||(ke1==4))
display(nh,nf);
P1=0xff;
if(key1==0)
{
delay1ms(10);
if(key1==0)
{
ke1++;
if(ke1>=5)
ke1=0;
while(!key1);
break;
}
}
if(key2==0)
{
delay1ms(10);
if(key2==0)
{
i=0;
*con+=1;
if(*con>up)
*con=0;
while(!key2);
}
}
if(key3==0)
{
delay1ms(10);
if(key3==0)
{
i=0;
*con-=1;
if(*con<don)
*con=up;
while(!key3);
}
}
}
}
void key()
{
P1=0xff;
if(key1==0)
{
EA=0;
delay1ms(20);
if(key1==0)
{
ke1++;
if(ke1>=5)
ke1=0;
while(!key1);
}
if(ke1==1)
{
keyf(&shi,23,0);
}
if(ke1==2)
{
keyf(&fen,59,0);
}
if(ke1==3)
{
keyf(&nh,23,0);
}
if(ke1==4)
{
keyf(&nf,59,0);
}
P1=0xff;P3=0xff;
write_add(0x01,nh);
delay1ms(5);
write_add(0x03,nf);
ke1=0;
EA=1;
}
}
void Timer0Init(void)
{
TMOD |= 0x01;
TL0 = 0xB0;
TH0 = 0x3C;
TF0 = 0;
TR0 = 1;
EA=1;
ET0=1;
}
void main()
{
P1=0xff;
P3=0xff;
init24c02();
nh=read_add(0x01);
delay1ms(50);
nf=read_add(0x03);
delay1ms(50);
Timer0Init();
while(1)
{
key();
display(shi,fen);
}
}
void tim0 ()interrupt 1
{
TH0=0X3C;
TL0=0XB0;
if(++mms>=10)
{
mms=0;
ms=!ms;
}
if(++mm>=20)
{
mm=0;
if(++mi>=60)
{
mi=0;
if(++fen>=60)
{
fen=0;
if(++shi>=24)
shi=0;
}
}
}
if(fen==0)
{
if((shi>=7)&&(shi<=20))
{
if((baos==0)&&(b==0))
{
baos=shi;
baos=baos*2;
b=1;
}
if(++a==10)
{
a=0;
if(--baos!=0)
{
bs=!bs;
}
else
{
bs=1;
}
}
}
}
else
b=0;
if(nh==shi)
{
if(nf==fen)
{
if(nt==0)
{
P1=0xff;
if(++mn==10)
{
mn=0;
kz=!kz;
}
if(key3==0)
{
nt=1;
kz=1;
}
}
}
else
{
nt=0;
kz=1;
}
}
}
⑵ C语言 闹钟程序
用C语言实现的闹钟程序
#include
#include
#include
#include
#include
void
main()
{
int
gd=DETECT,gm;
int
x=320,y=240,r=200,i,h,m,s,thetamin,thetasec;
struct
time
t;
char
n[12][3]={"3","2","1","12","11","10","9","8","7","6","5","4"};
initgraph(&gd,&gm,"g:\\tc\\bgi");//图形驱动器路径,根据自己的系统更换。
circle(x,y,210);
setcolor(4);
settextstyle(4,0,5);
for(i=0;i<12;i++)
{
if(i!=3)
outtextxy(x+(r-14)*cos(M_PI/6*i)-10,y-(r-14)*sin(M_PI/6*i)-26,n[i]);
else
outtextxy(x+(r-14)*cos(M_PI/6*i)-20,y-(r-14)*sin(M_PI/6*i)-26,n[i]);
}
gettime(&t);
printf("The
current
time
is:
%2d:%02d:%02d.%02d\n",t.ti_hour,
t.ti_min,
t.ti_sec,
t.ti_hund);
while(!kbhit())
{
setcolor(5);
setfillstyle(1,5);
circle(x,y,10);
floodfill(x,y,5);
gettime(&t);
if(t.ti_min!=m)
{
setcolor(0);
line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180
)));
circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))
,10);
line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h
-((m/2)*(M_PI/180))));
circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(
(m/2)*(M_PI/180))),10);
}
if(t.ti_hour>12)
t.ti_hour=t.ti_hour-12;
if(t.ti_hour<4)
h=abs(t.ti_hour-3);
else&n
bsp;
h=15-t.ti_hour;
m=t.ti_min;
if(t.ti_min<=15)
thetamin=(15-t.ti_min)*6;
else
thetamin=450-t.ti_min*6;
if(t.ti_sec<=15)
thetasec=(15-t.ti_sec)*6;
else
thetasec=450-t.ti_sec*6;
setcolor(4);
line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h
-((m/2)*(M_PI/180))));
circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(
(m/2)*(M_PI/180))),10);
line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180
)));
circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))
,10);
setcolor(15);
line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180
)));
delay(1000);
setcolor(0);
line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180
)));
}
}
该程序已在Turbo
C++3.0中通过编译.
⑶ 求一个关于用C语言编写的电子时钟的程序,内容要有闹钟,可以调整时间
#include<reg52.h>
#include<absacc.h>
#include<intrins.h>
#define unit unsigned int
#define uchar unsigned char
//#define HZ 12
sbit key0=P0^0; // 分钟调整
sbit key1=P0^1; // 小时调整
sbit P2_0=P2^7; //秒 指示灯
sbit MN_RXD=P3^6;
sbit MN_TXD=P3^7;
uchar data CLOCK[4]={0,0,0,12};//存放时钟时间(百分秒,秒,分,和时位)
//数码管显示表0-f 灭
uchar code TABLE[]={0xBE,0x06,0xEA,0x6E,0x56,0x7C,0xFC,0x0E,0xFE,0x7E,0x00};
//**********************************
//模拟串口发送一个字节数据 函数
//**********************************
void SendData(unsigned char senddata)
{
unsigned char i;
for(i=0;i<8;i++)
{
if((senddata&0x01)==0)
MN_RXD=0;
else
MN_RXD=1;
_nop_();
MN_TXD=0;
_nop_();
MN_TXD=1;
senddata=senddata>>1;
}
}
//**********************************
//显示程序函数
//**********************************
void display(void)
{
// unsigned int n;
uchar temp;
temp=CLOCK[1]; temp=temp%10; SendData(TABLE[temp]);
temp=CLOCK[1]; temp=temp/10; SendData(TABLE[temp]);
temp=CLOCK[2]; temp=temp%10; SendData(TABLE[temp]);
temp=CLOCK[2]; temp=temp/10; SendData(TABLE[temp]);
temp=CLOCK[3]; temp=temp%10; SendData(TABLE[temp]);
temp=CLOCK[3]; temp=temp/10; SendData(TABLE[temp]);
/*
for(n=0;n<5000;n++);
for(n=0;n<6;n++)
{
SendData(TABLE[10]);
}
*/
}
//**********************************
//按键控制函数
//**********************************
void keycan()
{
unsigned int n;
EA=0;
if(key0==0) // 分钟调整
{
for(n=0;n<10000;n++); //延时去抖动
while(key0==0);
CLOCK[2]=CLOCK[2]+1;
if(CLOCK[2]==60) //到一时
{
CLOCK[2]=0;
}
display();
}
if(key1==0) // 小时调整
{
for(n=0;n<10000;n++); //延时去抖动
while(key1==0);
CLOCK[3]=CLOCK[3]+1;
if(CLOCK[3]==24)
{
CLOCK[3]=0;
}
display();
}
EA=1;
}
//**********************************
//T0中断服务函数
//**********************************
void time0() interrupt 1 //using 1
{
TH0=0xD8; TL0=0xF0; //重置初值
// TH0=0xB1; TL0=0xE0;
//时钟处理
CLOCK[0]=CLOCK[0]+1;
}
//**********************************
//主函数
//**********************************
void main()
{
EA=1;
ET0=1;
TMOD=0x01; //T0方式1定时
TH0=0xD8; TL0=0xF0; //D8F0 定时10ms
// TH0=0xB1; TL0=0xE0; //定时 20ms
TR0=1;
for(;;)
{
if(CLOCK[0]==100) //到一秒 10ms*100
{
CLOCK[0]=0;
P2_0=~P2_0;
CLOCK[1]=CLOCK[1]+1;
if(CLOCK[1]==60) //到一分
{
CLOCK[1]=0;
CLOCK[2]=CLOCK[2]+1;
if(CLOCK[2]==60) //到一时
{
CLOCK[2]=0;
CLOCK[3]=CLOCK[3]+1;
if(CLOCK[3]==24)
{
CLOCK[3]=0;
}
}
}
display();
}
keycan();
}
}