单片机交通灯中断定时器c语言,模拟交通灯C程序,用定时器0,如何实现黄灯闪烁功能...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

sbit ADDR3 = P1^3;

sbit ENLED = P1^4;

bit flag1s=1;

unsigned char T0RH,T0RL;

unsigned char ledchar[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};

unsigned char ledbuff[7]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};

unsigned char color = 0;//0-绿灯1-黄灯2-红灯

void configtimer0(unsigned char ms);

void trafficlight();

void main()

{

EA = 1;

ADDR3 = 1;

ENLED = 0;

configtimer0(1);

while(1)

{

if(flag1s)

{

flag1s = 0;

trafficlight();

}

}

}

void configtimer0(unsigned char ms)

{

unsigned long tmp;

tmp = 11059200/12;

tmp = tmp*ms/1000;

tmp = 65536 - tmp + 12;

T0RH = (unsigned char)(tmp>>8);

T0RL = (unsigned char)tmp;

TMOD &= 0xF0;

TMOD |= 0x01;

TH0 = T0RH;

TL0 = T0RL;

ET0 = 1;

TR0 = 1;

}

void trafficlight()

{

static unsigned char timer = 0;

if(timer==0)

{

switch(color)

{

case 0:color = 1;timer = 2;ledbuff[6] = 0xE7;break;

case 1:color = 2;timer = 29;ledbuff[6] = 0xFC;break;

case 2:color = 0;timer = 39;ledbuff[6] = 0x3F;break;

default :break;

}

}

else

{

timer--;

}

ledbuff[0] = ledchar[timer%10];

ledbuff[1] = ledchar[timer/10];

}

void ledscan()

{

static unsigned char i = 0;

P0 = 0xFF;

P1 = (P1 & 0xF8)|i;

P0 = ledbuff[i];

i++;

if(i>=7)

i=0;

}

void interrupttimer0() interrupt 1

{

static unsigned int tmr1s = 0;

static unsigned int cnt = 0;

static bit dir=1;

TH0 = T0RH;

TL0 = T0RL;

tmr1s++;

if(tmr1s>=1000)

{

tmr1s = 0;

flag1s = 1;

}

if(color!=1)

ledscan();

else

{

if(dir) //dir=1,刷新数码管

{

ledscan();

cnt++;

if(cnt>=600)

{

cnt = 0;

dir = 0;

}

}

else //dir=0,不刷新数码管

{

cnt++;

if(cnt>=600)

{

cnt = 0;

dir = 1;

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值