1 /*
2 Copyright (C) 2024 HiHope Open Source Organization .
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 #include "pinctrl.h"
16 #include "adc.h"
17 #include "adc_porting.h"
18 #include "common_def.h"
19 #include "soc_osal.h"
20 #include "tcxo.h"
21 #include "ohos_init.h"
22 #include "cmsis_os2.h"
23
24 #define DELAY_10000MS 10000
25 #define CYCLES 10
26 #define ADC_TASK_PRIO 26
27 #define ADC_TASK_STACK_SIZE 0x1000
28
adc_task(const char * arg)29 static void *adc_task(const char *arg)
30 {
31 unused(arg);
32 printf("start adc sample\r\n");
33 uapi_adc_init(ADC_CLOCK_NONE);
34 uint8_t adc_channel = 1;
35 uint16_t voltage = 0;
36 uint32_t counter = 0;
37 uint32_t running = 1;
38 while (running) {
39
40 adc_port_read(adc_channel, &voltage);
41 if (voltage > 550 && voltage < 650) {
42 printf("button1,voltage: %d mv\r\n", voltage);
43 } else if (voltage > 950 && voltage < 1050) {
44 printf("button2,voltage: %d mv\r\n", voltage);
45 }
46 if (counter >= 100) {
47 running = 0;
48 }
49
50 osal_msleep(100);
51 }
52 /* 当前测量的电压值和实际值可能有较大差别,请确认是否有分压电阻,如果有分压电阻,则差别符合预期 */
53 uapi_adc_deinit();
54
55 return NULL;
56 }
57
adc_entry(void)58 static void adc_entry(void)
59 {
60
61 // 定义线程属性
62 osThreadAttr_t attr;
63 attr.name = "AdcTask";
64 attr.stack_size = 0x1000;
65 attr.priority = osPriorityNormal;
66
67 // 创建线程
68 if (osThreadNew(adc_task, NULL, &attr) == NULL) {
69 printf("[AdcExample] Falied to create AdcTask!\n");
70 }
71
72 }
73
74 // 运行入口函数
75 SYS_RUN(adc_entry);