查看: 229|回复: 2

需要一个支持stm32的定时器库

[复制链接]

9

主题

16

帖子

65

积分

注册会员

Rank: 2

积分
65
发表于 2021-9-17 17:55:23 | 显示全部楼层 |阅读模式
需要一个支持stm32的定时器库
回复

使用道具 举报

27

主题

70

帖子

981

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
981
发表于 2021-9-18 11:49:15 | 显示全部楼层
自行在库安装界面搜索下载这个库TimerInterrupt_Generic
库很多,需要其他的根据关键字搜下,基本上都有

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

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

  13. #ifndef LED_BUILTIN
  14.   #define LED_BUILTIN       PB0               // Pin 33/PB0 control on-board LED_GREEN on F767ZI
  15. #endif

  16. #ifndef LED_BLUE
  17.   #define LED_BLUE          PB7               // Pin 73/PB7 control on-board LED_BLUE on F767ZI
  18. #endif

  19. #ifndef LED_RED
  20.   #define LED_RED           PB14              // Pin 74/PB14 control on-board LED_BLUE on F767ZI
  21. #endif
  22.    
  23. #include "TimerInterrupt_Generic.h"
  24. #include "ISR_Timer_Generic.h"

  25. #define TIMER_INTERVAL_MS         100
  26. #define HW_TIMER_INTERVAL_MS      50

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

  31. // Init STM32 timer TIM1
  32. STM32Timer ITimer(TIM1);

  33. // Init STM32_ISR_Timer
  34. // Each STM32_ISR_Timer can service 16 different ISR-based timers
  35. ISR_Timer STM32_ISR_Timer;

  36. #define TIMER_INTERVAL_0_5S           500L
  37. #define TIMER_INTERVAL_1S             1000L
  38. #define TIMER_INTERVAL_1_5S           1500L

  39. void TimerHandler()
  40. {
  41.   STM32_ISR_Timer.run();
  42. }

  43. // In STM32, avoid doing something fancy in ISR, for example complex Serial.print with String() argument
  44. // The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment
  45. // Or you can get this run-time error / crash
  46. void doingSomething1()
  47. {
  48.   digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  49. }

  50. void doingSomething2()
  51. {
  52.   digitalWrite(LED_BLUE, !digitalRead(LED_BLUE));
  53. }
  54. void doingSomething3()
  55. {
  56.   digitalWrite(LED_RED, !digitalRead(LED_RED));
  57. }

  58. void setup()
  59. {
  60.   Serial.begin(115200);
  61.   while (!Serial);

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

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

  68.   // configure pin in output mode
  69.   pinMode(LED_BUILTIN,  OUTPUT);
  70.   pinMode(LED_BLUE,     OUTPUT);
  71.   pinMode(LED_RED,      OUTPUT);

  72.   // Interval in microsecs
  73.   if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS * 1000, TimerHandler))
  74.   {
  75.     Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis());
  76.   }
  77.   else
  78.     Serial.println(F("Can't set ITimer. Select another freq. or timer"));

  79.   // Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
  80.   // You can use up to 16 timer for each STM32_ISR_Timer
  81.   STM32_ISR_Timer.setInterval(TIMER_INTERVAL_0_5S,  doingSomething1);
  82.   STM32_ISR_Timer.setInterval(TIMER_INTERVAL_1S,    doingSomething2);
  83.   STM32_ISR_Timer.setInterval(TIMER_INTERVAL_1_5S,  doingSomething3);
  84. }


  85. void loop()
  86. {
  87.   /* Nothing to do all is done by hardware. Even no interrupt required. */
  88. }
复制代码
回复

使用道具 举报

9

主题

16

帖子

65

积分

注册会员

Rank: 2

积分
65
 楼主| 发表于 2021-9-23 18:20:13 | 显示全部楼层
文涛 发表于 2021-9-18 11:49
自行在库安装界面搜索下载这个库TimerInterrupt_Generic
库很多,需要其他的根据关键字搜下,基本上都有
...

感谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

热门推荐

连接ADS1292模拟前端芯片,使用SPI库,导致串口不输出数据
连接ADS1292模拟前端芯片
[md]连接ADS1292模拟前端芯片,使用SPI库,导致串口不输出数据 ![QSJSBEZF83I8C907%$
宏定义为什么会报错
宏定义为什么会报错
[md]!(data/attachment/forum/202109/25/173426ma339t23hx9uqq2t.png?imageMogr2/auto
怎样找到这个库Adafruit_I2CDevice
怎样找到这个库Adafruit_I
[md].piolibdepsnucleo_g474reAdafruit GFX LibraryAdafruit_GrayOLED.h:30:10: f
Stduino Nano&Uno模拟量值读取之电位器(12位精确度)
Stduino Nano&Uno模拟量值
通过Stduino IDE编辑几行代码便实现电位器模拟量读取,并通过串口每隔1秒输出一次,并
今天在文涛老大的帮助下我的stm32zet6终于把simlpefoc库跑起...
今天在文涛老大的帮助下我
[md]首先本身是刚刚从MDK转到arduino过来的新人,只因为要完开源的simplefoc,期间由
Stduino UNO mini与7线oled显示屏实现Message Box
Stduino UNO mini与7线ole
[md]手头上有块7线的Oled显示屏一直想用stm32驱动下,正好现在通过Stduino UNO mini和
DS1302时钟库分享
DS1302时钟库分享
大家好,最近发现IDE自身的库没有DS1302,因此依据网上的资料写了DS1302的库。这里将
©2001-2018  Stduino官网  Powered by©Discuz!   ( 皖ICP备17011998号 )
快速回复 返回顶部 返回列表