#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "timer.h"
#include "global.h"
#include "bsp_init.h"
#include "user_config.h"
#include "adc.h"
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
ADC_HandleTypeDef ADC1_Handler;//ADC¾ä±ú
ADC_HandleTypeDef ADC3_Handler;//ADC¾ä±ú
//3õê¼»ˉËùóDADC
//ch: ADC_channels
//í¨μàÖμ 0~16è¡Öμ·¶Î§Îa£oADC_CHANNEL_0~ADC_CHANNEL_16
void MY_ADC_Init1(void)
{
ADC1_Handler.Instance=ADC1;
ADC1_Handler.Init.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV4; //4·ÖÆ죬ADCCLK=PCLK2/4=108/4=27MHZ
ADC1_Handler.Init.Resolution=ADC_RESOLUTION_12B; //12λÄ£ê½
ADC1_Handler.Init.DataAlign=ADC_DATAALIGN_RIGHT; //óò¶ÔÆë
ADC1_Handler.Init.ScanConvMode=DISABLE; //·Çé¨ÃèÄ£ê½
ADC1_Handler.Init.EOCSelection=DISABLE; //1رÕEOCÖD¶Ï
ADC1_Handler.Init.ContinuousConvMode=DISABLE; //1رÕá¬Dø×a»»
ADC1_Handler.Init.NbrOfConversion=1; //1¸ö×a»»Ôú1æÔòDòáDÖD ò2¾íêÇÖ»×a»»1æÔòDòáD1
ADC1_Handler.Init.DiscontinuousConvMode=DISABLE; //½ûÖ12»á¬Dø2éÑùÄ£ê½
ADC1_Handler.Init.NbrOfDiscConversion=0; //2»á¬Dø2éÑùí¨μàêyÎa0
ADC1_Handler.Init.ExternalTrigConv=ADC_SOFTWARE_START; //èí¼t′¥·¢
ADC1_Handler.Init.ExternalTrigConvEdge=ADC_EXTERNALTRIGCONVEDGE_NONE;//ê1óÃèí¼t′¥·¢
ADC1_Handler.Init.DMAContinuousRequests=DISABLE; //1رÕDMAÇëÇó
HAL_ADC_Init(&ADC1_Handler); //3õê¼»ˉ
}
void MY_ADC_Init3(void)
{
ADC3_Handler.Instance=ADC3;
ADC3_Handler.Init.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV4; //4·ÖÆ죬ADCCLK=PCLK2/4=108/4=27MHZ
ADC3_Handler.Init.Resolution=ADC_RESOLUTION_12B; //12λÄ£ê½
ADC3_Handler.Init.DataAlign=ADC_DATAALIGN_RIGHT; //óò¶ÔÆë
ADC3_Handler.Init.ScanConvMode=DISABLE; //·Çé¨ÃèÄ£ê½
ADC3_Handler.Init.EOCSelection=DISABLE; //1رÕEOCÖD¶Ï
ADC3_Handler.Init.ContinuousConvMode=DISABLE; //1رÕá¬Dø×a»»
ADC3_Handler.Init.NbrOfConversion=1; //1¸ö×a»»Ôú1æÔòDòáDÖD ò2¾íêÇÖ»×a»»1æÔòDòáD1
ADC3_Handler.Init.DiscontinuousConvMode=DISABLE; //½ûÖ12»á¬Dø2éÑùÄ£ê½
ADC3_Handler.Init.NbrOfDiscConversion=0; //2»á¬Dø2éÑùí¨μàêyÎa0
ADC3_Handler.Init.ExternalTrigConv=ADC_SOFTWARE_START; //èí¼t′¥·¢
ADC3_Handler.Init.ExternalTrigConvEdge=ADC_EXTERNALTRIGCONVEDGE_NONE;//ê1óÃèí¼t′¥·¢
ADC3_Handler.Init.DMAContinuousRequests=DISABLE; //1رÕDMAÇëÇó
HAL_ADC_Init(&ADC3_Handler); //3õê¼»ˉ
}
//ADCμ×2ãÇy¶ˉ£¬òy½ÅÅäÖã¬ê±Öóê1Äü
//′Ëoˉêy»á±»HAL_ADC_Init()μ÷óÃ
//hadc:ADC¾ä±ú
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_ADC1_CLK_ENABLE(); //ê1ÄüADC1ê±Öó
__HAL_RCC_ADC3_CLK_ENABLE(); //ê1ÄüADC3ê±Öó
__HAL_RCC_GPIOB_CLK_ENABLE(); //¿aÆôGPIOBê±Öó
GPIO_Initure.Pin=GPIO_PIN_0; //PB0 adc ch:8
GPIO_Initure.Mode=GPIO_MODE_ANALOG; //Ä£Äa
GPIO_Initure.Pull=GPIO_NOPULL; //2»′øéÏÏÂà-
HAL_GPIO_Init(GPIOB,&GPIO_Initure);
__HAL_RCC_GPIOC_CLK_ENABLE(); //¿aÆôGPIOCê±Öó
GPIO_Initure.Pin=GPIO_PIN_1 | GPIO_PIN_5 |GPIO_PIN_4;
GPIO_Initure.Mode=GPIO_MODE_ANALOG; //Ä£Äa
GPIO_Initure.Pull=GPIO_NOPULL; //2»′øéÏÏÂà-
HAL_GPIO_Init(GPIOC,&GPIO_Initure);
__HAL_RCC_GPIOF_CLK_ENABLE(); //¿aÆôGPIOFê±Öó
GPIO_Initure.Pin=GPIO_PIN_6 | GPIO_PIN_7 |GPIO_PIN_9; //PB0 adc ch:8
GPIO_Initure.Mode=GPIO_MODE_ANALOG; //Ä£Äa
GPIO_Initure.Pull=GPIO_NOPULL; //2»′øéÏÏÂà-
HAL_GPIO_Init(GPIOF,&GPIO_Initure);
}
//»ñμÃADCÖμ
//ch: í¨μàÖμ 0~16£¬è¡Öμ·¶Î§Îa£oADC_CHANNEL_0~ADC_CHANNEL_16
//·μ»ØÖμ:×a»»½á1û
u16 Get_Adc1(u32 ch)
{
ADC_ChannelConfTypeDef ADC1_ChanConf;
ADC1_ChanConf.Channel=ch; //í¨μà
ADC1_ChanConf.Rank=1; //1¸öDòáD
ADC1_ChanConf.SamplingTime=ADC_SAMPLETIME_480CYCLES; //2éÑùê±¼ä
ADC1_ChanConf.Offset=0;
HAL_ADC_ConfigChannel(&ADC1_Handler,&ADC1_ChanConf); //í¨μàÅäÖÃ
HAL_ADC_Start(&ADC1_Handler); //¿aÆôADC
HAL_ADC_PollForConversion(&ADC1_Handler,10); //ÂÖÑˉ×a»»
return (u16)HAL_ADC_GetValue(&ADC1_Handler); //·μ»Ø×î½üò»′ÎADC11æÔò×éμÄ×a»»½á1û
}
u16 Get_Adc3(u32 ch)
{
ADC_ChannelConfTypeDef ADC3_ChanConf;
ADC3_ChanConf.Channel=ch; //í¨μà
ADC3_ChanConf.Rank=1; //1¸öDòáD
ADC3_ChanConf.SamplingTime=ADC_SAMPLETIME_480CYCLES; //2éÑùê±¼ä
ADC3_ChanConf.Offset=0;
HAL_ADC_ConfigChannel(&ADC3_Handler,&ADC3_ChanConf); //í¨μàÅäÖÃ
HAL_ADC_Start(&ADC3_Handler); //¿aÆôADC
HAL_ADC_PollForConversion(&ADC3_Handler,10); //ÂÖÑˉ×a»»
return (u16)HAL_ADC_GetValue(&ADC3_Handler); //·μ»Ø×î½üò»′ÎADC11æÔò×éμÄ×a»»½á1û
}
//»ñè¡Ö¸¶¨í¨μàμÄ×a»»Ö죬è¡times′Î,è»oóƽ¾ù
//times:»ñè¡′Îêy
//·μ»ØÖμ:í¨μàchμÄtimes′Î×a»»½á1ûƽ¾ùÖμ
u16 Get_Adc_Average1(u32 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
temp_val+=Get_Adc1(ch);
delay_ms(5);
}
return temp_val/times;
}
u16 Get_Adc_Average3(u32 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
temp_val+=Get_Adc3(ch);
delay_ms(5);
}
return temp_val/times;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
你也有这问题吗
一周热门 更多>