|
| 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