找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2312|回复: 0
打印 上一主题 下一主题
收起左侧

单片机——定时器控制交通指示灯 请大神看看有啥问题

[复制链接]
跳转到指定楼层
楼主
ID:136426 发表于 2016-8-10 14:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
单片机——定时器控制交通指示灯
任务要求:
1:东西方向的绿灯亮40S,之后黄灯开始闪烁,黄灯亮的时间为4S,绿灯变红灯:
2:南北方向的绿灯亮40S,之后黄灯开始闪烁,黄灯亮的时间为4S,绿灯变红灯,如此往复。
3:实现时间倒计时显示。
程序:
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
Sbit RED_A=P3^0;  //东西方向红灯
Sbit YELLOW_A=P3^0; //东西方向黄灯
Sbit GREEN_A=P3^0;  //东西方向绿灯
Sbit RED_B=P3^0;  //南北方向红灯
Sbit YELLOW_B=P3^0;  //南北方向黄灯
Sbit GREEN_B=P3^0;  //南北方向绿灯
//共阳极数码管断码表
Int code DSY_CODE[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XFF};
Int code scan_bits[]={0x02,0x01};
Int code scan_bits1[]={0x08,0x04};
Int data disp_buff[]={0,0};
Int data disp_buff1[]={0,0};
Int data disp_buff2[]={0,0};
Int data disp_buff3[]={0,0};
Int time_count=0;
Int flash_count=0;
Int operation_type=0;
Int m1=40,m2=4;m3=40,m4=4;
Int num;
Int=shu,shu1,shu2,shu3;
//延时函数:
Void delay(uint z)
{
Int i,j;
For(i=z;i<0;i--);
For(j=0;j<110;j++);
}
//数据显示函数:
Int i,j;
Int n,m;
//分解计数值:
disp_buff[0]=M1/10;
disp_buff[1]=M1%10;
If (disp_buff[0]==0)
{
disp_buff[0]=10;
}
disp_buff1[0]=M2/10;
disp_buff1[1]=M2%10;
If (disp_buff1[0]==0)
{
disp_buff1[0]=10;
}
disp_buff2[0]=M3/10;
disp_buff2[1]=M3%10;
If (disp_buff2[0]==0)
{
disp_buff2[0]=10;
}
Disp_buff3[0]=m4/10;
Disp_buff3[1]=m4%10;
If (disp_buff3[0]==0)
{
disp_buff3[0]=10;
}
For(i=0;i<2;i++)
{
P0=oxff;
P2= scan_bits;
P0=dsy_code[disp_buff];
Delay(1);
}
For(j=0;j<2;j++)
{
P0=oxff;
P2= scan_bits[j];
P0=dsy_code[disp_buff1[j]];
Delay(1);
}
For(n=0;n<2;n++)
{
P1=oxff;
P2= scan_bits1[n];
P1=dsy_code[disp_buff2[n]];
Delay(1);
}
For(m=0;m<2;m++)
{
P1=oxff;
P2= scan_bits1[m];
P1=dsy_codedisp_buff3[m]];
Delay(1);
}
//T0中断函数子程序:
Void to_isr() interrupt 1
{
TH1=-50000/256;
TL1=-50000%256;
Switch(operation_type)
{
Case 1://东西方向绿灯亮40S南北方向红灯亮40S
     RED_A=0; YELLOW_A=0;GREEN_A=1;
     RED_B=1; YELLOW_B=0;GREEN_B=0;
If(++Time_count !=800) return;
Time_count=0;
operation_type=2;
Break;
Case 2://东西方向黄灯开始闪烁,绿灯关闭
If(++Time_count !=80) return;
Time_count=0;
Yellow_A=!yellow_A; GREEN_A=0;
If(++flash_count !=4) return;
flash_count=0;
operation_type=3;
Break;
Case 3://南北方向绿灯亮40S东西方向红灯亮40S
     RED_A=1; YELLOW_A=0;GREEN_A=0;
     RED_B=0; YELLOW_B01;GREEN_B=1;
If(++Time_count !=800) return;
Time_count=0;
operation_type=4;
Break;
Case 4: //南北方向黄灯开始闪烁,绿灯关闭
If(++Time_count !=80) return;
Time_count=0;
Yellow_B=!yellow_B; GREEN_B=0;
If(++flash_count !=4)   return;
flash_count=0;
operation_type=1;
void T0_time() interrupt 0   // 定时器0的中断号
{
TH0 = (65536-50000)/256; //装初值,50ms申请一次中断
TL0 = (65536-50000)%256;
num++;
If(num==800)
(
Num=0;
m1--;
If(m1==0)
M1=40;
Shu=m1
}
num++;
If(num==80)
(
Num=0;
M2--;
If(m2==0)
m2=4;
Shu1=m1
}
Num++
If(num==800)
(
Num=0;
M3--;
If(m3==0)
M3=40;
Shu2=m3
}       
num++;
If(num==80)
(
Num=0;
M4--;
If(m4==0)
M4=4;
Shu3=m4
}
Void main()
{
Tmod=0x01;
TH0 = (65536-50000)/256; //装初值,50ms申请一次中断
TL0 = (65536-50000)%256;
TH1=-50000/256;
TL1=-50000%256;
EA = 1; //开总中断
ET0 = 1; //开定时器0中断
TR0 = 1;
ET1= 1; //开定时器0中断
TR1 = 1;
While(1)
{
dsy_code[disp_buff[shu];
dsy_code[disp_buff[shu1];
dsy_code[disp_buff[shu2];
dsy_code[disp_buff[shu3];       
}
}
   
   
  
   
     

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表