瑞萨单片机william hill官网
直播中

jf_15811252

3年用户 248经验值
擅长:嵌入式技术
私信 关注
[经验]

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

功能说明

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

手册说明

image.png

image.png

image.png

image.png

工程配置

新建工程:

image.png
选择串口输出:
image.png

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

定时器配置:
image.png
栈配置:
image.png
接下来生成代码

代码

#include "hal_data.h"

FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER

/*******************************************************************************************************************//**
 * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function
 * is called by main() when no RTOS is used.
 **********************************************************************************************************************/
void hal_entry(void)
{
    /* TODO: add your own code here */
	uint32_t time=0;
	fsp_err_t err = FSP_SUCCESS;
	err = R_GPT_Open(&g_timer3_ctrl, &g_timer3_cfg);
	/* Handle any errors. This function should be defined by the user. */
	assert(FSP_SUCCESS == err);
	/* Start the timer. */
	(void) R_GPT_Start(&g_timer3_ctrl);
    err = R_GPT_PeriodSet(&g_timer3_ctrl, 5000);// frequency
    assert(FSP_SUCCESS == err);

	while (1) 
	{
		if(time>5000)
		{
			time=0;
		}
	
		err = R_GPT_DutyCycleSet(&g_timer3_ctrl, time, GPT_IO_PIN_GTIOCB);// duty cycle
		R_BSP_SoftwareDelay (100, BSP_DELAY_UNITS_MILLISECONDS);
		time+=200;
	}
}

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

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

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

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

        /* Configure pins. */
        R_IOPORT_Open(&g_ioport_ctrl, g_ioport.p_cfg);
    }
}

#if BSP_TZ_SECURE_BUILD

BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();

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

}
#endif

结果

由于手上的蜂鸣器是无源的,同时没有进行外接威廉希尔官方网站 ,芯片不能带动,现在只能用LED2进行演示。

QQ短视频20221204232236

更多回帖

发帖
×
20
完善资料,
赚取积分