本帖最后由 niceguy 于 2022-11-30 11:01 编辑
由于N32 MCU PA15引脚比较特殊,上电默认是作为JTAG JTDI 的,如下截图所示:
如果要将PA15用作其他用途可通过设置 AFIO_RMP_CFG. SW_JTAG_CFG[2:0]位,可以改变上述重映像配置:
SW_JTAG_ CFG[2:0] | | | | | | | | | 完全SWD_JTAG(JTAG-DP + SW-DP) (复位状态) | | | | | | | 完全SWD_JTAG(JTAG-DP + SW-DP) 但没有NJTRST | | | | | | | | | | | | | | | | | | | | | |
|
|
|
|
|
这里举个例子把PA15用作PWM输出引脚,用定时器TIM8产生PWM,程序配置如下:
int main(void)
{
/* System Clocks Configuration */
RCC_Configuration();
/* GPIO Configuration */
GPIO_Configuration();
TIM_Intial(TIM8);
while (1)
{
}
}
void RCC_Configuration(void)
{
/* TIM8 clock enable */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM8, ENABLE);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO,ENABLE);
/* GPIOA,GPIOC,GPIOD clock enable */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOC | RCC_APB2_PERIPH_GPIOD , ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitType GPIO_InitStructure;
GPIO_ConfigPinRemap(GPIO_RMP_SW_JTAG_SW_ENABLE,ENABLE);
GPIO_ConfigPinRemap(GPIO_RMP1_TIM8,ENABLE);
/* GPIOA Configuration:TIM8 Channel1, 2, 3 and 4 as alternate function push-pull */
GPIO_InitStructure.Pin = GPIO_PIN_15 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_12 ;
GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_2 ;
GPIO_InitPeripheral(GPIOD, &GPIO_InitStructure);
}
void TIM_Intial(TIM_Module* TIMx)
{
/* TIMx Configuration ---------------------------------------------------
Generate 7 PWM signals with 4 different duty cycles:
TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock
SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
and Connectivity line devices and to 24 MHz for Low-Density Value line and
Medium-Density Value line devices
The objective is to generate 7 PWM signal at 17.57 KHz:
- TIMx_Period = (SystemCoreClock / 17570) - 1
The channel 1 and channel 1N duty cycle is set to 50%
The channel 2 and channel 2N duty cycle is set to 37.5%
The channel 3 and channel 3N duty cycle is set to 25%
The channel 4 duty cycle is set to 12.5%
The Timer pulse is calculated as follows:
- ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100
----------------------------------------------------------------------- */
/* Compute the value to be set in AR regiter to generate signal frequency at 17.57 Khz */
TimerPeriod = 50; //(SystemCoreClock / 17570 ) - 1;
/* Compute CCDAT1 value to generate a duty cycle at 50% for channel 1 and 1N */
Channel1Pulse = (uint16_t)(((uint32_t)5 * (TimerPeriod - 1)) / 10);
/* Compute CCDAT2 value to generate a duty cycle at 37.5% for channel 2 and 2N */
Channel2Pulse = (uint16_t)(((uint32_t)375 * (TimerPeriod - 1)) / 1000);
/* Compute CCDAT3 value to generate a duty cycle at 25% for channel 3 and 3N */
Channel3Pulse = (uint16_t)(((uint32_t)25 * (TimerPeriod - 1)) / 100);
/* Compute CCDAT4 value to generate a duty cycle at 12.5% for channel 4 */
Channel4Pulse = (uint16_t)(((uint32_t)125 * (TimerPeriod - 1)) / 1000);
/* Time Base configuration */
TIM_TimeBaseStructure.Prescaler = 0;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_TimeBaseStructure.Period = TimerPeriod;
TIM_TimeBaseStructure.ClkDiv = 0;
TIM_TimeBaseStructure.RepetCnt = 0;
TIM_InitTimeBase(TIMx, &TIM_TimeBaseStructure);
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM2;
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
TIM_OCInitStructure.OutputNState = TIM_OUTPUT_NSTATE_ENABLE;
TIM_OCInitStructure.Pulse = Channel1Pulse;
TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_HIGH;
TIM_OCInitStructure.OcNPolarity = TIM_OCN_POLARITY_HIGH;
TIM_OCInitStructure.OcIdleState = TIM_OC_IDLE_STATE_SET;
TIM_OCInitStructure.OcNIdleState = TIM_OC_IDLE_STATE_RESET;
TIM_InitOc1(TIMx, &TIM_OCInitStructure);
TIM_OCInitStructure.Pulse = Channel3Pulse;
TIM_InitOc3(TIMx, &TIM_OCInitStructure);
TIM_OCInitStructure.Pulse = Channel4Pulse;
TIM_InitOc4(TIMx, &TIM_OCInitStructure);
// TIM_OCInitStructure.OutputNState = TIM_OUTPUT_NSTATE_DISABLE;
TIM_OCInitStructure.Pulse = Channel2Pulse;
TIM_InitOc2(TIMx, &TIM_OCInitStructure);
/* TIM8 counter enable */
TIM_Enable(TIMx, ENABLE);
/* TIM8 Main Output Enable */
TIM_EnableCtrlPwmOutputs(TIMx, ENABLE);
}
|