部分代码简单介绍如下:
基于英飞凌iLLD库进行集成,主要用到的模块和函数介绍如下。
1) GPIO模块//存储引脚电平状态boolean IfxPort_getPinStateDio[5];void Dio_init_input(void){// 配置热释电1信号采集引脚P2.6为输入,上拉IfxPort_setPinMode(&MODULE_P02,6,IfxPort_Mode_inputPullUp); //X103连接器 PIN_19}void Dio_init_output(void){// 配置蜂鸣器驱动引脚P33.0为输出,上拉IfxPort_setPinMode(&MODULE_P33,0, IfxPort_Mode_outputPushPullGeneral);IfxPort_setPinHigh(&MODULE_P33,0); //默认禁能// 配置监控主机驱动控制引脚P14.9为输出,上拉IfxPort_setPinMode(&MODULE_P14,9, IfxPort_Mode_outputPushPullGeneral);IfxPort_setPinLow(&MODULE_P14,9);//默认禁能}void DIO_State_Read(void){//读取热释电1信号IfxPort_getPinStateDio[0] = IfxPort_getPinState(&MODULE_P02, 6);}2) LED模块介绍void GPIO_Led_init(void){// configure P33.8 as general outputIfxPort_setPinMode(&MODULE_P33,8, IfxPort_Mode_outputPushPullGeneral);// configure P33.9 as general outputIfxPort_setPinMode(&MODULE_P33,9, IfxPort_Mode_outputPushPullGeneral);// configure P33.10 as general outputIfxPort_setPinMode(&MODULE_P33,10, IfxPort_Mode_outputPushPullGeneral);// configure P33.11 as general outputIfxPort_setPinMode(&MODULE_P33, 11, IfxPort_Mode_outputPushPullGeneral);}void GPIO_Led(void){IfxPort_togglePin(&MODULE_P33, 8);IfxStm_waitTicks(&MODULE_STM0, 5000000);IfxPort_togglePin(&MODULE_P33, 9);IfxStm_waitTicks(&MODULE_STM0, 5000000);IfxPort_togglePin(&MODULE_P33, 10);IfxStm_waitTicks(&MODULE_STM0, 5000000);IfxPort_togglePin(&MODULE_P33, 11);IfxStm_waitTicks(&MODULE_STM0, 5000000);}3) 蜂鸣器模块//蜂鸣器报警void Beep_On(void){int i;for(i=0;i<10000;i++){IfxPort_setPinLow(&MODULE_P33,0);IfxStm_waitTicks(&MODULE_STM0, 50000);IfxPort_setPinHigh(&MODULE_P33,0);IfxStm_waitTicks(&MODULE_STM0, 50000);IfxPort_setPinLow(&MODULE_P33,0);IfxStm_waitTicks(&MODULE_STM0, 50000);IfxPort_setPinHigh(&MODULE_P33,0);IfxStm_waitTicks(&MODULE_STM0, 50000);}}//蜂鸣器报警禁能void Beep_Off(void){IfxPort_setPinHigh(&MODULE_P33,0);}4) CAN通信模块//定义全局变量IfxMultican_Message txMsg;uint32 dataLow = 0x03020100;uint32 dataHigh = 0x07060504;const unsigned id=0xcf00203;//定义扩展IDextern uint32 dataLow;extern uint32 dataHigh;extern IfxMultican_Message txMsg;//CAN handleIfxMultican_Can can;//指向全CAN的模块地址//Nodes handlesIfxMultican_Can_Node canSrcNode;//指向节点寄存器地址//Message Object handlesIfxMultican_Can_MsgObj canSrcMsgObj;//指向消息体寄存器地址//通道、节点、报文的初始化void CAN_Init(void){//CAN模块初始化 //CAN配置结构体IfxMultican_Can_Config canConfig;IfxMultican_Can_initModuleConfig(&canConfig, &MODULE_CAN);IfxMultican_Can_initModule(&can, &canConfig);//CAN节点初始化//CAN节点配置结构体IfxMultican_Can_NodeConfig canNodeConfig;IfxMultican_Can_Node_initConfig(&canNodeConfig, &can);canNodeConfig.baudrate = 1000000;//波特率设为1M//CAN源节点{canNodeConfig.nodeId = IfxMultican_NodeId_0;canNodeConfig.rxPin =&IfxMultican_RXD0B_P20_7_IN;//P20.7作为输入引脚canNodeConfig.rxPinMode = IfxPort_InputMode_pullUp;canNodeConfig.txPin = &IfxMultican_TXD0_P20_8_OUT;//P20.8作为输出引脚canNodeConfig.txPinMode = IfxPort_OutputMode_pushPull;// 源节点初始化IfxMultican_Can_Node_init(&canSrcNode,&canNodeConfig);}//Message Object Initialisation{// 创建报文对象配置IfxMultican_Can_MsgObjConfig canMsgObjConfig;IfxMultican_Can_MsgObj_initConfig(&canMsgObjConfig, &canSrcNode);// 分配报文对象canMsgObjConfig.msgObjId = 0;canMsgObjConfig.messageId = id;//消息IDcanMsgObjConfig.acceptanceMask =0x7FFFFFFFUL;canMsgObjConfig.frame = IfxMultican_Frame_transmit;canMsgObjConfig.control.messageLen=IfxMultican_DataLengthCode_8;//数据长度canMsgObjConfig.control.extendedFrame = TRUE;//扩展帧帧格式canMsgObjConfig.control.matchingId = TRUE;//匹配ID// 初始化报文对象IfxMultican_Can_MsgObj_init(&canSrcMsgObj, &canMsgObjConfig);}}//上面已经把通道 、节点、报文配置好了,下面开始收发void CAN_Trannsfer(void){//全CAN报文初始化IfxMultican_Message_init(&txMsg,id,dataLow,dataHigh, IfxMultican_DataLengthCode_8);//数据的高低位,低位在前,高位在后//发送数据while(IfxMultican_Can_MsgObj_sendMessage(&canSrcMsgObj, &txMsg)==IfxMultican_Status_notSentBusy);}5) ADC采集模块// VADC模块IfxVadc_Adc vadc;// VDC 组//选择ADC0转换器IfxVadc_Adc_Group adcGroup[1];// VDC 通道//选择AN0和AN1分别作为温度和湿度传感器输入通道IfxVadc_Adc_Channel adcChannel[1][2];// 存储转换结果uint16 ADC_result[1][2];void ADC_init(void){int chnIx;// 创建配置变量IfxVadc_Adc_Config adcConfig;IfxVadc_Adc_initModuleConfig(&adcConfig, &MODULE_VADC);//初始化ADC模块全局参数,采样周期和结果精度IfxVadc_Adc_initModule(&vadc, &adcConfig);// 创建组配置变量IfxVadc_Adc_GroupConfig adcGroupConfig;IfxVadc_Adc_initGroupConfig(&adcGroupConfig, &vadc);//初始化Convertor0的 AN0和AN1通道int group_i = 0;for(group_i = 0; group_i<1;group_i++){// 定义组IDadcGroupConfig.groupId = group_i;// 设置Master ID为自己的ID,即自己作为MasteradcGroupConfig.master = adcGroupConfig.groupId;// 使能队列中断源adcGroupConfig.arbiter.requestSlotQueueEnabled = TRUE;// 使能扫描中断源adcGroupConfig.arbiter.requestSlotScanEnabled = TRUE;// 使能后台中断源adcGroupConfig.arbiter.requestSlotBackgroundScanEnabled = TRUE;//使能所有源的门adcGroupConfig.queueRequest.triggerConfig.gatingMode = IfxVadc_GatingMode_always;adcGroupConfig.scanRequest.triggerConfig.gatingMode = IfxVadc_GatingMode_always;adcGroupConfig.backgroundScanRequest.triggerConfig.gatingMode = IfxVadc_GatingMode_always;// 初始化组配置IfxVadc_Adc_initGroup(&adcGroup[group_i], &adcGroupConfig);// 创建通道配置变量IfxVadc_Adc_ChannelConfig adcChannelConfig[2];for(chnIx=0; chnIx<2; ++chnIx){IfxVadc_Adc_initChannelConfig(&adcChannelConfig[chnIx], &adcGroup[group_i]);//配置通道IDadcChannelConfig[chnIx].channelId = (IfxVadc_ChannelId)(chnIx);//配置通道结果寄存器,采用专用结果寄存器adcChannelConfig[chnIx].resultRegister = (IfxVadc_ChannelResult)(chnIx);// 初始化通道IfxVadc_Adc_initChannel(&adcChannel[group_i][chnIx], &adcChannelConfig[chnIx]);//添加到扫描源unsigned channels = (1 << adcChannelConfig[chnIx].channelId);unsigned mask = channels;boolean continuous = TRUE;IfxVadc_Adc_setScan(&adcGroup[group_i],channels,mask, continuous);}}}void ADC_Scan(void){int group_m;unsigned char chnIx;Ifx_VADC_RES conversionResult;for (group_m = 0; group_m < 1; group_m++){// 开启自动扫描IfxVadc_Adc_startScan(&adcGroup[group_m]);// 判断转换结果是否 有效,然后读取转换结果for (chnIx = 0; chnIx < 2; ++chnIx){// wait for valid result//Ifx_VADC_RES conversionResult;do {conversionResult = IfxVadc_Adc_getResult(&adcChannel[group_m][chnIx]);} while (!conversionResult.B.VF);ADC_result[group_m][chnIx] = conversionResult.B.RESULT;}}}6) MAIN主程序int core0_main (void){//使能中断__enable ();//看门狗禁能/** !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!* Enable the watchdog in the demo if it is required and also service the watchdog periodically* */IfxScuWdt_disableCpuWatchdog (IfxScuWdt_getCpuWatchdogPassword ());IfxScuWdt_disableSafetyWatchdog (IfxScuWdt_getSafetyWatchdogPassword ());//变量初始化dataLow = 0x0;dataHigh = 0x0;//模块初始化GPIO_Led_init();CAN_Init();ADC_init();Dio_init_input();Dio_init_output();while (1){//LED点亮GPIO_Led();//ADC模块扫描ADC_Scan();//查询读取IO状态DIO_State_Read();//热释电传感器模块采集到人红外信号有效if(IfxPort_getPinStateDio[0]==0){//打开摄像头主机开始录像Camera_On();dataLow = 0xeeeeeeee;dataHigh = 0xeeeeeeee;//上报有人异常信息CAN_Trannsfer();//蜂鸣器报警Beep_On();}//热释电传感器模块没采集到人红外信号else{//关闭摄像头主机Camera_Off();//蜂鸣器不报警Beep_Off();//上报温湿度信息dataLow = (uint32)ADC_result[0][0];dataHigh = (uint32)ADC_result[0][1];CAN_Trannsfer();}}return (1);}