當(dāng)前位置首頁 > 報(bào)告/分析 > 交通
搜柄,搜必應(yīng)! 快速導(dǎo)航 | 使用教程

基于單片機(jī)STC89C52RC的交通燈

文檔格式:DOC| 13 頁|大小 63.01KB|積分 15|2025-03-16 發(fā)布|文檔ID:253592487
第1頁
第2頁
第3頁
下載文檔到電腦,查找使用更方便 還剩頁未讀,繼續(xù)閱讀>>
1 / 13
此文檔下載收益歸作者所有 下載文檔
  • 版權(quán)提示
  • 文本預(yù)覽
  • 常見問題
  • 基于單片機(jī)STC89C52RC的交通燈摘要:本交通燈系統(tǒng)采用STC89C52RC作為核心控制器,控制紅綠黃三盞燈,來達(dá)到模擬日常交通燈系統(tǒng),其中倒計(jì)時(shí)時(shí)間通過1602顯示出來關(guān)鍵詞:交通燈 1602液晶一、 設(shè)計(jì)方案整個(gè)模擬交通燈系統(tǒng)用單片機(jī)STC89C52RC作為核心控制器,有序的控制著紅黃綠三盞燈的運(yùn)行,實(shí)際交通燈系統(tǒng)所需的倒計(jì)時(shí)時(shí)間采用1602顯示出來,并且其倒計(jì)時(shí)時(shí)間可通過獨(dú)立式鍵盤進(jìn)行調(diào)整STC89C52紅黃綠LED1602液晶獨(dú)立式鍵盤 系統(tǒng)框圖二、 主程序流程圖開始系統(tǒng)初始化 等待中斷 N 按鍵是否按下? Y 調(diào)用按鍵處理函數(shù) 返回三、 附錄------源程序(C)一、the main function of this system /**用stc52實(shí)現(xiàn)簡(jiǎn)易交通燈*/#include#include#include "define.h"#include "lcd_1602.h"#include "delay.h"#include "some_inits.h"#include "control.h"/****************************************主函數(shù)void main()****************************************/void main(){ sys_init(); while(1) { key_scan(); //掃描鍵盤 delay_ms(100); }}二、include Some .h files(1)defines.h#ifndef _define_h#define _define_h#define uchar unsigned char#define unint unsigned int/* 液晶數(shù)據(jù)區(qū) */sfr lcd_bus=0x80; //液晶數(shù)據(jù)口sbit E=P2^7;sbit RW=P2^6;sbit RS=P2^5;/* 定義三盞led */sbit red=P2^0;sbit green=P2^1;sbit yellow=P2^2;/* 定義三個(gè)開關(guān) */sbit jia=P1^0; //加一鍵 sbit jian=P1^1; //減一鍵sbit ok=P1^2; //確認(rèn)鍵uchar flag=1; //1,2,3:綠燈,黃燈,紅燈bit first=0;/* 蜂鳴器 */sbit bell=P1^4;uchar sec_h; //秒針十位uchar sec_l; //秒針個(gè)位uchar g_h=1,g_l=0;//綠燈計(jì)時(shí)數(shù)據(jù)uchar y_h=1,y_l=0;//黃燈計(jì)時(shí)數(shù)據(jù)uchar r_h=1,r_l=0;//紅燈計(jì)時(shí)數(shù)據(jù)#endif(2)some_inits.h#ifndef _some_inits_h#define _some_inits_h#include "lcd_1602.h"#include "define.h"http://state some functionsvoid L1602_init();void Time0_init();/*系統(tǒng)初始化*/void sys_init(){ flag=1; //綠燈 green=1; red=0; yellow=0; L1602_init();//1602液晶初始化 Time0_init();//定時(shí)器0初始化 }/*1602初始化*/void L1602_init(void){ enable(0x01); enable(0x38); enable(0x0c); enable(0x06); enable(0xd0); L1602_string(1,2,"traffic lights"); sec_h=g_h; sec_l=g_l; L1602_char(2,8,sec_h+0x30); L1602_char(2,9,sec_l+0x30);}/* 定時(shí)器0初始化 */void Time0_init(){ TMOD=0x01; //time0,time1工作在方式一 IE=0x8a; //開EA,允許ET0,允許ET1 TH0=0x4c; //50ms,晶振是11.0592M TL0=0x00; //TH1=0xfc; //1ms //TL1=0x66; TR0=1; //time0開始計(jì)數(shù)}#endif (3)control.h#ifndef _control_h_#define _control_h_#include #include "lcd_1602.h"#include "define.h"#include "delay.h" /* state some functions */void inc();void dec();void fmq();/* 鍵盤掃描函數(shù) */void key_scan(){ if(ok==0) { delay_ms(5); if(ok==0) { fmq(); enable(0x0c); //關(guān)光標(biāo) first=0; TR0=1; //開始計(jì)數(shù) } else _nop_(); } else if(jia==0 || jian==0) { delay_ms(5); if(jia==0 || jian==0) { TR0=0; // 停止計(jì)數(shù) fmq(); while(!first) { first=1; enable(0x0f); //開光標(biāo),開反白 } if(jia==0) inc(); else if(jian==0) dec(); } else { _nop_(); } }}/* 加一 */void inc(void){ _nop_(); if(green==1 && yellow==0 && red==0) { if(g_l<9 && g_h<5) g_l++; else { if(g_h<5) { g_h++; g_l=0; } else { g_h=5; g_l=0; } } sec_h=g_h;sec_l=g_l; } else if(yellow==1 && red==0 && green==0) { if(y_l<9 && y_h<5) y_l++; else { if(y_h<5) { y_h++; y_l=0; } else { y_h=5; y_l=0; } } sec_h=y_h;sec_l=y_l; } else if(red==1 && green==0 && yellow==0 ) { if(r_l<9 && r_h<5) r_l++; else { if(r_h<5) { r_h++; r_l=0; } else { r_h=5; r_l=0; } } sec_h=r_h;sec_l=r_l; } L1602_char(2,9,sec_l+0x30); L1602_char(2,8,sec_h+0x30);}/* 減一 */void dec(void){ _nop_(); if(green==1 && yellow==0 && red==0) { if(g_l>0 && g_h>0) { g_l--; } else { if(g_h>0) { g_h--; g_l=9; } else if(g_l>0) { g_l--; } else { g_h=0; g_l=0; } } sec_h=g_h;sec_l=g_l; } else if(yellow==1 && green==0 && red==0) { if(y_l>0 && y_h>0) { y_l--; } else { if(y_h>0) { y_h--; y_l=9; } else if(y_l>0) { y_l--; } else { y_h=0; y_l=0; } } sec_h=y_h;sec_l=y_l; } else if(red==1 && yellow==0 && green==0) { if(r_l>0 && r_h>0) { r_l--; } else { if(r_h>0) { r_h--; r_l=9; } else if(r_l>0) { r_l--; } else { r_h=0; r_l=0; } } sec_h=r_h;sec_l=r_l; }L1602_char(2,9,sec_l+0x30);L1602_char(2,8,sec_h+0x30);}/* 蜂鳴器鳴響一聲 */void fmq(){ unint i=150; while(i--) { bell=~bell; delay_ms(1); } }/* time0 service function */void time0() interrupt 1{ static count=0; TR0=0; if(count==20) //1s { count=0; if(sec_l==0) { if(sec_h==0) //30s結(jié)束 { if(flag==1) { flag++; green=1; red=0; yellow=0; sec_h=g_h; sec_l=g_l; } else if(flag==2) //狀態(tài)二,亮黃燈 { flag++; green=0; red=0; yellow=1; sec_h=y_h; sec_l=y_l; } else if(flag==3)//狀態(tài)三,紅燈亮 { flag=1; //回到狀態(tài)一,綠燈亮 green=0; red=1; yellow=0; sec_h=r_h; sec_l=r_l; } } else //個(gè)位為0而已 { sec_h--; sec_l=9; } } else { sec_l--; } L1602_char(2,8,sec_h+0x30); L1602_char(2,9,sec_l+0x30); } else { count++; } TH0=0x4c; TL0=0x00; TR0=1;}#endif (4)lcd_1602.h#ifndef _lcd_1602_h#define _lcd_1602_h#include "define.h"#include "delay.h"uchar Convert(uchar In_Date){ uchar i, Out_Date = 0, temp = 0; for(i=0; i<8; i++) { temp = (In_Date >> i) & 0x01; Out_Date |= (temp << (7 - i)); } return Out_Date;}/*寫命令*/void enable(uchar del){ P0 = Convert(del); RS = 0; RW = 0; E = 0; delay(); E = 1; delay();}/*寫數(shù)據(jù)*/void write(uchar del){ P0 = Convert(del); RS = 1; RW = 0; E = 0; delay(); E = 1; delay();}/*寫字符*/void L1602_char(uchar hang,uchar lie,char sign){ uchar a; if(hang == 1) a = 0x80; if(hang == 2) a = 0xc0; a = a + lie - 1; enable(a); write(sign);}/*寫字符串*/void L1602_string(uchar hang,uchar lie,uchar *p){ uchar a; if(hang == 1) a = 0x80; if(hang == 2) a = 0xc0; a = a + lie - 1; enable(a); while(1) { if(*p == '\0') break; write(*p); p++; }}#endif。

    點(diǎn)擊閱讀更多內(nèi)容
    最新文檔
    傳統(tǒng)文化道德不是高懸的明月而是腳下的星光.pptx
    世界無煙日關(guān)注青少年成長(zhǎng)健康無煙為成長(zhǎng)護(hù)航.pptx
    五四青年節(jié)詩詞贊歌五四青年自強(qiáng)不息.pptx
    XX學(xué)校班主任培訓(xùn)用心管理慧做班主任.pptx
    拒絕熬夜健康養(yǎng)生規(guī)律作息遠(yuǎn)離亞健康.pptx
    兒童成長(zhǎng)手冊(cè)時(shí)光里的童真印記.pptx
    幼兒園夏季傳染病預(yù)防指南預(yù)見夏天健康童行夏季傳染病預(yù)防科普.pptx
    高中生心理健康教育主題班會(huì)快樂學(xué)習(xí)高效學(xué)習(xí)正視壓力學(xué)會(huì)減壓.pptx
    員工職業(yè)道德與職業(yè)素養(yǎng)培訓(xùn)遵守職業(yè)道德提高職業(yè)修養(yǎng).pptx
    2025職業(yè)病防治法宣傳周健康守護(hù)職防同行.pptx
    XX幼兒園防災(zāi)減災(zāi)安全教育臨災(zāi)不亂安全童行學(xué)會(huì)保護(hù)自己.pptx
    在2025年縣教育工作大會(huì)暨高考備考工作推進(jìn)會(huì)上的講話發(fā)言材料.docx
    在2025年縣全面從嚴(yán)治黨和黨風(fēng)廉政會(huì)議上的講話發(fā)言材料.docx
    在2025年全市慶?!拔逡弧濒邉趧?dòng)模范表彰大會(huì)上的講話發(fā)言材料多篇.docx
    2025年稅務(wù)局青年代表在五四青年座談會(huì)上的發(fā)言材料3篇.docx
    在2025年市委全體會(huì)議上的主持講話發(fā)言材料.docx
    2025年黨風(fēng)廉政建設(shè)工作要點(diǎn)材料.docx
    在2025年全市青年干部慶祝五四青年節(jié)大會(huì)上的講話發(fā)言材料多篇.docx
    在入黨積極分子培訓(xùn)班上的講話發(fā)言材料.docx
    縣文旅局黨組書記在五一假期及夏季旅游安全生產(chǎn)工作部署會(huì)議上的講話發(fā)言材料.docx
    賣家[上傳人]:仙人指路
    資質(zhì):實(shí)名認(rèn)證