int main(void)
{
static u8 flg=0;
__disable_irq();
Project_Init(); //初始化
InitLINData(); //初始化数据
EnabledLINChannel();//LIN输出通道使能
nextstp = 10; //启动流程
__enable_irq();
while(syswaittim < 100); //系统上电等待
PWM_Precent(0); //关PWM
while(1)
{
MyAddrNum = ReadToggleSwitch();
watchdog_feeddog();
//*********扫描流程***********************/
switch(step)
{
case 10: //切换至指定通道
// HandleChan = 7;
SelectLINChannel(HandleChan);//
ClearLINStruct();
nextstp = 20;
break;
case 20: //延时
if(waittim >= 1)
{
nextstp = 35;
}
break;
case 35: //写
if(SetDat.Write_flg == 1)
{
LINSendMsg(1,TRUE,Sed_Node);//发送数据
nextstp = 40;
}
else
{
LINSendMsg(1,FALSE,Node);//发送询问
nextstp = 60;
}
break;
case 40: //延时等待
if(waittim >= 1)
{
nextstp = 70;
}
break;
case 60: //延时等待
if(waittim >= 8)
{
nextstp = 65;
}
break;
case 65:
if(LINGetMsg(1,&Node)==1)
{
if(Node.identifier == GetDat.PID)
{
GetDat.Length[HandleChan] = Node.Lenght;
for(u8 j = 0;j < MAX_DATA;j++)
{
GetDat.Data[HandleChan][j] = Node.data_field[j];
}
}
}
else
{
GetDat.Length[HandleChan] = 0;
for(u8 j = 0;j < MAX_DATA;j++)
{
GetDat.Data[HandleChan][j] = 0;
}
}
nextstp = 68;
break;
case 68:
if(ValidData(HandleChan))
{
FeedSta |= (0x01<<HandleChan);
}
else
{
FeedSta &= ~(0x01<<HandleChan);
}
nextstp = 70;
break;
case 70: //切换下一通道
if(++HandleChan >= GroupofChannels)//++
{
HandleChan = 0;
SetDat.Write_flg = 0;
if(SetDat.Sleep_flg == 1) //Sleep
{
FeedSta = 0x00;
ClearSendDatStruct();
nextstp = 0;
}
else //active
{
nextstp = 10;
}
}
else //扫描未结束
{
nextstp = 10;
}
break;
default:
break;
}
/*************状态指示灯*******************/
if(wait_ledtim >= 250)
{
ledtim = tim_systick;
if((sta[0]&0x01) == 0x01) //测试中
{
if(SetDat.Sleep_flg == 0)
{
ContrlRsFlicker(FeedSta);
}
}
else
{
ContrlRs(0x00);
}
}
/*********485通讯协议***********************/
if(t_done == 0)
{
if(sent_fg)
{
sent_fg=0;
USART2_Sent();
}
}
if(r_done)//指令解析
{
r_done = 0;
if(Crc16_Chk(r_buf,comnum) && (GetHexStrTo8U(r_buf,2) == MyAddrNum)) //校验和地址OK
{
switch(r_buf[1]) //‘M'+ xxxx + 'A'
{
case 'A'://LIN 数据读取
sta[0] = GetHexStrTo8U(r_buf,4);
GetDat.rque_inter = GetData16U(6,4);
GetDat.PID = GetHexStrTo8U(r_buf,10);
Node.identifier = GetDat.PID;
SetDat.Sleep_flg = (sta[0]>>1)& 0x01;
if(SetDat.Sleep_flg == 0 && step==0)
{
HandleChan = 0;
nextstp = 10;
}
break;
case 'B': //LIN 狀態
sta[0] = GetHexStrTo8U(r_buf,4);
sta[1] = GetHexStrTo8U(r_buf,6);
sta[2] = GetHexStrTo8U(r_buf,8);
// __disable_irq();
ContrlEn(sta[2]);
// __enable_irq();
SetDat.Sleep_flg = (sta[0]>>1)& 0x01;
if((sta[0]&0x01) == 0x00) //未测试
{
if(flg == 1)
{
flg=0;
Speakstep = 10;
}
ContrlRs(sta[1]);
}
else
{
if(flg == 0)
{
flg=1;
}
}
break;
case 'C': //LIN 数据写入
sta[0] = GetHexStrTo8U(r_buf,4);
SetDat.PID = GetHexStrTo8U(r_buf,6);
Sed_Node.identifier = SetDat.PID;
SetDat.Length = r_buf[8]-0x30;
Sed_Node.Lenght = SetDat.Length;
SetDat.Sleep_flg = (sta[0]>>1)& 0x01;
for(u8 i = 0; i < 8;i++)
{
SetDat.Data[i] = GetHexStrTo8U(r_buf,9+i*2);
Sed_Node.data_field[i] = SetDat.Data[i];
}
if(SetDat.Sleep_flg) //Sleep
{
ContrlRs(0x00);
}
SetDat.Write_flg=1;
HandleChan = 0;
nextstp = 10;
break;
default:break;
}
sent_fg=1;
}
}
//------------蜂鸣器流程---------------------------------
switch(Speakstep)
{
case 10:
Speakcount = 0;
nextSpstp = 15;
break;
case 15:
PWM_Precent(50);
nextSpstp = 20;
break;
case 20:
if(waitSptim > 500)
{
PWM_Precent(0);
nextSpstp = 30;
}
break;
case 30:
if(waitSptim > 500)
{
if(++Speakcount>=5)
{
nextSpstp = 0;
}
else
{
nextSpstp = 15;
}
}
break;
default:
break;
}
}
|