查看: 569|回复: 1

【瑞萨RA4系列开发板体验】PWM驱动蜂鸣器

[复制链接]

116

主题

133

帖子

3768

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3768
发表于 2023-2-2 16:14:48 | 显示全部楼层 |阅读模式
【瑞萨RA4系列开发板体验】PWM驱动蜂鸣器
作者:jf_15811252


功能说明
PWM的全称是脉冲宽度调制(Pulse-width modulation),是通过将有效的电信号分散成离散形式从而来降低电信号所传递的平均功率的一种方式;
所以根据面积等效法则,可以通过对改变脉冲的时间宽度,来等效的获得所需要合成的相应幅值和频率的波形;

手册说明




工程配置
新建工程:

选择串口输出:

板子上没有板载蜂鸣器,所以直接使用蜂鸣器与单片机的IO进行输出,选择P404,复用关系如下所示:

引脚配置:

引脚配置:

栈配置:

接下来生成代码
代码
  1. #include "hal_data.h"

  2. FSP_CPP_HEADER
  3. void R_BSP_WarmStart(bsp_warm_start_event_t event);
  4. FSP_CPP_FOOTER

  5. /*******************************************************************************************************************//**
  6. * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function
  7. * is called by main() when no RTOS is used.
  8. **********************************************************************************************************************/
  9. void hal_entry(void)
  10. {
  11.     /* TODO: add your own code here */
  12.         uint32_t time=0;
  13.         fsp_err_t err = FSP_SUCCESS;
  14.         err = R_GPT_Open(&g_timer3_ctrl, &g_timer3_cfg);
  15.         /* Handle any errors. This function should be defined by the user. */
  16.         assert(FSP_SUCCESS == err);
  17.         /* Start the timer. */
  18.         (void) R_GPT_Start(&g_timer3_ctrl);
  19.     err = R_GPT_PeriodSet(&g_timer3_ctrl, 5000);// frequency
  20.     assert(FSP_SUCCESS == err);

  21.         while (1)
  22.         {
  23.                 if(time>5000)
  24.                 {
  25.                         time=0;
  26.                 }
  27.        
  28.                 err = R_GPT_DutyCycleSet(&g_timer3_ctrl, time, GPT_IO_PIN_GTIOCB);// duty cycle
  29.                 R_BSP_SoftwareDelay (100, BSP_DELAY_UNITS_MILLISECONDS);
  30.                 time+=200;
  31.         }
  32. }

  33. /*******************************************************************************************************************//**
  34. * This function is called at various points during the startup process.  This implementation uses the event that is
  35. * called right before main() to set up the pins.
  36. *
  37. * @param[in]  event    Where at in the start up process the code is currently at
  38. **********************************************************************************************************************/
  39. void R_BSP_WarmStart (bsp_warm_start_event_t event)
  40. {
  41.     if (BSP_WARM_START_RESET == event)
  42.     {
  43. #if BSP_FEATURE_FLASH_LP_VERSION != 0

  44.         /* Enable reading from data flash. */
  45.         R_FACI_LP->DFLCTL = 1U;

  46.         /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
  47.          * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
  48. #endif
  49.     }

  50.     if (BSP_WARM_START_POST_C == event)
  51.     {
  52.         /* C runtime environment and system clocks are setup. */

  53.         /* Configure pins. */
  54.         R_IOPORT_Open(&g_ioport_ctrl, g_ioport.p_cfg);
  55.     }
  56. }

  57. #if BSP_TZ_SECURE_BUILD

  58. BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();

  59. /* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
  60. BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
  61. {

  62. }
  63. #endif
复制代码
结果
由于手上的蜂鸣器是无源的,同时没有进行外接电路,芯片不能带动,现在只能用LED2进行演示。

本帖子中包含更多资源

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

x
回复

使用道具 举报

3

主题

195

帖子

1286

积分

金牌会员

Rank: 6Rank: 6

积分
1286
发表于 2023-3-14 11:10:56 | 显示全部楼层

不错,学习一下
回复

使用道具 举报

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

本版积分规则

用户排行榜

RA助手

主题: 116帖子:133精华:0

RA_Lance

主题: 92帖子:132精华:9

lugl

主题: 39帖子:127精华:0

xujiwei263

主题: 16帖子:73精华:0

books咦

主题: 11帖子:11精华:2

Juggernaut

主题: 9帖子:95精华:0
快速回复 返回顶部 返回列表