#include "pwm.h"
void gpio_init()
{
GPIO_InitTypeDef GPIO_TypeStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA
GPIO_TypeStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_TypeStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_TypeStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_TypeStructure);
}
void tim1_init(uint16_t arr, uint16_t psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructrue;
TIM_OCInitTypeDef TIM_OCInitTypeStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能TIM1
TIM_TimeBaseInitStructrue.TIM_Prescaler=psc;
TIM_TimeBaseInitStructrue.TIM_Period=arr;
TIM_TimeBaseInitStructrue.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructrue.TIM_ClockDivision=0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructrue);
TIM_OCInitTypeStructure.TIM_OCMode=TIM_OCMode_PWM2; //PWM模式2
TIM_OCInitTypeStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitTypeStructure.TIM_Pulse=0;
TIM_OCInitTypeStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init(TIM1,&TIM_OCInitTypeStructure); //TIM1的CH1初始化
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1,ENABLE);
TIM_Cmd(TIM1,ENABLE);
}
#include<stm32f10x.h>
#include "delay.h"
#include "pwm.h"
int main()
{
uint16_t time=1;
uint8_t mark=1;
gpio_init();
tim1_init(899,0);
while(1)
{
delay_ms(10);
if(mark)time++;
else
time--;
if(time>300)
mark=0;
if(time==0)
mark=1;
TIM_SetCompare1(TIM1,time);
}
}
灯一直亮,不知道为什么,求助
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>