#include "STM32f10x_lib.h"
#include "stdio.h"
#include"delay.h"
#include"sys.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM_Configuration(void);
void PWM_Enable(void); //启动PWM
void PWM_DISABLE(void); //失能PWM
unsigned int Light_level;//调脉宽
vu16 PWM_level[5]={500,25000,35000,45000,55000};//PWM脉宽
int main(void)
{ //初始化
RCC_Configuration();
GPIO_Configuration();
TIM_Configuration();
Light_level=PWM_level[4];
PWM_DISABLE();
PWM_Enable();
while(1)
{
}
}
void RCC_Configuration(void)
{ ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus=RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_HCLKConfig(RCC_HCLK_Div1);
RCC_HCLKConfig(RCC_HCLK_Div2);
FLASH_SetLatency(FLASH_Latency_2);
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource()!=0x08);
}
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE) ;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD,ENABLE);
}
void TIM_Configuration(void)//PWM
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Period=60000;
TIM_TimeBaseStructure.TIM_Prescaler=0;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse=Light_level;//设置脉宽
TIM_OC3Init(TIM3,&TIM_OCInitStructure);
}
void PWM_Enable(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_Pulse=Light_level;//设置脉宽
TIM_OC3Init(TIM3,&TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3,ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
void PWM_DISABLE(void)
{
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Disable);
TIM_ARRPreloadConfig(TIM3,DISABLE);
TIM_Cmd(TIM3,DISABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
我想用STM32的TIM3CH3(PB0)产生PWM,但是PB0所连接的LED一直没亮,不管怎么调TIM_OCInitStructure.TIM_Pulse和TIM_TimeBaseStructure.TIM_Period,求解答!! |