以下程序是要实现LED以一秒闪烁,但是调试后一直是亮的。。。找了很久,但还是不知错误在哪,请高手指点。。。用TIM3做PWM输出,
void main()
{
Init();
while(1);
}
/***************init**********************/
void Init()
{
RCC_Config();
GPIO_Config();
TIM_Config();
PWM_Config();
}
/***************RCC config*****************/
void RCC_Config()
{
RCC_DeInit(); //RCC reset
RCC_HSEConfig(RCC_HSE_ON); //HSE she zhi
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //config HSE
if (SUCCESS == HSEStartUpStatus) //wait for qi zheng
{
FLASH_SetLatency(FLASH_Latency_2); //set FLASH
RCC_PLLConfig(RCC_PLLSource_HSE_Div1 , RCC_PLLMul_9); //set PLL
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //set SYSCLK source
RCC_HCLKConfig(RCC_SYSCLK_Div1); //set HCLK = SYSCLK
RCC_PCLK1Config(RCC_HCLK_Div2); //set PCLK1 = HCLK/2
RCC_PCLK2Config(RCC_HCLK_Div1); //set PCLK2 = HCLK
RCC_PLLCmd(ENABLE); //shi neng PLL
}
}
/*************************time config*********************/
void TIM_Config()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);
TIM_DeInit(TIM3);
TIM_InternalClockConfig(TIM3); //设置为内部时钟
TIM_TimeBaseInitStructure.TIM_Period = 4000;
TIM_TimeBaseInitStructure.TIM_Prescaler = 0; //预分频后为2KHz
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInit(TIM2 , &TIM_TimeBaseInitStructure);
// TIM_SetCounter(TIM2 , 1000);
// TIM_CounterModeConfig(TIM3 , TIM_CounterMode_Up); //向上计数
// TIM_SetAutoreload(TIM2 , 4000); //自动重装载值
TIM_PrescalerConfig(TIM2 , (36000 -1) ,TIM_PSCReloadMode_Immediate); //PSC值为0以及方式设置为直接装载
TIM_ARRPreloadConfig(TIM2 , DISABLE); //使用预装载 在更新事件发生时装载,若不使能,则为直接装载
// TIM_Cmd(TIM3 , ENABLE); //开启定时器2
}
void PWM_Config()
{
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //输出模式
TIM_OCInitStructure.TIM_Channel = TIM_Channel_1; //输出通道选择
TIM_OCInitStructure.TIM_Pulse = 2000; //设置占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //设置输出极性
TIM_OCInit(TIM3 , &TIM_OCInitStructure);
TIM_Cmd(TIM3 , ENABLE);
}
/****************GPIO config**************/
void GPIO_Config()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO , ENABLE);
/********GPIOC Init***********************************/
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3 , ENABLE); //将TIM3设置为完全复用
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //C jiao tuiwan shuchu
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOC , &GPIO_InitStructure);
} |