• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2023 Unionman Technology Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include "adc_if.h"
20 #include "hdf_log.h"
21 #include "osal_time.h"
22 
23 /* 设备号0,通道号1 */
24 #define ADC_DEVICE_NUM  0
25 #define TEMP_CONST (1.8/(0.01*4096))
26 
main(int argc,char * argv[])27 int32_t main(int argc, char *argv[])
28 {
29     int32_t ret;
30     uint32_t channelNum = 0;
31     DevHandle adcHandle = NULL;
32     uint32_t read_val = 0;
33     double temperature = 0;
34     // 默认打开通道0,可以输入参数使用其他通道
35     if (argc == 2L) {
36         channelNum=atoi(argv[1]);
37     }
38     /* 打开ADC设备 */
39     adcHandle = AdcOpen(ADC_DEVICE_NUM);
40     if (adcHandle == NULL) {
41         HDF_LOGE("%s: Open ADC%u fail!", __func__, ADC_DEVICE_NUM);
42         return -1;
43     }
44     /* 读取ADC数据 */
45     ret = AdcRead(adcHandle, channelNum, &read_val);
46     if (ret != HDF_SUCCESS) {
47         HDF_LOGE("%s: ADC read fail!:%d", __func__, ret);
48         AdcClose(adcHandle);
49         return -1;
50     }
51     /* 计算温度 */
52     temperature = ((double)read_val) * TEMP_CONST;
53 
54     printf("Temperature is %.1f°C\n", temperature);
55 
56     /* 访问完毕关闭ADC设备 */
57     AdcClose(adcHandle);
58     return HDF_SUCCESS;
59 }
60