c语言红绿灯
1. 用C语言编程模拟交通路口(十字路口)红绿灯的控制功能
本系统的设计首先必须了解交通路灯的亮灭规律。设有一个十字路口,1、3 为南,北
方向, 2、4 为东,西方向,初始态为4 个路口的红灯全亮。之后, 1、3 路口的绿灯亮,
2、4 路口的红灯亮, 1、3 路口方向通车。延迟一段时间后, 1、3 路口的绿灯熄灭,而1、
3 路口的黄灯开始闪烁。闪烁若干次后, 1、3 路口的红灯亮, 同时 2、4 路口的绿灯亮, 2、
4 路口方向开始通车。延迟一段时间后, 2、4 路口的绿灯熄灭,而黄灯开始闪烁。闪烁若
干次后,再切换到1、3 路口方向。之后,重复上述过程。对于各组灯的亮灭,我们运用的
是8255A 的输入输出功能。
2. C语言程序设计编写一个交通红绿灯的控制,各位高手帮帮忙急!
unsigned char counter;
sbit red_nb=P2^0;
......
void main()
{
TMOD=
TH0
TL0
ET0
TR0
while(1) //根据楼主所给,有4种状态
{
state1;
state2;
state3;
state4;
}
}
void timer0() interrupt 1 //定时器0
{
TH0=0x3c;
TL0=0xb0;
counter++;
}
void state1() //控制南北红灯亮,东西绿灯亮,30s
{
unsigned char temp = 30;
red_nb=1;
.....
green_dx=1;
while(1)
{
if( counter>19){counter=0;temp--} //每次counter=20,即1s,temp自减
if(temp) break; //当temp为0时,即30秒,退出状态1,进入状态2
}
}
没经过测试,完全是5分诱惑不大。。
3. 单片机 用C语言 keil uVision2 编程 编一个红绿灯
#include<AT89X52.H>
#define LED_D P2
#define LED_S P1
unsigned char code table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
sfr P1M0=0xB2;
unsigned int m,i,post1,post2;
unsigned int k=0,nums=1,n=1;
void timer1() interrupt 1 using 1
{
i++;
TH0=0XB1; //初始化
TL0=0XE0; //初始化
post1=9-(i/50%10);
post2=2-i/500;
m=i/25;
if(m==60)
i=0;
}
void initialize()
{
P0=0XFF ;
TMOD=0X01;
TR0=1;
TH0=0XB1;
TL0=0XE0;
}
void show()
{ switch(n)
{ case 1:
LED_D=table[post1];
LED_S=0x01;
break;
case 3:
LED_D=table[post2];
LED_S=0x02;
break;
}
if(n++==4)
n=1;
switch(nums)
{
case 1:
if(m<52)
P0=0XDE;
if(m>=52&&m<59)
{P0_0=1;
if(k++>=50000)
{
P0_1=!P0_1;
k=0;
}
}
if(m==59)
nums=2;
break;
case 2:
if(m<52)
P0=0XF3;
if(m>=52&&m<59)
{
P0_3=1;
if(k++>=50000)
{
P0_4=!P0_4;
k=0;
}
}
if(m==59)
nums=1;
break;
default: break;
}
}
void main()
{
initialize();
EA=1;
P1M0=0XF0;
ET0=1;
TR0=1;
while(1)
{
show();
}
}
这是带数码管显示的红绿灯,主要用了四位8段的数码管显示时间。如果需要详细的继续追问。
4. 求一个C语言的红绿灯程序 80C51
#include<reg51.h>/*下面中n代表north. n s w e分别代表四个不同的方向*/
sbit rednts=P1^0;
sbit yellownts=P1^1;
sbit greennts=P1^2;
sbit redwte=P1^3;
sbit yellowwte=P1^4;
sbit greenwte=P1^5;//由于实验实的板只用了P1口作输入
int tmp1=0;
int tmp2=0;
int i;
void begins()
{
rednts=0;
yellownts=0;
greennts=0;
redwte=0;
yellowwte=0;
greenwte=0;
}
void TimeInt() interrupt 1 //这是中断函数,1对应是定时、计数器0
{
i++;
if(i==2000)
{
i=0;
tmp1++;
if(10==tmp1) tmp1=0;
}//若到了,得清0 //判断是否到了0.5MS*2000=1S}
void ExteriorInt() interrupt 0
{
tmp2=!tmp2;
}
void main()
{ //int time1s,time2s,time3;;
TMOD=0x02; //启用定时器工作模式2
TH0=6;//装定时器初值高8位,0.5ms中断一次 。2US*250=0.5MS
TL0=6;//装定时器初值低8位
EA=1; //打开总中断允许
ET0=1; //打开定时器0的中断允许
TR0=1; //T0工作1******以上是对定时器0的初始化********
TCON=0x11;//TCON的设置必须保持定时器0在工作
EX0=1;//********以上是对外部中断0的初始化***********
while(1)
{
if(tmp2)
{
begins();
rednts=1;
redwte=1;
}
else if(tmp1<3)
{
begins();
rednts=1;
greenwte=1;
}
else if(tmp1<4)
{
begins();
}
else if(tmp1<5)
{
yellownts=1;
yellowwte=1;
}
else if(tmp1<6)
{
begins();
}
else if(tmp1<7)
{
yellownts=1;
yellowwte=1;
}
else if(tmp1<10)
{
begins();
greennts=1;
redwte=1;
}
}}
5. 鍗旷墖链篊璇瑷绾㈢豢𨱔绋嫔簭锛岃佹眰锛氭暟镰佺℃樉绀烘椂闂村掕℃椂銆16*16LED镣归樀鏄剧ず妯″潡鏄剧ず绾㈢豢榛勭伅銆
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
#define BLKN 2 //鍒楅挛瀛桦櫒鏁
sbit G = P1^7; //P1.7涓烘樉绀轰笡闄鍏佽告带鍒朵俊鍙风鍙
sbit RCLK = P2^0; //P2.0涓鸿緭鍑洪挛瀛桦拹锣勫櫒镞堕挓淇″彿绔
sbit SCLR = P2^1; //P2.1涓虹Щ浣嶅瘎瀛桦櫒娓呪棆绔
void delay(uint); //寤舵椂鍑芥暟
uchar data display[32]; //鏄剧ず缂揿瓨
uchar code BMP[][32]={{16,0,16,0,37,248,36,64,120,64,8,64,16,64,32,64,124,64,0,64,0,64,12,64,112,64,3,252,0,0,0,0},
{16,0,19,240,32,16,41,240,120,16,19,252,32,64,122,72,1,80,0,224,25,80,102,76,0,64,1,192,0,0,0,0},
{4,64,4,64,63,248,4,64,127,252,1,0,31,240,17,16,31,240,17,16,31,240,8,32,16,16,32,8,0,0,0,0}}; //瀛楁ā琛'绾⑩樷欑豢钬樷橀粍钬
void main()
{
register uchar i,j;
SCON=0x00; //涓插彛宸ヤ綔妯″纺0锛氱Щ浣嶅瘎瀛桦櫒鏂瑰纺
TMOD=0x01; //瀹氭椂鍣═0宸ヤ綔鏂瑰纺1锛16浣嶆柟寮
P1=0x3f; //P1绔鍙e埯鍊硷细鍏佽告帴鏀躲侀挛瀛樸佹樉绀
TR0=1; //钖锷ㄥ畾镞跺櫒T0
ET0=1; //鍏佽稿畾镞跺櫒T0涓鏂
EA=1; //寮钖镐讳腑鏂锛
while(1)
{
delay(2000);
for(j=0;j<3;j++)
{
for(i=0;i<32;i++)
{
display[i]=~BMP[j][i];
if (i%2) delay(100);
}
delay(10000);
}
}
}
void delay(uint x)
{
register uchar i,j;
for (i=x;i>0;i--)
for (j=120;j>0;j--);
}
void leddisplay(void) interrupt 1 using 1
{
register uchar i, j=BLKN;
TH0 =(65536-100)/256; //琛¢儜瀵熻惧畾鏄剧ず灞忓埛鏂扮巼姣忕60甯
TL0 =(65536-100)%256;
i = P1; //璇诲彇褰揿墠鏄剧ず镄勮屽彿
i = ++i & 0x0f; //琛屽彿锷1锛屽睆钄介珮4浣
while(j)
{
j--;
SBUF = display[i*BLKN + j]; //阃佹樉绀烘暟鎹
while (!TI);
TI = 0;
} //瀹屾垚涓琛屾暟鎹镄勫彂阃
G = 1; //娑堥殣锛埚叧闂鏄剧ず锛
P1 &= 0xf0; //琛屽彿绔鍙f竻鈼
RCLK = 1; //鏄剧ず鏁版嵁镓揿叆杈揿嚭阌佸瓨鍣
P1 |= i; //鍐椤叆琛屽彿
RCLK = 0; //阌佸瓨鏄剧ず鏁版嵁
G = 0; //镓揿紑鏄剧ず
}
6. 交通灯 C语言 单片机。中断函数问题
参考《51单片机C语言创新教程》温子祺等着。
源码转自:《51单片机C语言创新教程》。
/*实验名称:交通灯实验
*描述:交通灯实验要求红灯亮15秒,绿灯亮10秒,黄灯亮5秒,
当红灯切换为绿灯或者绿灯切换为红灯,
要实现灯闪烁。红灯、绿灯、黄灯的点亮持续时间可以通过串口来修改,
并在下一个循环中更新数值。
*作者:温子祺
*修改日期:2010/5/4
*说明:代码注释与讲解详见《51单片机C语言创新教程》温子祺等着,北京航空航天大学出版社
*/
#include"stc.h"
typedefunsignedcharUINT8;
typedefunsignedint UINT16;
typedefunsignedlongUINT32;
typedefcharINT8;
typedefintINT16;
typedeflongINT32;
#defineTIMER0_INITIAL_VALUE5000
#defineHIGH1
#defineLOW0
#defineON1
#defineOFF0
#defineSEG_PORTP0
#defineLS164_DATA(x){if((x))P0_4=1;elseP0_4=0;}
#defineLS164_CLK(x){if((x))P0_5=1;elseP0_5=0;}
#defineNORTH_R_LIGHT(x){if((x))P2_0=0;elseP2_0=1;}
#defineNORTH_Y_LIGHT(x){if((x))P2_1=0;elseP2_1=1;}
#defineNORTH_G_LIGHT(x){if((x))P2_2=0;elseP2_2=1;}
#defineSOUTH_R_LIGHT(x){if((x))P2_3=0;elseP2_3=1;}
#defineSOUTH_Y_LIGHT(x){if((x))P2_4=0;elseP2_4=1;}
#defineSOUTH_G_LIGHT(x){if((x))P2_5=0;elseP2_5=1;}
#defineTRAFFIC_STATUS_10
#defineTRAFFIC_STATUS_21
#defineTRAFFIC_STATUS_32
#defineUART_MARKER0xEE
UINT8Timer0IRQEvent=0;
UINT8Time1SecEvent=0;
UINT8Time500MsEvent=0;
UINT8TimeCount=0;
UINT8SegCurPosition=0;
UINT8LightOrgCount[4]={15,5,15,5};
UINT8LightCurCount[4]={15,5,15,5};
UINT8TrafficLightStatus=0;
codeUINT8SegCode[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
UINT8SegBuf[4]={0};
codeUINT8SegPosition[4]={0x07,0x0b,0x0d,0x0e};
typedefstruct_LIGHT_VAL
{
UINT8Head;
UINT8val[4];
}LIGHT_VAL;
typedefunion_LIGHT_VAL_EX
{
LIGHT_VALlv;
UINT8p[5];
}LIGHT_VAL_EX;
voidLS164Send(UINT8byte)
{
UINT8j;
for(j=0;j<=7;j++)
{
if(byte&(1<<(7-j)))
{
LS164_DATA(HIGH);
}
else
{
LS164_DATA(LOW);
}
LS164_CLK(LOW);
LS164_CLK(HIGH);
}
}
voidRefreshDisplayBuf(UINT8s1) //刷新显示缓存
{
SegBuf[0]=s1%10;
SegBuf[1]=s1/10;
SegBuf[2]=s1%10;
SegBuf[3]=s1/10;
}
voidSegDisplay(void)
{
UINT8t;
t=SegCode[SegBuf[SegCurPosition]];
SEG_PORT|=0x0f;
LS164Send(t);
SEG_PORT=(SEG_PORT|0x0f)&SegPosition[SegCurPosition];
if(++SegCurPosition>=4)
{
SegCurPosition=0;
}
}
voidTimerInit(void)
{
TH1=0;
TL1=0;
TH0=(65536-TIMER0_INITIAL_VALUE)/256;
TL0=(65536-TIMER0_INITIAL_VALUE)%256;//定时1MS
TMOD=0x51; /*01010001T1计数,T0定时*/
}
voidTimer0Start(void)
{
TR0=1; //启动计时器1
ET0=1;
}
voidTimer0Stop(void)
{
TR0=0; //启动计时器1
ET0=0;
}
voidPortInit(void)
{
P0=P1=P2=P3=0xFF;
}
voidUartInit(void)
{
SCON=0x40;
T2CON=0x34;
RCAP2L=0xD9;
RCAP2H=0xFF;
REN=1;
ES=1;
}
voidUartSendByte(UINT8byte)
{
SBUF=byte;
while(TI==0);
TI=0;
}
voidUartPrintfString(INT8*str)
{
while(str&&*str)
{
UartSendByte(*str++);
}
}
voidmain(void)
{
UINT8i=0;
PortInit();
TimerInit();
Timer0Start();
UartInit();
RefreshDisplayBuf(LightCurCount[0]);
EA=1;
NORTH_R_LIGHT(ON);
SOUTH_G_LIGHT(ON);
while(1)
{
if(Timer0IRQEvent)
{
Timer0IRQEvent=0;
TimeCount++;
if(TimeCount>=200)
{
TimeCount=0;
if(LightCurCount[0])
{
TrafficLightStatus=0;
}
elseif(LightCurCount[1])
{
TrafficLightStatus=1;
}
elseif(LightCurCount[2])
{
TrafficLightStatus=2;
}
elseif(LightCurCount[3])
{
TrafficLightStatus=3;
}
else
{
for(i=0;i<4;i++)
{
LightCurCount[i]=LightOrgCount[i];
}
TrafficLightStatus=0;
}
switch(TrafficLightStatus)
{
case0:
{
NORTH_R_LIGHT(ON);
SOUTH_R_LIGHT(OFF);
NORTH_G_LIGHT(OFF);
SOUTH_G_LIGHT(ON);
NORTH_Y_LIGHT(OFF);
SOUTH_Y_LIGHT(OFF);
}
break;
case1:
{
if(LightCurCount[1]%2)
{
NORTH_R_LIGHT(ON);
SOUTH_G_LIGHT(ON);
}
else
{
NORTH_R_LIGHT(OFF);
SOUTH_G_LIGHT(OFF);
}
NORTH_Y_LIGHT(ON);
SOUTH_Y_LIGHT(ON);
}
break;
case2:
{
NORTH_R_LIGHT(OFF);
SOUTH_R_LIGHT(ON);
NORTH_G_LIGHT(ON);
SOUTH_G_LIGHT(OFF);
NORTH_Y_LIGHT(OFF);
SOUTH_Y_LIGHT(OFF);
}
break;
case3:
{
if(LightCurCount[3]%2)
{
NORTH_G_LIGHT(ON);
SOUTH_R_LIGHT(ON);
}
else
{
NORTH_G_LIGHT(OFF);
SOUTH_R_LIGHT(OFF);
}
NORTH_Y_LIGHT(ON);
SOUTH_Y_LIGHT(ON);
}
break;
default:break;
}
RefreshDisplayBuf(LightCurCount[TrafficLightStatus]);
LightCurCount[TrafficLightStatus]--;
}
SegDisplay();
}
}
}
voidUartIRQ(void)interrupt4
{
staticUINT8cnt=0;
staticLIGHT_VAL_EXLightValEx;
if(RI)
{
RI=0;
LightValEx.p[cnt++]=SBUF;
if(LightValEx.lv.Head==UART_MARKER)
{
if(cnt>=5)
{
for(cnt=1;cnt<5;cnt++)
{
LightOrgCount[cnt-1]=LightValEx.lv.val[cnt];
LightCurCount[cnt-1]=LightValEx.lv.val[cnt];
}
cnt=0;
UartPrintfString("设置交通灯完成 ");
}
}
else
{
cnt=0;
}
}
}
voidTimer0IRQ(void)interrupt1
{
ET0 =0;
TH0=(65536-TIMER0_INITIAL_VALUE)/256;
TL0=(65536-TIMER0_INITIAL_VALUE)%256;//定时1MS
Timer0IRQEvent=1;
ET0 =1;
}
=====================================================================
坐等拿分!