查看: 1396|回复: 3
收起左侧

RVB2601开发板ADC读取实验

[复制链接]

  离线 

  • TA的每日心情
    拍拍
    2022-6-27 11:09
  • 签到天数: 25 天

    [LV.4]

    发表于 2021-5-5 10:32:22 | 显示全部楼层 |阅读模式

    有人预言,RISC-V或将是继Intel和Arm之后的第三大主流处理器体系。欢迎访问全球首家只专注于RISC-V单片机行业应用的中文网站

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    本帖最后由 sky 于 2021-5-5 10:32 编辑

    1. 前言

    利用RVB2601板实现模拟量读取并串口打印,输出电压值单位:mV。

    2. 硬件配置

    ADC_VREFP与3.3V连接,ADC_VREFN与GND连接,将原开发板CH2601_PA4端子帽拔掉,断开与LED_BULE连接,模拟量(可调电阻分压值)与CH2601_PA4端子连接。

    备注:旁边引脚开发板丝印PA6,实际PA25,同时“RVB2601应用开发实战系列二: 跑马灯”一文原理图引用也错误。

    3. 剑池CDK下载ch2601_gui_demo例程
    3.1、新建工程:

    打开CDK,点击HOME图标,点击右上角新建工程。

    CH2601 单片机芯片及应用-RVB2601开发板ADC读取实验risc-v单片机中文社区(1)

    3.2、搜索工程

    搜索ch2601_gui_demo,然后点击创建工程,如下图所示:

    CH2601 单片机芯片及应用-RVB2601开发板ADC读取实验risc-v单片机中文社区(2)

    3.3、输入工程名:
    输入“ch2601_adc”,然后点击下载方案:

    CH2601 单片机芯片及应用-RVB2601开发板ADC读取实验risc-v单片机中文社区(3)

    3.4、激活工程:
    由于我下载了多个工程,需要在ch2601_adc上右击,选择“Set As Active”,激活。

    CH2601 单片机芯片及应用-RVB2601开发板ADC读取实验risc-v单片机中文社区(4)

    4、修改工程
    4.1、创建adc.c文件到工程:

    CH2601 单片机芯片及应用-RVB2601开发板ADC读取实验risc-v单片机中文社区(5)

    4.2、添加代码:
    在此感谢郭昊天,刚接触这个CDK,没注意到配置文件“board_config.h”改了AD端口,还是按照芯片管脚定义来,导致数据一致不对:
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <unistd.h>
    4. #include <time.h>
    5. #include <aos/aos.h>
    6. #include "app_config.h"
    7. #include "app_init.h"
    8. #include "csi_config.h"
    9. #include "hw_config.h"
    10. #include "board_config.h"
    11. #include <drv/pin.h>
    12. #include <drv/adc.h>
    13. #include <drv/common.h>
    14. static csi_adc_t  adc;
    15. void adc_init()
    16. {
    17.     csi_error_t ret;
    18.     csi_pin_set_mux(EXAMPLE_ADC_CH1, PA4_ADC_A2);
    19.         
    20.     ret = csi_adc_init(&adc, 0);
    21.         
    22.     if (ret != CSI_OK)
    23.     {
    24.       printf("===%s, %d %d \n", __FUNCTION__, __LINE__,ret);
    25.         
    26.      }
    27.         
    28.         
    29.     /* Configure frequency divsion, this value can be one of(4 8 16 32 64 128) */
    30.     uint32_t  freq_value = csi_adc_freq_div(&adc, 128);
    31.     printf("get freq_value: %d\n", freq_value);
    32.     /* Configure sampling time */
    33.     ret = csi_adc_sampling_time(&adc, 2);
    34.     if (ret != CSI_OK)
    35.     {
    36.       printf("===%s, %d %d \n", __FUNCTION__, __LINE__,ret);
    37.         
    38.     }
    39.     /* Enable channel */
    40.     ret = csi_adc_channel_enable(&adc, 1, true);
    41.     if (ret != CSI_OK)
    42.     {
    43.       printf("===%s, %d %d \n", __FUNCTION__, __LINE__,ret);
    44.         
    45.     }
    46.     /* Trigger new conversion */
    47.     ret = csi_adc_start(&adc);
    48.     if (ret != CSI_OK)
    49.     {
    50.       printf("===%s, %d %d \n", __FUNCTION__, __LINE__,ret);
    51.         
    52.     }
    53.     /* Read result */
    54.     int32_t data = csi_adc_read(&adc);
    55.     printf("get adc result: %d\n", data);
    56.         
    57.     printf("the voltage is: %d mV\n", data*3300/4096);
    58.     /* Uninit adc */
    59.     csi_adc_uninit(&adc);
    60.         
    61. }
    复制代码

    4.3、修改初始化文件

    app_init.h添加void adc_init();

    4.4、更改main.c文件:
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <unistd.h>
    4. #include <time.h>
    5. #include <aos/aos.h>
    6. #include "aos/cli.h"
    7. #include "app_init.h"
    8. #include "lvgl.h"
    9. #include "lv_label.h"
    10. #include "oled.h"
    11. #include "board_config.h"
    12. #include "drv/gpio_pin.h"
    13. #include <drv/pin.h>
    14. #include <drv/adc.h>
    15. /*********************
    16. *      DEFINES
    17. *********************/
    18. #define TAG "app"
    19. /**********************
    20. *      TYPEDEFS
    21. **********************/
    22. /**********************
    23. *  STATIC PROTOTYPES
    24. **********************/
    25. // adc
    26. static void adc_lvgl_task(void *arg);
    27. /**********************
    28. *   GLOBAL FUNCTIONS
    29. **********************/
    30. static void adc_lvgl_task(void *arg);
    31. #include "csi_core.h"
    32. /**
    33. * main
    34. */
    35. int main(void)
    36. {
    37.     board_yoc_init();
    38.     aos_task_new("adc", adc_lvgl_task, NULL, 10 * 1024);
    39.     return 0;
    40. }
    41. static void gui_label_create(void)
    42. {
    43.     lv_obj_t *p = lv_label_create(lv_scr_act(), NULL);
    44.     lv_label_set_long_mode(p, LV_LABEL_LONG_BREAK);
    45.     lv_label_set_align(p, LV_LABEL_ALIGN_CENTER);
    46.     lv_obj_set_pos(p, 0, 4);
    47.     lv_obj_set_size(p, 128, 60);
    48.     lv_label_set_text(p, "THEAD RISC-V\nADC \nDEMO");
    49. }
    50. static void adc_lvgl_task(void *arg)
    51. {
    52.     lv_init();
    53.     /*Initialize for LittlevGL*/
    54.     oled_init();
    55.     adc_init();
    56.     /*Select display 1*/
    57.     // demo_create();
    58.     gui_label_create();
    59.     while (1)
    60.     {
    61.         /* Periodically call the lv_task handler.
    62.          * It could be done in a timer interrupt or an OS task too.*/
    63.         lv_task_handler();
    64.         aos_msleep(5);
    65.         lv_tick_inc(1);
    66.     }
    67. }
    复制代码

    4.5、编译,下载、运行:
    CH2601 单片机芯片及应用-RVB2601开发板ADC读取实验risc-v单片机中文社区(6)
    CH2601 单片机芯片及应用-RVB2601开发板ADC读取实验risc-v单片机中文社区(7)

    5、小结:

    初次使用,有不到之处还请各位大佬指正、海涵!







    上一篇:使用CDK在RVB2061上编写IIC软件驱动
    下一篇:第一步【调试CH2601必装软件】--- RVB2601开发板CDK IDE及驱动安装下载CP210x/PL2303
    RISCV作者优文
    全球首家只专注于RISC-V单片机行业应用的中文网站
    回复

    使用道具 举报

      离线 

  • TA的每日心情
    奋斗
    2022-11-4 00:50
  • 签到天数: 45 天

    [LV.5]

    发表于 2022-6-22 19:09:21 | 显示全部楼层
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <time.h>
    #include <aos/aos.h>
    #include "aos/cli.h"
    #include "app_init.h"
    #include "lvgl.h"
    #include "lv_label.h"
    #include "oled.h"
    #include "board_config.h"
    #include "drv/gpio_pin.h"
    #include <drv/pin.h>
    #include <drv/adc.h>
    #include "csi_core.h"

    //include "app_config.h"
    //#include "csi_config.h"
    //#include "hw_config.h"
    //#include <drv/common.h>


    #define TAG "app"

    static csi_adc_t adc;

    uint32_t i=1;

    static csi_adc_t adc;

    void adc_init()
    {
        csi_error_t ret;
           
        csi_pin_set_mux(EXAMPLE_ADC_CH1, PA4_ADC_A2);
            
        ret = csi_adc_init(&adc, 0);
            
        if (ret != CSI_OK)
        {
          printf("===%s, %d %d \n", __FUNCTION__, __LINE__,ret);
            
         }
        uint32_t  freq_value = csi_adc_freq_div(&adc, 128);
           
        printf("get freq_value: %d\n", freq_value);
           
        /* Configure sampling time */
        ret = csi_adc_sampling_time(&adc, 2);
        if (ret != CSI_OK)
        {
          printf("===%s, %d %d \n", __FUNCTION__, __LINE__,ret);
            
        }
        /* Enable channel */
        ret = csi_adc_channel_enable(&adc, 1, true);
        if (ret != CSI_OK)
        {
          printf("===%s, %d %d \n", __FUNCTION__, __LINE__,ret);
            
        }
        /* Trigger new conversion */
           
        ret = csi_adc_start(&adc);
           
        if (ret != CSI_OK)
        {
          printf("===%s, %d %d \n", __FUNCTION__, __LINE__,ret);
            
        }
           
        /* Read result */
           
        int32_t data = csi_adc_read(&adc);
           
        printf("get adc result: %d\n", data);
            
        printf("the voltage is: %d mV\n", data*3300/4096);
           
        /* Uninit adc */
           
        //csi_adc_uninit(&adc);
            
    }
    static void gui_label_create(void)
    {
        lv_obj_t *p = lv_label_create(lv_scr_act(), NULL);
        lv_label_set_long_mode(p, LV_LABEL_LONG_BREAK);
        lv_label_set_align(p, LV_LABEL_ALIGN_CENTER);
        lv_obj_set_pos(p, 0, 4);
        lv_obj_set_size(p, 128, 60);
        lv_label_set_text(p, "THEAD RISC-V\nADC \nDEMO");
    }

    static void adc_lvgl_task(void *arg)
    {
        lv_init();
        /*Initialize for LittlevGL*/
        oled_init();
        adc_init();
        /*Select display 1*/
        // demo_create();
        gui_label_create();
        while (1)
        {
            /* Periodically call the lv_task handler.
             * It could be done in a timer interrupt or an OS task too.*/
            lv_task_handler();
            aos_msleep(5);
            lv_tick_inc(1);
                    int32_t data = csi_adc_read(&adc);
               printf("get adc result: %d\n", data);
           printf("the voltage is: %d mV\n", data*3300/4096);
                   
        }
    }

    static void adc_lvgl_task(void *arg);

    int main(void)
    {
        board_yoc_init();
            aos_task_new("adc", adc_lvgl_task, NULL, 10 * 1024);
           
        return 0;
    }


    点评

    这个代码只能打印一次?帮忙看看问题?[attachimg]9082[/attachimg]  详情 回复 发表于 2022-6-22 19:11
    全球首家只专注于RISC-V单片机行业应用的中文网站

      离线 

  • TA的每日心情
    奋斗
    2022-11-4 00:50
  • 签到天数: 45 天

    [LV.5]

    发表于 2022-6-22 19:11:36 | 显示全部楼层
    郑同学 发表于 2022-6-22 19:09
    #include
    #include
    #include

    这个代码只能打印一次?帮忙看看问题? CH2601 单片机芯片及应用-RVB2601开发板ADC读取实验risc-v单片机中文社区(8)
    全球首家只专注于RISC-V单片机行业应用的中文网站

      离线 

  • TA的每日心情

    2022-6-26 13:17
  • 签到天数: 4 天

    [LV.2]

    发表于 2022-6-24 15:04:01 | 显示全部楼层
    采集ADC正确吗?
    全球首家只专注于RISC-V单片机行业应用的中文网站
    高级模式
    B Color Image Link Quote Code Smilies

    本版积分规则

    关闭

    RISC-V单片机中文网上一条 /2 下一条



    版权及免责声明|RISC-V单片机中文网 |网站地图

    GMT+8, 2024-4-20 02:45 , Processed in 0.553037 second(s), 58 queries .

    快速回复 返回顶部 返回列表