求各位大佬我这个程序是交通信号灯,需要在报警的时候加一个直流电机转动,应该怎样添加程序  #include<reg52.h> //头文件 #include"lcd.h" typedef unsigned int u16; //对数据类型进行声明定义 typedef unsigned char u8; uchar show_l[] = {0x3c,0x42,0x42,0x3c,0x42,0xc3,0x7e,0x24}; //无符号字符型 unsigned char Second[]="Second: "; //8 unsigned char Help_msg[]="your call 110 "; unsigned char Clean_msg[]=" "; sbit YELLOW =P1^0; // 黄灯 sbit GREEN =P1^1; // 绿灯 sbit RED =P1^2; // 红灯 sbit BEEP=P1^4; // 报警 sbit KEY_1=P3^4; // 按键1按下红灯时间增加 sbit KEY_2=P3^5; // 按键2按下红灯时间减少 sbit KEY_3=P1^3; // 按键3按下报警 sbit E1_138=P3^3; // 74LS138的E1 译码器 sbit A_138=P3^0; // 74LS138的A sbit B_138=P3^1; // 74LS138的B sbit C_138=P3^2; // 74LS138的C #define DZ P0 //定义点阵驱动IO口 u8 count; u8 green_count =10; 计时 void leddz_show(uchar *show); void Timer0Init() 定时器0 { TMOD|=0X01;//选择为定时器0模式,工作方式1,仅用TR0打开启动。 TH0=0XFC; //给定时器赋初值,定时1ms 高电平 TL0=0X18; 低电平 ET0=1;//打开定时器0中断允许 EA=1;//打开总中断 TR0=1;//打开定时器 } void Delay10us(u8 z)//延时函数 { u8 x,y; for(x=z;x>0;x--) for(y=210;y>0;y--); } void Delay10Ms(u8 z)//延时函数 { unsigned char i, j; while(z--) { i = 150; j = 200; do { while (--j); } while (--i); } } void main() { count=0;// 循环的周期 E1_138 = 0; //关闭点阵显示 DZ = 0; Timer0Init();//初始化计数器 LcdInit(); //LCD1602初始化 YELLOW = 1; //黄灯关闭 GREEN = 1; //绿灯关闭 RED =1; //红灯关闭 LcdWriteStr(1,16,Second); //LCD1602显示 第一行 while(1) 无限循环 { if(KEY_1 & KEY_2 & KEY_3) //如果没有按键按下 { if( count >= (13+green_count)) //如果计数超过1个周期重新计数 { count=0; } if(count<10)//如果在15秒以内亮绿灯 { LcdWriteCom(0x88); LcdWriteData((10-count)/10+0x30); LcdWriteData((10-count)%10+0x30); GREEN = 1; YELLOW = 1; RED = 0; } else if(count<13)//如果超过15秒低于18秒亮黄灯 { LcdWriteCom(0x88); LcdWriteData((13-count)/10+0x30); LcdWriteData((13-count)%10+0x30); GREEN = 1; YELLOW = 0; RED = 1; } else if( count< (green_count+13) )//如果超过红灯18秒亮红灯 { LcdWriteCom(0x88); LcdWriteData(((13+green_count)-count)/10+0x30); LcdWriteData(((13+green_count)-count)%10+0x30); GREEN = 0; YELLOW =1; RED = 1; } } else//如果有按键按下 { if(KEY_1 == 0) //如果按下按键1 { if(green_count<=30) //如果红灯时间没有超过30秒 { green_count ++; //红灯时间增加 } while(!KEY_1);//等待按键1释放 } if(KEY_2 == 0)//如果按下按键2 { if(green_count>=5)//如果红灯时间大于5秒 { green_count --;//红灯时间减少 } while(!KEY_2 == 0); //等待按键2释放 } if(KEY_3 == 0)//如果按下按键3 { BEEP = 0; //蜂鸣器报警 LcdWriteStr(2,16,Help_msg);//显示报警信息 E1_138 = 1;//使能点阵 while(!KEY_3)//等待释放按键3 { leddz_show(show_l); } E1_138 = 0;//释放按键后关闭点阵 关闭蜂鸣器 清除LCD1602警报信息 BEEP = 1; 蜂鸣器 LcdWriteStr(2,16,Clean_msg); 清除 } } } } void Timer0() interrupt 1 中断函数0定时器0 { static u16 i; 两字节的无符号整型 静态分配 TH0=0XFC; //给定时器赋初值,定时1ms TL0=0X18; i++; if(i==1000) { i=0; count ++; } } void leddz_show(uchar *show) { uchar i; for(i =0;i<8;i++) { P0 = *show++; P3 = i | 0XF8; Delay10us(10); P0 = 0;//消影 } }
|