该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include "reg52.h" //头文件typedef unsigned char u8;typedef unsigned int u16;u8 code table [16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};#define FOSC 11059200 // 定义系统时钟频率(跟随晶振频率设置)#definetime0 (u16)(65535-((FOSC/12.0/1000000L)*1000))// 计算1ms定时器值// 交通灯时长#define G1_TIME 25//宏定义g1所要的时间#define Y1_TIME 3//宏定义y1所要的时间#define R1_TIME 28//宏定义r1所要的时间#define G2_TIME 25//宏定义g1所要的时间#define Y2_TIME 3//宏定义y1所要的时间#define R2_TIME 28//宏定义r1所要的时间#define TARFFICLIGHTS_STATE time_state// 交通灯状态#define Glight_1 1#define Ylight_1 2#define Rlight_1 3#define G2ight_2 4#define Y2ight_2 5#define R2ight_2 6sbit dula=P2^0; //数码管断选sbit wela=P2^1; //数码管位选sbit key =P2^4; //按键s1口sbit r1=P1^0;//南北交通灯红sbit y1=P1^1;//南北交通灯黄sbit g1=P1^2;//南北交通灯绿sbit r2=P1^3;//东西交通灯红sbit y2=P1^4;//东西交通灯黄sbit g2=P1^5;//东西交通灯绿u16 number,shi,ge;//定义全局变量u16 number1;time_state=0;//初始化time_statevoid key_scan();//按键扫描程序void key_program();//按键功能程序void delay(u16 x)//延时函数{u16 i,j;for(i=x;i--;i>0)for(j=11;j--;j--);}void main()//主函数{TMOD =0x01;//同时设置定时器1和0为工作方式1(0001 0001)TH0=0x00;//给高位或者地位赋值TL0=0x00;EA=1;//开总中断ET0=1;//开定时器0中断TR0=1;//启动定时器0key_scan();//按键扫描while(1){if(key!=0){if(number1==0) {switch(time_state){case Glight_1 :number1=G1_TIME;g1=0;r1=1;y1=1;time_state = Ylight_1;break;case Ylight_1 :number1=Y1_TIME;g1=1;y1=~y1;time_state=Rlight_1;break;case Rlight_1 :number1=R1_TIME;y1=1;r1=0;time_state=Glight_1;break;default :time_state = Glight_1;number1 = 0;break;}}}elseif(key==0){while(1){if(number1==0) {switch(time_state){case G2ight_2 :number1=G2_TIME;g1=1;y1=1;r1=1;g2=0;r2=1;y2=1;time_state = Y2ight_2;break;case Y2ight_2 :number1=Y2_TIME;g1=1;y1=1;r1=1;g2=1;y2=~y2;time_state=R2ight_2;break;case R2ight_2 :number1=R2_TIME;g1=1;y1=1;r1=1;y2=1;r2=0;time_state=G2ight_2;break;default :time_state = G2ight_2;number1 = 0;break;}} shi=number1/10;//求模运算,就是求出number中的有多少个10的整数倍ge=number1%10;//求余运算,就是求出number中去除整数倍10的后的余}}shi=number1/10;//求模运算,就是求出number中的有多少个10的整数倍ge=number1%10;//求余运算,就是求出number中去除整数倍10的后的余}}bit KEY_PRESS=0;void key_scan()//按键扫描程序{bit key_pop_flag=1;if(key_pop_flag != 0){if(KEY_PRESS == 0){if(key == 0){delay(1);//消抖if(key == 0){KEY_PRESS = 1;key_pop_flag = 0;}}}}else{KEY_PRESS = 0;if(key != 0){ //松手检测key_pop_flag = 1;}}}bit print_flag=0 ; void suizhixiang_tinme0() interrupt 1 //定时器0{TH0=time0>>8;//定义的值为1毫秒TL0=time0;number++;if(number>=1000){number=0;if(number1>0){number1--;}}P0 = 0xFF;//段选位选分开消隐dula = 1;dula = 0;P0 = 0x00;wela = 1;wela = 0;if(print_flag != 1)//运用一个语句要十位和个位分开来刷数值,以防十位个位刷数值太快看不到{P0=table[shi];dula=1;dula=0;P0=0xfd;wela=1;wela=0;print_flag = 1;}else{P0=table[ge];dula=1;dula=0;P0=0xfe;wela=1;wela=0;print_flag =0 ;}els}