wifiiot_adc.h接口简介
创建任务1秒读取一次ADC
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include <string.h>
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_adc.h"
#include "wifiiot_errno.h"
static float get_voltage(void)
{
unsigned int ret;
unsigned short data;
ret = AdcRead(WIFI_IOT_ADC_CHANNEL_5,&data,WIFI_IOT_ADC_EQU_MODEL_8,WIFI_IOT_ADC_CUR_BAIS_DEFAULT,0xff);
if (ret != WIFI_IOT_SUCCESS)
{
printf("ADC Read Fail\n");
}
return (float)data*1.8f*4/4096;
}
static void adc_func(void)
{
GpioInit();
IoSetPull(WIFI_IOT_IO_NAME_GPIO_11,WIFI_IOT_IO_PULL_UP);
float voltage;
while(1)
{
voltage = get_voltage();
printf("voltage:%.3fV\n", voltage);
osDelay(100);
}
}
static void my_led_example(void)
{
osThreadAttr_t attr;
attr.attr_bits = 0;
attr.name = "adc";
attr.cb_mem = NULL;
attr.cb_size = 0;
attr.priority = 24;
attr.stack_size = 1024*8;
if(osThreadNew((osThreadFunc_t)adc_func,NULL,&attr) == NULL)
{
printf("Falied to create adc_func!\n");
}
}
SYS_RUN(my_led_example);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 运行效果,通过按键1改变电压值