【Vision Board 创客营】Vision Board上的DAC实践

描述

1、概述

感谢官方举办【Vision Board 创客营】活动,使得我有机会试用Vision Board开发板,体验嵌入式AI的快乐。在此,再次感谢官方以及导师们的热情帮助。在本次活动中,本人选择的是DAC功能的测试。

2、环境配置

配置

​ 系统:Win11

​ 开发板:RA8-M85-vision-board

​ 开发IDE:RTThread Studio 版本: 2.2.6

环境配置
  • 下载[开发所需软件资料]
  • 根据官方提供的[文档]( [Vision Board 环境搭建 (qq.com)])搭建开发环境

3、新建工程以及编写测试例程

  1. 新建工程
    • 打开RTThread Studio IDE ,选择文件 --> 新建 --> RT-Thread 项目
    • 输入工程信息,可参考下图:

dac

  • 使能DAC驱动框架,可参考下图:

dac

dac

  • 打开RA Smart Configurator (记得选择sc_v2023-10_fsp_v5.1.0 文件夹),进行DAC硬件功能配置,具体可参考下图:

dac

dac

dac

  1. 测试程序
    • 在工程src目录中,右键新建 test_dac.c 文件,文件内容如下:
      /**
       功能说明:通过DAC生成一个正弦波,其中每一个dac的输出值,是通过py程序自动生成,可在附录中自取
      */
      /*
       * Copyright (c) 2006-2021, RT-Thread Development Team
       *
       * SPDX-License-Identifier: Apache-2.0
       *
       * Change Logs:
       * Date           Author       Notes
       * 2024-04-20     MagicKingC       the first version
       */
      #include < rtthread.h >
      #include < rtdevice.h >
      
      #define DAC_DEV_NAME    "dac0"
      #define DAC_DEV_CHANNEL 0
      
      static rt_uint32_t sin_wave_vaule[]={
              2048,2073,2098,2123,2149,2174,2199,2224,2249,2274,
              2299,2324,2349,2374,2399,2423,2448,2473,2497,2522,
              2546,2571,2595,2619,2643,2667,2691,2715,2739,2763,
              2786,2810,2833,2856,2879,2902,2925,2947,2970,2992,
              3015,3037,3059,3081,3102,3124,3145,3166,3187,3208,
              3229,3249,3269,3290,3309,3329,3349,3368,3387,3406,
              3425,3443,3462,3480,3498,3515,3533,3550,3567,3584,
              3600,3617,3633,3648,3664,3679,3694,3709,3724,3738,
              3752,3766,3780,3793,3806,3819,3831,3843,3855,3867,
              3878,3890,3900,3911,3921,3931,3941,3950,3960,3968,
              3977,3985,3993,4001,4008,4015,4022,4029,4035,4041,
              4046,4052,4057,4061,4066,4070,4074,4077,4080,4083,
              4086,4088,4090,4092,4093,4094,4095,4095,4095,4095,
              4094,4093,4092,4091,4089,4087,4084,4082,4079,4075,
              4072,4068,4064,4059,4054,4049,4044,4038,4032,4026,
              4019,4012,4005,3997,3989,3981,3973,3964,3955,3946,
              3936,3926,3916,3906,3895,3884,3873,3861,3849,3837,
              3825,3812,3799,3786,3773,3759,3745,3731,3717,3702,
              3687,3672,3656,3641,3625,3608,3592,3575,3559,3541,
              3524,3507,3489,3471,3453,3434,3416,3397,3378,3358,
              3339,3319,3300,3280,3259,3239,3218,3198,3177,3156,
              3134,3113,3091,3070,3048,3026,3004,2981,2959,2936,
              2913,2891,2868,2844,2821,2798,2774,2751,2727,2703,
              2679,2655,2631,2607,2583,2559,2534,2510,2485,2460,
              2436,2411,2386,2361,2336,2312,2287,2262,2237,2211,
              2186,2161,2136,2111,2086,2061,2035,2010,1985,1960,
              1935,1910,1885,1859,1834,1809,1784,1760,1735,1710,
              1685,1660,1636,1611,1586,1562,1537,1513,1489,1465,
              1441,1417,1393,1369,1345,1322,1298,1275,1252,1228,
              1205,1183,1160,1137,1115,1092,1070,1048,1026,1005,
              983,962,940,919,898,878,857,837,816,796,
              777,757,738,718,699,680,662,643,625,607,
              589,572,555,537,521,504,488,471,455,440,
              424,409,394,379,365,351,337,323,310,297,
              284,271,259,247,235,223,212,201,190,180,
              170,160,150,141,132,123,115,107,99,91,
              84,77,70,64,58,52,47,42,37,32,
              28,24,21,17,14,12,9,7,5,4,
              3,2,1,1,1,1,2,3,4,6,
              8,10,13,16,19,22,26,30,35,39,
              44,50,55,61,67,74,81,88,95,103,
              111,119,128,136,146,155,165,175,185,196,
              206,218,229,241,253,265,277,290,303,316,
              330,344,358,372,387,402,417,432,448,463,
              479,496,512,529,546,563,581,598,616,634,
              653,671,690,709,728,747,767,787,806,827,
              847,867,888,909,930,951,972,994,1015,1037,
              1059,1081,1104,1126,1149,1171,1194,1217,1240,1263,
              1286,1310,1333,1357,1381,1405,1429,1453,1477,1501,
              1525,1550,1574,1599,1623,1648,1673,1697,1722,1747,
              1772,1797,1822,1847,1872,1897,1922,1947,1973,1998,
              2023,2048
      };
      
      /**
       * 通过dac生成sin波形
       * @param agrc
       * @param agrv
       * @return
       */
      static int sin_dac_sample(int agrc, char **agrv){
          rt_dac_device_t dac_dev;
      
          //打开硬件外设
          dac_dev = (rt_dac_device_t)rt_device_find(DAC_DEV_NAME);
          if (dac_dev == RT_NULL) {
              rt_kprintf("can't find %s devicern",DAC_DEV_NAME);
              return -RT_ERROR;
          }
      
          rt_dac_enable(dac_dev, DAC_DEV_CHANNEL);
      
          rt_kprintf("begin echo sin wavern");
      
          for (int i = 0; i < 300; ++i)
          {
              for (int j = 0; j < sizeof(sin_wave_vaule)/sizeof(rt_uint32_t); ++j) {
                  rt_dac_write(dac_dev, DAC_DEV_CHANNEL, sin_wave_vaule[j]);
                  rt_thread_mdelay(1);
              }
          }
      
          rt_kprintf("end echo sin wavern");
      
          return 0;
      }
      
      MSH_CMD_EXPORT(sin_dac_sample,dac out sin wave sample)
      
    • 进行程序编译,然后烧录到开发板

4、测试

  1. 打开串口工具输入 ,在finsh 命令行中输入 sin_dac_sample ,然后回车,现象可看下图:

dac

dac

到此测试完毕

5、总结

到此 DAC 测试已经完毕,感觉在本次测试下来的很顺利,开发速度非常块,需要简单的几个配置,就可以将DAC使用起来。

附录

sin.py

import numpy as np
import matplotlib.pyplot as plt

t = np.linspace(0,1,512)
y = (np.sin(2*np.pi*t) * 2047) + 2048

count = 0
for num in np.round(y):
    print("%d" % num, end=',')
    count = count +1
    if count % 10 == 0:
        print()


plt.scatter(t,y)
plt.show()

审核编辑 黄宇

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分