【Follow me第二季第3期】拓展任务-手搓一个信号发生器
[复制链接]
前言拓展任务就是制作一个信号发生器,这里我们在DAC的功能上面进行拓展,同时实现使用按键,操作输出的频率;
一:软件代码配置
如上图所示:配置本次项目中,按键检测功能,所使用到的引脚,DAC输出和存储部分的
二:软件编写流程
1:初始化按键、DAC和存储使用的IO 口
2:配置DAC的输出
3:检测是否有按键按下, 当有按键按下时,执行更改DAC输出功能
三:软件代码如下:
/* TODO: add your own code here */
fsp_err_t err = FSP_SUCCESS;
double w = 0,dac_value = 0;
double freq = 10;
uint8_t dac_freq = 10;
/* Open the DAC channel */
err = R_DAC_Open (&g_dac_ctrl, &g_dac_cfg);
if (FSP_SUCCESS != err)
{
/* DAC module open failed */
APP_PRINT("DAC open falied\r\n");
R_DAC_Close(&g_dac_ctrl);
}
getDACsetferq(flash_value);
if (flash_value[0] != 0)
{
freq = flash_value[0];
}
APP_PRINT("read DAC value is:%d\r\n",dac_freq);
/* handle error */
while (1)
{
//qspi_DAC_input_value = process_input_data();
if( Key_Scan(KEY1_SW2_PIN) == KEY_ON )
{
dac_freq += 10;
freq = dac_freq;
flash_value[0] = freq;
saveDACfreq(flash_value);
APP_PRINT("read DAC value is:%d\r\n",dac_freq);
}
if( Key_Scan(KEY2_SW1_PIN) == KEY_ON )
{
dac_freq -= 10;
if (dac_freq <= 0)
{
dac_freq = 10;
}
flash_value[0] = freq;
saveDACfreq(flash_value);
APP_PRINT("read DAC value is:%d\r\n",dac_freq);
}
w += (PI / freq);
if (w >= (2 * PI))
{
w = 0;
}
dac_value = (sin(w) + 1) / 2 * 4095;
R_DAC_Write(&g_dac_ctrl, (uint16_t)dac_value);
R_DAC_Start(&g_dac_ctrl);
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MICROSECONDS);
}
实物测试如下:
测试视频如下:
WeChat_20250112215515
|