小学生
最后登录1970-1-1
在线时间 小时
注册时间2016-8-2
|
楼主 |
发表于 2016-8-2 13:28:31
|
显示全部楼层
#include "bsp_adc.h"
__IO uint16_t ADC_ConvertedValue[macNOFCHANEL]={0,0,0,0};
/**
* @brief ADC GPIO ³õʼ»¯
* @param ÎÞ
* @retval ÎÞ
*/
static void ADCx_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// ´ò¿ª ADC IO¶Ë¿ÚʱÖÓ
macADC_GPIO_APBxClock_FUN ( macADC_GPIO_CLK, ENABLE );
// ÅäÖà ADC IO Òý½Åģʽ
// ADC 3¸öͨµÀ
GPIO_InitStructure.GPIO_Pin = macADC_PIN1;
GPIO_InitStructure.GPIO_Pin = macADC_PIN2;
GPIO_InitStructure.GPIO_Pin = macADC_PIN3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
// ³õʼ»¯ ADC IO
GPIO_Init(macADC_PORT, &GPIO_InitStructure);
}
/**
* @brief ÅäÖÃADC¹¤×÷ģʽ
* @param ÎÞ
* @retval ÎÞ
*/
static void ADCx_Mode_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
// ´ò¿ªDMAʱÖÓ
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
// ´ò¿ªADCʱÖÓ
macADC_APBxClock_FUN ( macADC_CLK, ENABLE );
// ¸´Î»DMA¿ØÖÆÆ÷
DMA_DeInit(macADC_DMA_CHANNEL);
// ÅäÖà DMA ³õʼ»¯½á¹¹Ìå
// ÍâÉè»ùַΪ£ºADC Êý¾Ý¼Ä´æÆ÷µØÖ·
DMA_InitStructure.DMA_PeripheralBaseAddr = ( u32 ) ( & ( macADCx->DR ) );
// ´æ´¢Æ÷µØÖ·
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_ConvertedValue;
// Êý¾ÝÔ´À´×ÔÍâÉè
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
// »º³åÇø´óС£¬Ó¦¸ÃµÈÓÚÊý¾ÝÄ¿µÄµØµÄ´óС
DMA_InitStructure.DMA_BufferSize = macNOFCHANEL;
// ÍâÉè¼Ä´æÆ÷Ö»ÓÐÒ»¸ö£¬µØÖ·²»ÓõÝÔö
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
// ´æ´¢Æ÷µØÖ·µÝÔö
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
// ÍâÉèÊý¾Ý´óСΪ°ë×Ö£¬¼´Á½¸ö×Ö½Ú
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
// ÄÚ´æÊý¾Ý´óСҲΪ°ë×Ö£¬¸úÍâÉèÊý¾Ý´óСÏàͬ
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
// Ñ­»·´«Êäģʽ
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
// DMA ´«ÊäͨµÀÓÅÏȼ¶Îª¸ß£¬µ±Ê¹ÓÃÒ»¸öDMAͨµÀʱ£¬ÓÅÏȼ¶ÉèÖò»Ó°Ïì
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
// ½ûÖ¹´æ´¢Æ÷µ½´æ´¢Æ÷ģʽ£¬ÒòΪÊÇ´ÓÍâÉèµ½´æ´¢Æ÷
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
// ³õʼ»¯DMA
DMA_Init(macADC_DMA_CHANNEL, &DMA_InitStructure);
// ʹÄÜ DMA ͨµÀ
DMA_Cmd(macADC_DMA_CHANNEL , ENABLE);
// ADC ģʽÅäÖÃ
// ֻʹÓÃÒ»¸öADC£¬ÊôÓÚµ¥Ä£Ê½
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
// ɨÃèģʽ
ADC_InitStructure.ADC_ScanConvMode = ENABLE ;
// Á¬Ðøת»»Ä£Ê½
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
// ²»ÓÃÍⲿ´¥·¢×ª»»£¬Èí¼þ¿ªÆô¼´¿É
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
// ת»»½á¹ûÓÒ¶ÔÆë
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
// ת»»Í¨µÀ¸öÊý
ADC_InitStructure.ADC_NbrOfChannel = macNOFCHANEL;
// ³õʼ»¯ADC
ADC_Init(macADCx, &ADC_InitStructure);
// ÅäÖÃADCʱÖӣΪCLK2µÄ8·ÖƵ£¬¼´9MHz
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
// ÅäÖÃADC ͨµÀµÄת»»Ë³ÐòºÍ²ÉÑùʱ¼ä
ADC_RegularChannelConfig(macADCx, macADC_CHANNEL1, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(macADCx, macADC_CHANNEL2, 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(macADCx, macADC_CHANNEL3, 3, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(macADCx, macADC_CHANNEL4, 4, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(macADCx, macADC_CHANNEL5, 5, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(macADCx, macADC_CHANNEL6, 6, ADC_SampleTime_55Cycles5);
// ʹÄÜADC DMA ÇëÇó
ADC_DMACmd(macADCx, ENABLE);
// ¿ªÆôADC £¬²¢¿ªÊ¼×ª»»
ADC_Cmd(macADCx, ENABLE);
// ³õʼ»¯ADC У׼¼Ä´æÆ÷
ADC_ResetCalibration(macADCx);
// µÈ´ýУ׼¼Ä´æÆ÷³õʼ»¯Íê³É
while(ADC_GetResetCalibrationStatus(macADCx));
// ADC¿ªÊ¼Ð£×¼
ADC_StartCalibration(macADCx);
// µÈ´ýУ׼Íê³É
while(ADC_GetCalibrationStatus(macADCx));
// ÓÉÓÚûÓвÉÓÃÍⲿ´¥·¢£¬ËùÒÔʹÓÃÈí¼þ´¥·¢ADCת»»
ADC_SoftwareStartConvCmd(macADCx, ENABLE);
}
/**
* @brief ADC³õʼ»¯
* @param ÎÞ
* @retval ÎÞ
*/
void ADCx_Init(void)
{
ADCx_GPIO_Config();
ADCx_Mode_Config();
}
/*********************************************END OF FILE**********************/
|
|