要发送的const数组:
const unsigned char acucHead[7]={"\n$SIG,"};
const unsigned char acucCmd[6][10]={{"ON\n"},{"OFF\n"},{"FAIL\n"},{"REST\n"},{"TEST ON\n"},{"TEST OFF\n"}};
const unsigned char acucName[27][25]={
{"STEERING LIGHT.P(01),"},
{"DANGEROUS LIGHT(02),"},
{"SUEZ SIGNAL L.T-R.P(03),"},
{"SUEZ SIGNAL L.T-W.P(04),"},
{"SUEZ SIGNAL L.T-G.P(05),"},
{"SUEZ SIGNAL L.T-R.P(06),"},
{"SUEZ SIGNAL L.T-R.P(07),"},
{"SUEZ SIGNAL L.T-W.P(08),"},
{"STEERING LIGHT.S(09),"},
{"SUEZ SIGNAL L.T-R.S(10),"},
{"SUEZ SIGNAL L.T-W.S(11),"},
{"SUEZ SIGNAL L.T-R.S(12),"},
{"SUEZ SIGNAL L.T-W.S(13),"},
{"SUEZ SIGNAL L.T-R.S(14),"},
{"SUEZ SIGNAL L.T-R.S(15),"},
{"SUEZ SIGNAL L.T(16),"},
{"NULL(17),"},
{"NULL(18),"},
{"NULL(19),"},
{"NULL(20),"},
{"NULL(21),"},
{"NULL(22),"},
{"NULL(23),"},
{"NULL(24),"},
{"POWER MSB,"},
{"POWER ESB,"},
{"SYSTEM,"}
};
中断程序:
if(TXIE&&TXIF) //USART发送中断
{
GIE=0;
switch(ucIndex)
{
case 0x00: TXREG=acucHead[ucElmtPlace];
if((ucElmtPlace>=7)||(acucHead[ucElmtPlace]==','))
{
ucIndex=1;
ucElmtPlace=0;
}
else
{
ucElmtPlace++;
}
break;
case 0x01: TXREG=acucName[ucNameIndex][ucElmtPlace];
if((ucElmtPlace>=33)||(acucName[ucNameIndex][ucElmtPlace]==','))
{
ucIndex=2;
ucElmtPlace=0;
}
else
{
ucElmtPlace++;
}
break;
case 0x02: TXREG=acucCmd[ucCmdIndex][ucElmtPlace];
if((ucElmtPlace>=10)||(acucCmd[ucCmdIndex][ucElmtPlace]=='\n'))
{
ucIndex=0;
ucElmtPlace=0;
TXIE=0;
}
else
{
ucElmtPlace++;
}
break;
default: TXREG='\n'; ucIndex=0; TXIE=0; break;
}
GIE=1;
}
问题1:
同样是开启串口中断,为何放在前一个位置就导致不断复位,而放到后面就正常呢?
放到前面时,可以看到发出“\n$”后单片机就会复位,如果是中断程序有问题,应该放到后面发送就正常了啊!
main()
{
ADCON1=0b00000110; //将A口设置为数字口
InitFirst();
InitPWM(); //PWM工作模式初始化
InitUart();
// TXREG='\n';
WrLedData(0); //显示清0
WrLightData();
ALARM=0;
BEEP=1;
Delay200ms();
BEEP=0;
Delay200ms();
Delay200ms();
Delay200ms();
BEEP=1;
Delay200ms();
BEEP=0;
WrLedData(0); //再次显示清0,防止干扰
WrLightData();
//请注意下面这段这段,已注释掉
// ucNameIndex=26; //发送系统开始工作
// ucCmdIndex=0;
// TXIE=1;
if(TEST_HOST)
LED_HOST=0; //显示主电有电
else
LED_HOST=1; //显示主电没电
if(TEST_PREPARE)
LED_PREPARE=0; //显示备电有电
else
LED_PREPARE=1; //显示备电没电
BEEP=1;
Delay200ms();
BEEP=0;
EN_24=1; //继电器动作允许
//将刚才注释掉的语句放到这里,系统就能正常工作
ucNameIndex=26; //发送系统开始工作
ucCmdIndex=0;
TXIE=1;
while(1)
{
asm("clrwdt");
KeyManage();
PowerCheck();
if((OnOffFlog1.AllBits!=0x00)||(OnOffFlog2.AllBits!=0x00)||(OnOffFlog3.AllBits!=0x00))
{
LightTest();
}
}
}
问题2:
曾经在两块板上出现过过死机现象。看门狗不能复位。掉电后仍然不能工作。怀疑是晶振不起振,结果换晶振后依然不能工作。
重烧程序,还是不工作。
改程序,不开USART使能,不工作!
再次烧不开USART使能的程序,开始工作。
问题3:
有一段子程序如下:
if(ErroCode3!=0x00)
{
aa=0x80;
for(i=0;i<8;i++)
{
bb=ErroCode3&aa;
switch(bb)
{
case 0b00000001: OnOffFlog3.OneBit.b1=0; ucNameIndex=16; ucErrNum++; break;
case 0b00000010: OnOffFlog3.OneBit.b2=0; ucNameIndex=17; ucErrNum++; break;
case 0b00000100: OnOffFlog3.OneBit.b3=0; ucNameIndex=18; ucErrNum++; break;
case 0b00001000: OnOffFlog3.OneBit.b4=0; ucNameIndex=19; ucErrNum++; break;
case 0b00010000: OnOffFlog3.OneBit.b5=0; ucNameIndex=20; ucErrNum++; break;
case 0b00100000: OnOffFlog3.OneBit.b6=0; ucNameIndex=21; ucErrNum++; break;
case 0b01000000: OnOffFlog3.OneBit.b7=0; ucNameIndex=22; ucErrNum++; break;
case 0b10000000: OnOffFlog3.OneBit.b8=0; ucNameIndex=23; ucErrNum++; break;
default: break;
}
aa>>=1;
}
}
CountLight(); //计算开启的灯数量
ErroBackup1|=ErroCode1; //错误位备份
ErroBackup2|=ErroCode2;
ErroBackup3|=ErroCode3;
Get595Code();
WrLightData();
Delay100ms();
WrLedData(0);
/* asm("clrwdt");
if(ucErrNum>=0x02) //多路灯同时发生故障,则发送系统失败
{
ucIndex=0;
ucElmtPlace=0;
ucErrNum=0;
ucNameIndex=26;
}*/
ucCmdIndex=2; //发送FAIL
TXIE=1;
一旦将上面注释掉的程序开启,进入该段程序就会复位重启。
而改为不判断ucErrNum,就正常。
问题4:
程序共开2个中断,还有一个定时器0,做1ms定时用(不精确)
程序在开启USART情况下变得不稳定。是否因为中断冲突问题?
if(T0IF&&T0IE)
{
GIE=0;
T0IF=0;
TMR0=6;
CountTime++;
if(CountTime>=10)
{
CountTime=0;
if(flash_fg==0)
{
if(bad_flog)
{
if(test_flog==1) //测试打开时有错误位则错误位闪动,其它全亮
{
GerTestErrCode();
Intr_WrLedData(3);
}
else //正常位保持不变,错误位闪动
{
Intr_WrLedData(2);
}
}
if(power_alarm) //主电失电闪烁
{
if(power_host) LED_HOST=0;
if(power_prepare) LED_PREPARE=0;
}
}
else
{
if(bad_flog)
{
if(test_flog==1) //测试打开时有错误位则错误位闪动,其它全亮
{
Intr_WrLedData(1); //全亮
}
else //正常位保持不变,错误位闪动
{
Intr_WrLedData(0); //正常的亮
}
}
if(power_alarm) //主电失电闪烁
{
if(power_host) LED_HOST=1;
if(power_prepare) LED_PREPARE=1;
}
}
flash_fg=!flash_fg;
}
GIE=1;
} |