goo blog サービス終了のお知らせ 

忘備録-備忘録

技術的な備忘録

時間関連の関数

2025-01-20 18:44:19 | ch32v003
ch32v003funでArduinoの時間関係の関数が使用できるようにするためのプログラムです。サンプルプログラムをほんの少し書き換えたものです。
  • millis()
  • micros()
  • delay()
  • delayMicroseconds()
が使用可能になります。

 
  1. // Example for using SysTick with IRQs
  2. // 03-25-2023 E. Brombaugh
  3. // 05-12-2023 C. Lohr (Modified to reflect updated sysclk)
  4. // 09-25-2024 ADBeta (Minor updates to main loop, comments and added
  5. // convenient macro function)

  6. #include "ch32v003fun.h"
  7. #include <stdio.h>

  8. // Number of ticks elapsed per millisecond (48,000 when using 48MHz Clock)
  9. #define SYSTICK_ONE_MILLISECOND ((uint32_t)FUNCONF_SYSTEM_CORE_CLOCK / 1000 / 8)
  10. // Number of ticks elapsed per microsecond (48 when using 48MHz Clock)
  11. #define SYSTICK_ONE_MICROSECOND ((uint32_t)FUNCONF_SYSTEM_CORE_CLOCK / 1000000 / 8)

  12. // Simple macro functions to give a arduino-like functions to call
  13. // millis() reads the incremented systick variable
  14. // micros() reads the raw SysTick Count, and divides it by the number of
  15. // ticks per microsecond ( WARN: Wraps every 90 seconds!)
  16. #define millis() (systick_millis)
  17. #define micros() (SysTick->CNT / SYSTICK_ONE_MICROSECOND)

  18. #define delay Delay_Ms
  19. #define delayMicroseconds Delay_Us

  20. // Incremented in the SysTick IRQ - in this example once per millisecond
  21. volatile uint32_t systick_millis;

  22. // Initialises the SysTick to trigger an IRQ with auto-reload, using HCLK/8 as
  23. // its clock source
  24. void systick_init(void)
  25. {
  26.     // Reset any pre-existing configuration
  27.     SysTick->CTLR = 0x0000;

  28.     // Set the compare register to trigger once per millisecond
  29.     SysTick->CMP = SYSTICK_ONE_MILLISECOND - 1;

  30.     // Reset the Count Register, and the global millis counter to 0
  31.     SysTick->CNT = 0x00000000;
  32.     systick_millis = 0x00000000;

  33.     // Set the SysTick Configuration
  34.     // NOTE: By not setting SYSTICK_CTLR_STRE, we maintain compatibility with
  35.     // busywait delay funtions used by ch32v003_fun.
  36.     SysTick->CTLR |= SYSTICK_CTLR_STE | // Enable Counter
  37.                                         // SYSTICK_CTLR_STRE <- Enable auto reload
  38.                      SYSTICK_CTLR_STIE; // Enable Interrupts
  39.                                         // SYSTICK_CTLR_STCLK <- HCLK/1; // Set Clock Source to HCLK/8

  40.     // Enable the SysTick IRQ
  41.     NVIC_EnableIRQ(SysTicK_IRQn);
  42. }

  43. // SysTick ISR - must be lightweight to prevent the CPU from bogging down.
  44. // Increments Compare Register and systick_millis when triggered (every 1ms)
  45. // NOTE: the `__attribute__((interrupt))` attribute is very important
  46. void SysTick_Handler(void) __attribute__((interrupt));
  47. void SysTick_Handler(void)
  48. {
  49.     // Increment the Compare Register for the next trigger
  50.     // If more than this number of ticks elapse before the trigger is reset,
  51.     // you may miss your next interrupt trigger
  52.     // (Make sure the IQR is lightweight and CMP value is reasonable)
  53.     SysTick->CMP += SYSTICK_ONE_MILLISECOND;

  54.     // Clear the trigger state for the next IRQ
  55.     SysTick->SR = 0x00000000;

  56.     // Increment the milliseconds count
  57.     systick_millis++;
  58. }

  59. int main(void)
  60. {
  61.     SystemInit();
  62.     delay(100);

  63.     printf("\n\nsystick_irq example\n\r");

  64.     // Initialise the IRQ
  65.     printf("initializing systick...");
  66.     systick_init();
  67.     printf("done.\n\r");

  68.     // Enable GPIOs for demonstration
  69.     funGpioInitAll();
  70.     funPinMode(PA2, GPIO_Speed_10MHz | GPIO_CNF_OUT_PP);
  71.     funPinMode(PC1, GPIO_Speed_10MHz | GPIO_CNF_OUT_PP);
  72.     funPinMode(PC2, GPIO_Speed_10MHz | GPIO_CNF_OUT_PP);

  73.     printf("Beginning Loop...\n\r");
  74.     while (1)
  75.     {
  76.         // Toggle the GPIO Pins with a delay - total delay will be 500ms
  77.         uint32_t start_millis = millis();
  78.         // On
  79.         funDigitalWrite(PA2, FUN_HIGH);
  80.         funDigitalWrite(PC1, FUN_HIGH);
  81.         funDigitalWrite(PC2, FUN_HIGH);
  82.         delay(500);
  83.         // Off
  84.         funDigitalWrite(PA2, FUN_LOW);
  85.         funDigitalWrite(PC1, FUN_LOW);
  86.         funDigitalWrite(PC2, FUN_LOW);
  87.         delay(500);
  88.         uint32_t end_millis = millis();

  89.         // NOTE: Due to the time it takes for printf(), the Current Millis will
  90.         // increment more than 500 per loop
  91.         printf("\nMilliseconds taken:\t%lu\n\r", end_millis - start_millis);
  92.         printf("Current Milliseconds:\t%lu\n\r", millis());
  93.         printf("Current Microseconds:\t%lu\n\r", micros());
  94.         printf("SysTick->CNT:\t\t%lu\n\r", SysTick->CNT);
  95.     }
  96. }


参考URL

最新の画像もっと見る

コメントを投稿