首页 > 其他分享 >STM32使用定时器在普通gpio上模拟pwm-红牛开发板LED1的亮度调节

STM32使用定时器在普通gpio上模拟pwm-红牛开发板LED1的亮度调节

时间:2024-06-02 20:44:34浏览次数:25  
标签:LED1 LED defined ISR Timer 开发板 STM32 define

stm32F103zet只有固定的几个针脚可以输出tim定时器信号,在不支持tim输出的口上就没法输出pwm,在红牛开发版上的表现就是控制lcd屏幕亮度的a1针脚,可以输出pwm,屏幕亮度可以无极调节,但是4个led灯就只能控制开关。使用arduino的analogWrite函数,只能调节开关。

可以用定时器的中断来控制普通io口的开关,达到模拟pwm调制的结果。参考这文章  STM32普通io口模拟pwm输出的三种方法_普通io口模拟输出pwm-CSDN博客

这里使用第一个办法。使用的是arduino的库  STM32TimerInterrupt,避免了对tim结构体的复杂操作。使用的是红牛开发板

/****************************************************************************************************************************
  TimerInterruptLEDDemo.ino
  For STM32 boards
  Written by Khoi Hoang
  
  Built by Khoi Hoang https://github.com/khoih-prog/STM32_TimerInterrupt
  Licensed under MIT license
  
  Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
  unsigned long miliseconds), you just consume only one STM32 timer and avoid conflicting with other cores' tasks.
  The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
  Therefore, their executions are not blocked by bad-behaving functions / tasks.
  This important feature is absolutely necessary for mission-critical tasks.
*****************************************************************************************************************************/

/*
   Notes:
   Special design is necessary to share data between interrupt code and the rest of your program.
   Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
   variable can not spontaneously change. Because your function may change variables while your program is using them,
   the compiler needs this hint. But volatile alone is often not enough.
   When accessing shared variables, usually interrupts must be disabled. Even with volatile,
   if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
   If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
   or the entire sequence of your code which accesses the data.
*/

#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3)  ||defined(STM32F4) || defined(STM32F7) || \
       defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7)  ||defined(STM32G0) || defined(STM32G4) || \
       defined(STM32WB) || defined(STM32MP1) || defined(STM32L5) )
  #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting.
#endif

// These define's must be placed at the beginning before #include "STM32TimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
// Don't define TIMER_INTERRUPT_DEBUG > 2. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG         1
#define _TIMERINTERRUPT_LOGLEVEL_     4

#include "STM32TimerInterrupt.h"

// #ifndef LED_BUILTIN
  #define LED_BUILTIN       PF6               // Pin 33/PB0 control on-board LED_GREEN on F767ZI
// #endif

#ifndef LED_BLUE
  #define LED_BLUE          PF7               // Pin 73/PB7 control on-board LED_BLUE on F767ZI
#endif

#ifndef LED_RED
  #define LED_RED           PF8             // Pin 74/PB14 control on-board LED_BLUE on F767ZI
#endif
   
#include "STM32TimerInterrupt.h"
#include "STM32_ISR_Timer.h"

#define TIMER_INTERVAL_MS         100
#define HW_TIMER_INTERVAL_MS      50

// Depending on the board, you can select STM32 Hardware Timer from TIM1-TIM22
// For example, F767ZI can select Timer from TIM1-TIM14
// If you select a Timer not correctly, you'll get a message from ci[ompiler
// 'TIMxx' was not declared in this scope; did you mean 'TIMyy'? 

// Init STM32 timer TIM1
STM32Timer ITimer(TIM1);

// Init STM32_ISR_Timer
// Each STM32_ISR_Timer can service 16 different ISR-based timers
STM32_ISR_Timer ISR_Timer;

#define TIMER_INTERVAL_0_5S           500L
#define TIMER_INTERVAL_1S             1000L
#define TIMER_INTERVAL_1_5S           1500L

void TimerHandler()
{
  ISR_Timer.run();
}

// In STM32, avoid doing something fancy in ISR, for example complex Serial.print with String() argument
// The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment
// Or you can get this run-time error / crash
unsigned int count=0;
unsigned int  i = 1;//i就是占空比
void doingSomething1()
{
  count++;//计中断次数
  if(count%100<i)//i:占空比值,
        digitalWrite(LED_BUILTIN, 1);//高电平
        
        else
        digitalWrite(LED_BUILTIN, 0);//高电平;
  //digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  
}

void doingSomething2()
{
  digitalWrite(LED_BLUE, !digitalRead(LED_BLUE));
}
void doingSomething3()
{
  digitalWrite(LED_RED, !digitalRead(LED_RED));
}

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  delay(100);

  Serial.print(F("\nStarting TimerInterruptLEDDemo on ")); Serial.println(BOARD_NAME);
  Serial.println(STM32_TIMER_INTERRUPT_VERSION);
  Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));

  // Instantiate HardwareTimer object. Thanks to 'new' instanciation, HardwareTimer is not destructed when setup() function is finished.
  //HardwareTimer *MyTim = new HardwareTimer(Instance);

  // configure pin in output mode
  pinMode(LED_BUILTIN,  OUTPUT);
  pinMode(LED_BLUE,     OUTPUT);
  pinMode(LED_RED,      OUTPUT);

  // Interval in microsecs
  if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS*1000 , TimerHandler))//过小的HW_TIMER_INTERVAL_MS会导致loop无法运行,这里测试过不需要调节
  {
    Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis());
  }
  else
    Serial.println(F("Can't set ITimer. Select another freq. or timer"));

  // Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
  // You can use up to 16 timer for each ISR_Timer
  //ISR_Timer.setInterval(1000,  doingSomething1);
  ITimer.setFrequency(100000,  doingSomething1);//实测200k频率可以运行,300k不行,使用这个函数可以突破1ms的间隔限制
  ISR_Timer.setInterval(TIMER_INTERVAL_1S,    doingSomething2);
  ISR_Timer.setInterval(TIMER_INTERVAL_1_5S,  doingSomething3);
  
}


void loop()
{
  /* Nothing to do all is done by hardware. Even no interrupt required. */

  i++;
  //Serial.println(count);
  Serial.println(i);
  delay(40);
  if(i == 100)
  {
    
    i = 0;
    
  }//循环调节占空比,用来演示灯亮度变化


  
  
}

 

标签:LED1,LED,defined,ISR,Timer,开发板,STM32,define
From: https://www.cnblogs.com/kyo413/p/18227574

相关文章

  • 基于DAYU800开发板的OpenHarmony设备发环境搭建
    简介润和-SCDAYU800开发平台基于平头哥高性能RISC-V开源架构曳影TH1520芯片,集成4核高性能RISC-V处理器玄铁C910的平头哥曳影1520,AI算力达4TOPs支持蓝牙、音频、视频和摄像头等功能,支持多种视频输入输出接口,并提供丰富的扩展接口,可用于工控平板、智慧大屏、智......
  • 【STM32基础学习】--GPIO原理
    一.GPIO入门知识全称: generalpurposeintputoutpot(通用输入输出端口)。可以做输入也可以做输出。STM32芯片的GPIO引脚与外部设备连接起来,从而实现与外部通讯,控制以及数据采集的功能。二.GPIO的八种工作模式1.4种输入模式(可以读取端口的高低电平或者电压,用于读取按键......
  • stm32 f4 SRAM
        备用SRAM是很好的临时数据保存单元,一些需要掉电后保存的数据建议使用备用SRAM保存,只有需要永久保存的数据再用FLASH进行保存。SRAM地址范围电源与复位相关的库文件(备用域的电池管理)#ifndefBKPSRAM_BKPSRAM_H_#defineBKPSRAM_BKPSRAM_H_#include"stm32......
  • 基于stm32的智能家居系统
    目录1.课题研究目的和内容1.1课题研究目的1.2课题研究内容2.系统总体方案设计及功能模块介绍2.1总体方案设计2.2 DHT11模块介绍2.3  TFTLCD显示功能模块介绍2.4 ESP8266WIFI模块介绍2.5 MQ-135空气质量模块介绍2.6 步进电机模块介绍2.7 ......
  • 基于标准库的STM32的外部中断EXTI
            毕设已经告一段落了,接下来准备开始整理一下毕设中用到的知识与技术细节,今天整理的是STM32从编码器获取数据的方式-----外部中断(EXTI):外部中断分为四个硬件相关外设,GPIO/AFIO/EXTI/NVIC(EXTI/NVIC不需要开启时钟)1.RCC开启时钟RCC_APB2PeriphClockCmd(RCC_APB2P......
  • 基于FREERTOS的STM32多功能手表(软件设计)
    目录前言程序现象 项目背景项目介绍目前版本实现的功能设计到的freertos知识使用到的硬件硬件连线图实现思路任务调度流程图​编辑 任务具体操作导图      代码讲解freertos初始化按键中断回调函数显示时间任务显示菜单任务其它任务(ShowCalenda......
  • 基于MBD的电机控制算法开发-STM32
    使用simulink搭建V/F电机控制框架,并集成到STM32F4中1.Simulink模型搭建本例子使用V/F拖动启动方法控制永磁同步电机启动,simulink模型其中V/F启动部分输出d,q轴的期望电压,并通过SVPWM调制算法施加到电机的三相:模型的输入为空,模型输出为为三路PWM波的占空比。function[ud_out,u......
  • STM32学习笔记(二)流水灯
    STM32学习笔记(二)流水灯一、原理部分1.1LED原理1.2GPIO原理二、工程部分三、加入宏定义这次我们来实现LED流水灯成为点灯大师。使用的核心板的MCU型号为STM32F103ZET6,使用标准库函数来实现。一、原理部分1.1LED原理其中PWR是系统电源指示灯,为蓝色。LED0......
  • 杂项——STM32ZET6要注意的一些问题——高级定时器问题和PB3,PB4引脚问题
    ZET6可能会用到定时器,高级定时器要输出PWM要加上这样一行代码,否则无法正常输出PWM波TIM_CtrlPWMOutputs(TIM8,ENABLE); //主输出使能,当使用的是通用定时器时,这句不需要ZET6中PB3,PB4引脚默认功能是JTDO和NJTRST,如果想将其当作正常IO口使用需要加上两行代码 RCC_APB2Pe......
  • Linux低功耗Suspend/Resume梳理(基于STM32MP1)
    基于STM32MP1简单梳理Linuxsuspend/resume涉及到的内容:触发Suspend流程,以及唤醒手段和后续resume流程。Linuxkernel中Suspend/Resume流程。TFA中冷启动、热启动、SMC处理、PSCI实现等等。其他低功耗相关:poweroff、reboot、fiq处理。PowerDomainTree介绍;PSCI移植指导等。......