|
《單片機(jī)原理》期中考試
適用班級(jí):電子A1621,電子A1622
注意事項(xiàng):
1. 課外開(kāi)放設(shè)計(jì),開(kāi)卷考核,可查閱任意資料。
2. 單人考核,獨(dú)立完成,如有雷同,一律0分。
3. 不允許對(duì)電路圖做任意形式修改。
4. 提交以“電子A1622_01_陳**_期中測(cè)試”格式命名的項(xiàng)目文件夾。
這是我們學(xué)校單片機(jī)課程的期中考試自己寫(xiě)的程序,花了兩周時(shí)間,壓縮包里面包含了keil的源代碼和protuse電路仿真圖,可能也還有很多不完善的地方,望大家多多見(jiàn)諒。
電路如圖所示,設(shè)計(jì)程序?qū)崿F(xiàn)如下功能:
1. 能根據(jù)交通規(guī)律正確設(shè)置交通燈狀態(tài)及數(shù)碼管顯示內(nèi)容(60分)
2. 能手動(dòng)調(diào)節(jié)時(shí)間(30分)
3. 能根據(jù)交通狀況自動(dòng)延長(zhǎng)直行時(shí)間;(10分)
帶行人與繁忙檢測(cè)的單片機(jī)交通燈仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
0.png (33.05 KB, 下載次數(shù): 86)
下載附件
2018-5-16 17:15 上傳
注:通過(guò)鍵盤(pán)調(diào)節(jié)時(shí)間項(xiàng)目中,分為東西直行時(shí)間、東西左轉(zhuǎn)時(shí)間、南北直行時(shí)間、南北左轉(zhuǎn)時(shí)間4個(gè)項(xiàng)目,每個(gè)項(xiàng)目7.5分,小數(shù)點(diǎn)四舍五入。
0.png (20.39 KB, 下載次數(shù): 79)
下載附件
2018-5-16 17:16 上傳
東西向交通繁忙探頭,1正常,0繁忙
南北向交通繁忙探頭,1正常,0繁忙
1. 模式鍵用來(lái)選擇設(shè)定的時(shí)間,如東西直行時(shí)間、南北左轉(zhuǎn)時(shí)間等。
時(shí)間加減鍵用于手動(dòng)調(diào)節(jié)直行時(shí)間,每按鍵一次加減5s。
2. 交通繁忙探頭用于探測(cè)道路交通狀況,如果當(dāng)前道路排隊(duì)較長(zhǎng),
則繁忙探頭返回0,可臨時(shí)延長(zhǎng)當(dāng)前方向直行通道時(shí)間10s,一個(gè)
紅綠燈周期最多延長(zhǎng)3次。本設(shè)計(jì)用邏輯狀態(tài)輸入端口模擬。
0.png (18.76 KB, 下載次數(shù): 88)
下載附件
2018-5-16 17:17 上傳
0.png (20.97 KB, 下載次數(shù): 110)
下載附件
2018-5-16 17:17 上傳
單片機(jī)源程序如下:
- #include <REGX51.H>
- #define uchar unsigned char
- sbit NSgreen = P3^1; //南北人行道綠燈
- sbit WEgreen = P3^0; //東西人行道綠燈
- sbit Add_Button = P3^5;//加時(shí)間按鍵
- sbit Sub_Button = P3^4;//減時(shí)間按鍵
- sbit Reduces_Button = P3^3;//模式選擇按鍵
- sbit Y1 = P3^2;
- sbit WEbuy_Button = P3^6 ;
- sbit NSbuy_Button = P3^7 ;
- uchar qiehuan=0;
- uchar red = 33,yellow = 3,green_strant = 30,green_left = 30;
- uchar Mode = 0;
- uchar KeyDown=0;
- uchar DisplayTab[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x79}; // 0 1 2 3 4 5 6 7 8 9 E
- uchar led[8];
- uchar Display_Number[] = {0x82,0x84,0x28,0x48,0x81,0x84,0x18,0x48}; //路口紅綠燈狀態(tài)
- //延時(shí)
- void Delay_ms(unsigned int Cnt )
- {
- unsigned int t;
- while (Cnt--)
- {
- t=100;
- while(t--);
- }
- }
-
- //動(dòng)態(tài)顯示
- void Dynamic_display()
- {
- uchar i = 0;
- for(i = 0; i < 8; i++)
- {
- P0 = DisplayTab[ led[i] ];
- P2 = ~(0x01<<i);
- Delay_ms(1);
- P0 = 0x00;
- }
- }
- /***************中斷程序***********************************/
- /*******外部中斷0**********/
- void function_isr(void) interrupt 0
- {
- EX0=0; //關(guān)中斷
- qiehuan++;
- if(qiehuan == 5) Mode=0;
- EX0=1; //開(kāi)中斷
-
- }
- //狀態(tài)1
- void luzhi1()
- {
- led[4] = green_strant % 10;
- led[5] = green_strant / 10;
- led[6] = 1;
- led[7] = 10;
- if(WEbuy_Button == 0) //時(shí)間加
- {
- green_strant += 10;
- }
- }
- //狀態(tài)2
- void luzhi2()
- {
- led[4] = green_strant % 10;
- led[5] = green_strant / 10;
- led[6] = 3;
- led[7] = 5;
- if(NSbuy_Button == 0) //時(shí)間加
- {
- green_strant += 10;
-
- }
- }
- /******外部中斷2********/
- void function_lib(void) interrupt 2
- {
- Mode++;
- if(Mode == 5) Mode=0;
- KeyDown=1;
- }
- //模式1
- void function1()
- {
- led[4] = green_strant % 10;
- led[5] = green_strant / 10;
- led[6] = 1;
- led[7] = 10;
- if(Add_Button == 0) //時(shí)間加
- {
- green_strant += 5;
- green_left += 5;
- red += 5;
- }
- if(Sub_Button == 0) //時(shí)間減
- {
- green_strant -= 5;
- green_left -= 5;
- red -= 5;
- }
-
- }
- //模式2
- void function2()
- {
- led[4] = green_left % 10;
- led[5] = green_left / 10;
- led[6] = 2;
- led[7] = 10;
- if(Add_Button == 0) //時(shí)間加
- {
- green_strant += 5;
- green_left += 5;
- red += 5;
-
- }
- if(Sub_Button == 0) //時(shí)間減
- {
- green_strant -= 5;
- green_left -= 5;
- red -= 5;
- }
-
- }
- //模式3
- void function3()
- {
- led[4] = green_strant % 10;
- led[5] = green_strant / 10;
- led[6] = 3;
- led[7] = 5;
- if(Add_Button == 0) //時(shí)間加
- {
- green_strant += 5;
- green_left += 5;
- red += 5;
- }
- if(Sub_Button == 0) //時(shí)間減
- {
- green_strant -= 5;
- green_left -= 5;
- red -= 5;
- }
- }
- //模式4
- void function4()
- {
- led[4] = green_left % 10;
- led[5] = green_left / 10;
- led[6] = 4;
- led[7] = 5;
- if(Add_Button == 0) //時(shí)間加
- {
- green_strant += 5;
- green_left += 5;
- red += 5;
- }
- if(Sub_Button == 0) //時(shí)間減
- {
- green_strant -= 5;
- green_left += 5;
- red -= 5;
- }
- }
- /*************計(jì)時(shí)程序*************/
- void count_down()
- {
- uchar time = 0;
- uchar Temp,state;
- uchar Red,Yellow,Greens,Greenl;
- Red = red;
- Yellow = yellow;
- Greens = green_strant;
- Greenl = green_left;
-
- while(1)
- {
- if(time < green_strant )
- {
- led[0] = Red % 10;
- led[1] = Red / 10;
- led[2] = Greens % 10;
- led[3] = Greens / 10;
- state=0;
- WEgreen = 1;
- NSgreen = 0;
- Red--;
- Greens--;
- }
- if(time >= green_strant & time < (green_strant +yellow ))
- {
- led[0]=Red % 10;
- led[1]=Red / 10;
- led[2]=Yellow % 10;
- led[3]=Yellow / 10;
- state=1;
- WEgreen = 1;
- NSgreen = 0;
- Red--;
- Yellow--;
- Greens=green_strant; //給直行綠燈恢復(fù)初值
- }
- if(time >= (green_strant +yellow ) & time < (green_strant +yellow+green_strant ) )
- {
- if(time == (green_strant +yellow ))
- {
- Red = red; //給紅燈賦值
- }
- led[0]= Greens % 10;
- led[1]= Greens / 10;
- led[2]= Red % 10;
- led[3]= Red / 10;
- state=2;
- WEgreen = 0;
- NSgreen = 1;
- Red--;
- Greens--;
- Yellow=yellow; //給黃燈恢復(fù)初值
- }
- if(time >= (green_strant +yellow+green_strant ) & time < (green_strant +yellow+green_strant+yellow ) )
- {
- led[0]= Yellow % 10;
- led[1]= Yellow / 10;
- led[2]= Red % 10;
- led[3]= Red / 10;
-
- state=3;
- WEgreen = 0;
- NSgreen = 1;
- Greens=green_strant; //給直行綠燈恢復(fù)初值
- Yellow--;
- Red--;
-
- }
- if(time >= (green_strant +yellow+green_strant+yellow ) & time < (green_strant +yellow+green_strant+yellow+green_left ) )
- {
- if(time == (green_strant +yellow+green_strant+yellow ) )
- {
- Red = red; //給紅燈賦值
- }
- led[0] = Red % 10;
- led[1] = Red / 10;
- led[2]= Greenl % 10;
- led[3]= Greenl / 10;
- state=4;
- WEgreen = 1;
- NSgreen = 0;
- Greenl--;
- Red--;
- Yellow = yellow; //給黃燈賦初值
- }
- if(time >= (green_strant +yellow+green_strant+yellow+green_left ) & time < (green_strant +yellow+green_strant+yellow+green_left+yellow ) )
- {
- led[0] = Red % 10;
- led[1] = Red / 10;
- led[2] = Yellow % 10;
- led[3] = Yellow / 10;
-
- state=5;
- WEgreen = 1;
- NSgreen = 0;
- Yellow--;
- Red--;
- Greenl = green_left; //給左轉(zhuǎn)綠燈賦初值
- }
- if(time >= (green_strant +yellow+green_strant+yellow+green_left+yellow ) & time < (green_strant +yellow+green_strant+yellow+green_left+yellow + green_left) )
- {
- if(time == (green_strant +yellow+green_strant+yellow+green_left+yellow ) )
- {
- Red = red; //給紅燈賦值
- }
- led[0] = Greenl % 10;
- led[1] = Greenl / 10;
- led[2] = Red % 10;
- led[3] = Red / 10;
-
- state=6;
- WEgreen = 0;
- NSgreen = 1;
- Yellow = yellow; //給黃燈賦初值
- Greenl--;
- Red--;
- }
- if(time >= (green_strant +yellow+green_strant+yellow+green_left+yellow + green_left) & time < (green_strant +yellow+green_strant+yellow+green_left+yellow + green_left+yellow) )
- {
- led[0]= Yellow % 10;
- led[1]= Yellow / 10;
- led[2]= Red % 10;
- led[3]= Red / 10;
- state=7;
- WEgreen = 0;
- NSgreen = 1;
- Greenl = green_left; //給左轉(zhuǎn)綠燈賦初值
- Yellow--;
- Red--;
- }
- if(time >= (green_strant +yellow+green_strant+yellow+green_left+yellow + green_left+yellow))
- {
- Yellow = yellow; //給黃燈賦初值
- break;
- }
- time++;
- Y1= 1 ;
- Reduces_Button = 1;
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
0.jpg (46.22 KB, 下載次數(shù): 79)
下載附件
2018-5-16 17:14 上傳
所有資料51hei提供下載:
51單片機(jī)交通燈程序_期中測(cè)試.zip
(307.52 KB, 下載次數(shù): 260)
2018-5-16 09:46 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|