初级会员

- 积分
- 57
- 金钱
- 57
- 注册时间
- 2020-5-19
- 在线时间
- 16 小时
|
2金钱
我改了下实验,想实现LED0先亮2秒,后灭4秒,依次循环;
但是代码下载后,现象为LED0先灭4秒,后亮2秒,依次循环。
tim14.c文件代码如下:
#include "sys.h"
#include "tim14.h"
void TIM14_Init(u16 rload, u16 pre)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource9, GPIO_AF_TIM14);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
GPIO_Init(GPIOF, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
TIM_TimeBaseStructure.TIM_Period = rload;
TIM_TimeBaseStructure.TIM_Prescaler = pre;
TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//配置为PWM1模式,低于crr为有效
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//有效电平为低电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC1Init(TIM14, &TIM_OCInitStructure);
TIM_ARRPreloadConfig(TIM14, ENABLE);
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);
TIM_Cmd(TIM14, ENABLE);
}
主函数:
int main(void)
{
TIM14_Init(60000-1, 8400-1);//一个周期为6s
TIM_SetCompare1(TIM14, 20000);
while(1) {
}
}
|
|