野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1150|回复: 0

【瑞萨RA MCU创意氛围赛】1. PWM驱动LED以及STLINK下载配置

[复制链接]
发表于 2023-5-26 10:29:13 | 显示全部楼层 |阅读模式
前言
板子拿到手已经一周了,看着官方文档研究了一番,发现启明R6M5的板载资源是真的丰富,各种外设俱全,考虑的很周到,而瑞萨的配套资源用起来也是相当的丝滑。介于这款板子网上资料丰富,就不写开箱贴了,直接上手试验一下使用PWM驱动LED。
1. 准备工作
如果手头有JLINK V9级以上版本,可以使用E2studio开发,e2studio是基于eclipse二次开发的,使用起来还是相当的方便。
由于本人缺少JLINK,所以采用KEIL5来开发,调试器使用的十几块钱的STLINK V2。
开发环境:
  • 电脑:Win10
  • IDE:KEIL V5.34
  • 配置软件:RASC V4.0.0
使用STLINK下载配置如下图:
2. 配置
关于如何创建一个KEIL工程,可以直接参考野火提供的参考资料(点此链接查看教程),本文不做赘述。
直接讲解如何配置GPIO口为PWM。
2.1 端口复用信息
打开启明RA6M5的原理图,查看LED线路连接:
  • LED1 --> P400
  • LED2 --> P403
  • LED3 --> P404

打开数据手册查看这三个引脚连接的GPT端口:
  • P400 --> GTIOC6A(GPT6A)
  • P403 --> GTIOC3A(GPT3A)
  • P404 --> GTIOC3B(GPT3B)

2.2 RASC配置
打开RASC配置相应的端口。
如下,需要使能GPT3和GPT6,根据复用信息配置即可。
在Pin配置页面下配置如下信息:
在stack下配置PWM:
下图以P400配置为例,配置使能GPT6A,如下:
3. 代码编辑
代码上是集成了GPIO控制的逻辑:
app_led.c
  1. /*
  2. @hehung
  3. 2023-5-22
  4. email: 1398660197@qq.com
  5. wechat: hehung95
  6. reproduced and please indicate the source @hehung
  7. */

  8. #include "hal_data.h"
  9. #include "app_led.h"

  10. #define LED_DEBUG
  11. #undef LED_DEBUG

  12. #ifdef LED_DEBUG
  13. #include <stdio.h>
  14. #endif


  15. #if (LED_CTRL_TYPE == LED_CTRL_METHOD_IO)

  16. #define LED_ON                     (BSP_IO_LEVEL_HIGH)
  17. #define LED_OFF                    (BSP_IO_LEVEL_LOW)

  18. static bsp_io_port_pin_t led_pin[LED_TOTAL_NUM] =
  19. {
  20.         BSP_IO_PORT_04_PIN_00,    /* LED1 */
  21.         BSP_IO_PORT_04_PIN_03,    /* LED2 */
  22.         BSP_IO_PORT_04_PIN_04     /* LED3 */
  23. };
  24. #endif

  25. #if (LED_CTRL_TYPE == LED_CTRL_METHOD_IO)
  26. void Led_Ctrl(uint8_t led_num, uint8_t led_level)
  27. {
  28.     (void)R_IOPORT_PinWrite(&g_ioport_ctrl, led_pin[led_num], (bsp_io_level_t)led_level);
  29. }
  30. #endif

  31. void Led_Init(void)
  32. {
  33.         #if (LED_CTRL_TYPE == LED_CTRL_METHOD_PWM)
  34.         /* Initialize Gpt6 */
  35.         (void)R_GPT_Open(&g_timer6_ctrl, &g_timer6_cfg);
  36.         /* Enable Gpt6 */
  37.         (void)R_GPT_Enable(&g_timer6_ctrl);
  38.         /* Start Gpt6 */
  39.         (void)R_GPT_Start(&g_timer6_ctrl);

  40.         /* Initialize Gpt3 */
  41.         (void)R_GPT_Open(&g_timer3_ctrl, &g_timer3_cfg);
  42.         /* Enable Gpt3 */
  43.         (void)R_GPT_Enable(&g_timer3_ctrl);
  44.         /* Start Gpt3 */
  45.         (void)R_GPT_Start(&g_timer3_ctrl);

  46.         /* setting the frequency */
  47.         R_GPT_PeriodSet(&g_timer6_ctrl, 10000);  // frequency
  48.         /* setting the frequency */
  49.         R_GPT_PeriodSet(&g_timer3_ctrl, 10000);  // frequency

  50.         #endif
  51. }

  52. void Led_Running(void)
  53. {
  54. #if (LED_CTRL_TYPE == LED_CTRL_METHOD_IO)
  55.         static uint8_t i = 0;
  56.         static uint8_t flag = 0;

  57.         Led_Ctrl(i, flag);

  58.         i = (i < LED_TOTAL_NUM) ? (i+1) : (0);
  59.         if (i == 0)
  60.         {
  61.                 flag ^= 1;
  62.         }
  63. #elif (LED_CTRL_TYPE == LED_CTRL_METHOD_PWM)
  64.         fsp_err_t err = FSP_SUCCESS;
  65.         static uint16_t duty_cycle = 0;

  66.         duty_cycle = (duty_cycle < 10000) ? (duty_cycle + 100) : (0);

  67.         err = R_GPT_DutyCycleSet(&g_timer6_ctrl, duty_cycle, GPT_IO_PIN_GTIOCA);// duty cycle
  68.     assert(FSP_SUCCESS == err);

  69.         err = R_GPT_DutyCycleSet(&g_timer3_ctrl, duty_cycle, GPT_IO_PIN_GTIOCA);// duty cycle
  70.     assert(FSP_SUCCESS == err);

  71.         err = R_GPT_DutyCycleSet(&g_timer3_ctrl, duty_cycle, GPT_IO_PIN_GTIOCB);// duty cycle
  72.     assert(FSP_SUCCESS == err);
  73. #endif
  74. }
复制代码

app_led.h
  1. /*
  2. @hehung
  3. 2023-5-22
  4. email: 1398660197@qq.com
  5. wechat: hehung95
  6. reproduced and please indicate the source @hehung
  7. */

  8. #ifndef APP_LED_H_
  9. #define APP_LED_H_

  10. #include "hal_data.h"


  11. #define LED_CTRL_METHOD_PWM        (1U)
  12. #define LED_CTRL_METHOD_IO         (0U)
  13. #define LED_CTRL_TYPE              (LED_CTRL_METHOD_PWM)


  14. // Total number of leds
  15. #define LED_TOTAL_NUM              (3U)
  16. #define LED_3                      (2U)
  17. #define LED_2                      (1U)
  18. #define LED_1                      (0U)


  19. extern void Led_Init(void);
  20. extern void Led_Ctrl(uint8_t led_num, uint8_t led_level);
  21. extern void Led_Running(void);

  22. #endif /* APP_LED_H_ */
复制代码

主函数逻辑如下:
  1. Led_Init();

  2.         while(1)
  3.         {
  4.                 Led_Running();
  5.         }
复制代码

3. 试验效果
野火论坛202305261029035213..gif


回复

使用道具 举报

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

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-20 07:29 , Processed in 0.027892 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表