Skip to content

Commit dfd1eb0

Browse files
nmaggionixiaoxiang781216
authored andcommitted
examples/ads7046: Add new example for ADS7046 ADC
Add a new example that shows how to read a sample from an ADS7046 ADC sensor registered on the SPI bus. Signed-off-by: Niccolò Maggioni <[email protected]>
1 parent 14a39ee commit dfd1eb0

File tree

5 files changed

+222
-0
lines changed

5 files changed

+222
-0
lines changed

examples/ads7046/CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# ##############################################################################
2+
# apps/examples/ads7046/CMakeLists.txt
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
7+
# license agreements. See the NOTICE file distributed with this work for
8+
# additional information regarding copyright ownership. The ASF licenses this
9+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
10+
# use this file except in compliance with the License. You may obtain a copy of
11+
# the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations under
19+
# the License.
20+
#
21+
# ##############################################################################
22+
23+
if(CONFIG_EXAMPLES_ADS7046)
24+
nuttx_add_application(
25+
NAME
26+
${CONFIG_EXAMPLES_ADS7046_PROGNAME}
27+
PRIORITY
28+
${CONFIG_EXAMPLES_ADS7046_PRIORITY}
29+
STACKSIZE
30+
${CONFIG_EXAMPLES_ADS7046_STACKSIZE}
31+
MODULE
32+
${CONFIG_EXAMPLES_ADS7046}
33+
SRCS
34+
ads7046_main.c)
35+
endif()

examples/ads7046/Kconfig

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config EXAMPLES_ADS7046
7+
tristate "ADS7046 ADC example"
8+
default n
9+
depends on ADC_ADS7046
10+
---help---
11+
Enable the ADS7046 example
12+
13+
if EXAMPLES_ADS7046
14+
15+
config EXAMPLES_ADS7046_PROGNAME
16+
string "Program name"
17+
default "ads7046"
18+
---help---
19+
This is the name of the program that will be used when the NSH ELF
20+
program is installed.
21+
22+
config EXAMPLES_ADS7046_PRIORITY
23+
int "ADS7046 task priority"
24+
default 100
25+
26+
config EXAMPLES_ADS7046_STACKSIZE
27+
int "ADS7046 stack size"
28+
default DEFAULT_TASK_STACKSIZE
29+
30+
config EXAMPLES_ADS7046_DEVPATH
31+
string "ADS7046 device path"
32+
default "/dev/adc0"
33+
---help---
34+
The default path to the ADS7046 ADC device
35+
36+
endif

examples/ads7046/Make.defs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
############################################################################
2+
# apps/examples/ads7046/Make.defs
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
23+
ifneq ($(CONFIG_EXAMPLES_ADS7046),)
24+
CONFIGURED_APPS += $(APPDIR)/examples/ads7046
25+
endif

examples/ads7046/Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
############################################################################
2+
# apps/examples/ads7046/Makefile
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
23+
include $(APPDIR)/Make.defs
24+
25+
# ADS7046 ADC example built-in application info
26+
27+
PROGNAME = $(CONFIG_EXAMPLES_ADS7046_PROGNAME)
28+
PRIORITY = $(CONFIG_EXAMPLES_ADS7046_PRIORITY)
29+
STACKSIZE = $(CONFIG_EXAMPLES_ADS7046_STACKSIZE)
30+
MODULE = $(CONFIG_EXAMPLES_ADS7046)
31+
32+
# ADS7046 ADC example
33+
34+
MAINSRC = ads7046_main.c
35+
36+
include $(APPDIR)/Application.mk

examples/ads7046/ads7046_main.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/****************************************************************************
2+
* apps/examples/ads7046/ads7046_main.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <nuttx/config.h>
28+
#include <stdio.h>
29+
#include <fcntl.h>
30+
#include <unistd.h>
31+
#include <sys/ioctl.h>
32+
#include <nuttx/analog/ioctl.h>
33+
#include <nuttx/analog/ads7046.h>
34+
35+
/****************************************************************************
36+
* Pre-processor Definitions
37+
****************************************************************************/
38+
39+
/* As per the datasheet, the ADS7046 returns the following readings:
40+
* INPUT VOLTAGE (AINP - AINM) DESCRIPTION HEX
41+
* -------------------------------------- ------------------------ ---
42+
* <= 1 LSB Negative full-scale code 000
43+
* 1 LSB to 2 LSB - 001
44+
* V_REF / 2 to V_REF / 2 + 1 LSB Mid code 7FF
45+
* V_REF / 2 + 1 LSB to V_REF / 2 + 2 LSB - 800
46+
* >= V_REF - 1 LSB Positive full-scale code FFF
47+
*/
48+
49+
#define SAMPLE_TO_PCT(sample) (((sample) * 100) / 0xfff)
50+
51+
/****************************************************************************
52+
* Public Functions
53+
****************************************************************************/
54+
55+
/****************************************************************************
56+
* Name: ads7046_main
57+
****************************************************************************/
58+
59+
int main(const int argc, FAR char *argv[])
60+
{
61+
UNUSED(argc);
62+
UNUSED(argv);
63+
64+
int ret;
65+
int fd;
66+
uint16_t sample;
67+
68+
fd = open(CONFIG_EXAMPLES_ADS7046_DEVPATH, O_RDONLY);
69+
if (fd < 0)
70+
{
71+
printf("Failed to open %s: %s (%d)\n", CONFIG_EXAMPLES_ADS7046_DEVPATH,
72+
strerror(errno), errno);
73+
return EXIT_FAILURE;
74+
}
75+
76+
ret = ioctl(fd, ANIOC_ADS7046_READ, &sample);
77+
if (ret != OK)
78+
{
79+
perror("Could not ioctl fd");
80+
close(fd);
81+
return EXIT_FAILURE;
82+
}
83+
84+
printf("ADS7046: hex=%x, dec=%"PRIu16", adc_percentage=%u%%\n",
85+
sample, sample, SAMPLE_TO_PCT(sample));
86+
87+
close(fd);
88+
89+
return EXIT_SUCCESS;
90+
}

0 commit comments

Comments
 (0)