STM8S103F3P 无法使用PD4上的TIM2_CH1进行输入捕获,TIM2的CH3用于PWM输出,驱动LED,该功能正常。
OptionByte配置如图:
option.jpg (35.54 KB, 下载次数: 0)
下载附件
2014-8-10 18:08 上传
代码如下:
- #pragma vector = TIM2_OVR_UIF_vector
- __interrupt void TIM2_UIF_IRQHandler(void){
- u16LedCnt++;
- if(u16LedCnt == u16LedPeriod){
- u16Led = (uint16_t)(u16Led + s8LedStep);
- if(u16Led > TIM2_PERIOD && s8LedStep < 0){
- u16Led = 0;
- }
- TIM2_CCR3H = (uint8_t)((u16Led & 0xFF00) >> 8);
- TIM2_CCR3L = (uint8_t)(u16Led & 0x00FF);
- u16LedCnt = 0;
- }
-
- if(u16Led >= TIM2_PERIOD){
- s8LedStep = -80;
- }else if(u16Led < 1){
- s8LedStep = 80;
- }
-
- if(TIM2_CCER1_bit.CC1P){
- //计数器溢出,且没有捕获到下降沿,重新开始捕获上升沿
- TIM2_CCER1_bit.CC1P = 0;
- }
- TIM2_SR1_bit.UIF = 0;
- }
- #pragma vector = TIM2_CAPCOM_CC1IF_vector
- __interrupt void TIM2_CC_IRQHandler(void){
- if(TIM2_SR1_bit.CC1IF){
- if(TIM2_CCER1_bit.CC1P){
- //当前捕获到下降沿,完成一次PWM宽度获取,开始下次捕获上升沿
- TIM2_CCER1_bit.CC1P = 0;
- u16PwmWidth = (uint16_t)(TIM2_CCR1H << 8);
- u16PwmWidth |= TIM2_CCR1L;
- }else{
- //当前上升沿, 开始捕获下降沿
- TIM2_CCR1H = 0;
- TIM2_CCR1L = 0;
- TIM2_CCER1_bit.CC1P = 1;
- }
- TIM2_SR1_bit.CC1IF = 0;
- }
- }
- void Tim2Config(void){
- //PD4为PWM输入,配置为浮空输入模式
- PD_DDR_bit.DDR4 = 0;
- PD_CR1_bit.C14 = 0;
- PD_CR2_bit.C24 = 0;
- //PA3为LED输出 OpenDrain
- PA_DDR_bit.DDR3 = 1;
- PA_CR1_bit.C13 = 0;
- PA_CR2_bit.C23 = 1;
- PA_ODR_bit.ODR3 = 1;
- /* CH1用于输入捕获, CH3用于LED呼吸灯 */
- TIM2_PSCR = 0x01;
-
- TIM2_CR1_bit.ARPE = 1;
- TIM2_CR1_bit.URS = 0;
- TIM2_CR1_bit.UDIS = 0;
- TIM2_CR1_bit.OPM = 0;
-
-
- TIM2_CCMR1 = 0xC1; //输入模式
-
- TIM2_CCMR3_bit.OC3M = 0x06;
- TIM2_CCMR3_bit.CC3S = 0x00; //输出模式
- TIM2_CCMR3_bit.OC3PE = 0;
-
- TIM2_CCER1_bit.CC1E = 1;
- TIM2_CCER1_bit.CC1P = 0; //上升沿捕获
-
- TIM2_CCER2_bit.CC3E = 1;
- TIM2_CCER2_bit.CC3P = 1;
-
- TIM2_ARRH = (uint8_t)((TIM2_PERIOD & 0xFF00) >> 8);
- TIM2_ARRL = (uint8_t)(TIM2_PERIOD & 0x00FF);
-
- u16Led = 0x0001;
- s8LedStep = 80;
-
- TIM2_CCR1H = 0;
- TIM2_CCR1L = 0;
-
- TIM2_CCR3H = (uint8_t)((u16Led & 0xFF00) >> 8);
- TIM2_CCR3L = (uint8_t)(u16Led & 0x00FF);
-
-
- TIM2_IER_bit.CC1IE = 1;
- TIM2_IER_bit.UIE = 1;
-
- ITC_SPR4_bit.VECT15SPR = 3;
- ITC_SPR5_bit.VECT16SPR = 3;
-
-
- TIM2_CR1_bit.CEN = 1;
- TIM2_SR1_bit.CC1IF = 0;
- TIM2_EGR_bit.CC1G = 1;
- }
复制代码
在开启中断之后立即清除一次中断标志位,这样就不会有误中断进入。ST公司的就这样。
一周热门 更多>