新手入门
- 积分
- 3
- 金钱
- 3
- 注册时间
- 2018-9-22
- 在线时间
- 0 小时
|
1金钱
[mw_shl_code=applescript,true]#include "stm32f10x.h"
void LED_Init(void);
void PWM_Init(void);
void LED_Init(void) //初始化 PA8
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void PWM_Init(void)
{
//时基单元初始化
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period=2000-1;
TIM_TimeBaseInitStructure.TIM_Prescaler=36000;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);
//输出比较初始化
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_Pulse=800;
TIM_OC1Init(TIM3,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);//启用或禁用CCR1上的Timx外围预加载寄存器
TIM_ARRPreloadConfig(TIM3,ENABLE);//启用或禁用ARM上的Timx外围预加载寄存器。
TIM_CtrlPWMOutputs(TIM3,ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
int main()
{
while(1)
{
LED_Init();
PWM_Init();
}
}
[/mw_shl_code]
|
|