#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);
}
}
灯一直亮,不知道为什么,求助
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
---------------------------------
我的这个最暗时是灭的.....TIM_SetCompare1(TIM1,0);是灭的。TIM_SetCompare1(TIM1,300);是亮的。但是就是实现不了呼吸灯,
将time定义成0试试
---------------------------------
只是灭
---------------------------------
你不说time 为300是亮的么?怎么又是灭? 你的led电路怎么接的,一端接IO,另一端接限流电阻到地还是Vcc?
一周热门 更多>