2015-04-07 04:26 AM
Hello
I have a STM32F105RC which i connect a Potentiometer in pin PB8. I have a external clock of 25MHz I configure the ADC with this functionErrorStatus HSEStartUpStatus_Anal;
ADC_InitTypeDef ADC_InitStructure;
void INputAnal_Init(INPUTANALs_TypeDef INputAnal)
{
// System clocks configuration ---------------------------------------------
//////////////////////////////////////////////////////////////////////////////
{
// RCC system reset(for debug purpose)
RCC_DeInit();
// Enable HSE
RCC_HSEConfig(RCC_HSE_ON);
// Wait till HSE is ready
HSEStartUpStatus_Anal = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus_Anal == SUCCESS)
{
// Enable Prefetch Buffer
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
// Flash 2 wait state
FLASH_SetLatency(FLASH_Latency_2);
// HCLK = SYSCLK
RCC_HCLKConfig(RCC_SYSCLK_Div1);
// PCLK2 = HCLK
RCC_PCLK2Config(RCC_HCLK_Div1);
// PCLK1 = HCLK/2
RCC_PCLK1Config(RCC_HCLK_Div2);
// ADCCLK = PCLK2/4
RCC_ADCCLKConfig(RCC_PCLK2_Div4);
// Configure PLLs ********************************************************
// PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz
RCC_PREDIV2Config(RCC_PREDIV2_Div5);
RCC_PLL2Config(RCC_PLL2Mul_8);
// Enable PLL2
RCC_PLL2Cmd(ENABLE);
// Wait till PLL2 is ready
while (RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET) { }
// PLL configuration: PLLCLK = (PLL2 / 5) * 7 = 56 MHz
RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5);
RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_7);
// Enable PLL
RCC_PLLCmd(ENABLE);
// Wait till PLL is ready
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { }
// Select PLL as system clock source
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
// Wait till PLL is used as system clock source
while(RCC_GetSYSCLKSource() != 0x08) { }
}
// Enable DMA1 clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
// Enable peripheral clocks --------------------------------------------------
// Enable ADC1 and GPIOB clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);
}
//////////////////////////////////////////////////////////////////////////////
// GPIO configuration --------------------------------------------------------
// Configure pins as Analog ////////////////////////////////////////////////
GPIO_InitTypeDef GPIO_InitStruct;
// Enable or disable APB2 peripheral clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// Configure GPIO pin : PB
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStruct);
//////////////////////////////////////////////////////////////////////////////
// NVIC configuration ------------------------------------------------------
NVIC_InitTypeDef NVIC_InitStructure;
// Configure and enable ADC interrupt
NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//////////////////////////////////////////////////////////////////////////////
// ADC1 configuration ------------------------------------------------------
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
// ADC1 regular channels configuration
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_28Cycles5);
//////////////////////////////////////////////////////////////////////////////
// Enable ADC1 EOC interupt
ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
//////////////////////////////////////////////////////////////////////////////
// Enable ADC1
ADC_Cmd(ADC1, ENABLE);
// Enable ADC1 reset calibaration register
ADC_ResetCalibration(ADC1);
// Check the end of ADC1 reset calibration register
while(ADC_GetResetCalibrationStatus(ADC1));
// Start ADC1 calibaration
ADC_StartCalibration(ADC1);
// Check the end of ADC1 calibration
while(ADC_GetCalibrationStatus(ADC1));
// Start ADC1 Software Conversion
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
// while (1)
// {
// }
//////////////////////////////////////////////////////////////////////////////
}
And I try read the pin port value with this code
/**
* @brief This function handles ADC1 and ADC2 global interrupts requests.
* @param None
* @retval None
*/
extern __IO uint16_t ADC1ConvertedValue;
void ADC1_2_IRQHandler(void)
{
/* Clear ADC1 EOC pending interrupt bit */
ADC_ClearITPendingBit(ADC1, ADC_IT_EOC);
/* Get converted value of Channel14 converted by ADC1 */
ADC1ConvertedValue = ADC_GetConversionValue(ADC1);
}
I try although this another version
Config
/**
* @brief Initializes ADC, used to detect motion of Joystick available on
* adafruit 1.8'' TFT shield.
* @param None
* @retval None
*/
void STM_ADC_Config(void)
{
RCC_ClocksTypeDef RCC_Clocks;
/* SysTick end of count event each 1ms */
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
/* Configure the ADC clock (must not exceed 14MHz) */
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
/* Enable ADC1 clock */
RCC_APB2PeriphClockCmd(ADC_CLK, ENABLE);
/* ADC Channel configuration */
/* ADC GPIO clock enable */
RCC_APB2PeriphClockCmd(ADC_GPIO_CLK, ENABLE);
/* Configure ADC1 Channel8 as analog input */
GPIO_InitStructure.GPIO_Pin = ADC_GPIO_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADC_GPIO_PORT, &GPIO_InitStructure);
/* ADC1 configuration */
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 = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel8 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1))
{
}
/* Start ADC1 calibration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
read
byte STM_Get_JOYState(void)
{
byte state;
uint16_t KeyConvertedValue = 0;
KeyConvertedValue = ADC_GetConversionValue(ADCx);
if((KeyConvertedValue > 2010) && (KeyConvertedValue < 2090))
{
state = 1;//JOY_UP;
}
else if((KeyConvertedValue > 680) && (KeyConvertedValue < 780))
{
state = 2;//JOY_RIGHT;
}
else if((KeyConvertedValue > 1270) && (KeyConvertedValue < 1350))
{
state = 3;//JOY_SEL;
}
else if((KeyConvertedValue > 50) && (KeyConvertedValue < 130))
{
state = 5;//JOY_DOWN;
}
else if((KeyConvertedValue > 3680) && (KeyConvertedValue < 3760))
{
state = 5;//JOY_LEFT;
}
else
{
state = 6;//JOY_NONE;
}
/* Loop while a key is pressed */
if(state != 0)
{
while(KeyConvertedValue < 4000)
{
KeyConvertedValue = ADC_GetConversionValue(ADCx);
}
}
/* Return the code of the Joystick key pressed*/
return state;
}
But i don't read nothing i read always the same valeu.
Some One can help me?
#adc #stm32f105