STM32编程疑难杂症

电子说

1.3w人已加入

描述

最近遇到一个奇怪的问题:

如果用一个程序单独用定时器内中断每隔1s点亮LED,此时LED亮度是正常的,单独的呼吸灯程序LED亮度也正常

后来另外写了一个程序将上面定时器内部中断每隔1s点亮LED的功能跟PWM呼吸灯的程序合并在一起后呼吸灯亮度就变的很暗了,但是呼吸灯是能够正常呼吸的

以上是什么原因导致的?

ledled

程序如下:

#include "stm32f10x.h"                  // Device header
#include "Led.h" 
#include "Timer.h"
#include "PWM.h"
#include "Delay.h"
#include "SysTick.h"

uint16_t i;

int main(void)
{
// SysTick_Init(72);
Timer_Init();
LED_Init();
PWM_Init(100,720); //1000Hz
while(1)
{
 for(i=0; i<=100; i++)
 {
  PWM_SetCompare1(i);
  PWM_SetCompare2(i);
  Delay_ms(10);
 }
 
 for(i=0; i<=100; i++)
 {
  PWM_SetCompare1(100-i);
  PWM_SetCompare2(100-i);
  Delay_ms(10);
 } 
}
}

#include "stm32f10x.h"                  // Device header


void PWM_Init(uint16_t Per, uint16_t Psc)
{
//ÍâÉè²ÎÊý½á¹¹Ìå
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;

//ÍâÉèʱÖÓ¿ªÆô
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

//ÖØÓ³ÉäÒý½ÅPA6µ½PC6
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);

//GPIO½á¹¹Ìå²ÎÊýÉèÖÃ
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStruct);

//Ñ¡Ôñʱ»ùµ¥ÔªµÄʱÖÓÔ´
TIM_InternalClockConfig(TIM3);

//¶¨Ê±Æ÷ʱ»ù½á¹¹Ìå²ÎÊýÉèÖÃ
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = Per-1;  //ARR Ô¤ÖØ×°ÔØÖµ
TIM_TimeBaseInitStruct.TIM_Prescaler = Psc-1;    //PSC Ô¤·ÖƵϵÊý
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);

//Êä³ö±È½Ï½á¹¹Ìå²ÎÊýÉèÖÃ
TIM_OCStructInit(&TIM_OCInitStruct);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse = 0;       //CCR ¼ÆÊýÆ÷
TIM_OC1Init(TIM3, &TIM_OCInitStruct);
TIM_OC2Init(TIM3, &TIM_OCInitStruct);

// TIM_ARRPreloadConfig(TIM3, ENABLE);
// TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);

//ʹÄܶ¨Ê±Æ÷
TIM_Cmd(TIM3, ENABLE);
 
}


//ÉèÖÃCCR,¸Ä±äPWMÕ¼¿Õ±È
void PWM_SetCompare1(uint16_t Compare)
{
TIM_SetCompare1(TIM3, Compare);
}


void PWM_SetCompare2(uint16_t Compare)
{
TIM_SetCompare2(TIM3, Compare);
}

//ÉèÖÃÔ¤·ÖƵֵ,¸Ä±äPWMƵÂÊ
void PWM_Prescaler(uint16_t Prescaler)
{
TIM_PrescalerConfig(TIM3, Prescaler, TIM_PSCReloadMode_Immediate);
}

审核编辑 黄宇

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分