這個是我寫的作業的方案,單片機課程結課作業,以下是我寫的設計方案,寫的不咋滴 歡迎拍磚。。
一、詳細功能設計
1、基于51單片機倒計時秒表,最大值為9999秒,計時單位為0.1秒;
2、計時的初始值為組員學號后4位,鍵盤啟動/停止計時;
3、設計多個按鍵,使用鍵盤選擇不同成員的學號作為初始值。
二、8031最小系統設計
1、最小系統設計原理圖如圖1.0所示:
2、8031最小系統概述;
8031最小系統包含晶振電路與復位電路。 晶振選擇12MHZ晶振,晶振電容使用30pf電容瓷片電容。復位電路選擇上電復位。
三、硬件設計
1、數碼管電路
在本設計中,使用7段共陰極數碼管,選擇數碼管的型號為7SEG-MPX6-CC;
2、數碼管片選電路;
由于本設計需要顯示五位數字,同時為了驅動數碼管,使用了74HC154芯片。與此同時,使用74HC154芯片還可以擴展數碼管顯示數字的個數。
三、軟件設計
1、程序流程圖如圖3.0所示:
圖3.0 程序流程圖
2、程序流程說明
數碼管顯示和掃描鍵盤子函數放在0.1秒的定時器中斷里,而主函數的while(1)函數向數碼管顯示數據變量送數據。數碼管顯示函數接受鍵盤狀態,根據狀態執行開始計時、暫停、選擇學號等功能。
四、調試說明
1、仿真調試如圖4.0所示:
2、按下以組員的名字命名的按鍵時,可以選擇相應同學的學號的后四位為倒計時的初始值,如不設置初始值為0336,若按下“張洪榮”按鍵,初始值為0337;按下“開始”按鍵時開始倒計時;按下“暫停”按鍵時,計時停止;
附錄:源代碼下載:http://www.zg4o1577.cn/f/szgg.rar
1,程序源碼;
#include <string.h>
#include <stdio.h>
#include <reg51.h>
int num =03; //我們學號的班別 03
int stu_num0 =36; //學號后兩位
int stu_num1 =37;
int stu_num2 =42;
int stu_num3 =11;
int stu_num4 =22
int num_count = 36; //將學號吼兩位選擇的值賦給 num_count,初始值為36
sbit start = P1^0; //開始按鍵
sbit stop = P1^1; //停止按鍵
sbit key0 = P1^2; //選擇同學
sbit key1 = P1^3; //選擇同學
sbit key2 = P1^4; //選擇同
sbit key3 = P1^5; //選擇同學
sbit key4 = P1^6; //選擇同學
sbit key5 = P1^7;
unsigned int T1msc;
unsigned char Ledc;
unsigned char Leds[]={8,6,0x13,5,1,2,3,4};
bit isStart;
int temp=9; //0.1秒存儲變量 初始值 9
code char LEDTAB[]=
{
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,
0x00,
};
void Delayms(int ms)
{
while(ms)
{
unsigned char i;
ms--;
for (i=0; i<240; i++);
}
}
void display()
{
char v,c;
P3=0x11;
v=Leds[Ledc];
c=LEDTAB[v & 0x0f];
if (v&0x10) c|=0x80;
P2=c;
P3=Ledc;
Ledc++;
if (Ledc>=8) Ledc=0;
}
void readkey() //讀按鍵
{
if(start==0) isStart = 1; //真為開始
if(stop==0) isStart = 0; //假為停止
if(key0 == 0) num_count = stu_num0; //選擇曾路榮
if(key1 == 0) num_count = stu_num1;//選擇張洪榮
if(key2 == 0) num_count = stu_num2;
if(key3 == 0) num_count = stu_num3;
if(key4 == 0) num_count = stu_num4;
if(key5 == 0) num_count = stu_num5;
}
void t0int() interrupt 1 //1ms定時器中斷服務函數
{
TH0=(-1000)>>8;
TL0=-1000;
display();
readkey();
T1msc++;
if (isStart) //開始倒計時
{
if ((T1msc%100)==0) //滿 0.1秒
{
temp--;
if(temp==-1)
{
num_count--;
temp = 10; //0.1秒恢復9
}
}
if(num_count == -1) //
{
num_count = 99;
num--;
if(num == -1) num = 99;
}
}
if (T1msc>=1000)
{
T1msc=0;
}
}
void NumToLeds()
{
Leds[1]=num/10%10;
Leds[2]=num%10;
Leds[3]=num_count/10%10;
Leds[4]=num_count%10+0x10;
Leds[5]=temp; //0.1秒顯示
}
void main()
{
Delayms(10);
TMOD=0x01;
TR0=1;
ET0=1;
EA=1;
while(1)
{
NumToLeds(); //送數據
}
}