测试ADC采集,调整开发板上的电位器,串口打印输出采集的电压值。
一、硬件部分电路
ADC硬件电路
电路板上有两个电位器,连接两路ADC接口
二、程序部分
2.1、adc.c
#include "ac7840x.h"
#include "adc_drv.h"
#include "gpio_drv.h"
#include "adc/adc.h"
#define ADC_INDEX (0U)
uint16_t g_adc_array[4] = {0};
/*!
* [url=home.php?mod=space&uid=247401]@brief[/url] ADCÊý¾Ýת»»´¥·¢²âÊÔ.
*
* @param[in] none
*
* [url=home.php?mod=space&uid=266161]@return[/url] none
*/
void ADC_TriggerTest(void)
{
ADC_DRV_SoftwareStartRegularConvert(ADC_INDEX); /*! ADCÈí¼þ´¥·¢*/
while (!ADC_DRV_GetConvCompleteFlag(ADC_INDEX, ADC_RSEQ_3)); /*! µÈ´ýADC×îºóÒ»¸öͨµÀת»»Íê³É*/
ADC_DRV_GetSeqResult(ADC_INDEX, ADC_RSEQ_0, &g_adc_array[0]); /*! »ñÈ¡¹æÔò×é0µÄת»»Öµ*/
ADC_DRV_GetSeqResult(ADC_INDEX, ADC_RSEQ_1, &g_adc_array[1]);
ADC_DRV_GetSeqResult(ADC_INDEX, ADC_RSEQ_2, &g_adc_array[2]);
ADC_DRV_GetSeqResult(ADC_INDEX, ADC_RSEQ_3, &g_adc_array[3]);
//printf("ADC_value: VR1 = 0x%-5x VR2 = 0x%-5x TSENSOR = 0x%-5x SUPPLY = 0x%-5x \r\n", g_adc_array[0], g_adc_array[1], g_adc_array[2], g_adc_array[3]);
printf("ADC_value: VR1 = %2.2fV VR2 = %2.2fV \r\n", (float)(g_adc_array[0]*3.3/4096), (float)(g_adc_array[1]*3.3/4096) );
}
/*!
* @brief ÅäÖÃADCͨµÀ
*
* @param[in] instance: ADC instance number
* @param[in] seq: adc sequence
* @param[in] channel: adc channel
* @return none
*/
void ADC_ConfigChannel(const uint32_t instance, const adc_sequence_t seq, adc_inputchannel_t channel)
{
adc_chan_config_t adcChConfig;
/*!
ADC²ÉÑùת»»Ê±¼ä¼ÆË㣺
FCLK ʱÖÓԴΪSPLLDIV2_CLK,ƵÂÊΪ60MHz
ADCCLK = FCLK/clockDivide = 60/6 = 10MHz
×Üת»»Ê±¼ä = (SPT+ resolution£¨12/10/8£©)¡ÁADCCLKÖÜÆÚ+5¸öFCLK = (5+12)/10+5/60 ¡Ö 1.78us
×¢Ò⣺12bit·Ö±æÂÊ×î´óת»»ÂÊΪ1Msps£¬10bit·Ö±æÂÊ×î´óת»»ÂÊΪ1.2Msps£¬8bit·Ö±æÂÊ×î´óת»»ÂÊΪ1.4Msps
*/
ADC_DRV_InitChanStruct(&adcChConfig); /*! ½á¹¹Ìå³õʼ»¯ÎªÄ¬ÈÏÖµ*/
adcChConfig.channel = channel; /*! ÊäÈëͨµÀÑ¡Ôñ*/
adcChConfig.spt = ADC_SPT_CLK_5; /*! ²ÉÑùʱ¼ä*/
adcChConfig.interruptEn = false; /*! EOCÖжÏʹÄÜ*/
ADC_DRV_ConfigChan(instance, seq, &adcChConfig); /*! Ó¦ÓÃͨµÀÅäÖÃ*/
}
void init_adc(void)
{
adc_converter_config_t adcConfig;
/*! ADCÊäÈë¶Ë¿Ú£¨µçλÆ÷£©ÅäÖÃ*/
GPIO_DRV_SetMuxModeSel(VR1_PORT, VR1_PIN, PORT_PIN_DISABLED); /*! µçλÆ÷VR1*/
GPIO_DRV_SetMuxModeSel(VR2_PORT, VR2_PIN, PORT_PIN_DISABLED); /*! µçλÆ÷VR2*/
/*! ADCÅäÖÃ
ÅäÖÃADCΪ12λ·Ö±æÂÊ£¬¹¤×÷ģʽΪMODE3¹æÔò×éɨÃè+×¢Èë×éɨÃèģʽ¶àͨµÀµ¥´Îת»»£¬¹æÔò×鳤¶ÈΪ4¡£
ÿ´¥·¢Ò»´Î½øÐÐÒ»´ÎADCת»»£¬Ã¿´Îת»»¹æÔò×éÖеÄ4¸öͨµÀ¡£
*/
ADC_DRV_Init(ADC_INDEX);
ADC_DRV_InitConverterStruct(&adcConfig);
adcConfig.clockDivide = ADC_CLK_DIVIDE_6; /*! ADCʱÖÓ·ÖƵ¡£ADCʱÖÓÔ´Ñ¡ÔñÔÚʱÖÓ³õʼ»¯ÀïÅäÖ㬴ËÀý³ÌÑ¡ÔñΪSPLLDIV2_CLK,ƵÂÊΪ60MHz*/
adcConfig.resolution = ADC_RESOLUTION_12BIT; /*! ADC·Ö±æÂÊÉèÖÃ*/
adcConfig.regularTrigger = ADC_TRIGGER_INTERNAL; /*! ¹æÔò×é´¥·¢Ô´ÀàÐÍÑ¡Ôñ*/
adcConfig.injectTrigger = ADC_TRIGGER_INTERNAL; /*! ×¢Èë×é´¥·¢Ô´ÀàÐÍÑ¡Ôñ*/
adcConfig.dmaEnable = false; /*! DMAʹÄÜλ*/
adcConfig.voltageRef = ADC_VOLTAGEREF_VREF; /*! ADC²Î¿¼µçѹԴѡÔñ£¬VREF/VDDA*/
adcConfig.scanModeEn = true; /*! ¹¤×÷ģʽÅäÖãºÉ¨ÃèģʽʹÄÜ룬¶àͨµÀʱÐèҪʹÄÜɨÃèģʽ£¬µ¥¸öͨµÀʱ²»ÐèҪʹÄÜɨÃè*/
adcConfig.continuousModeEn = false; /*! ¹¤×÷ģʽÅäÖãºÁ¬ÐøģʽʹÄÜ룬1£º´¥·¢ºó¿ÉÁ¬Ðøת»»£¬0£º´¥·¢Ò»´Îֻת»»Ò»´Î£¬×ª»»Íê³ÉºóÍ£Ö¹*/
adcConfig.regularDiscontinuousModeEn = false; /*! ¹¤×÷ģʽÅäÖ㺹æÔò×é²»Á¬ÐøģʽʹÄÜλ*/
adcConfig.injectDiscontinuousModeEn = false; /*! ¹¤×÷ģʽÅäÖãº×¢Èë×é²»Á¬ÐøģʽʹÄÜλ*/
adcConfig.injectAutoModeEn = false; /*! ¹¤×÷ģʽÅäÖãº×Ô¶¯×¢ÈëģʽʹÄÜλ*/
adcConfig.intervalModeEn = false; /*! ¹¤×÷ģʽÅäÖ㺼ä¸ôģʽʹÄÜλ*/
adcConfig.regularDiscontinuousNum = 0; /*! ¹æÔò×é×Ó×鳤¶È (ÓÃÓÚmode7)*/
adcConfig.regularSequenceLength = 4; /*! ¹æÔò×鳤¶ÈÉèÖÃ*/
adcConfig.injectSequenceLength = 0; /*! ×¢Èë×鳤¶ÈÉèÖÃ*/
adcConfig.powerEn = true; /*! ADCÉϵç*/
ADC_DRV_ConfigConverter(ADC_INDEX, &adcConfig); /*! ³õʼ»¯ADCÅäÖÃ*/
/*! ADCͨµÀÅäÖÃ*/
ADC_ConfigChannel(ADC_INDEX, ADC_RSEQ_0, VR1_ADC_CH); /*! µçλÆ÷VR1¶ÔӦͨµÀ*/
ADC_ConfigChannel(ADC_INDEX, ADC_RSEQ_1, VR2_ADC_CH); /*! µçλÆ÷VR2¶ÔӦͨµÀ*/
ADC_ConfigChannel(ADC_INDEX, ADC_RSEQ_2, ADC_CH_TSENSOR); /*! ÄÚ²¿Î¶ȴ«¸ÐÆ÷ͨµÀ*/
ADC_ConfigChannel(ADC_INDEX, ADC_RSEQ_3, ADC_CH_SUPPLY); /*! ÄÚ²¿¹©µçµçѹͨµÀ*/
}
2.2、adc.h
#ifndef _ADC_H
#define _ADC_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define VR1_PORT (PORTC)
#define VR1_PIN (2)
#define VR1_ADC_CH (ADC_CH_10)
#define VR2_PORT (PORTC)
#define VR2_PIN (3)
#define VR2_ADC_CH (ADC_CH_11)
void init_adc(void);
void ADC_TriggerTest(void);
#ifdef __cplusplus
}
#endif
#endif
2.3、main.c
int main(void)
{
SystemClock_Config();
init_led();
init_key();
init_adc();
InitDebug();
while (1)
{
led3_on();
OSIF_TimeDelay(100);
led3_off();
OSIF_TimeDelay(100);
ADC_TriggerTest();
}
}
三、程序运行
编译后下载程序运行,串口输出
调整电位器VR1和VR2,相应通道的输出电压发生变化。
|