学习190的板子也还是老套路,先是建立一个模板工程,之后点灯……这些都没有什么难度,就不发帖子了。需要注意的是190的结构和STM32还是有点区别的,时钟的设置略有差别。这次想拿小红板用来控制电机,因此PWM是必不可少的。由于没有示波器,就拿LED用来验证占空比是否改变。
以下是定时器2的初始化设置部分代码:
另外,在使用小红版的过程中,频繁出现小红板与电脑断开连接的问题,一开始以为是数据线的问题,换过几根数据线后,并没有解决问题,应该是小红版的问题,不知道是否和u***hub芯片的发热有关。- 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);
- }
[color=rgb(51, 102, 153) !important]复制代码
0
|
|
|
|
实验中使用两个用户按键对占空比进行调节,为了让先是效果比较显著,调节的步长比较大。- void EXIT_Init(void)
- {
- GPIO_InitPara GPIO_InitStructure;
- EXTI_InitPara EXTI_InitStructure;
- NVIC_InitPara NVIC_InitStructure;
- RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_GPIOB,ENABLE);
- GPIO_InitStructure.GPIO_Pin=GPIO_PIN_6;
- GPIO_InitStructure.GPIO_Mode=GPIO_MODE_IN;
- GPIO_InitStructure.GPIO_PuPd=GPIO_PUPD_NOPULL;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin=GPIO_PIN_6;
- GPIO_InitStructure.GPIO_Mode=GPIO_MODE_IN;
- GPIO_InitStructure.GPIO_PuPd=GPIO_PUPD_NOPULL;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin=GPIO_PIN_7;
- GPIO_InitStructure.GPIO_Mode=GPIO_MODE_IN;
- GPIO_InitStructure.GPIO_PuPd=GPIO_PUPD_NOPULL;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_CFG,ENABLE);
- SYSCFG_EXTILine_Config(EXTI_SOURCE_GPIOB,EXTI_SOURCE_PIN6);
- SYSCFG_EXTILine_Config(EXTI_SOURCE_GPIOB,EXTI_SOURCE_PIN7);
- EXTI_InitStructure.EXTI_LINE=EXTI_LINE6;
- EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_LINEEnable=ENABLE;
- EXTI_Init(&EXTI_InitStructure);
- EXTI_ClearIntBitState(EXTI_LINE6);
- EXTI_InitStructure.EXTI_LINE=EXTI_LINE7;
- EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_LINEEnable=ENABLE;
- EXTI_Init(&EXTI_InitStructure);
- EXTI_ClearIntBitState(EXTI_LINE7);
- NVIC_PRIGroup_Enable(NVIC_PRIGROUP_1);
- NVIC_InitStructure.NVIC_IRQ=EXTI4_15_IRQn;
- NVIC_InitStructure.NVIC_IRQPreemptPriority=0;
- NVIC_InitStructure.NVIC_IRQSubPriority=0;
- NVIC_InitStructure.NVIC_IRQEnable=ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- int KEY_SCAN(void)
- {
- int count=0xfff;
- if(EXTI_GetIntBitState(EXTI_LINE6)!=RESET)
- {
- while(count--)
- ;
- if(EXTI_GetIntBitState(EXTI_LINE6)!=RESET)
- {
- EXTI_ClearIntBitState(EXTI_LINE6);
- return 1;
- }
- }
- if(EXTI_GetIntBitState(EXTI_LINE7)!=RESET)
- {
- while(count--)
- ;
- if(EXTI_GetIntBitState(EXTI_LINE7)!=RESET)
- {
- EXTI_ClearIntBitState(EXTI_LINE7);
- return 2;
- }
- }
- return 0;
- }
- void EXTI4_15_IRQHandler(void)
- {
- unsigned int temp=15999;
- int key;
- key=KEY_SCAN();
- if(key==1)
- {
- t+=500;
- if(t>=16000)
- t=15999;
- TIMER_Compare3Config( TIMER2, t);
- }
- if(key==2)
- {
- t-=500;
- if(t<=500)
- t=500;
- TIMER_Compare3Config( TIMER2, t);
- }
- }
[color=rgb(51, 102, 153) !important]复制代码
|
|
|
|
|