【Follow me第二季第3期】拓展任务-手搓一个信号发生器
<p>前言拓展任务就是制作一个信号发生器,这里我们在DAC的功能上面进行拓展,同时实现使用按键,操作输出的频率;</p><p>一:软件代码配置 </p>
<div style="text-align: center;"></div>
<div>如上图所示:配置本次项目中,按键检测功能,所使用到的引脚,DAC输出和存储部分的</div>
<div>二:软件编写流程</div>
<div>1:初始化按键、DAC和存储使用的IO 口</div>
<div>2:配置DAC的输出</div>
<div>3:检测是否有按键按下, 当有按键按下时,执行更改DAC输出功能</div>
<div>三:软件代码如下:</div>
<div>
<pre>
<code class="language-cpp"> /* 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)
{
freq = flash_value;
}
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 = 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 = 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);
}</code></pre>
<p>实物测试如下:</p>
<div style="text-align: center;"></div>
<div style="text-align: center;"></div>
<div style="text-align: center;"></div>
<p> </p>
</div>
<p>测试视频如下:</p>
<p>46f7c0d40b868b87116c43f3965b4149<br />
</p>
页:
[1]