大家好,我写了基于CC2541的timer1的PWM波形输出代码,测试的时候用示波器发现了P0_3和P0_5输出波形有奇怪现象
我的相关代码在页尾贴出,真诚希望大家可以下载,测试一下,帮助小弟一起讨论,揭开这个面纱!
timer1 的channel 1 和 channel 3 是配置成PWM输出,timer1的工作模式是modulo 模式。 我设计的周期是25HZ
现象一:
当CC1 = 5000和CC3 = 5000, P0_3(channel 1) 和P0_5(channel 3) 都是20ms高电平,是正常的
当CC1 = 8000和CC3 = 5000,P0_5(channel 3)是20ms的高电平PWM,是正常的。但是P0_3(channel 1)很奇怪,刚开始输出正常的PWM波形(时间大约100s多一点),然后输出持续高电平(时间大约100s多一点),然后输出是正常PWM的波形(时间大约100s多一点),就这样周而复始……
当CC1 = 5000和CC3 = 2500,P0_3(channel 1)是20ms的高电平PWM,是正常的。但是P0_5(channel 3)同样的现象,刚开始输出正常的PWM波形(时间大约100s多一点),然后输出持续低电平(时间大约100s多一点),然后输出是正常PWM的波形(时间大约100s多一点),周而复始
现象二:
当CC1 = 0和CC3 = 0, P0_3和P0_5 会输出一个脉冲(大约10ms),周期大约300s多一点。
我真心的希望大家可以帮助我,测试一下,讨论! 在此先说声谢谢了!
- void timer1_init(void)
- {
- uint16 pertiod = 0;
-
- halIntState_t intState;
-
- HAL_ENTER_CRITICAL_SECTION(intState); //close interrupt
-
- /*IO configuration, use in compare output
- channel 1 -> P0_3, channel 3 -> P0_5 */
- P0SEL |= (BV(3)|BV(5));
- P0DIR |= (BV(3)|BV(5));
-
- /*USART0 / USART1 IO location -> Alternative 2 location*/
- PERCFG |= 0x03;
-
- /*Set the prescaler divider value
- DIV[1:0]=11 f= 32MHZ / 128=0.25MHZ */
- T1CTL |= 0x0C;
-
- /*timer1 channel 0 register configuration*/
- T1CCTL0 |= 0x04; //select compare mode
- T1CCTL0 |= 0x40; //enable interrupt request
-
- /*timer1 channel 1 register configuration, */
- T1CCTL1 |= 0x04; //select compare mode
- T1CCTL1 |= 0x30; //CMP[2:0]=110, set when equal T1CC0,clear when equal T1CC1
- T1CCTL1 &= ~0x40; //disable interrupt request
-
- /*timer1 channel 3 register configuration, */
- T1CCTL3 |= 0x04; //select compare mode
- T1CCTL3 |= 0x30; //CMP[2:0]=110, set when equal T1CC0,clear when equal T1CC3
- T1CCTL3 &= ~0x40; //disable interrupt request
- /*enable T1 interrupt*/
- IEN1 |= 0x02;
-
- /*close timer1 overflow interrupt*/
- TIMIF &= ~0x40;
-
- /*close timer1 channel 2&4 interrupt*/
- T1CCTL2 &= ~0x40;
- T1CCTL4 &= ~0x40;
-
- /*set timer1 period T1CC0
- //0x2710 = 10000; T=10000*4(us); f = 25HZ
- */
- period = 0x2710;
- T1CC0H = (uint8)((period&0xFF00)>>8);
- T1CC0L = (uint8)((period&0x00FF));
-
- /*clear timer1 counter*/
- T1CNT = 0x00;
-
- /*set timer1 mode,and start timer1*/
- T1CTL |= 0x02;
-
- HAL_EXIT_CRITICAL_SECTION(intState); //open interrupt
-
- }
- void change_pwm_value(uint8 channel, uint16 value)
- {
- switch(channel)
- {
- case 1:
- {
- T1CC1H = (uint8)((value&0xFF00)>>8);
- T1CC1L = (uint8)((value&0x00FF));
- }
- break;
- case 3:
- {
- T1CC3H = (uint8)((value&0xFF00)>>8);
- T1CC3L = (uint8)((value&0x00FF));
- }
- break;
- default:
- break;
- }
- }
- static void simpleBLEPeripheral_HandleKeys( uint8 shift, uint8 keys )
- {
- VOID shift; // Intentionally unreferenced parameter
-
- if( keys & HAL_KEY_SW_1)
- {
- change_pwm_value(1, 5000);
- change_pwm_value(3, 5000);
- }
-
- if( keys & HAL_KEY_SW_2)
- {
- change_pwm_value(1, 8000);
- change_pwm_value(3, 5000);
- }
-
- if( keys & HAL_KEY_SW_3)
- {
- change_pwm_value(1, 5000);
- change_pwm_value(3, 2500);
- }
- }
-
复制代码
明天我到公司再烧一下裸机程序进去测试一下,周五那天着实很郁闷
一周热门 更多>