當前位置:首頁 » 編程語言 » 單片機c語言帶聲音提示秒錶模擬實驗
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

單片機c語言帶聲音提示秒錶模擬實驗

發布時間: 2023-04-24 17:02:27

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];
}
}