1. 用51单片机c语言版设计秒表倒计时
K1 EQU P1.0
K2 EQU P1.1
K3 EQU P1.2
ORG 0000H
LJMP MAIN
ORG 000BH
LJMP T0ISR
ORG 0030H
MAIN:
MOV TMOD,#01H
MOV TH0,#HIGH(65536-10000)
MOV TL0,#LOW(65536-10000)
SETB ET0
SETB TR0
SETB EA
MOV R0,#9
MOV R1,#9
MOV R2,#0
MOV R3,#99
CLR 00H
CLR 01H
LOOP:
JB K1,LP01
JNB K1,$
CLR 01H
INC R3
CJNE R3,#100,LOOP0
MOV R3,#0
SJMP LOOP0
LP01:
JB K2,LP02
JNB K2,$
CLR 01H
CJNE R3,#0,LP011
MOV R3,#99
SJMP LOOP0
LP011:
DEC R3
SJMP LOOP0
LP02:
JB K3,LOOP
JNB K3,$
SETB 01H
SJMP LOOP
LOOP0:
MOV A,R3
MOV B,#10
DIV AB
MOV R0,B
MOV R1,A
SJMP LOOP
T0ISR:
CLR TR0
MOV TH0,#HIGH(65536-10000)
MOV TL0,#LOW(65536-10000)
SETB TR0
MOV DPTR,#TABLE
T0C:
CPL 00H
JB 00H,T001
MOV P3,#0FDH
MOV A,R1
MOVC A,@A+DPTR
MOV P2,A
SJMP T002
T001:
MOV P3,#0FEH
MOV A,R0
MOVC A,@A+DPTR
MOV P2,A
T002:
JNB 01H,T0E
INC R2
MOV A,R2
CJNE A,#100,T0E
MOV R2,#0
MOV A,R0
JZ T003
DEC R0
SJMP T0E
T003:
MOV A,R1
JZ T004
DEC R1
MOV R0,#9
T0E:
RETI
T004:
CLR 01H
MOV R4,#16
T005:
CPL P0.0
LCALL DELAY
DJNZ R4,T005
RETI
DELAY:
MOV R6,#200
DLY:
MOV R7,#250
DJNZ R7,$
DJNZ R6,DLY
RET
TABLE: ; 共阴极数码管显示代码表
DB 3FH,06H,5BH,4FH,66H ;01234
DB 6DH,7DH,07H,7FH,6FH ;56789
END
2. 用c语言编写一个有滴答声音的全屏模拟时钟。
我在TC上调试通过了,不过时间好像过了额...
/*开发环境:turbo c 2.0模拟时钟转动程序代码*/
#include"graphics.h"
#include"math.h"
#include"dos.h"
#define pi 3.1415926
#define X(a,b,c) x=a*cos(b*c*pi/180-pi/2)+300
#define Y(a,b,c) y=a*sin(b*c*pi/180-pi/2)+240
#define d(a,b,c) X(a,b,c);Y(a,b,c);line(300,240,x,y)
void init() /*划时钟边框函数*/
{
int i,l,x1,x2,y1,y2;
setbkcolor(1);
circle(300,240,200);
circle(300,240,205);
circle(300,240,5);
for(i=0;i<60;i++) /*划钟点上的短线*/
{
if(i%5==0)
l=15;
else
l=5;
x1=200*sin(i*6*pi/180)+300;
y1=200*cos(i*6*pi/180)+240;
x2=(200-l)*sin(i*6*pi/180)+300;
y2=(200-l)*cos(i*6*pi/180)+240;
line(x1,y1,x2,y2);
}
}
main()
{
int x,y,i,k=1;
int gdriver=9,gmode=2;
unsigned char h,m,s;
int o,p,q;
float n;
struct time t[1];
struct date d[1];
initgraph(&gdriver,&gmode,"c:\\tc");
initgraph(&gdriver,&gmode,"c:\\tc");
for(i=0;i<=6;i++)
{
settextstyle(TRIPLEX_FONT,HORIZ_DIR,i); /*控制输出字符的字体,方向,大小*/
cleardevice();
settextjustify(1,1); /*在指定坐标上输出字符串*/
outtextxy(300,80,"12") ;
outtextxy(300,390,"6");
outtextxy(140,230,"9");
outtextxy(460,230,"3");
outtextxy(380,100,"1");
outtextxy(220,100,"11");
outtextxy(430,160,"2");
outtextxy(430,310,"4");
outtextxy(380,370,"5");
outtextxy(220,370,"7");
outtextxy(160,160,"10");
outtextxy(160,310,"8");
}
init();
setwritemode(1); /*设置画线的输出模式*/
if(k!=0)
{
getdate(d); /*获得系统日期函数*/
o=d[0].da_year;
p=d[0].da_mon;
q=d[0].da_day;
gettime(t); /*获得系统时间函数*/
h=t[0].ti_hour;
m=t[0].ti_min;
s=t[0].ti_sec;
}
setcolor(7); /*设置时针颜色*/
n=(float)h+(float)m/60;
d(150,n,30); /*画出时针*/
setcolor(14); /*设置分针颜色*/
d(170,m,6); /*画出分针*/
setcolor(4); /*设置秒针颜色*/
d(190,s,6); /*画出秒针*/
while(!kbhit()) /*控制程序按下任意键退出*/
{
while(t[0].ti_sec==s)
gettime(t);
gotoxy(44,18); /*使光标移动到指定坐标*/
printf("\b\b\b\b\b\b\b\b\b"); /*退格,使表示时间的字符串不断变化*/
sound(400); /*按给定的频率打开PC扬声器*/
delay(70); /*中断程序的执行,时间为70毫秒*/
sound(200);
delay(30);
nosound(); /*按给定的频率关闭PC扬声器*/
setcolor(4);
d(190,s,6);
s=t[0].ti_sec;
d(190,s,6);
if(t[0].ti_min!=m)
{
setcolor(14);
d(170,m,6);
m=t[0].ti_min;
d(170,m,6);
}
if(t[0].ti_hour!=h)
{
setcolor(7);
d(150,h,30);
h=t[0].ti_hour;
d(150,h,30);
sound(1000);
delay(240);
nosound();
delay(140);
sound(2000);
delay(240);
nosound();
}
if(s<10) /*用字符的形式输出时间*/
{ if(m<10)
printf("%u:0%u:0%u",h,m,s);
else
printf("%u:%u:0%u",h,m,s);
}
else
{ if(m<10)
printf("%u:0%u:%u",h,m,s);
else
printf("%u:%u:%u",h,m,s);
}
gotoxy(34,19); /*在指定坐标上输出日期*/
printf("%d年%d月%d日",o,p,q);
printf("\b\b\b\b\b\b\b\b\b");
}
getch();
closegraph();
}
3. c语言 提示时间 程序(带声音提示时间、就是闹铃)要在DOS在运行的。做好了加50分
#include <at89x52.h>
#include <DS1302.h>
//定义共阳极字型码0123456789-
unsigned char code dispcode[]=;
unsigned char time[]=;//用来储存时间
unsigned char date[]=;//用来储存日期
unsigned char year[]=;//用来储存年份
unsigned char week[]=;//用来储存星期
SYSTEMTIME time1; //在ds1302.h文件中已经定义了一个名字为SYSTEMTIME的结构体,在这里我们定义一个变量名
//为time的SYSTEMTIME结构体
void delay(unsigned char i) //延时子程序
{
unsigned char j;
while((i--)!=0)
{
for(j=123;j>0;j--);
}
}
unsigned char button_time(n,x,y) //时钟调整子程序
unsigned char n,x,y;
{
if(P1_7==0)
{
delay(50);
if(P1_7==0)
{
n++;
if(n==x)
n=0;
while(P1_7==0); //等待按键释放
}
}
if(P1_5==0)
{
delay(50);
if(P1_5==0)
{
if(n==0)
n=y;
else
n--;
while(P1_5==0);
}
}
return n;
}
unsigned char button_date(n,x,y) //日期调整子程序
unsigned char n,x,y;
{
if(P1_7==0)
{
delay(50);
if(P1_7==0)
{
n++;
if(n==x)
n=1;
while(P1_7==0);
}
}
if(P1_5==0)
{
delay(50);
if(P1_5==0)
{
if(n==1)
n=y;
else
n--;
while(P1_5==0);
}
}
return n;
}
unsigned char button_year(n,x,y) //年份调整子程序
unsigned char n,x,y;
{
if(P1_7==0)
{
delay(50);
if(P1_7==0)
{
n++;
if(n==x)
n=2;
while(P1_7==0);
}
}
if(P1_5==0)
{
delay(50);
if(P1_5==0)
{
if(n==2)
n=y;
else
n--;
while(P1_5==0);
}
}
return n;
}
void display(minute10,minute1,hour10,hour1) //显示子程序
unsigned char minute10,minute1,hour10,hour1;
{
P2=0xfe;
P0=dispcode[minute10];//显示分的十位
delay(1);
P2=0xfd;
P0=dispcode[minute1]; //显示分的个位
delay(1);
P2=0xfb;
P0=dispcode[hour10]; //显示时的十位
delay(1);
P2=0xf7;
P0=dispcode[hour1]; //显示时的个位
delay(1);
}
void display1(week1)
unsigned char week1;
{
P2=0xef;
P0=dispcode[week1];
delay(1);
}
void main()
{
unsigned char flag,aa=2,bb=0;
Initial_DS1302(); //初始化DS1302这个时钟芯片,
while(1)
{
DS1302_GetTime(&time1); //读取时间参数
time[3]=(time1.Minute)%10; //把分的个位数据存入time[3]
time[2]=(time1.Minute)/10; //把分的十位数据存入time[2]
time[1]=(time1.Hour)%10; //把时的个位数据存入time[1]
time[0]=(time1.Hour)/10; //把时的十位数据存入time[0]
date[3]=(time1.Day)%10;
date[2]=(time1.Day)/10;
date[1]=(time1.Month)%10;
date[0]=(time1.Month)/10;
year[1]=(time1.Year)%10;
year[0]=(time1.Year)/10;
week[0]=(time1.Week);
display1(week[0]); //显示星期
if(P1_4==0) //如果按下Time Start键一下,时钟开始正常显示时间,再按一下,显示日期,再按一下,显示年份
{
delay(50);
if(P1_4==0)
{
flag++;
if(flag>2)
{
flag=0;
}
}
while(P1_4==0);
}
if(P1_6==0) //如果按下Time Set键一下,开始显示日期,再按一下开始显示年份,再按一下,进入年份,日期,星期和时间的调节模式
{
delay(50);
if(P1_6==0)
{
flag++;
if(flag>8)
{
flag=0;
}
}
while(P1_6==0);
}
switch(flag)
{
case 0:display(time[0],time[1],time[2],time[3]); //调用子函数display,把存入数组time的数据给显示出来
break;
case 1:display(date[0],date[1],date[2],date[3]); //调用子函数display,把存入数组date的数据给显示出来
break;
case 2:display(aa,bb,year[0],year[1]); //调用子函数display,把存入数组year的数据给显示出来
break;
case 3:time1.Year=button_year(time1.Year,2099,2098); //调整年
DS1302_SetTime(0x8c,time1.Year);
display(aa,bb,year[0],year[1]);
break;
case 4:time1.Month=button_date(time1.Month,13,12); //调整月
DS1302_SetTime(0x88,time1.Month);
display(date[0],date[1],10,10);
break;
case 5:time1.Day=button_date(time1.Day,32,31); //调整日
DS1302_SetTime(0x86,time1.Day);
display(10,10,date[2],date[3]);
break;
case 6:time1.Week=button_date(time1.Week,8,7); //调整星期
DS1302_SetTime(0x8a,time1.Week);
display1(week[0]);
break;
case 7:time1.Hour=button_time(time1.Hour,24,23); //调整时
DS1302_SetTime(0x84,time1.Hour);
display(time[0],time[1],10,10);
break;
case 8:time1.Minute=button_time(time1.Minute,60,59); //调整分
DS1302_SetTime(0x82,time1.Minute);
display(10,10,time[2],time[3]);
break;
}
}
}
4. 怎么用c语言写单片机89C51的程序,要写的程序是,0~99秒的秒表,到99s停止,并且有滴的一声
可根据以下程序修改:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar a , b=0;
sbit wei1=P1.0;
sbit wei2=P1.1;
//共阴数码管七段码
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f,0x77};
void Delay1ms(uint i) //1ms延时程序
{
uint j;
for(;i>0;i--)
{
for(j=0;j<164;j++)
{;}
}
}
void display( uchar a)) //显示程序
{
P0= table[a/10];//十位
wei1=1;
Delay1ms(2) ;
wei1=0;
P0= table[a%10];//个位
wei2=1;
Delay1ms(2) ;
wei2=0;
}
void main(void )//主程序
{
TMOD=0x01;// 定时器1工作方式一
ET0=1;
EA=1;
TH0=(65536-50000)/256;//定时50ms
TL0=(65536-50000)%256;
TR0=1;
while(1)
{
display(b);
}
}
//中断子程序
//因为 中断程序很短,只花不到1ms,而中断间隔50ms,完全没必要关中断
void Timer0(void) interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
a++;
if(a==100)//100次是5 秒
{ a=0;b++;}
if(b==100)
{ b=0;}
}
5. 用C语言编写秒表的程序(单片机控制的时钟设计)
这个程序可以实现秒的计时,按键控制开始、暂停、清零功能,更多功能自己在看清程序的基础上进行改进。
#include <reg51.H>
sbit P3_5 =P3^5;
unsigned char code dispcode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0x00};
unsigned char second;
unsigned char keycnt;
unsigned int tcnt;
void main(void)
{
unsigned char i,j;
TMOD=0x02;
ET0=1;
EA=1;
second=0;
P1=dispcode[second/10];
P2=dispcode[second%10];
while(1)
{
if(P3_5==0)
{
for(i=20;i>0;i--)
for(j=248;j>0;j--);
if(P3_5==0)
{
keycnt++;
switch(keycnt)
{
case 1:
TH0=0x06;
TL0=0x06;
TR0=1;
break;
case 2:
TR0=0;
break;
case 3:
keycnt=0;
second=0;
P1=dispcode[second/10];
P2=dispcode[second%10];
break;
}
while(P3_5==0);
}
}
}
}
void t0(void) interrupt 1 using 0
{
tcnt++;
if(tcnt==4000)
{
tcnt=0;
second++;
if(second==100)
{
second=0;
}
P1=dispcode[second/10];
P2=dispcode[second%10];
}
}