test_dac.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-12-30 CDT first version
  9. */
  10. /*
  11. * 程序清单: DAC 设备使用例程
  12. * 例程导出了 dac_vol_sample 命令到控制终端
  13. * 命令调用格式:dac_vol_sample 参数1:dac1 | dac2 参数2:DAC配置值 (可选,范围0-4095),默认1365≈1.1V
  14. * 程序功能:通过 DAC 设备将数字值转换为模拟量,并输出电压值。将示波器通道连接到输出引脚,观察输出电压值
  15. * 示例代码参考电压为3.3V,转换位数为12位。
  16. */
  17. #include <rtthread.h>
  18. #include <rtdevice.h>
  19. #include <stdlib.h>
  20. #ifdef BSP_USING_DAC
  21. #define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/
  22. #define DAC_MAX_OUTPUT_VALUE 4095
  23. static int dac_vol_sample(int argc, char *argv[])
  24. {
  25. char dac_device_name[] = "dac1";
  26. rt_uint8_t channel = 1;
  27. rt_dac_device_t dac_dev;
  28. rt_uint32_t value = 1365; /* 默认1.1V */
  29. rt_uint32_t convertBits;
  30. rt_uint32_t vol;
  31. rt_err_t ret = RT_EOK;
  32. /* 参数无输入或者输入错误按照默认值处理 */
  33. if (argc >= 2)
  34. {
  35. if (0 == rt_strcmp(argv[1], "dac1"))
  36. {
  37. rt_strcpy(dac_device_name, "dac1");
  38. }
  39. #if defined (HC32F4A0) || defined (HC32F472)
  40. else if (0 == rt_strcmp(argv[1], "dac2"))
  41. {
  42. rt_strcpy(dac_device_name, "dac2");
  43. }
  44. #endif
  45. #if defined (HC32F472)
  46. else if (0 == rt_strcmp(argv[1], "dac3"))
  47. {
  48. rt_strcpy(dac_device_name, "dac3");
  49. }
  50. else if (0 == rt_strcmp(argv[1], "dac4"))
  51. {
  52. rt_strcpy(dac_device_name, "dac4");
  53. }
  54. #endif
  55. else
  56. {
  57. rt_kprintf("The chip hasn't the dac unit!\r\n");
  58. return -RT_ERROR;
  59. }
  60. }
  61. /* 查找设备 */
  62. dac_dev = (rt_dac_device_t)rt_device_find(dac_device_name);
  63. if (dac_dev == RT_NULL)
  64. {
  65. rt_kprintf("dac sample run failed! can't find %s device!\n", dac_device_name);
  66. return -RT_ERROR;
  67. }
  68. if (RT_EOK != rt_device_control(&dac_dev->parent, RT_DAC_CMD_GET_RESOLUTION, &convertBits))
  69. {
  70. rt_kprintf("dac sample can't read resolution!\n");
  71. return -RT_ERROR;
  72. }
  73. convertBits = (1 << (rt_uint8_t)convertBits);
  74. for (channel = 1; channel < 3; channel++)
  75. {
  76. /* 打开通道 */
  77. ret = rt_dac_enable(dac_dev, channel);
  78. /* 设置输出值 */
  79. if (argc >= 3)
  80. {
  81. value = atoi(argv[2]);
  82. if (value > DAC_MAX_OUTPUT_VALUE)
  83. {
  84. rt_kprintf("invalid dac value!!! \n");
  85. return -RT_ERROR;
  86. }
  87. }
  88. rt_dac_write(dac_dev, channel, value);
  89. rt_kprintf("%s CH%d Value is :%d \n", dac_device_name, channel, value);
  90. /* 转换为对应电压值 */
  91. vol = value * REFER_VOLTAGE / convertBits;
  92. rt_kprintf("%s CH%d Voltage is :%d.%02dV \n", dac_device_name, channel, vol / 100, vol % 100);
  93. }
  94. return ret;
  95. }
  96. /* 导出到 msh 命令列表中 */
  97. MSH_CMD_EXPORT(dac_vol_sample, dac voltage convert sample < dac1 | dac2 value >);
  98. #endif