MCU:CH32V003F4U6
症状:用同样的方式直接单独使用TIM2控制一路PWM输出没有问题,集成到这个程序中用串口命令控制 TIM2控制的两个channel无法输出,tim1控制的ch2无法输出,其他三个输出正常。
引脚对应:TIM1CH1--PD2 TIM1CH2--PA1 TIM1CH3--PC3 TIM1CH4--PC4 TIM2CH2--PD3 TIM2CH3--PC0
代码:
#include "debug.h"
#include
#include
/* Global define */
#define TxSize1 (size(TxBuffer1))
#define size(a) (sizeof(a) / sizeof(*(a)))
/* Global Variable */
u8 TxBuffer1[] = "CH32V003 is ready for manipulating, Use'PIN1+0' command to manipulate";
u8 RxBuffer1[6] = {0};
volatile u8 TxCnt1 = 0, RxCnt1 = 0;
uint16_t selected_pin = 0;
uint16_t ccp = 0;
/* PWM Output Mode Definition */
#define PWM_MODE1 0
#define PWM_MODE2 1
/* PWM Output Mode Selection */
//#define PWM_MODE PWM_MODE1
#define PWM_MODE PWM_MODE2
/*********************************************************************
* @fn TIM1_OutCompare_Init
*
* @brief Initializes TIM1 output compare.
*
* @param arr - the period value.
* psc - the prescaler value.
* ccp - the pulse value.
*
* @return none
*/
void TIM1_PWMOut_Init(u16 arr, u16 psc, u8* buffer)
{
GPIO_InitTypeDef GPIO_InitStructure={0};
TIM_OCInitTypeDef TIM_OCInitStructure={0};
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};
char pin = buffer[3];
if (pin=='1'||'2'||'3'||'4')
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC |RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOD | RCC_APB2Periph_TIM1 | RCC_APB2Periph_AFIO, ENABLE );
else
RCC_APB1PeriphClockCmd( RCC_APB2Periph_GPIOC |RCC_APB2Periph_GPIOD | RCC_APB1Periph_TIM2 , ENABLE );
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
switch (pin) {
case '1':
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init( GPIOD, &GPIO_InitStructure );
break;
case '2':
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init( GPIOA, &GPIO_InitStructure );
break;
case '3':
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init( GPIOC, &GPIO_InitStructure );
break;
case '4':
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Init( GPIOC, &GPIO_InitStructure );
break;
case '5':
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init( GPIOD, &GPIO_InitStructure );
break;
case '6':
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init( GPIOC, &GPIO_InitStructure );
break;
default:
return;
}
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
#if (PWM_MODE == PWM_MODE1)
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
#elif (PWM_MODE == PWM_MODE2)
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
#endif
char volt = buffer[5];
switch (volt){
case '0': ccp = 18;break;
case '1': ccp = 19;break;
default:
return;
}
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = ccp;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
switch (pin) {
case '1':
TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);
TIM_OC1Init( TIM1, &TIM_OCInitStructure );
TIM_CtrlPWMOutputs(TIM1, ENABLE );
TIM_OC1PreloadConfig( TIM1, TIM_OCPreload_Disable );
TIM_ARRPreloadConfig( TIM1, ENABLE );
TIM_Cmd( TIM1, ENABLE );
break;
case '2':
TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);
TIM_OC2Init( TIM1, &TIM_OCInitStructure );
TIM_CtrlPWMOutputs(TIM1, ENABLE );
TIM_OC2PreloadConfig( TIM1, TIM_OCPreload_Disable );
TIM_ARRPreloadConfig( TIM1, ENABLE );
TIM_Cmd( TIM1, ENABLE );
break;
case '3':
TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);
TIM_OC3Init( TIM1, &TIM_OCInitStructure );
TIM_CtrlPWMOutputs(TIM1, ENABLE );
TIM_OC3PreloadConfig( TIM1, TIM_OCPreload_Disable );
TIM_ARRPreloadConfig( TIM1, ENABLE );
TIM_Cmd( TIM1, ENABLE );
break;
case '4':
TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);
TIM_OC4Init( TIM1, &TIM_OCInitStructure );
TIM_CtrlPWMOutputs(TIM1, ENABLE );
TIM_OC4PreloadConfig( TIM1, TIM_OCPreload_Disable );
TIM_ARRPreloadConfig( TIM1, ENABLE );
TIM_Cmd( TIM1, ENABLE );
break;
case '5':
TIM_TimeBaseInit( TIM2, &TIM_TimeBaseInitStructure);
TIM_OC2Init( TIM2, &TIM_OCInitStructure );
TIM_CtrlPWMOutputs(TIM2, ENABLE );
TIM_OC2PreloadConfig( TIM2, TIM_OCPreload_Disable );
TIM_ARRPreloadConfig( TIM2, ENABLE );
TIM_Cmd( TIM2, ENABLE );
break;
case '6':
TIM_TimeBaseInit( TIM2, &TIM_TimeBaseInitStructure);
TIM_OC3Init( TIM2, &TIM_OCInitStructure );
TIM_CtrlPWMOutputs(TIM2, ENABLE );
TIM_OC3PreloadConfig( TIM2, TIM_OCPreload_Disable );
TIM_ARRPreloadConfig( TIM2, ENABLE );
TIM_Cmd( TIM2, ENABLE );
break;
default:
return;
}
}
/*********************************************************************
* @fn main
*
* @brief Main program.
*
* @return none
*/
/*********************************************************************
* @fn GPIO_Toggle_INIT
*
* @brief Initializes GPIOA.0
*
* @return none
*/
/*********************************************************************
* @fn USARTx_CFG
*
* @brief Initializes the USART2 & USART3 peripheral.
*
* @return none
*/
void USARTx_CFG(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
USART_InitTypeDef USART_InitStructure = {0};
NVIC_InitTypeDef NVIC_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_USART1, ENABLE);
/* USART1 TX-->D.5 RX-->D.6 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);;
USART_Cmd(USART1, ENABLE);
}
/*********************************************************************
* @fn main
*
* @brief Main program.
*
* @return none
*/
void main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
SystemCoreClockUpdate();
Delay_Init();
#if (SDI_PRINT == SDI_PR_OPEN)
SDI_Printf_Enable();
#else
USART_Printf_Init(115200);
#endif
printf("SystemClk:%d\r\n",SystemCoreClock);
printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
USARTx_CFG();
Delay_Ms(1000);
while(TxCnt1 < TxSize1)
{
Delay_Ms(10);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) /* waiting for sending finish */
{
}
USART_SendData(USART1, TxBuffer1[TxCnt1++]);
}
while(1)
{
}
}
void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
/*********************************************************************
* @fn USART1_IRQHandler
*
* @brief This function handles USART3 global interrupt request.
*
* @return none
*/
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
RxBuffer1[RxCnt1++] = USART_ReceiveData(USART1);
if(RxCnt1 == 6)
{
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
TIM1_PWMOut_Init(20, 48000 - 2250, RxBuffer1);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
RxCnt1 = 0;
}
}
}