[PIC32/SAM] ATSAMD51 EVK评估板评测4定时器点灯

[复制链接]
876|7
 楼主| 比神乐 发表于 2022-12-4 09:33 | 显示全部楼层 |阅读模式
今天捣鼓了一下定时器点灯:
代码:
  1. // *****************************************************************************
  2. // *****************************************************************************
  3. // Section: Included Files
  4. // *****************************************************************************
  5. // *****************************************************************************

  6. #include <stddef.h>                     // Defines NULL
  7. #include <stdbool.h>                    // Defines true
  8. #include <stdlib.h>                     // Defines EXIT_FAILURE
  9. #include "definitions.h"                // SYS function prototypes

  10. // DOM-IGNORE-BEGIN
  11. /*******************************************************************************
  12. * Copyright (C) 2018 Microchip Technology Inc. and its subsidiaries.
  13. *
  14. * Subject to your compliance with these terms, you may use Microchip software
  15. * and any derivatives exclusively with Microchip products. It is your
  16. * responsibility to comply with third party license terms applicable to your
  17. * use of third party software (including open source software) that may
  18. * accompany Microchip software.
  19. *
  20. * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
  21. * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
  22. * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
  23. * PARTICULAR PURPOSE.
  24. *
  25. * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
  26. * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
  27. * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
  28. * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
  29. * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
  30. * ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  31. * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  32. *******************************************************************************/
  33. // DOM-IGNORE-END

  34. // *****************************************************************************
  35. // *****************************************************************************
  36. // Section: Main Entry Point
  37. // *****************************************************************************
  38. // *****************************************************************************

  39. /* This function is called after period expires */
  40. void TC0_Callback_InterruptHandler(TC_TIMER_STATUS status, uintptr_t context)
  41. {
  42.     /* Toggle LED */
  43.     LED_Toggle();
  44. }

  45. // *****************************************************************************
  46. // *****************************************************************************
  47. // Section: Main Entry Point
  48. // *****************************************************************************
  49. // *****************************************************************************

  50. int main ( void )
  51. {
  52.     /* Initialize all modules */
  53.     SYS_Initialize ( NULL );
  54.    
  55.     /* Register callback function for TC0 period interrupt */
  56.     TC0_TimerCallbackRegister(TC0_Callback_InterruptHandler, (uintptr_t)NULL);
  57.    
  58.     /* Start the timer channel 0*/
  59.     TC0_TimerStart();

  60.     while ( true )
  61.     {
  62.         /* Maintain state machines of all polled MPLAB Harmony modules. */
  63.         SYS_Tasks ( );
  64.     }

  65.     /* Execution should not come here during normal operation */

  66.     return ( EXIT_FAILURE );
  67. }


  68. /*******************************************************************************
  69. End of File
  70. */

初始化代码:
  1. void SYS_Initialize ( void* data )
  2. {

  3.     NVMCTRL_Initialize( );

  4.   
  5.     PORT_Initialize();

  6.     CLOCK_Initialize();




  7.     EVSYS_Initialize();

  8.     TC0_TimerInitialize();





  9.     NVIC_Initialize();

  10. }

  11. /* Register callback function */
  12. void TC0_TimerCallbackRegister( TC_TIMER_CALLBACK callback, uintptr_t context )
  13. {
  14.     TC0_CallbackObject.callback = callback;

  15.     TC0_CallbackObject.context = context;
  16. }

  17. /* Enable the TC counter */
  18. void TC0_TimerStart( void )
  19. {
  20.     TC0_REGS->COUNT16.TC_CTRLA |= TC_CTRLA_ENABLE_Msk;
  21.     while((TC0_REGS->COUNT16.TC_SYNCBUSY & TC_SYNCBUSY_ENABLE_Msk) == TC_SYNCBUSY_ENABLE_Msk)
  22.     {
  23.         /* Wait for Write Synchronization */
  24.     }
  25. }
效果图:






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
tpgf 发表于 2023-1-3 10:35 | 显示全部楼层
用好了的话 可以做出视觉效果非常不错的工程
nawu 发表于 2023-1-3 11:05 | 显示全部楼层
如果闪烁的频率足够快 是不是给人的感觉就是常亮了啊
aoyi 发表于 2023-1-3 11:14 | 显示全部楼层
这么简单的小程序 如果在中断里边去点灯 是不是没有什么影响呢
zljiu 发表于 2023-1-3 11:23 | 显示全部楼层
功能简单的程序 像这种的 有没有必要进入低功耗呢
gwsan 发表于 2023-1-3 11:54 | 显示全部楼层
代码非常的简洁 我看着像是使用的中断方式是吗
 楼主| 比神乐 发表于 2023-1-3 12:01 | 显示全部楼层
gwsan 发表于 2023-1-3 11:54
代码非常的简洁 我看着像是使用的中断方式是吗

是的
tfqi 发表于 2023-1-3 12:01 | 显示全部楼层
结构非常不错 中断函数非常干净 主函数调用的非常清晰
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部