TA的每日心情 | 奋斗 2024-9-22 22:20 |
---|
签到天数: 944 天 连续签到: 1 天 [LV.10]以坛为家III
|
收到小红版GD32F190后一直没有发帖分享,最近比较懒!。。。学习190的板子也还是老套路,先是建立一个模板工程,之后点灯……这些都没有什么难度,就不发帖子了。需要注意的是190的结构和STM32还是有点区别的,时钟的设置略有差别。这次想拿小红板用来控制电机,因此PWM是必不可少的。由于没有示波器,就拿LED用来验证占空比是否改变。
以下是定时器2的初始化设置部分代码:
另外,在使用小红版的过程中,频繁出现小红板与电脑断开连接的问题,一开始以为是数据线的问题,换过几根数据线后,并没有解决问题,应该是小红版的问题,不知道是否和usbhub芯片的发热有关。- void TIMER_Configuration(void)
- {
- /* -----------------------------------------------------------------------
- TIMER2 Configuration: generate 3 PWM signals with 3 different duty cycles:
- TIMER2CLK = SystemCoreClock / 72 = 1MHz
- TIMER2 Channel2 duty cycle = (4000/ 16000)* 100 = 25%
- TIMER2 Channel3 duty cycle = (8000/ 16000)* 100 = 50%
- TIMER2 Channel4 duty cycle = (12000/ 16000)* 100 = 75%
- ----------------------------------------------------------------------- */
- GPIO_InitPara GPIO_InitStructure;
- TIMER_BaseInitPara TIMER_TimeBaseStructure;
- TIMER_OCInitPara TIMER_OCInitStructure;
- NVIC_InitPara NVIC_InitStructure;
-
- RCC_AHBPeriphClock_Enable( RCC_AHBPERIPH_GPIOB, ENABLE);
- /* TIMERS clock enable */
- RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_TIMER1|RCC_APB2PERIPH_TIMER15|RCC_APB2PERIPH_TIMER16|RCC_APB2PERIPH_TIMER17,ENABLE);
- RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_TIMER2|RCC_APB1PERIPH_TIMER3|RCC_APB1PERIPH_TIMER6|RCC_APB1PERIPH_TIMER14,ENABLE);
-
- /*Configure PB3 PB10 PB11(TIMER2 CH2 CH3 CH4) as alternate function*/
- GPIO_InitStructure.GPIO_Pin = GPIO_PIN_3|GPIO_PIN_10|GPIO_PIN_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
- GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOB,GPIO_PINSOURCE3,GPIO_AF_2);
- GPIO_PinAFConfig(GPIOB,GPIO_PINSOURCE10,GPIO_AF_2);
- GPIO_PinAFConfig(GPIOB,GPIO_PINSOURCE11,GPIO_AF_2);
- /* TIMER2 configuration */
- TIMER_DeInit(TIMER2);
- TIMER_TimeBaseStructure.TIMER_Prescaler = 71;
- TIMER_TimeBaseStructure.TIMER_CounterMode = TIMER_COUNTER_UP;
- TIMER_TimeBaseStructure.TIMER_Period = 15999;
- TIMER_TimeBaseStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1;
- TIMER_BaseInit(TIMER2,&TIMER_TimeBaseStructure);
-
- TIMER_INTConfig(TIMER2,TIMER_INT_UPDATE,ENABLE);
- /* CH2,CH3 and CH4 Configuration in PWM mode */
- TIMER_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_PWM1;
- TIMER_OCInitStructure.TIMER_OCPolarity = TIMER_OC_POLARITY_HIGH;
- TIMER_OCInitStructure.TIMER_OutputState = TIMER_OUTPUT_STATE_ENABLE;
- TIMER_OCInitStructure.TIMER_Pulse = 39;
- TIMER_OC2_Init(TIMER2, &TIMER_OCInitStructure);
- TIMER_OC2_Preload(TIMER2,TIMER_OC_PRELOAD_DISABLE);
- TIMER_OCInitStructure.TIMER_Pulse = 79;
- TIMER_OC3_Init(TIMER2, &TIMER_OCInitStructure);
- TIMER_OC3_Preload(TIMER2,TIMER_OC_PRELOAD_DISABLE);
- TIMER_OCInitStructure.TIMER_Pulse = 119;
- TIMER_OC4_Init(TIMER2, &TIMER_OCInitStructure);
- TIMER_OC4_Preload(TIMER2,TIMER_OC_PRELOAD_DISABLE);
- /* Auto-reload preload enable */
- TIMER_CARLPreloadConfig(TIMER2,ENABLE);
- /* TIMER enable counter*/
- TIMER_Enable( TIMER2, ENABLE );
-
- NVIC_PRIGroup_Enable(NVIC_PRIGROUP_1);
-
- NVIC_InitStructure.NVIC_IRQ=TIMER2_IRQn;
- NVIC_InitStructure.NVIC_IRQPreemptPriority=0;
- NVIC_InitStructure.NVIC_IRQSubPriority=1;
- NVIC_InitStructure.NVIC_IRQEnable=ENABLE;
-
- NVIC_Init(&NVIC_InitStructure);
-
-
- }
复制代码 实验中使用两个用户按键对占空比进行调节,为了让先是效果比较显著,调节的步长比较大。 |
|