初级会员
- 积分
- 81
- 金钱
- 81
- 注册时间
- 2017-4-6
- 在线时间
- 22 小时
|
楼主 |
发表于 2017-10-25 17:52:43
|
显示全部楼层
这是main.c
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "pwm.h"
int main(void)
{
SystemInit();
delay_init();
pwm_init();
pwm_led ();
delay_ms(10)
while(1)
{
TIM_SetCompare2(TIM3,13);
}
}
这是pwm.c
#include "pwm.h"
#include "sys.h"
void pwm_init(void)
{
GPIO_InitTypeDef GPIO_InitStructer;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//ê±Öóê1Äü¶Ë¿ú£¬ÇD¼Ç
GPIO_InitStructer.GPIO_Mode=GPIO_Mode_AF_PP;//GPIO_Mode_Out_PP
GPIO_InitStructer.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_6;
GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructer);
}
void pwm_led(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructer;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
//GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 199;
TIM_TimeBaseStructure.TIM_Prescaler =7199;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_OCInitStructer.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructer.TIM_OutputState = TIM_OutputState_Enable;
//TIM_OCInitStructer.TIM_Pulse=13;
TIM_OCInitStructer.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3,&TIM_OCInitStructer);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
[/mw_shl_code] |
|