初级会员

- 积分
- 59
- 金钱
- 59
- 注册时间
- 2018-4-1
- 在线时间
- 9 小时
|
#include "pwm.h"
#include "stm32f10x.h"
#include "sys.h"
#include "led.h"
void TIM3_PWM_Init(u16 psc,u16 arr)
{
GPIO_InitTypeDef GPIO_InitStructure; //¶¨òåGPIO½á11ìå
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //¶¨òåTIM3õê¼»ˉ½á11ìå
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
//TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
//TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
//TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
#include "stm32f10x.h"
#include "sys.h"
#include "led.h"
#include "delay.h"
#include "pwm.h"
void main(void)
{
u8 dir=1;
u16 ledpwmval=0;
delay_init();
LED_Init();
TIM3_PWM_Init(0,899);
while(1)
{
delay_ms(10);
if(dir==1)
{ledpwmval++;
}
else
{
ledpwmval--;
}
if(ledpwmval>300) {
dir=0;
}
if(ledpwmval==0) dir=1;
TIM_SetCompare1(TIM3,ledpwmval);
TIM_SetCompare2(TIM3,ledpwmval);
}
}
我是想用tim3的ch1和ch2输出两路pwm控制两个LED的亮度的,但是一个灯都点不亮。。。求大佬指点
|
|