1. Use of M601 ADC

1 ADC related data structure and API


1.1 Introduction


OpenCPU supports two analog input pins that can be used to detect external voltage. Please refer to the pin definition and ADC hardware characteristics. The voltage range that can be detected can be divided into four gears, namely 1V, 2V, and 3V. The data structure and API introduced in this article can refer to the Zyf_adc.h file in the SDK.

 

1.2 Usage


The ADC value can be read by directly calling the ZYF_AdcRead interface. In order to confirm the reliability of the ADC reading, it is recommended to read it multiple times and perform the average processing.

 

1.3 ADC related API


1.3.1 ZYF_AdcRead to read ADC value
 Function prototype
int32_t ZYF_AdcRead(uint8_t adc_channel, uint8_t adc_scale)
 Parameter
adc_channel:
[input] sampling channel, ADC pin is AUXIN4, channel ID value is 4
adc_scale:
[input] range
 Return The result is
ZYF_RET_ERR_CHANNEL_OUTRANGE, ADC channel is out of range.
ZYF_RET_ERR FATAL, some fatal errors occurred when reading ADC value.
Others, ADC sampling results. It is a 10-bit A/D converter with an input voltage range of 0~2800mV.

 

2 Introduction to ADC routines


This chapter mainly introduces how to use example_adc.c in the SDK to test the adc function separately.
Compiling method: .\examples\build\ corresponds to the .bat file by double-clicking to execute or opening it to compile.
Generated file: .\out\corresponding directory\hex\M601_example_**.pac  

The routine of adc is mainly by creating a timer, in which the
adc sampling value is continuously read according to the set time interval . ADC sampling result. It is a 10-bit A/D converter with an input voltage
range of 0~2800mV.


#include <stdint.h>

#include "zyf_trace.h"
#include "zyf_adc.h"
#include "zyf_timer.h"
#include "zyf_app.h"
#include "zyf_uart.h"
#include "zyf_thread.h"



#define TIMER_INTERVAL 1000 //units ms

typedef struct {
    ZYF_Timer_t *ptTimer;
    uint32_t    wParam;
    uint32_t    runtimes;
} AppTimer_t;

static AppTimer_t s_tTimer;

static Uart_Param_t g_uart1param;

static void ZYF_TimerCallback(void *pvArg)
{
    AppTimer_t *ptAppTimer = (AppTimer_t *)pvArg;
    int32_t value = -1;
    static uint32_t runcnt = 0;

    runcnt++;
    if (ptAppTimer != NULL)
    {
        ZYF_LOG("timer run times %d.",runcnt);
        ZYF_StartTimer(ptAppTimer->ptTimer, TIMER_INTERVAL);
    }

    if (runcnt > ptAppTimer->runtimes && ZYF_StopTimer(ptAppTimer->ptTimer) < 0)
    {
        runcnt = 0;
        ZYF_LOG("Failed to delete timer");
    }

    value  = ZYF_AdcRead(ZYF_ADC_CHANNEL_0, ZYF_ADC_SCALE_5V000);
    ZYF_LOG("ADC0: %d", value);
    value  = ZYF_AdcRead(ZYF_ADC_CHANNEL_1, ZYF_ADC_SCALE_5V000);
    ZYF_LOG("ADC1: %d", value);
}

void ZYF_ADCTimerInit(void)
{
    s_tTimer.runtimes = 500;
    s_tTimer.ptTimer = ZYF_CreateTimer(ZYF_TimerCallback, (void *)&s_tTimer);
    if (s_tTimer.ptTimer != NULL) 
    {
        ZYF_StartTimer(s_tTimer.ptTimer, TIMER_INTERVAL);
    }
    
}



void ADCThread_Example(void * Param)
{
    ZYF_MsgQ_t *ptMsg;
    ZYF_AppMsg_t tMsg;
    int iRet = -1;
    ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t));
    ZYF_LOG("thread enter!");

    ZYF_ADCTimerInit();
    
    while (1) {
        ZYF_LOG("in while.");
        iRet = ZYF_MsgQGet(ptMsg, (void *)&tMsg);
        if (iRet < 0) {
            ZYF_LOG("Failed to get msg");
            ZYF_ThreadSleep(1000);
        }
    }

}


void UartWriteCallBack(void* Param) // general com
{
    Uart_Param_t *uartparam = (Uart_Param_t *)Param; 
    if(Param == NULL)
    {
        return;
    }    

    ZYF_UartWrite(uartparam->port,(uint8_t *)"UartWrite succeed\r\n",strlen("UartWrite succeed\r\n"));
    ZYF_UartWriteCallbackSwitch(uartparam->port,false);

}

void UartReadCallBack(void* Param) // 
{
    uint32_t recvlen = 0;
    Uart_Param_t *uartparam = (Uart_Param_t *)Param; 

    ZYF_LOG("Uart%d recv",uartparam->port);

    while(ZYF_UartRead(uartparam->port, &(uartparam->uartbuf[recvlen]), 1))
    {
        ZYF_LOG("recv :%02x",uartparam->uartbuf[recvlen]);
        recvlen++;
    }
    ZYF_UartWrite(uartparam->port,uartparam->uartbuf,recvlen);
    ZYF_UartWriteCallbackSwitch(uartparam->port,true);
}


static void AppUartInit(void)
{
    int32_t ret;
    g_uart1param.port = DEBUG_PORT;
    ZYF_UartRegister(g_uart1param.port, UartReadCallBack,&g_uart1param);
    ZYF_UartWriteCbRegister(g_uart1param.port,UartWriteCallBack,&g_uart1param);
    ZYF_UartOpen(g_uart1param.port, 115200, ZYF_FC_NONE);

    ZYF_LOG("AppUartInit");
    return;
}


static void prvInvokeGlobalCtors(void)
{
    extern void (*__init_array_start[])();
    extern void (*__init_array_end[])();

    size_t count = __init_array_end - __init_array_start;
    for (size_t i = 0; i < count; ++i)
        __init_array_start[i]();
}


int appimg_enter(void *param)
{
    AppUartInit();
    ZYF_LOG("application image enter, param 0x%x", param);

    prvInvokeGlobalCtors();

    ZYF_ThreadCreate("UartThread_Example", ADCThread_Example, NULL, ZYF_PRIORITY_HIGH, 10*1024);
    return 0;
}

void appimg_exit(void)
{
    OSI_LOGI(0, "application image exit");
}



 

Guess you like

Origin blog.csdn.net/w_hizyf_m/article/details/107082442