最近做了一个关最近做了一个关于8155模拟交通灯的运行系统,写的不好,还有很多不足,有的地方还不完善,请大家多多指教。
电路原理图如下:
交通灯仿真.jpg
单片机源程序如下:
- #include<reg52.h>
- # include <absacc.h>
- #define uchar unsigned char //定义为uchar类型
- #define uint unsigned int //定义为uint类型
- # define COM XBYTE[0XFE00] //控制字地址(总控制,决定了PA/B/C是写还是读等)
- # define PA XBYTE[0XFE01] //控制PA地址(总控制写的是读时,里面存的是读的值;
- // 写的是写时,里面要写入控制值)
- # define PB XBYTE[0XFE02] //控制PB地址
- # define PC XBYTE[0XFE03] //控制PC地址
- sbit g = P2^1;
- sbit s = P2^2;
- sbit eg = P2^3;
- sbit ws = P2^4;
- //sbit Key_N = P1^4; //独立按键
- //sbit Key_S = P1^5;
- //sbit Key_W = P1^6;
- //sbit Key_E = P1^7;
- sbit KEYA = P1^4;
- sbit KEYB = P1^5;
- uint ge,shi; //定义全局变量
- uint m = 30;
- uint n;
- uint num;
- /*交通灯的数值*/
- uchar lcd1 = 10; //倒计时初始值为25用于改变
- uchar lcd2 = 15; //的倒计时初始值为30用于改变
- uchar lcd11; //倒计时
- uchar lcd22; //倒计时
- uchar change1; //LCD1红绿灯转换
- uchar yellow1 = 3;
- /*一些辅助标志位的声明*/
- uchar code smguan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //数码管显示0~9
- uchar t=3;
- uchar sign = 0;
- uchar time_Count; //进行计时
- void delay(uint t)
- {
- while(t--);
- }
- void Digital(uint ge,uint shi) //数码管显示
- {
- PA = smguan[ge];
- s = 1;
- delay(500);
- s = 0;
- PA = smguan[shi];
- g = 1;
- delay(500);
- g = 0;
- PB = smguan[ge];
- eg = 1;
- delay(500);
- eg = 0;
- PB = smguan[shi];
- ws = 1;
- delay(500);
- ws = 0;
- }
-
- void Traffic_lights()
- {
- if(t == 3)
- {
- P1 = 0x01;
- if(m == 0)
- {
- m = 3;
- t = 2;
- }
- }
- if(t == 2)
- {
- P1 = 0x02;
- if(m == 0)
- {
- m = 30;
- t = 1;
- }
- }
-
- if(t == 1)
- {
- P1 = 0x04;
- if(m == 0)
- {
- m = 3;
- t = 0;
- }
- }
- if(t == 0)
- {
- P1 = 0x02;
- if(m == 0)
- {
- m = 30;
- t = 3;
- }
- }
- }
- void INIT()
- {
- IT0=1;
- IT1=1;
- EX0=1;
- EX1=1;
- EA=1;
- }
复制代码
|