交通灯 C语言 单片机。中断函数问题 单片机c语言中断函数
参考《51单片机C语言创新教程》温子祺等著。
源码转自:《51单片机C语言创新教程》。
/*实验名称:交通灯实验
*描 述:交通灯实验要求红灯亮15秒,绿灯亮10秒,黄灯亮5秒,
当红灯切换为绿灯或者绿灯切换为红灯,
要实现灯闪烁。红灯、绿灯、黄灯的点亮持续时间可以通过串口来修改,
并在下一个循环中更新数值。
*作 者:温子祺
*修改日期:2010/5/4
*说 明:代码注释与讲解详见《51单片机C语言创新教程》温子祺等著,北京航空航天大学出版社
*/
#include "stc.h"
typedef unsigned char UINT8;
typedef unsigned int UINT16;
typedef unsigned long UINT32;
typedef char INT8;
typedef int INT16;
typedef long INT32;
#define TIMER0_INITIAL_VALUE 5000
#define HIGH 1
#define LOW 0
#define ON 1
#define OFF 0
#define SEG_PORT P0
#define LS164_DATA(x) {if((x))P0_4=1;else P0_4=0;}
#define LS164_CLK(x) {if((x))P0_5=1;else P0_5=0;}
#define NORTH_R_LIGHT(x) {if((x))P2_0=0;else P2_0=1;}
#define NORTH_Y_LIGHT(x) {if((x))P2_1=0;else P2_1=1;}
#define NORTH_G_LIGHT(x) {if((x))P2_2=0;else P2_2=1;}
#define SOUTH_R_LIGHT(x) {if((x))P2_3=0;else P2_3=1;}
#define SOUTH_Y_LIGHT(x) {if((x))P2_4=0;else P2_4=1;}
#define SOUTH_G_LIGHT(x) {if((x))P2_5=0;else P2_5=1;}
#define TRAFFIC_STATUS_1 0
#define TRAFFIC_STATUS_2 1
#define TRAFFIC_STATUS_3 2
#define UART_MARKER 0xEE
UINT8 Timer0IRQEvent=0;
UINT8 Time1SecEvent=0;
UINT8 Time500MsEvent=0;
UINT8 TimeCount=0;
UINT8 SegCurPosition=0;
UINT8 LightOrgCount[4]={15,5,15,5};
UINT8 LightCurCount[4]={15,5,15,5};
UINT8 TrafficLightStatus=0;
code UINT8 SegCode[10] ={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
UINT8 SegBuf[4] ={0};
code UINT8 SegPosition[4]={0x07,0x0b,0x0d,0x0e};
typedef struct _LIGHT_VAL
{
UINT8 Head;
UINT8 val[4];
}LIGHT_VAL;
typedef union _LIGHT_VAL_EX
{
LIGHT_VAL lv;
UINT8 p[5];
}LIGHT_VAL_EX;
void LS164Send(UINT8 byte)
{
UINT8 j;
for(j=0;j<=7;j++)
{
if(byte&(1<<(7-j)))
{
LS164_DATA(HIGH);
}
else
{
LS164_DATA(LOW);
}
LS164_CLK(LOW);
LS164_CLK(HIGH);
}
}
void RefreshDisplayBuf(UINT8 s1) //刷新显示缓存
{
SegBuf[0] = s1%10;
SegBuf[1] = s1/10;
SegBuf[2] = s1%10;
SegBuf[3] = s1/10;
}
void SegDisplay(void)
{
UINT8 t;
t = SegCode[SegBuf[SegCurPosition]];
SEG_PORT |= 0x0f;
LS164Send(t);
SEG_PORT = (SEG_PORT|0x0f) & SegPosition[SegCurPosition];
if(++SegCurPosition>=4)
{
SegCurPosition=0;
}
}
void TimerInit(void)
{
TH1 = 0;
TL1 = 0;
TH0 = (65536-TIMER0_INITIAL_VALUE)/256;
TL0 = (65536-TIMER0_INITIAL_VALUE)%256; //定时1MS
TMOD = 0x51; /*01010001 T1计数,T0定时*/
}
void Timer0Start(void)
{
TR0 = 1; //启动计时器1
ET0 = 1;
}
void Timer0Stop(void)
{
TR0 = 0; //启动计时器1
ET0 = 0;
}
void PortInit(void)
{
P0=P1=P2=P3=0xFF;
}
void UartInit(void)
{
SCON=0x40;
T2CON=0x34;
RCAP2L=0xD9;
RCAP2H=0xFF;
REN=1;
ES=1;
}
void UartSendByte(UINT8 byte)
{
SBUF=byte;
while(TI==0);
TI=0;
}
void UartPrintfString(INT8 *str)
{
while(str && *str)
{
UartSendByte(*str++);
}
}
void main(void)
{
UINT8 i=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;
}
else if(LightCurCount[1])
{
TrafficLightStatus=1;
}
else if(LightCurCount[2])
{
TrafficLightStatus=2;
}
else if(LightCurCount[3])
{
TrafficLightStatus=3;
}
else
{
for(i=0;i<4;i++)
{
LightCurCount[i]=LightOrgCount[i];
}
TrafficLightStatus=0;
}
switch(TrafficLightStatus)
{
case 0:
{
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;
case 1:
{
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;
case 2:
{
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;
case 3:
{
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();
}
}
}
void UartIRQ(void)interrupt 4
{
static UINT8 cnt=0;
static LIGHT_VAL_EX LightValEx;
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;
}
}
}
void Timer0IRQ(void) interrupt 1
{
ET0 = 0;
TH0 = (65536-TIMER0_INITIAL_VALUE)/256;
TL0 = (65536-TIMER0_INITIAL_VALUE)%256; //定时1MS
Timer0IRQEvent=1;
ET0 = 1;
}
=====================================================================
坐等拿分!
#include <reg51.h>
int x,y,i,j,k; //变量声明
int d0,d1,d2,d3;
void com_led(); //函数声明
void com_interrupt();
void delay(x);
void main()
{com_led(); // 灯初始化
com_interrupt(); // 外部中断0初始化
do{P1=0x0C; //东西向亮红灯55秒,南北向亮绿灯55秒,
delay(11);
P1=0x0A; //东西向亮红灯5秒,南北向亮绿灯5秒,
delay(1);
P1=0x21; //东西向亮绿灯55秒,南北向亮红灯55秒,
delay(11);
P1=0x11; //东西向亮黄灯55秒,南北向亮红灯55秒,
delay(1);
}
while(1);
}
//外部中段0服务子程序
int accident() interrupt 0
{y=P1; //现场保护
d0=x;
d1=i;
d2=j;
d3=k;
P1=0x09; //急救车通过,东西南北向均亮红灯5秒
delay(1);
x=d0; //现场恢复 1
i=d1;
j=d2;
k=d3;
P1=y;
}
//交通灯初始化函数
void com_led()
{P1=0x09; //东西南北向灯均亮5秒
delay(1);
}
//外部中段0初始化函数
void com_interrupt()
{EA=1;
EX0=1;
IT0=1;
}
//延时5*X秒函数
void delay(x)
{do{for(i=0;i<7;i++)
for(j=0;j<215;j++)
for(k=0;k<200;k++);
}
while(--x);
}
求助,C语言写得单片机交通灯控制.紧急中断后不能返回~
uchar h;
这个变量,你在 主函数 和 中断函数 中,都使用了,两个函数互相添乱,程序不能正常工作,确实是很正常的。
-------------------------------------
在主函数,每次对“端口状态”进行修改时,都要把改动的数值,同时也存放到一个变量中,这可称为“备份当前状态”。
在中断函数结束时,再把“备份的当前状态”恢复到端口即可。
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar h, bf;
uint s;
uchar code dis[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
void delay(uint c)
{
uint i, j;
for(i = 0; i < c; i++) for(j = 0; j < 100; j++);
}
//外部中断int0
void int0( void) interrupt 0 using 1
{
uchar x;
uint y;
delay(2);
if(INT0 == 0) {
P1=0xf6; P0=0xff;
}
for(x = 20; x > 0; x--) {
for(y = 248; y > 0; y--) {
P0 = 0xff; P2 = 0x01;
P0 = dis[x / 10]; delay(1);
P0 = 0xff; P2 = 0x02;
P0 = dis[x % 10]; delay(1);
}
}
P1 = bf; //恢复备份的状态
}
void main(void)
{
EA = 1;
EX0 = 1;
P1 = 0xf6; //初始状态全红灯
bf = 0xf6; //备份当前状态
delay(1000);
while(1) {
P1 = 0xf3; //状态1,南北绿灯通车,东西红灯
bf = 0xf3; //备份当前状态
for(h = 30; h > 10; h--) { //30秒倒数
for(s = 248; s > 0; s--) {
P0 = 0xff; P2 = 0x01;
P0 = dis[h / 10]; delay(1);
P0 = 0xff; P2 = 0x02;
P0 = dis[h % 10]; delay(1);
}
}
P1 = 0xf7; //状态2,南北绿灯闪几次转亮黄灯,延时几秒,东西仍然红灯
bf = 0xf7; //备份当前状态
for(h = 10; h > 5; h--) {
P1 = 0xf3;
bf = 0xf3; //备份当前状态
delay(100);
for(s = 248; s > 0; s--) {
P1 = 0xf7;
bf = 0xf7; //备份当前状态
P0 = 0xff; P2 = 0x01;
P0 = dis[h / 10]; delay(1);
P0 = 0xff; P2 = 0x02;
P0 = dis[h % 10]; delay(1);
}
}
P1 = 0xf5;
bf = 0xf5; //备份当前状态
for(h = 5; h > 0; h--) {
for(s = 248; s > 0; s--) {
P0 = 0xff; P2 = 0x01;
P0 = dis[h / 10]; delay(1);
P0 = 0xff; P2 = 0x02;
P0 = dis[h % 10]; delay(1);
}
}
P1 = 0xde; // 状态3,东西绿灯通车,南北红灯
bf = 0xde; //备份当前状态
for(h = 30; h > 10; h--) {
for(s = 248; s > 0; s--) {
P0 = 0xff; P2 = 0x01;
P0 = dis[h / 10]; delay(1);
P0 = 0xff; P2 = 0x02;
P0 = dis[h % 10]; delay(1);
}
}
P1 = 0xfe; //状态4,东西绿灯闪几次转亮黄灯,延时几秒,南北仍然红灯。
bf = 0xfe; //备份当前状态
for(h = 10; h > 5; h--) {
P1 = 0xde;
bf = 0xde; //备份当前状态
delay(100);
for(s = 248; s > 0; s--) {
P1 = 0xfe;
bf = 0xfe; //备份当前状态
P0 = 0xff; P2 = 0x01;
P0 = dis[h / 10]; delay(1);
P0 = 0xff; P2 = 0x02;
P0 = dis[h % 10]; delay(1);
}
}
P1 = 0xee;
bf = 0xee; //备份当前状态
for(h = 5; h > 0; h--) {
for(s = 248; s >0; s--) {
P0 = 0xff; P2 = 0x01;
P0 = dis[h / 10]; delay(1);
P0 = 0xff; P2 = 0x02;
P0 = dis[h % 10]; delay(1);
}
}
}
}
using1
表示用寄存器工作组1.。其实不加也可以,在c语言中,是自动分配寄存器的,所以可以不加。。
interrupt
1
后面数字是中断源
,表示启用中断源1(定时器0).(51单片机里有5个中断源)