/** * @brief This function handles TIM1 global interrupt request. * @param None * @retval None */ void TIM1_CC_IRQHandler(void) { if(TIM_GetITStatus(TIM1, TIM_IT_CC2) == SET) { /* Clear TIM1 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM1, TIM_IT_CC2); if(uhCaptureNumber == 0) { /* Get the Input Capture value */ uhIC3ReadValue1 = TIM_GetCapture2(TIM1); uhCaptureNumber = 1; } else if(uhCaptureNumber == 1) { /* Get the Input Capture value */ uhIC3ReadValue2 = TIM_GetCapture2(TIM1); /* Capture computation */ if (uhIC3ReadValue2 > uhIC3ReadValue1) { uwCapture = (uhIC3ReadValue2 - uhIC3ReadValue1); } else if (uhIC3ReadValue2 < uhIC3ReadValue1) { uwCapture = ((0xFFFF - uhIC3ReadValue1) + uhIC3ReadValue2); } else { uwCapture = 0; } /* Frequency computation */ uwTIM1Freq = (uint32_t) SystemCoreClock / uwCapture; uhCaptureNumber = 0; } } }
//定时器3中断服务程序 void TIM3_IRQHandler(void) { if((TIM3CH1_CAPTURE_STA&0X80)==0)//CH1 还未成功捕获 { if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)//捕获1发生捕获事件 { TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); if(TIM3CH1_CAPTURE_STA&0X40) //捕获到一个下降沿为真 发生上升沿中断后再次发生中断为下降沿 { TIM3CH1_CAPTURE_DOWNVAL=TIM_GetCapture1(TIM3); //获取当前的捕获值 if(TIM3CH1_CAPTURE_DOWNVAL<TIM3CH1_CAPTURE_UPVAL) { tim3_T=65535; } else tim3_T=0; tempup1=TIM3CH1_CAPTURE_DOWNVAL-TIM3CH1_CAPTURE_UPVAL+tim3_T;//得到总的高电平时间 pwmout1=tempup1; TIM3CH1_CAPTURE_STA=0; TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获 } else //发生捕获事件但不是下降沿,第一次捕获上升沿,清零,定时器开始计数 { TIM3CH1_CAPTURE_UPVAL=TIM_GetCapture1(TIM3); //获取上升沿的数据 TIM3CH1_CAPTURE_STA|=0X40; //标记以捕获到了上升沿 TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获 } } } if((TIM3CH2_CAPTURE_STA&0X80)==0)//CH1 还未成功捕获 { if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)//捕获1发生捕获事件 { TIM_ClearITPendingBit(TIM3,TIM_IT_CC2); if(TIM3CH2_CAPTURE_STA&0X40) //捕获到一个下降沿为真 发生上升沿中断后再次发生中断为下降沿 { TIM3CH2_CAPTURE_DOWNVAL=TIM_GetCapture2(TIM3); if(TIM3CH2_CAPTURE_DOWNVAL<TIM3CH2_CAPTURE_UPVAL) { tim3_T=65535; } else tim3_T=0; tempup2=TIM3CH2_CAPTURE_DOWNVAL-TIM3CH2_CAPTURE_UPVAL+tim3_T;//得到总的高电平时间 pwmout2=tempup2; TIM3CH2_CAPTURE_STA=0; TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获 } else //发生捕获事件但不是下降沿,第一次捕获上升沿,清零,定时器开始计数 { TIM3CH2_CAPTURE_UPVAL=TIM_GetCapture2(TIM3); //获取上升沿的数据 TIM3CH2_CAPTURE_STA|=0X40; //标记以捕获到了上升沿 TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获 } } } if((TIM3CH3_CAPTURE_STA&0X80)==0)//CH3 还未成功捕获 { if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)//捕获3发生捕获事件 { TIM_ClearITPendingBit(TIM3, TIM_IT_CC3); if(TIM3CH3_CAPTURE_STA&0X40) //捕获到一个下降沿为真 发生上升沿中断后再次发生中断为下降沿 { TIM3CH3_CAPTURE_DOWNVAL=TIM_GetCapture3(TIM3); if(TIM3CH3_CAPTURE_DOWNVAL<TIM3CH3_CAPTURE_UPVAL) { tim3_T=65535; } else tim3_T=0; tempup3=TIM3CH3_CAPTURE_DOWNVAL-TIM3CH3_CAPTURE_UPVAL+tim3_T;//得到总的高电平时间 pwmout3=tempup3; TIM3CH3_CAPTURE_STA=0; TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC3P=0 设置为上升沿捕获 } else //发生捕获事件但不是下降沿,第一次捕获上升沿,清零,定时器开始计数 { TIM3CH3_CAPTURE_UPVAL=TIM_GetCapture3(TIM3); //获取上升沿的数据 TIM3CH3_CAPTURE_STA|=0X40; //标记以捕获到了上升沿 TIM_OC3PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获 } } } if((TIM3CH4_CAPTURE_STA&0X80)==0)//CH4 还未成功捕获 { if (TIM_GetITStatus(TIM3, TIM_IT_CC4) != RESET)//捕获1发生捕获事件 { TIM_ClearITPendingBit(TIM3,TIM_IT_CC4); if(TIM3CH4_CAPTURE_STA&0X40) //捕获到一个下降沿为真 发生上升沿中断后再次发生中断为下降沿 { TIM3CH4_CAPTURE_DOWNVAL=TIM_GetCapture4(TIM3); if(TIM3CH4_CAPTURE_DOWNVAL<TIM3CH4_CAPTURE_UPVAL) { tim3_T=65535; } else tim3_T=0; tempup4=TIM3CH4_CAPTURE_DOWNVAL-TIM3CH4_CAPTURE_UPVAL+tim3_T;//得到总的高电平时间 pwmout4=tempup4; TIM3CH4_CAPTURE_STA=0; TIM_OC4PolarityConfig(TIM3,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获 } else //发生捕获事件但不是下降沿,第一次捕获上升沿,清零,定时器开始计数 { TIM3CH4_CAPTURE_UPVAL=TIM_GetCapture4(TIM3); //获取上升沿的数据 TIM3CH4_CAPTURE_STA|=0X40; //标记以捕获到了上升沿 TIM_OC4PolarityConfig(TIM3,TIM_ICPolarity_Falling); //CC4P=1 设置为下降沿捕获 } } } }
/*矩阵键盘10ms扫描中断*/ void TIM3_IRQHandler(void) { if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM3, TIM_IT_Update); /*下面是定时器中断到来该做的程序*/ //每次按键扫描都先扫一次(真的一次Once)旋转开关,判断是否与之前的旋转开关值相同, //若不同,则等待5个TIM3定时时间,再次扫一次旋转开关, if(iii == 0) KeyValue = OnceRotaryKey()&0x0F; /*旋转开关处理程序*/ if(KeyValue != RotaryKeyValue_before)//有扫到旋转开关按键值与之前不同,应是真有开关变化 { iii++; if(iii > 5) { u8 temp=KEY_NULL; temp = OnceRotaryKey()&0x0F; if(temp == KeyValue) { if(KeyValue >= KEY_VALUE_6)//0x06->KEY_VALUE_6,KeyValue有0x46,0x47,0x48,0x49,0x4A { RotaryKeyChanged_flag = 1; RotaryKeyValue = (KeyValue&0x0F);//只保留KeyValue中的按键值6-10 RotaryKeyValue_before = RotaryKeyValue;//RotaryKeyValue_before只在前面if(KeyValue != RotaryKeyValue_before)比较用到。 } } iii=0; KEY_LINE_write_high_all(); KeyValue = KEY_NULL;//按键值、状态清零 TIM_Cmd(TIM3, DISABLE);//同时关闭定时器 EXTI->IMR |= Keyboard_EXTI_Line;//同时开放来自线上的中断请求 } } /*软按钮处理程序,及开关抖动处理程序*/ else//若扫到旋转开关按键值与之前相同,可能是旋转开关抖动或有软按键,则扫软按键,若没有软按键,也将TIM3关闭,打开外部中断 { jjj++; KeyValue = ScanKey(); /*开关抖动处理程序*/ if(((KeyValue&0x0F) == KEY_NULL) && jjj > 100+6)//表示无软按键,100表示长按检测次数 (长按键检测阀值为100 所以这里的超时应该大于100) { jjj=0; KEY_LINE_write_high_all(); KeyValue = KEY_NULL;//按键值、状态清零 TIM_Cmd(TIM3, DISABLE);//同时关闭定时器 EXTI->IMR |= Keyboard_EXTI_Line;//同时开放来自线上的中断请求 6789 } /*软按钮处理程序*/ if((KeyValue&0xF0) != 0x00 && (KeyValue&0x0F) < KEY_VALUE_6)//有按键也有状态(短按、长按),其中0x0F->KEY_NULL { jjj=0; SoftKeyChanged_flag = 1; SoftKeyValue_before = SoftKeyValue; SoftKeyValue = (KeyValue&0x0F);//只保留KeyValue中的按键值0-5 if((KeyValue&0xF0)==0x90)//0x90=0b10010000 ShortKey_flag = 1; else if((KeyValue&0xF0)==KEY_LONG) LongKey_flag = 1; KEY_LINE_write_high_all(); KeyValue = KEY_NULL;//按键值、状态清零 TIM_Cmd(TIM3, DISABLE);//同时关闭定时器 EXTI->IMR |= Keyboard_EXTI_Line;//同时开放来自线上的中断请求 } } } }
/********************************************************************************* *Function : void TIM4_Tone_Interrupt_Handler(void) *Description : set duty cycle *Input : none *Output : none *Return : none *author : lz *date : 6-December-2013 *Others : **********************************************************************************/ void TIM4_Tone_Interrupt_Handler(void) { uint16_t capture; if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_CC1 ); capture = TIM_GetCapture1(TIM4); TIM_SetCompare1(TIM4, capture + PIN_MAP[9].timer_ccr); if(PIN_MAP[9].user_property != -1) { if (PIN_MAP[9].user_property > 0) { PIN_MAP[9].user_property -= 1; } else { noTone(9); } } } if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_CC2); capture = TIM_GetCapture2(TIM4); TIM_SetCompare2(TIM4, capture + PIN_MAP[8].timer_ccr); if(PIN_MAP[8].user_property != -1) { if (PIN_MAP[8].user_property > 0) { PIN_MAP[8].user_property -= 1; } else { noTone(8); } } } if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_CC3); capture = TIM_GetCapture2(TIM4); TIM_SetCompare2(TIM4, capture + PIN_MAP[6].timer_ccr); if(PIN_MAP[6].user_property != -1) { if (PIN_MAP[6].user_property > 0) { PIN_MAP[6].user_property -= 1; } else { noTone(6); } } } if (TIM_GetITStatus(TIM4, TIM_IT_CC4) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_CC4); capture = TIM_GetCapture2(TIM4); TIM_SetCompare2(TIM4, capture + PIN_MAP[7].timer_ccr); if(PIN_MAP[7].user_property != -1) { if (PIN_MAP[7].user_property > 0) { PIN_MAP[7].user_property -= 1; } else { noTone(7); } } } }
void TIM1_BRK_TIM9_IRQHandler(void) //-- UART2 Tx { // tn_event_iset(&event_all_uart, EVENT_UART3_TX_END); //-- Clear int flag /* TIM9_CH2 toggling with frequency = 366.2 Hz */ if (TIM_GetITStatus(TIM9, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM9, TIM_IT_Update); } if (beep.intSoundOn || beep.shortSoundOn) { GPIO_ToggleBits ( GPIOC, GPIO_Pin_13); } else { GPIO_ResetBits ( GPIOC, GPIO_Pin_13); } if (beep.shortSoundOn) { if (beep.ShortDownCounter--) { } else { beep.shortSoundOn = 0; } } if (beep.intSoundEnable) { if (beep.intSoundCounter) { beep.intSoundCounter--; beep.intSoundOn = 1; beep.intPauseCounter = 2000; } else { if (beep.intPauseCounter) { beep.intPauseCounter--; beep.intSoundOn = 0; } else { beep.intSoundCounter = 2000; } } } else { beep.intSoundOn = 0; } #ifdef TNKERNEL_PORT_CORTEXM3 tn_int_exit(); #endif }
/*void PPP_IRQHandler(void) { }*/ void macTIMx_IRQHandler(void) { static uint8_t pwm_index = 0; //用于PWM查表 static uint8_t period_cnt = 0; //用于计算周期数 if (TIM_GetITStatus(macTIMx, TIM_IT_Update) != RESET) //TIM_IT_Update { time_cnt++; if(time_cnt>20000) { period_cnt++; enter =1; if(period_cnt >= 1) //若输出的周期数大于10,输出下一种脉冲宽的PWM波 { macTIMx->macCCRx = indexWave[pwm_index]; //根据PWM表修改定时器的比较寄存器值 pwm_index++; //标志PWM表的下一个元素 if( pwm_index >= PWM_count) //若PWM脉冲表已经输出完成一遍,重置PWM查表标志 { pwm_index=0; enter = 0; time_cnt=0; } period_cnt=0; //重置周期计数标志 } } //else if(time_cnt>1000) //{macTIMx->macCCRx = 0;} else {macTIMx->macCCRx = 0;} if(time_cnt == 0) { //五次更改一次 高占比时间 实现 这里需要完善一下 if(back_flag==1) { if(change_Hduty_cnt>=4) { change_Hduty_cnt = 0; if(H_count <= 1) { H_count=0; L_count=0; } else if (stop_Hduty_change) { H_count -= 1; L_count=50; } else { H_count = 0; L_count=0; } PWM_control(); } else change_Hduty_cnt +=1; } if(time_count_flag) { if(count_times>=100) //测试 1000次 { time_count_flag =0; H_count=0; L_count=0; PWM_control(); } else {count_times =count_times+1;} } } TIM_ClearITPendingBit (macTIMx, TIM_IT_Update); //必须要清除中断标志位 } }
/** * @brief This function handles TIM3 global interrupt request. * @param None * @retval None */ void TIM3_IRQHandler(void) { if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) { /* Get ADC values */ uint32_t bat_level_value=ADC_ConvertedValueTabPointer[1]*/*41*/ADC_TO_MV_CONSTANT; uint32_t regulatedOutputCurrentValue=ADC_ConvertedValueTabPointer[2]*ADC_TO_MA_CONSTANT; uint32_t bat_current_value=ADC_ConvertedValueTabPointer[3]*/*32*/ADC_TO_MA_CONSTANT; /* printf("Bat Level: %d\n\r", bat_level_value); printf("Bat charge current: %d\n\r", bat_current_value); printf("Reg output Level: %d\n\r", ADC_ConvertedValueTabPointer[0]*ADC_TO_MV_CONSTANT); printf("Reg output current: %d\n\r", regulatedOutputCurrentValue); printf("Charger Level: %d\n\r", ADC_ConvertedValueTabPointer[4]*ADC_TO_MV_CONSTANT); */ /* Check for the events related to the ADC values */ /* PC is turned off (it was previously on) */ if (state.regulatedOutputState==ON && regulatedOutputCurrentValue<CURRENT_ON_OFF_THRESHOLD && TurnOnDelyTime==0 && !noCurrentFlag /* && state.externalPowerSourceState==DISCONNECTED */ && pcOn) { NoCurrentTimeout=2; noCurrentFlag=true; } else if(noCurrentFlag && regulatedOutputCurrentValue>CURRENT_ON_OFF_THRESHOLD) { noCurrentFlag=false; if(!pcOn) pcOn=true; } if(noCurrentFlag && NoCurrentTimeout<=0 && pcOn) { /* Event EV.3 detected */ noCurrentFlag=false; EXTI_GenerateSWInterrupt(EXTI_Line9); } /* Battery below KO level */ if (state.batteryState==DISCHARGING && bat_level_value<BATTERY_KO_THRESHOLD && !batteryKoFlag) { BatteryKoTimeout=5; batteryKoFlag=true; } else if(batteryKoFlag) { if(bat_level_value>BATTERY_KO_THRESHOLD || state.externalPowerSourceState==CONNECTED) batteryKoFlag=false; } if(batteryKoFlag && BatteryKoTimeout<=0) { /* Event EV.6 detected */ batteryKoFlag=false; EXTI_GenerateSWInterrupt(EXTI_Line6); } /* Battery charge to constant voltage mode */ else if (state.batteryState==CHARGING_CC && bat_level_value>BAT_CC_CV_THRESHOLD) { /* Event EV.4 detected */ EXTI_GenerateSWInterrupt(EXTI_Line8); } /* Battery fully charged */ else if (state.batteryState==CHARGING_CV && bat_current_value<BAT_CURRENT_CHARGED_THRESHOLD) { /* Event EV.5 detected */ EXTI_GenerateSWInterrupt(EXTI_Line7); } getBatteryLevel(4, 3300, 3600, state.batteryState, bat_level_value); TIM_ClearITPendingBit(TIM3, TIM_IT_Update); } }
static void PIOS_TIM_generic_irq_handler(TIM_TypeDef * timer) { /* Iterate over all registered clients of the TIM layer to find channels on this timer */ for (uint8_t i = 0; i < pios_tim_num_devs; i++) { const struct pios_tim_dev * tim_dev = &pios_tim_devs[i]; if (!tim_dev->channels || tim_dev->num_channels == 0) { /* No channels to process on this client */ continue; } /* Check for an overflow event on this timer */ bool overflow_event; uint16_t overflow_count; if (TIM_GetITStatus(timer, TIM_IT_Update) == SET) { TIM_ClearITPendingBit(timer, TIM_IT_Update); overflow_count = timer->ARR; overflow_event = true; } else { overflow_count = 0; overflow_event = false; } for (uint8_t j = 0; j < tim_dev->num_channels; j++) { const struct pios_tim_channel * chan = &tim_dev->channels[j]; if (chan->timer != timer) { /* channel is not on this timer */ continue; } /* Figure out which interrupt bit we should be looking at */ uint16_t timer_it; switch (chan->timer_chan) { case TIM_Channel_1: timer_it = TIM_IT_CC1; break; case TIM_Channel_2: timer_it = TIM_IT_CC2; break; case TIM_Channel_3: timer_it = TIM_IT_CC3; break; case TIM_Channel_4: timer_it = TIM_IT_CC4; break; default: PIOS_Assert(0); break; } bool edge_event; uint16_t edge_count; if (TIM_GetITStatus(chan->timer, timer_it) == SET) { TIM_ClearITPendingBit(chan->timer, timer_it); /* Read the current counter */ switch(chan->timer_chan) { case TIM_Channel_1: edge_count = TIM_GetCapture1(chan->timer); break; case TIM_Channel_2: edge_count = TIM_GetCapture2(chan->timer); break; case TIM_Channel_3: edge_count = TIM_GetCapture3(chan->timer); break; case TIM_Channel_4: edge_count = TIM_GetCapture4(chan->timer); break; default: PIOS_Assert(0); break; } edge_event = true; } else { edge_event = false; edge_count = 0; } if (!tim_dev->callbacks) { /* No callbacks registered, we're done with this channel */ continue; } /* Generate the appropriate callbacks */ if (overflow_event & edge_event) { /* * When both edge and overflow happen in the same interrupt, we * need a heuristic to determine the order of the edge and overflow * events so that the callbacks happen in the right order. If we * get the order wrong, our pulse width calculations could be off by up * to ARR ticks. That could be bad. * * Heuristic: If the edge_count is < 16 ticks above zero then we assume the * edge happened just after the overflow. */ if (edge_count < 16) { /* Call the overflow callback first */ if (tim_dev->callbacks->overflow) { (*tim_dev->callbacks->overflow)((uintptr_t)tim_dev, tim_dev->context, j, overflow_count); } /* Call the edge callback second */ if (tim_dev->callbacks->edge) { (*tim_dev->callbacks->edge)((uintptr_t)tim_dev, tim_dev->context, j, edge_count); } } else { /* Call the edge callback first */ if (tim_dev->callbacks->edge) { (*tim_dev->callbacks->edge)((uintptr_t)tim_dev, tim_dev->context, j, edge_count); } /* Call the overflow callback second */ if (tim_dev->callbacks->overflow) { (*tim_dev->callbacks->overflow)((uintptr_t)tim_dev, tim_dev->context, j, overflow_count); } } } else if (overflow_event && tim_dev->callbacks->overflow) { (*tim_dev->callbacks->overflow)((uintptr_t)tim_dev, tim_dev->context, j, overflow_count); } else if (edge_event && tim_dev->callbacks->edge) { (*tim_dev->callbacks->edge)((uintptr_t)tim_dev, tim_dev->context, j, edge_count); } } } }
void us_ticker_clear_interrupt(void) { if (TIM_GetITStatus(TIM_MST, TIM_IT_CC1) == SET) { TIM_ClearITPendingBit(TIM_MST, TIM_IT_CC1); } }
void TIM8_UP_TIM13_IRQHandler(void) { float I1 = 0, I2 = 0, power = 0; static float percent = 0, percent1 = 0 ; float error_pid = 0; static uint16_t interrupt_times = 0; // static uint8_t flag_modbus = 0; // rt_enter_critical();/*调度器上锁*/ rt_interrupt_enter(); logic_out(1,1); I1 = adc_get(0);//电流值 // I2 = adc_get(1); power = adc_get(2);//功率百分比 trig_adc(); if(interrupt_times < 500) { p_get[interrupt_times] = power; i_get[interrupt_times] = I1; } if(TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM8, TIM_IT_Update); if(pwm_struct[select_pwm].mode == 0) { // TIM_SetCompare1(TIM8, (uint16_t)(percent * pwm_period)); if(interrupt_times < pwm_struct[select_pwm]. positive_pulse) { pwm_struct[select_pwm].busy_flag = 1; pwm_ena(1); { error_pid = pwm_struct[select_pwm].p_array[interrupt_times] \ * program_data[select_pwm].current_max - I1; // logic_out(1,1); percent = pid3(error_pid); // logic_out(1,0); if(percent > 0.98f) percent = 0.98f; if(percent < 0.0f) percent = 0.0f; // percent = 0.3f; interrupt_times++; } } else { interrupt_times++; percent = 0; if( interrupt_times > pwm_struct[select_pwm].positive_pulse + pwm_struct[select_pwm].negative_pulse) { TIM_Cmd(TIM8, DISABLE); TIM_ClearITPendingBit(TIM8, TIM_IT_Update); pwm_ena(0); interrupt_times = 0; pwm_struct[select_pwm].busy_flag = 0; IntTerm_C3 = 0; PrevError_C3 = 0; } } } else { if(interrupt_times < pwm_struct[select_pwm]. positive_pulse) { pwm_struct[select_pwm].busy_flag = 1; pwm_ena(1); { // logic_out(1,1); error_pid = pwm_struct[select_pwm].p_array[interrupt_times] - power; percent1 = pid1(error_pid);//percent1为计算得出的电流值 if(percent1 > program_data[select_pwm].current_max) percent1 = program_data[select_pwm].current_max; if(percent1 < 0.0f) percent1 = 0.0f; error_pid = percent1 - I1; percent = pid3(error_pid); // logic_out(1,0); if(percent > 0.98f) percent = 0.98f; if(percent < 0.0f) percent = 0.0f; // percent = 0.3f; interrupt_times++; } } else { interrupt_times++; percent = 0; if( interrupt_times > pwm_struct[select_pwm].positive_pulse + pwm_struct[select_pwm].negative_pulse) { TIM_Cmd(TIM8, DISABLE); TIM_ClearITPendingBit(TIM8, TIM_IT_Update); pwm_ena(0); interrupt_times = 0; pwm_struct[select_pwm].busy_flag = 0; IntTerm_C3 = 0; PrevError_C3 = 0; IntTerm_C1 = 0; PrevError_C1 = 0; } } } TIM_SetCompare1(TIM8, (uint16_t)(pwm_period * percent)); if(interrupt_times < 500) { pid_out[interrupt_times] = percent; code[interrupt_times] = TIM8->CCR1; } } logic_out(1,0); rt_interrupt_leave(); // rt_exit_critical();/* 退出临界区*/ }
//A0 void TIM5_IRQHandler(void) { if (TIM_GetITStatus(TIM5, TIM_IT_CC1) != RESET){ TIM_ClearITPendingBit(TIM5, TIM_IT_CC1); } }
//// } //// /*加速处理过程结束*/ //// //// /*减速处理过程*/ //// if(MotorFreqD == 1) //// { //// MotorFreqD = 2; //// FreqFlag = 0; //// } //// if(MotorFreqD == 2) //// { //// TIM_Cmd(TIM1,DISABLE); //// if(FreqFlag < MotorFreqFlag) //// { //// if(FreqFlag != (MotorFreqFlag - 1)) //// { //// PTO_HZ_Period(MotorFreqNow - DEC_HZ_MIN, DEC_PERIOD); //// } //// else if(FreqFlag == (MotorFreqFlag - 1)) //// { //// PTO_Stop(); //// TIM_Cmd(TIM5,DISABLE);//关闭定时 //// //// MotorFreqD = 0; //// MotorFreqFlag = 0;/*恢复初值*/ //// FreqFlag = 0; /*恢复中间变量初值*/ //// } //// FreqFlag ++; //// } //// } //// } //// TIM_ClearITPendingBit(TIM5, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源 //// OSIntExit(); ////} ////定时器1更新中断服务程序 void TIM1_UP_IRQHandler(void) { static u8 tempflag = 0; OSIntEnter(); if(MotorDir == 1) PulseNum_Global++; else if(MotorDir == 2) PulseNum_Global--; if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 { switch(MotorActionState) { case 1://启动加速状态 tempflag = 0; /*计算中间变量 设定初值 0*/ PulseNum_TEMP =(MotorDir > 1)? (PulseNum_Global +1):(PulseNum_Global - 1);/*记录初始加速脉冲初值*/ MotorActionState = 2;//转移状态 2 加速过程中 break; case 2://加速过程 处理 if((u32)abs(PulseNum_Global - PulseNum_TEMP) > (PulseNum_Buf[tempflag] - 1)) //输出达到指定脉冲数 { PulseNum_TEMP = PulseNum_Global;/*记录初始加速脉冲初值*/ tempflag++; if(tempflag == ACC_Status_Flag) // { if(PulseNum_ConstantVel == 0) /*如果设定脉冲数等于2倍的加减速所需脉冲个数 即匀速动作脉冲数为 0 加速完毕直接进入减速过程*/ { tempflag = 0; MotorActionState = 4;//跳至减速状态 break; } else { MotorActionState = 3;//进入恒速动作过程 PTO_Cycle(MotorCycleTarget); break; } } if(tempflag < ACC_Status_Flag) //未达到目标频率 继续加速 { PTO_Cycle(MotorCycle_Buf[tempflag]); } } break; case 3://恒速动作 if((u32)abs(PulseNum_Global - PulseNum_TEMP) > (PulseNum_ConstantVel - 1))//输出达到指定脉冲数 { tempflag = 0; MotorActionState = 4; //转移状态 4 需要进入减速 } break; case 4://启动减速 tempflag = DEC_Status_Flag; /*计算中间变量 设定初值 0*/ PulseNum_TEMP =(MotorDir > 1)? (PulseNum_Global +1):(PulseNum_Global - 1); PTO_Cycle(MotorCycle_Buf[tempflag - 1]); MotorActionState = 5; //转移状态5 减速过程中 break; case 5://减速过程中 if((u32)abs(PulseNum_Global - PulseNum_TEMP) > ((PulseNum_Buf[tempflag -1] - 1)))//达到指定脉冲数 { PulseNum_TEMP = PulseNum_Global;/*记录脉冲值*/ tempflag--; if(tempflag == 0) //达到0 { PTO_Stop(); } if(tempflag > 0) { PTO_Cycle(MotorCycle_Buf[tempflag - 1]); } } break; default: break; } } TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //清除TIMx的中断待处理位:TIM 中断源 OSIntExit(); }
void TIM2_IRQHandler() { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); io.interrupt(); } }
void TIM3_Tone_Interrupt_Handler(void) { uint16_t capture; if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET) { TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 ); capture = TIM_GetCapture1(TIM3); TIM_SetCompare1(TIM3, capture + PIN_MAP[14].timer_ccr); if(PIN_MAP[14].user_property != -1) { if (PIN_MAP[14].user_property > 0) { PIN_MAP[14].user_property -= 1; } else { noTone(14); } } } if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET) { TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); capture = TIM_GetCapture2(TIM3); TIM_SetCompare2(TIM3, capture + PIN_MAP[15].timer_ccr); if(PIN_MAP[15].user_property != -1) { if (PIN_MAP[15].user_property > 0) { PIN_MAP[15].user_property -= 1; } else { noTone(15); } } } if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET) { TIM_ClearITPendingBit(TIM3, TIM_IT_CC3); capture = TIM_GetCapture3(TIM3); TIM_SetCompare3(TIM3, capture + PIN_MAP[16].timer_ccr); if(PIN_MAP[16].user_property != -1) { if (PIN_MAP[16].user_property > 0) { PIN_MAP[16].user_property -= 1; } else { noTone(16); } } } if (TIM_GetITStatus(TIM3, TIM_IT_CC4) != RESET) { TIM_ClearITPendingBit(TIM3, TIM_IT_CC4); capture = TIM_GetCapture4(TIM3); TIM_SetCompare4(TIM3, capture + PIN_MAP[17].timer_ccr); if(PIN_MAP[17].user_property != -1) { if (PIN_MAP[17].user_property > 0) { PIN_MAP[17].user_property -= 1; } else { noTone(17); } } } }
void TIM8_CC_IRQHandler() { TIM_ICInitTypeDef TIM_ICInitStructure; TIM_ICStructInit(&TIM_ICInitStructure); //Initialize the structure with default setting if(TIM_GetITStatus(TIM8, TIM_IT_CC1) == SET) { TIM_ClearITPendingBit(TIM8, TIM_IT_CC1); TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; if(capture_state[THROTTLE_CHANNEL] == RISE) { //Rised edge capture rise_value[THROTTLE_CHANNEL] = TIM_GetCapture1(TIM8); //Change to measure the falled edge capture_state[THROTTLE_CHANNEL] = FALL; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; } else { //Falled edge capture fall_value[THROTTLE_CHANNEL] = TIM_GetCapture1(TIM8); /* Calculate the RC high pulse value */ if(fall_value[THROTTLE_CHANNEL] > rise_value[THROTTLE_CHANNEL]) rc_value[THROTTLE_CHANNEL] = fall_value[THROTTLE_CHANNEL] - rise_value[THROTTLE_CHANNEL]; else if(fall_value[THROTTLE_CHANNEL] == 0 && rise_value[THROTTLE_CHANNEL] == 0) rc_value[THROTTLE_CHANNEL] = 0; //No signal else /* falled edge measured value is overflow */ rc_value[THROTTLE_CHANNEL] = (fall_value[THROTTLE_CHANNEL] + 10000) - rise_value[THROTTLE_CHANNEL]; //Change to measure the rised edge capture_state[THROTTLE_CHANNEL] = RISE; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; } TIM_ICInit(TIM8, &TIM_ICInitStructure); } if(TIM_GetITStatus(TIM8, TIM_IT_CC2) == SET) { TIM_ClearITPendingBit(TIM8, TIM_IT_CC2); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; if(capture_state[YAW_CHANNEL] == RISE) { //Rised edge capture rise_value[YAW_CHANNEL] = TIM_GetCapture2(TIM8); //Change to measure the falled edge capture_state[YAW_CHANNEL] = FALL; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; } else { //Falled edge capture fall_value[YAW_CHANNEL] = TIM_GetCapture2(TIM8); /* Calculate the RC high pulse value */ if(fall_value[YAW_CHANNEL] > rise_value[YAW_CHANNEL]) rc_value[YAW_CHANNEL] = fall_value[YAW_CHANNEL] - rise_value[YAW_CHANNEL]; else if(fall_value[YAW_CHANNEL] == 0 && rise_value[YAW_CHANNEL] == 0) rc_value[YAW_CHANNEL] = 0; //No signal else /* falled edge measured value is overflow */ rc_value[YAW_CHANNEL] = (fall_value[YAW_CHANNEL] + 10000) - rise_value[YAW_CHANNEL]; //Change to measure the rised edge capture_state[YAW_CHANNEL] = RISE; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; } TIM_ICInit(TIM8, &TIM_ICInitStructure); } }
/* Button event timer */ void tim6_dac_handler(void) { if (TIM_GetITStatus(TIM6, TIM_IT_Update)) { buttons_event(); TIM_ClearITPendingBit(TIM6, TIM_IT_Update); } }
// Sending out data regulary ~ 0,25 sec period void TIM7_IRQHandler(void) { if (TIM_GetITStatus(TIM7, TIM_IT_Update) == SET) { TIM_ClearITPendingBit(TIM7, TIM_IT_Update); uint8_t received_data; // Sending out data if client started the system if(WORK == 1) { /*Temperature message: * 1 byte - START * 1 byte - HEADER * 4 byte - Length - Client needs this length * 2 byte - Data * TOTAL: 8 byte */ uint8_t message_temperature[temperature_message_length]; //Sending temperature 1 data temperature = 0; temperature = get_temperature(1); construct_temperature_message(message_temperature,temperature,ID_temp1); Send_data(message_temperature,temperature_message_length); //Sending temperature 2 data temperature = 0; temperature = get_temperature(2); //temperature = 0; construct_temperature_message(message_temperature,temperature,ID_temp2); Send_data(message_temperature,temperature_message_length); //Sending temperature 3 data temperature = 0; temperature = get_temperature(3); construct_temperature_message(message_temperature,temperature,ID_temp3); Send_data(message_temperature,temperature_message_length); //Sending temperature 4 data temperature = 0; temperature = get_temperature(4); //temperature = 0; construct_temperature_message(message_temperature,temperature,ID_temp4); Send_data(message_temperature,temperature_message_length); //free(message); /*Fan frequency message: * 1 byte - START * 1 byte - HEADER * 4 byte - Length - Client needs this length * 1 byte - Data * TOTAL: 7 byte */ uint8_t message_frequency[fan_frequency_message_length]; // Sending frequency of Fan 1 if(Fan_1_frequency == Fan_1_frequency_previous && Fan_1_frequency < 15) { Fan_1_frequency = 0; Fan_1_frequency_previous = 0; } construct_fan_frequency_message(message_frequency,Fan_1_frequency,ID_freq1); Send_data(message_frequency,fan_frequency_message_length); // Sending frequency of Fan 3 if(Fan_3_frequency == Fan_3_frequency_previous && Fan_3_frequency < 15) { Fan_3_frequency = 0; Fan_3_frequency_previous = 0; } construct_fan_frequency_message(message_frequency,Fan_3_frequency,ID_freq3); Send_data(message_frequency,fan_frequency_message_length); /*Fan PWM message: * 1 byte - START * 1 byte - HEADER * 4 byte - Length - Client needs this length * 1 byte - Data * TOTAL: 7 byte */ uint8_t message_PWM[fan_PWM_message_length]; // Sending PWM of fan 1 construct_fan_PWM_message(message_PWM,Fan_1_PWM,ID_fan_1_PWM); Send_data(message_PWM,fan_PWM_message_length); // Sending PWM of fan 3 construct_fan_PWM_message(message_PWM,Fan_3_PWM,ID_fan_3_PWM); Send_data(message_PWM,fan_PWM_message_length); } else { //If sending out data is not allowed, send halted message continuously uint8_t message_Halted[halted_message_length]; construct_halt_message(message_Halted); Send_data(message_Halted,halted_message_length); } // Processing received data while(Get_data(&received_data) == 1){ if(received_data == 0xff){ start_arrived = 1; header = 0; } else if(start_arrived){ header = received_data; start_arrived = 0; header_arrived = 1; } else if(header_arrived){ length += ((uint32_t)received_data) << (length_byte_count * 8); length_byte_count++; if(length_byte_count == 4){ header_arrived = 0; } } else if(length_byte_count == 4){ data[data_byte_count] = received_data; data_byte_count++; if(data_byte_count == length){ length_byte_count = 0; length = 0; data_byte_count = 0; Parse_message(header,data); } } } } }
// Used to increment the slave counter static void tim_update_irq_handler(void) { if (TIM_GetITStatus(TIM_MST, TIM_IT_Update) == SET) { TIM_ClearITPendingBit(TIM_MST, TIM_IT_Update); SlaveCounter++; } }
/** * @brief This function handles TIM2 global interrupt request. * @param None * @retval None */ void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { if(goingOn) { TurnOnDelyTime--; if(TurnOnDelyTime<=0) { goingOn=false; } } if(goingOff) { TurnOffDelyTime--; if(TurnOffDelyTime<=0) { pcOn=false; goingOff=false; state.unregulatedOutputState=NOT_ACTIVE; GPIO_ResetBits(GPIOB,GPIO_Pin_14); if (state.externalPowerSourceState==DISCONNECTED) { /* Execute actions asociated */ /* Turn UnRegulatedOutput off */ //TurnPowerExternal(); /* PW_CTR to disabled state */ TurnPowerOff(); //It is possible to change the led status /* Change to the new state */ /* Turn RegulatedOutputDC off */ state.regulatedOutputState=OFF; /* The PWM control signal goes to off state afterwards, in the main loop */ #ifdef DEBUG_INFO_USART printf("Status number %d\n", getStateId(state)); #endif /* Disable Power Lines */ #ifdef TR_INVERSE GPIO_SetBits(GPIOA,GPIO_Pin_8); #else GPIO_ResetBits(GPIOA,GPIO_Pin_8); #endif GPIO_ResetBits(GPIOA,GPIO_Pin_11); /* Disabel devices */ TIM_SetCompare3(TIM3,0); /* Enter STOP mode */ #ifdef DEBUG_INFO_USART printf("Going to STOP mode\n"); #endif #ifndef DEBUG_NO_STOP TIM_ClearITPendingBit(TIM2, TIM_IT_Update); PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); #endif } } } if(batteryKoFlag) { BatteryKoTimeout--; } if(noCurrentFlag) { NoCurrentTimeout--; } if(sendPcPulse && pcPulseStarted) { GPIO_SetBits(GPIOB,GPIO_Pin_8); sendPcPulse=false; pcPulseStarted=false; } TIM_ClearITPendingBit(TIM2, TIM_IT_Update); } else if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) { if(sendPcPulse) { GPIO_ResetBits(GPIOB,GPIO_Pin_8); pcPulseStarted=true; } TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); } }
void TIM2_IRQHandler(void){ struct CNC_Movement_t movement; BaseType_t xTaskWokenByReceive = pdFALSE; uint32_t xLimitState = 1; //TODO: pulled up, can cancel uint32_t yLimitState = 1; uint32_t zLimitState = 1; if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET){ if(timer2State){ if(xQueueReceiveFromISR( movementQueue, &movement, &xTaskWokenByReceive) != pdFALSE){ if(movement.speed != -1){ TIM_PrescalerConfig(TIM2, 10000 / movement.speed, TIM_PSCReloadMode_Update); } xLimitState = GPIO_ReadInputDataBit(Limit1PinPort, XLimit1Pin) & GPIO_ReadInputDataBit(Limit2PinPort, XLimit2Pin); yLimitState = GPIO_ReadInputDataBit(Limit1PinPort, YLimit1Pin) & GPIO_ReadInputDataBit(Limit2PinPort, YLimit2Pin); zLimitState = GPIO_ReadInputDataBit(Limit1PinPort, ZLimit1Pin) & GPIO_ReadInputDataBit(Limit2PinPort, ZLimit2Pin); if(!xLimitState){ xPos = 0; } if(!yLimitState){ yPos = 0; } if(!zLimitState){ zPos = 0; } //TODO: Shrink This to fit 2 switch if(xInvertCoefficient * movement.x < 0){ if(!xLimitState){ movement.x = 0; } GPIO_ResetBits(DirPinPort, XDirPin); }else{ if(xPos >= X_STEP_LIMIT){ movement.x = 0; } GPIO_SetBits(DirPinPort, XDirPin); } if(yInvertCoefficient * movement.y < 0){ if(!yLimitState){ movement.y = 0; } GPIO_SetBits(DirPinPort, YDirPin); }else{ if(yPos >= Y_STEP_LIMIT){ movement.y = 0; } GPIO_ResetBits(DirPinPort, YDirPin); } if(zInvertCoefficient * movement.z < 0){ if(zPos >= Z_STEP_LIMIT){ movement.z = 0; } GPIO_SetBits(DirPinPort, ZDirPin); }else{ if(!zLimitState){ movement.z = 0; } GPIO_ResetBits(DirPinPort, ZDirPin); } if(movement.x && movement.y){ GPIO_SetBits(StepPinPort, XStepPin | YStepPin); xPos += movement.x; yPos += movement.y; TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update); }else if(movement.x){ GPIO_SetBits(StepPinPort, XStepPin); xPos += movement.x; TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update); }else if(movement.y){ GPIO_SetBits(StepPinPort, YStepPin); yPos += movement.y; TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update); } if(movement.z){ GPIO_SetBits(StepPinPort, ZStepPin); zPos += movement.z; TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update); } if(xPos < 0) xPos = 0; if(yPos < 0) yPos = 0; if(zPos < 0) zPos = 0; } }else{ GPIO_ResetBits(StepPinPort, XStepPin | YStepPin | ZStepPin); TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update); } timer2State = !timer2State; if( xTaskWokenByReceive != pdFALSE ){ portYIELD_FROM_ISR( xTaskWokenByReceive ); } } }
void TIM3_IRQHandler(void){ if(TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET){ TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); (*tim3Callback)(); } }
static inline void pwmIRQHandler(TIM_TypeDef *tim) { pwm_in_counter++; uint8_t i; uint16_t val = 0; uint16_t time_on = 0; uint16_t time_off = 0; for (i = 0; i < PWMIN_CHANNEL_NUM; i++) { struct TIM_Channel channel = Channels[i]; struct PWM_State *input = &Inputs[i]; if (channel.tim == tim && (TIM_GetITStatus(tim, channel.tim_cc) == SET)) { TIM_ClearITPendingBit(channel.tim, channel.tim_cc); switch (channel.tim_channel) { case TIM_Channel_1: val = TIM_GetCapture1(channel.tim); break; case TIM_Channel_2: val = TIM_GetCapture2(channel.tim); break; case TIM_Channel_3: val = TIM_GetCapture3(channel.tim); break; case TIM_Channel_4: val = TIM_GetCapture4(channel.tim); break; } if (input->state == 0) { input->rise = val; if(input->rise > input->fall) time_off = input->rise - input->fall; else time_off = ((0xFFFF - input->fall) + input->rise); // switch states if ((time_off >= MINOFFWIDTH) && (time_off <= MAXOFFWIDTH)){ input->state = 1; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; TIM_ICInitStructure.TIM_Channel = channel.tim_channel; TIM_ICInit(channel.tim, &TIM_ICInitStructure); } } else { input->fall = val; if (input->fall > input->rise) time_on = (input->fall - input->rise); else time_on = ((0xFFFF - input->rise) + input->fall); if ((time_on >= MINONWIDTH) && (time_on <= MAXONWIDTH)) { // compute capture input->capture = time_on; // switch state input->state = 0; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_Channel = channel.tim_channel; TIM_ICInit(channel.tim, &TIM_ICInitStructure); } } } } }
/** * @brief This function handles TIM2 global interrupt request. * @param None * @retval None */ void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //IRQ_count++; } }
int main() { RCC_Config(); // to read and write to AFIO_remap AFIO_clock must first be eanble NVIC_Config(); TIM_Config(); GPIO_Config(); EXTI_Configuration(); initUsart(); //initBT(); // Only for debug the clock tree *********************************************** // Put the clock configuration into RCC_APB2PeriphClockCmd RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* Output clock on MCO pin ---------------------------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // RCC_MCOConfig(RCC_MCO_HSE); // Put on MCO pin the: freq. of external crystal RCC_MCOConfig(RCC_MCO_SYSCLK); // Put on MCO pin the: System clock selected // //RTCInit(); RCC_GetClocksFreq(&RCC_Clocks); writeStringUSART2("\033[2J"); //clear usart 2 screen writeStringUSART1("\033[2J"); //clear usart 1 screen writeStringUSART1(" <<<<- Welcome To Hinkens Cobra Bluetooth ->>>>\n\n\r"); while(1) { if(GPIO_ReadInputDataBit(GPIOB, COBRA_BOARDSWITCH)== false) writeStringUSART1("On Hook\n\r"); else //Luren är lyft { writeStringUSART1("Phone is off hook!\n\r Dial: \n\r"); acceptCall(); //accept incoming call Delay(Delay_10ms); //for(counter = 0 ; counter < 10 ; counter++) while(TIM_GetITStatus(TIM2, TIM_IT_Update) == RESET) // { dialed_number = 0; while(GPIO_ReadInputDataBit(GPIOB, COBRA_DIALING)== true && GPIO_ReadInputDataBit(GPIOB, COBRA_BOARDSWITCH)== true)//Wait for user to start dialing { //writeStringUSART1("Timer Counter: %d\r\n",TIM_GetCounter(TIM2)); if(state == 1 && dialingFlag == true) //check if timer has run out and user has started a call { writeStringUSART1("time is up dialing number: "); for (int x = 0; x < counter; x++) { //printf("%d", number[x] ); } sendPhoneNumber(number, counter); writeStringUSART1("\r\n"); dialingFlag = false; state = 0; counter = 0; } } Delay(Delay_100ms); //Wait for switch to debounce while(GPIO_ReadInputDataBit(GPIOB, COBRA_DIALING) == false) { dialingFlag = true; state = 0; TIM_SetCounter(TIM2, 0); TIM_Cmd(TIM2, ENABLE); //Start timer //RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); Delay(Delay_10ms); Delay(Delay_10ms); //Now count how many times the mechnical switch toggles if(GPIO_ReadInputDataBit(GPIOB, COBRA_PULSE)== false && pulseFlag == 1) { pulseFlag = 0; dialed_number ++; } else if(GPIO_ReadInputDataBit(GPIOB, COBRA_PULSE) == true && pulseFlag == 0) { pulseFlag = 1; //Do notthing just idle.. } Delay(Delay_10ms); } Delay(Delay_100ms); if(dialed_number != 0) // to handle the error of a extra loop after dialed number { dialed_number--; //Rotary always has one extra closure that must be taken off number[counter] = dialed_number; counter ++; } if(GPIO_ReadInputDataBit(GPIOB, COBRA_BOARDSWITCH)== false) //Hunng upp { hangUp(); //hang-up command to bluettooh writeStringUSART1("Hung up!\n\r"); counter = 0; break; } //printf("%d\n\r", dialed_number); }//end for loop } Delay(Delay_100ms); }//end while(1) }//end main()
/********************************************************************************* *Function : void TIM2_Tone_Interrupt_Handler(void) *Description : set duty cycle *Input : none *Output : none *Return : none *author : lz *date : 6-December-2013 *Others : **********************************************************************************/ void TIM2_Tone_Interrupt_Handler(void) { uint16_t capture; if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 ); capture = TIM_GetCapture1(TIM2); TIM_SetCompare1(TIM2, capture + PIN_MAP[30].timer_ccr); if(PIN_MAP[30].user_property != -1) { if (PIN_MAP[30].user_property > 0) { PIN_MAP[30].user_property -= 1; } else { noTone(30); } } } if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); capture = TIM_GetCapture2(TIM2); TIM_SetCompare2(TIM2, capture + PIN_MAP[31].timer_ccr); if(PIN_MAP[31].user_property != -1) { if (PIN_MAP[31].user_property > 0) { PIN_MAP[31].user_property -= 1; } else { noTone(31); } } } if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_CC3); capture = TIM_GetCapture3(TIM2); TIM_SetCompare3(TIM2, capture + PIN_MAP[33].timer_ccr); if(PIN_MAP[33].user_property != -1) { if (PIN_MAP[33].user_property > 0) { PIN_MAP[33].user_property -= 1; } else { noTone(33); } } } if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_CC4); capture = TIM_GetCapture4(TIM2); TIM_SetCompare4(TIM2, capture + PIN_MAP[32].timer_ccr); if(PIN_MAP[32].user_property != -1) { if (PIN_MAP[32].user_property > 0) { PIN_MAP[32].user_property -= 1; } else { noTone(32); } } } }
//*****TIM2中断函数2ms*****// void TIM2_IRQHandler(void) { if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)//检测TIM2溢出中断是否发生 { TIM_ClearITPendingBit(TIM2,TIM_IT_Update); time2_tick++; time2_tick = time2_tick%1000;//2HZ //LED0(ON); //START////////////////////数据读取+姿态解算+PID控制 1.4ms//////////////////////////// MPU6050_READ();//读取 acc[0] = MPU6050_ACC_LAST.X; acc[1] = MPU6050_ACC_LAST.Y; acc[2] = MPU6050_ACC_LAST.Z; gyro[0] = MPU6050_GYRO_LAST.X - GYRO_OFFSET.X; gyro[1] = -(MPU6050_GYRO_LAST.Y - GYRO_OFFSET.Y); gyro[2] = MPU6050_GYRO_LAST.Z - GYRO_OFFSET.Z; if(gyro[2]>=-5 &&gyro[2]<=5) gyro[2] = 0;//Z轴陀螺仪,漂移处理 //printf("%d,%d,%d",acc[0],acc[1],acc[2]); IMUupdate(gyro[0],gyro[1],gyro[2],acc[0],acc[1],acc[2],Vaule_2_Gyro());//非全姿态角解算 //IMUupdate1(number_to_dps1(gyro[0]),number_to_dps1(gyro[1]),number_to_dps1(gyro[2]),acc[0],acc[1],acc[2]); //IMUupdate2(number_to_dps1(gyro[0]),number_to_dps1(gyro[1]),number_to_dps1(gyro[2]),acc[0],acc[1],acc[2]); CONTROL(Q_ANGLE.ROLL,Q_ANGLE.PITCH,Q_ANGLE.YAW,Value_2_Thr(),Value_2_Roll(),Value_2_Pitch(),Vaule_2_Gyro());//对电机进行PID控制 //CONTROL1(Q_ANGLE.ROLL,Q_ANGLE.PITCH,Q_ANGLE.YAW,Value_2_Thr(),Value_2_Roll(),Value_2_Pitch(),Vaule_2_Gyro(), acc, gyro); //END/////////////////////////////////////////////////////////////////////// //LED0(OFF); if(time2_tick%5 == 0)//10ms -- 100HZ进行一次 { } if(time2_tick%25 == 0)//50ms -- 20HZ进行一次 { DMA_DATA_SEND_FLAG = 1;//上传数据 } if(time2_tick%50 == 0)//100ms -- 10HZ进行一次 { } if(time2_tick%500 == 0)//1000ms -- 1HZ进行一次 { if(ARMED == 1) { Is_DisArmed(RC_CH[3-1],RC_CH[4-1]);//是否加锁 } else { Is_Armed(RC_CH[3-1],RC_CH[4-1]);//是否解锁 } } time2_led++; if(time2_led>=1000) time2_led=time2_led-1000; // time2_led = time2_led%1000;//2ms一个周期 switch(led_state) { case 0:Led_Flash0();break; case 1:Led_Flash1();break; case 2:Led_Flash2();break; case 3:Led_Flash3();break; } } }
/**************************************************************** * 函数名称: * 功 能: 2015-4-28新编中断服务函数。 * 2016-06-08 lea比较其开始后捕获第二次中断为相位起始和结束标志 * 当A相电压大于100V,在开启了比较器、输入捕获之后,捕获到A相经过比较器之后的上升沿,则定时器开始计数。 关闭比较器,使无比较输出,即无捕获输入。 * 这时,同时需要判断再次检测到的电压要大于100V,再开启捕获功能,记录下再次捕获到的另一相的上升沿时的计数值。 * 供相序判断。 * 参 数: 无 * 返 回 值: 无 *****************************************************************/ void TIM5_IRQHandler(void) { if(phasestatus != state0)//相序测量状态下比较用于触发相序的时间间隔测量 { if((TIM5CH4_CAPTURE_STA&0x8000)==0)//还未成功捕获A相与B相orC相之间的时间 { // if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET) // { // if(TIM5CH4_CAPTURE_STA&0x4000)//已经捕获到A相的上升沿了 // { // if((TIM5CH4_CAPTURE_STA&0x3FFF)==0x3FFF)//两次捕获A相与B相或C相的时间太长了,0x3F=4.194304s(1MHz计数0xFFFF次,再累加0x3F次) // { // TIM5CH4_CAPTURE_STA|=0x8000;//标记成功捕获了一次 // TIM5CH4_CAPTURE_VAL=0xFFFF; // // COMP_Cmd(COMP_Selection_COMP1, DISABLE);//关闭比较器 // } // else TIM5CH4_CAPTURE_STA++; // } // } if(TIM_GetITStatus(TIM5, TIM_IT_CC4) != RESET)//捕获4发生捕获事件 { if(TIM5CH4_CAPTURE_STA&0x4000) //已经捕获到一次,再次捕获到一个上升沿 { /**************测试两次读取判断第二相线频率脉冲计数个数*/ //20160607lea 两相线上的起始脉冲均从第二个开始,因为刚打开比较器时若此时输入脉冲处于正半周则会立即触发输入捕捉中断 //所以从第二个脉冲开始判定。相序测量功能 相位差的测量基本正确。 if(capstats==0) { capstats=1; } else if(capstats==1) { TIM2_cap1 = TIM2->CNT; TIM5_cap1 = TIM_GetCapture4(TIM5); capstats=2; } else { TIM5CH4_CAPTURE_VAL = TIM_GetCapture4(TIM5); TIM5_cap2 = TIM5CH4_CAPTURE_VAL; TIM_Cmd(TIM5, DISABLE); TIM2_cap2 = TIM2->CNT;//TIM_GetCapture4(TIM2); TIM_Cmd(TIM2, DISABLE); TIM5CH4_CAPTURE_STA|=0x8000; //标记成功捕获到B或C相的上升沿 COMP_Cmd(COMP_Selection_COMP1, DISABLE);//关闭比较器 TIM_Cmd(TIM14, DISABLE);//0.5s定时关闭 TIM5_cap3=TIM5_cap2-TIM5_cap1; //一个周期的计数个数 用于对总计数个数取余后求相位差,确定第二相相序 CCR4_Val = TIM5_cap3; TIM2_cap3=TIM2_cap2-TIM2_cap1; capstats=0; //BUZZER_Open(0);//蜂鸣一声 } /****************/ } else //已经检测到A相电压大于100V,开始比较器和定时器后,第一次捕获上升沿 { if(capstats==0) { capstats=1; } else { TIM5CH4_CAPTURE_STA=0; //清空,此标志除了用来0x4000,0x8000做A、B/C两次的标记外,还用来定时器溢出计时 TIM5CH4_CAPTURE_VAL=0; TIM_SetCounter(TIM5,0); TIM_SetCounter(TIM2,0); TIM5CH4_CAPTURE_STA|=0x4000; //标记捕获到了A相经过比较器之后的上升沿 TIM_Cmd(TIM5, ENABLE);//开启定时器计数 TIM_Cmd(TIM2, ENABLE);//测试 在捕捉到A相上升沿后打开,输出一个脉冲对比待测的相位差 COMP_Cmd(COMP_Selection_COMP1, DISABLE);//关闭比较器,使无比较输出,即无捕获输入。等待下次检测到100V以上电压,再开启 BUZZER_Open(0);//蜂鸣一声 capstats=0; } } } } } else//正常模式下,比较器用于过零点触发交流的采样 { if(TIM_GetITStatus(TIM5, TIM_IT_CC4) != RESET) { /* Clear TIM2 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM5, TIM_IT_CC4); if(VadENA!=1) { VadENA=1; TIM_Cmd(TIM19, ENABLE); //printf("*converting!*\r\n"); } } } TIM_ClearITPendingBit(TIM5, TIM_IT_CC4|TIM_IT_Update); //清除中断标志位 }
/** * @brief Handler for TIM4 global interrupt * @details TIM4 handler */ void TIM4_IRQHandler(void) { if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_CC1); _ge_ic_chan_captures_last[IC_CHAN1] = _ge_ic_chan_captures[IC_CHAN1]; _ge_ic_chan_captures[IC_CHAN1] = TIM_GetCapture1(TIM4); // set updated available flag _ge_ic_chan_freq_avail[IC_CHAN1] = true; //calculate actual counts based on timer overflow count if (_ge_ic_chan_captures_last[IC_CHAN1] > _ge_ic_chan_captures[IC_CHAN1]) { _ge_ic_chan_period[IC_CHAN1] = (uint32_t) _ge_ic_chan_captures[IC_CHAN1] + 65536 * (_ge_ic_chan_ovf[IC_CHAN1] - 1) - _ge_ic_chan_captures_last[IC_CHAN1]; } else { _ge_ic_chan_period[IC_CHAN1] = (uint32_t) _ge_ic_chan_captures[IC_CHAN1] + 65536 * (_ge_ic_chan_ovf[IC_CHAN1]) - _ge_ic_chan_captures_last[IC_CHAN1]; } _ge_ic_chan_ovf[IC_CHAN1] = 0; //reenable chan 1 IC TIM_ICInitTypeDef ic_init_struct; TIM_ICStructInit(&ic_init_struct); ic_init_struct.TIM_Channel = TIM_Channel_1; ic_init_struct.TIM_ICPolarity = TIM_ICPolarity_Rising; ic_init_struct.TIM_ICSelection = TIM_ICSelection_DirectTI; ic_init_struct.TIM_ICPrescaler = TIM_ICPSC_DIV1; ic_init_struct.TIM_ICFilter = GE_IC_IFILTER; TIM_ICInit(TIM4, &ic_init_struct); // _ge_ic_chan_captures[IC_CHAN1] = 3000; } if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_CC2); _ge_ic_chan_captures_last[IC_CHAN2] = _ge_ic_chan_captures[IC_CHAN2]; _ge_ic_chan_captures[IC_CHAN2] = TIM_GetCapture2(TIM4); // set updated available flag _ge_ic_chan_freq_avail[IC_CHAN2] = true; //calculate actual counts based on timer overflow count if (_ge_ic_chan_captures_last[IC_CHAN2] > _ge_ic_chan_captures[IC_CHAN2]) { _ge_ic_chan_period[IC_CHAN2] = (uint32_t) _ge_ic_chan_captures[IC_CHAN2] + 65536 * (_ge_ic_chan_ovf[IC_CHAN2] - 1) - _ge_ic_chan_captures_last[IC_CHAN2]; } else { _ge_ic_chan_period[IC_CHAN2] = (uint32_t) _ge_ic_chan_captures[IC_CHAN2] + 65536 * (_ge_ic_chan_ovf[IC_CHAN2]) - _ge_ic_chan_captures_last[IC_CHAN2]; } _ge_ic_chan_ovf[IC_CHAN2] = 0; //reenable chan 2 IC TIM_ICInitTypeDef ic_init_struct; TIM_ICStructInit(&ic_init_struct); ic_init_struct.TIM_Channel = TIM_Channel_2; ic_init_struct.TIM_ICPolarity = TIM_ICPolarity_Rising; ic_init_struct.TIM_ICSelection = TIM_ICSelection_DirectTI; ic_init_struct.TIM_ICPrescaler = TIM_ICPSC_DIV1; ic_init_struct.TIM_ICFilter = GE_IC_IFILTER; TIM_ICInit(TIM4, &ic_init_struct); } if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_CC3); _ge_ic_chan_captures_last[IC_CHAN3] = _ge_ic_chan_captures[IC_CHAN3]; _ge_ic_chan_captures[IC_CHAN3] = TIM_GetCapture3(TIM4); // set updated available flag _ge_ic_chan_freq_avail[IC_CHAN3] = true; //calculate actual counts based on timer overflow count if (_ge_ic_chan_captures_last[IC_CHAN3] > _ge_ic_chan_captures[IC_CHAN3]) { _ge_ic_chan_period[IC_CHAN3] = (uint32_t) _ge_ic_chan_captures[IC_CHAN3] + 65536 * (_ge_ic_chan_ovf[IC_CHAN3] - 1) - _ge_ic_chan_captures_last[IC_CHAN3]; } else { _ge_ic_chan_period[IC_CHAN3] = (uint32_t) _ge_ic_chan_captures[IC_CHAN3] + 65536 * (_ge_ic_chan_ovf[IC_CHAN3]) - _ge_ic_chan_captures_last[IC_CHAN3]; } _ge_ic_chan_ovf[IC_CHAN3] = 0; //reenable chan 3 IC TIM_ICInitTypeDef ic_init_struct; TIM_ICStructInit(&ic_init_struct); ic_init_struct.TIM_Channel = TIM_Channel_3; ic_init_struct.TIM_ICPolarity = TIM_ICPolarity_Rising; ic_init_struct.TIM_ICSelection = TIM_ICSelection_DirectTI; ic_init_struct.TIM_ICPrescaler = TIM_ICPSC_DIV1; ic_init_struct.TIM_ICFilter = GE_IC_IFILTER; TIM_ICInit(TIM4, &ic_init_struct); } if (TIM_GetITStatus(TIM4, TIM_IT_CC4) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_CC4); _ge_ic_chan_captures_last[IC_CHAN4] = _ge_ic_chan_captures[IC_CHAN4]; _ge_ic_chan_captures[IC_CHAN4] = TIM_GetCapture4(TIM4); // set updated available flag _ge_ic_chan_freq_avail[IC_CHAN4] = true; //calculate actual counts based on timer overflow count if (_ge_ic_chan_captures_last[IC_CHAN4] > _ge_ic_chan_captures[IC_CHAN4]) { _ge_ic_chan_period[IC_CHAN4] = (uint32_t) _ge_ic_chan_captures[IC_CHAN4] + 65536 * (_ge_ic_chan_ovf[IC_CHAN4] - 1) - _ge_ic_chan_captures_last[IC_CHAN4]; } else { _ge_ic_chan_period[IC_CHAN4] = (uint32_t) _ge_ic_chan_captures[IC_CHAN4] + 65536 * (_ge_ic_chan_ovf[IC_CHAN4]) - _ge_ic_chan_captures_last[IC_CHAN4]; } _ge_ic_chan_ovf[IC_CHAN4] = 0; //reenable chan 4 IC TIM_ICInitTypeDef ic_init_struct; TIM_ICStructInit(&ic_init_struct); ic_init_struct.TIM_Channel = TIM_Channel_4; ic_init_struct.TIM_ICPolarity = TIM_ICPolarity_Rising; ic_init_struct.TIM_ICSelection = TIM_ICSelection_DirectTI; ic_init_struct.TIM_ICPrescaler = TIM_ICPSC_DIV1; ic_init_struct.TIM_ICFilter = GE_IC_IFILTER; TIM_ICInit(TIM4, &ic_init_struct); } if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM4, TIM_IT_Update); for (int i = 0; i < 4; i++) { _ge_ic_chan_ovf[i]++; } } }
void TIM2_IRQHandler(void) { uint32_t current[4]; TIM_ICInitTypeDef TIM_ICInitStructure; TIM_ICStructInit(&TIM_ICInitStructure); if (TIM_GetITStatus(TIM2, TIM_IT_CC1) == SET) { /* Clear TIM1 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); if ( inc[INC6].status == RISING ) { TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; /* Get the Input Capture value */ inc[INC6].prev_value = TIM_GetCapture1(TIM2); inc[INC6].status = FALLING; } else { TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //Get the Input Capture value current[0] = TIM_GetCapture1(TIM2); if (current[0] > inc[INC6].prev_value) inc[INC6].curr_value = current[0] - inc[INC6].prev_value ; else if (current[0] < inc[INC6].prev_value) inc[INC6].curr_value = 0xFFFF - inc[INC6].prev_value + current[0] ; inc[INC6].status = RISING; } TIM_ICInit(TIM2, &TIM_ICInitStructure); } if (TIM_GetITStatus(TIM2, TIM_IT_CC2) == SET) { /* Clear TIM1 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); if (inc[INC5].status == RISING) { TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; /* Get the Input Capture value */ inc[INC5].prev_value = TIM_GetCapture2(TIM2); inc[INC5].status = FALLING; } else { TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; /* Get the Input Capture value */ current[1] = TIM_GetCapture2(TIM2); if (current[1] > inc[INC5].prev_value) inc[INC5].curr_value = current[1] - inc[INC5].prev_value ; else if (current[1] < inc[INC5].prev_value) inc[INC5].curr_value = 0xFFFF - inc[INC5].prev_value + current[1] ; inc[INC5].status = RISING; } TIM_ICInit(TIM2, &TIM_ICInitStructure); } if (TIM_GetITStatus(TIM2, TIM_IT_CC3) == SET) { /* Clear TIM1 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM2, TIM_IT_CC3); if (inc[INC2].status == RISING) { TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; /* Get the Input Capture value */ inc[INC2].prev_value = TIM_GetCapture3(TIM2); inc[INC2].status = FALLING; } else { TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; /* Get the Input Capture value */ current[2] = TIM_GetCapture3(TIM2); if (current[2] > inc[INC2].prev_value) inc[INC2].curr_value= current[2] - inc[INC2].prev_value; else if (current[2] < inc[INC2].prev_value) inc[INC2].curr_value = 0xFFFF - inc[INC2].prev_value + current[2] ; inc[INC2].status = RISING; } TIM_ICInit(TIM2, &TIM_ICInitStructure); } if (TIM_GetITStatus(TIM2, TIM_IT_CC4) == SET) { /* Clear TIM1 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM2, TIM_IT_CC4); if (inc[INC1].status == RISING) { TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; /* Get the Input Capture value */ inc[INC1].prev_value = TIM_GetCapture4(TIM2); inc[INC1].status = FALLING; } else { TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; /* Get the Input Capture value */ current[3] = TIM_GetCapture4(TIM2); if (current[3] > inc[INC1].prev_value) inc[INC1].curr_value = current[3] - inc[INC1].prev_value; else if (current[3] < inc[INC1].prev_value) inc[INC1].curr_value = 0xFFFF - inc[INC1].prev_value+ current[3] ; inc[INC1].status = RISING; } TIM_ICInit(TIM2, &TIM_ICInitStructure); } }
extern "C" void TIM2_IRQHandler() { open_node(); if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); uint8_t pcd = RC522_PCD_1; do { // Chose rfid device. rc522_pcd_select(pcd); uint8_t status = mfrc522_read(Status1Reg); __disable_irq(); // If timer is not running and interrupt timer flag is not active reinit device. if (!(status & TRunning)) { uint8_t need_reinit = 0; switch (pcd) { case RC522_PCD_1: if (EXTI_GetITStatus(EXTI_Line10) == RESET) need_reinit = 1; break; case RC522_PCD_2: if (EXTI_GetITStatus(EXTI_Line11) == RESET) need_reinit = 1; break; } if (need_reinit) { spi_hardware_failure_signal(); mfrc522_init(); __enable_irq(); rc522_irq_prepare(); } } __enable_irq(); } while (pcd++ < RC522_PCD_2); // Check enc28j60 chip and restart if needed. if (!enc28j60_revid || (enc28j60_revid != enc28j60_rcr(EREVID)) || (GPIO_ReadInputDataBit(ETH_GPIO, ETH_IRQ_PIN) == RESET && (EXTI_GetITStatus(EXTI_Line2) == RESET))) { enc28j60_init(mac_addr); } uint16_t phstat1 = enc28j60_read_phy(PHSTAT1); // Если ethernet провод вытаскивали, обновить DHCP. // Пока отключено, т.к. по непонятным причинам LLSTAT падает иногда // хотя коннект сохраняется, что вызывает провалы в доступности интерфейса на 3-10 секунд, // пока интерфейс не поднимится по DHCP заного, однако если согласно LLSTAT линк выключен // но мы не гасим интерфейс он продолжает нормально работать. if(!(phstat1 & PHSTAT1_LLSTAT)) { static uint16_t link_dhcp_time; // Avoid frequently link checks. if (ticks - link_dhcp_time > 5000) { link_dhcp_time = ticks; // Обновим адрес через 5 секунд // (после того, как линк появится) dhcp_status = DHCP_INIT; dhcp_retry_time = RTC_GetCounter() + 2; // Линка нет - опускаем интерфейс ip_addr = 0; ip_mask = 0; ip_gateway = 0; enc28j60_init(mac_addr); } } } }