📄 adc_example.c
字号:
/***********************************************************************
* $Workfile: adc_example.c $
* $Revision: 1.0 $
* $Author: WellsK $
* $Date: Oct 15 2003 16:42:00 $
*
* Project: ADC driver example
*
* Description:
* A simple polled ADC driver example.
*
* Revision History:
* $Log: //smaicnt2/pvcs/VM/sharpmcu/archives/sharpmcu/software/csps/lh7a404/bsps/sdk7a404/examples/adc_polled/adc_example.c-arc $
*
* Rev 1.0 Oct 15 2003 16:42:00 WellsK
* Initial revision.
*
*
***********************************************************************
* SHARP MICROELECTRONICS OF THE AMERICAS MAKES NO REPRESENTATION
* OR WARRANTIES WITH RESPECT TO THE PERFORMANCE OF THIS SOFTWARE,
* AND SPECIFICALLY DISCLAIMS ANY RESPONSIBILITY FOR ANY DAMAGES,
* SPECIAL OR CONSEQUENTIAL, CONNECTED WITH THE USE OF THIS SOFTWARE.
*
* SHARP MICROELECTRONICS OF THE AMERICAS PROVIDES THIS SOFTWARE SOLELY
* FOR THE PURPOSE OF SOFTWARE DEVELOPMENT INCORPORATING THE USE OF A
* SHARP MICROCONTROLLER OR SYSTEM-ON-CHIP PRODUCT. USE OF THIS SOURCE
* FILE IMPLIES ACCEPTANCE OF THESE CONDITIONS.
*
* COPYRIGHT (C) 2001 SHARP MICROELECTRONICS OF THE AMERICAS, INC.
* CAMAS, WA
**********************************************************************/
#include "abl_types.h"
#include "lh7a404_adc_driver.h"
/* ADC device handle */
STATIC INT_32 adcdev;
/* ADC sample settling time in clocks */
#define ADC_SETTLING_TIME 100
/* Number of samples */
#define MAX_SAMPLES 5000
/* Sample array */
static UNS_16 sample[MAX_SAMPLES];
/* Number of analog inputs to sample */
#define ADC_INPUTS 4
/***********************************************************************
*
* Function: c_entry
*
* Purpose: ADC example
*
* Processing:
* This example will configure the Touchscreen controller (ADC) and
* ADC driver to continuously sample a set of analog inputs at
* 8Ksamples/sec per input. The inputs can be driven with any
* voltage between GND and 3.3v (referenced to EVB gound). This uses
* a polling method and will save 5000 samples in a local array.
*
* Parameters: None
*
* Outputs: None
*
* Returns: Always returns 1
*
* Notes: None
*
**********************************************************************/
int c_entry(void)
{
INT_32 clocks_per_sample, clocks_per_sequence, adc_clock;
INT_32 idx;
ADC_CH_CONV_T chdata;
/* Open ADC */
if ((adcdev = adc_open(ADC, 0)) == 0x00000000)
{
return 0;
}
/* Reset the ADC and driver to a known 'clean' state. This isn't
needed directly after open, but is good practice */
adc_ioctl(adcdev, ADC_SET_STATE, ADC_MODE_RESET);
/* To get each analog input to sample at 8KHz, the ADC clock
speed needs to be computed based on number of samples
(ADC_INPUTS), the settling time (ADC_SETTLING_TIME), and the
17 clock per sample conversion time (ADC_SAMPLE_CLKS) */
clocks_per_sample = ADC_SAMPLE_CLKS + ADC_SETTLING_TIME;
clocks_per_sequence = clocks_per_sample * ADC_INPUTS;
adc_clock = clocks_per_sequence * 8000;
/* Set ADC clock speed to get 8KHz sampling speed */
adc_ioctl(adcdev, ADC_SET_CLOCK, adc_clock);
/* Setup idle channel (not used, but should be setup) */
chdata.settle_clks = ADC_SETTLING_TIME;
chdata.inpc = ADC_ANALOG_2;
chdata.innc = ADC_REFN_VREFN;
chdata.vrefp = ADC_REFP_AN0;
chdata.vrefn = ADC_REFN_AN1;
chdata.bias = 0;
adc_ioctl(adcdev, ADC_SET_IDLE_CH, (INT_32) &chdata);
/* Setup analog inputs as open-ended inputs on each channel */
for (idx = 0; idx < ADC_INPUTS; idx++)
{
chdata.settle_clks = ADC_SETTLING_TIME;
chdata.inpc = (ADC_INPUTP_CH_T) idx;
chdata.innc = ADC_REFN_GND;
chdata.vrefp = ADC_REFP_VREF;
chdata.vrefn = ADC_REFN_VREF;
adc_ioctl(adcdev, ADC_ADD_CHANNEL, (INT_32) &chdata);
}
/* Disable internal reference voltage */
adc_ioctl(adcdev, ADC_ENABLE_IREF, 0);
/* Set sample mode to continuous sampling */
adc_ioctl(adcdev, ADC_SET_STATE, ADC_CONTINUOUS);
/* Loop until all samples are fetched */
idx = 0;
while (idx < MAX_SAMPLES)
{
/* Read and display samples */
idx = idx + (adc_read_polled(adcdev, &sample[idx], 16) / 2);
}
/* Close ADC */
adc_close(adcdev);
return 1;
}
#ifndef __GNUC__
/* With ARM and GHS toolsets, the entry point is main() - this will
allow the linker to generate wrapper code to setup stacks, allocate
heap area, and initialize and copy code and data segments. For GNU
toolsets, the entry point is through __start() in the crt0_gnu.asm
file, and that startup code will setup stacks and data */
int main(void)
{
return c_entry();
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -