给大家分享一个刚刚写好的led呼吸灯代码,(测试过的 无误)

2019-07-21 02:14发布

#include "led_breath.h"
#include "timer.h"

uint8_t pwm_high;
uint8_t pwm_fq;
breath_LT led_breath;

void led_breath_init(void)
{
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6,ENABLE);

  SysTick_Config_Init();
  led_init();
  time2_config();
  breath_pwm_change();
}

void led_init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;   //50mhz
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_Init(GPIOB,&GPIO_InitStructure);
}

void tim2_nvic_config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

void time2_config(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

  tim2_nvic_config();

  TIM_DeInit(TIM6);

  TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInitStruct.TIM_Period = 12;         //定时周期
  TIM_TimeBaseInitStruct.TIM_Prescaler = 12;      //预分频值
  TIM_TimeBaseInit(TIM6,&TIM_TimeBaseInitStruct);

  TIM_ClearFlag(TIM6,TIM_FLAG_Update);              //清除溢出中断标志
  TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE);
  TIM_Cmd(TIM6,ENABLE);
}

void breath_pwm_change(void)
{
  if(led_breath.high_breath_counter >= 65)
  {
    if(led_breath.high_flag == 0x00)
    {
      if(led_breath.pwm_step <= 65)
      {
        led_breath.pwm_step ++;
      }
      else
      {
        led_breath.high_flag = 0xff;
      }
    }
    else if(led_breath.high_flag == 0xff)
    {
      if(led_breath.pwm_step > 0)
      {
        led_breath.pwm_step --;
      }
      else
      {
        led_breath.high_flag = 0x00;
      }
    }
    set_pwm_high(led_breath.pwm_step);
    led_breath.high_breath_counter = 0;
  }
}

void set_pwm_high(uint8_t high_tm)
{
  pwm_high = high_tm;
}

void TIM6_IRQHandler(void)
{
  if(TIM_GetFlagStatus(TIM6,TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM6,TIM_IT_Update);
    pwm_fq ++;
    if(pwm_fq < pwm_high)
    {
      GPIO_ResetBits(GPIOB,GPIO_Pin_7);
    }
    else if(pwm_fq < 100)
    {
      GPIO_SetBits(GPIOB,GPIO_Pin_7);
    }
    else
    {
      pwm_fq = 0;
      led_breath.high_breath_counter ++;
    }
  }
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
3条回答
peng1554
1楼-- · 2019-07-21 05:27
多谢分享~~
1208
2楼-- · 2019-07-21 07:27
这个是外接一个LED灯还是,有没接了其它的
1599064432
3楼-- · 2019-07-21 08:21
 精彩回答 2  元偷偷看……

一周热门 更多>