(stm32f103c8t6)关于PWM呼吸灯的问题,为什么我的灯一直亮着,没有动静啊?
时间:10-02
整理:3721RD
点击:
/*******************头文件*****************/
#ifndef _pwm_H
#define _pwm_H
#include "stm32f10x.h"
void pwm_Init(void);
#endif
/*****************************************/
#include "pwm.h"
void pwm_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init(TIM3,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_TimeBaseStructure.TIM_Period=899;
TIM_TimeBaseStructure.TIM_Prescaler=0;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_Cmd(TIM3,ENABLE);
}
/*******************主函数**********************/
#include "stm32f10x.h"
#include "pwm.h"
#include "systick.h"
int main()
{
u8 fx=1;
u32 ti=0;
pwm_Init();
while(1)
{
delay_ms(10);
if(fx==1)
{
ti++;
if(ti>300)
{
fx=0;
}
}
else
{
ti--;
if(ti==0)
{
fx=1;
}
}
TIM_SetCompare1(TIM3,ti);
}
}
#ifndef _pwm_H
#define _pwm_H
#include "stm32f10x.h"
void pwm_Init(void);
#endif
/*****************************************/
#include "pwm.h"
void pwm_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init(TIM3,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_TimeBaseStructure.TIM_Period=899;
TIM_TimeBaseStructure.TIM_Prescaler=0;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_Cmd(TIM3,ENABLE);
}
/*******************主函数**********************/
#include "stm32f10x.h"
#include "pwm.h"
#include "systick.h"
int main()
{
u8 fx=1;
u32 ti=0;
pwm_Init();
while(1)
{
delay_ms(10);
if(fx==1)
{
ti++;
if(ti>300)
{
fx=0;
}
}
else
{
ti--;
if(ti==0)
{
fx=1;
}
}
TIM_SetCompare1(TIM3,ti);
}
}
是不是频率太快,人眼发现不了,加个延时试试
刷新速率的问题吧 虽然我不懂软件 但是整天在论坛内受熏陶
谢谢楼上解答的各位了,因为我是初学者,对于芯片等都不是很了解,现在刚刚凑巧把问题解决了,具体原因是由于我的c8t6封装是48管脚的,所以,并不支持TIM3的部分和完全重印象,所以,改成PA.6管脚就好了。谢谢楼上的各位了!