Topic : please help me ADC sampling rate 1000 ksp/s every 20ms is not ok

Forum : ARM

Original Post
Post Information Post
August 10, 2013 - 4:45pm
Guest

i used stm32f107vct6 to kept adc sampling rate 1000 ksp/s every 20ms in not ok

i don't know code is anywheare is wrong please help me thank you

/*---------------------------------- Includes -------------------------------------*/

#include "stm32f10x.h"
#include
#include
#include

/*------------------------------------- Delay -------------------------------------*/

void delay(unsigned long ms)
{
volatile unsigned long i,j;
for(i=0;iLibraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

/*------------------------ Private function prototypes ----------------------------*/

void RCC_setup(void);
void GPIO_setup(void);
void DMA_setup(void);
void ADC_setup(void);
void NVIC_setup(void);
void TIM1_setup(void);
void USART1_setup(void);
void USART2_setup(void);
/*------------------------------- RCC Function ------------------------------------*/

void RCC_setup(void)
{
RCC_ClocksTypeDef RCC_ClockFreq;
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if (HSEStartUpStatus == SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
RCC_PREDIV2Config(RCC_PREDIV2_Div5);
RCC_PLL2Config(RCC_PLL2Mul_8);
RCC_PLL2Cmd(ENABLE);
while (RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET)
{}
RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5);
RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while (RCC_GetSYSCLKSource() != 0x08)
{}
}
else
{
while (1)
{}
}

/*Initial System Clock Complete Start Initial Peripheral Clock*/
RCC_GetClocksFreq(&RCC_ClockFreq);

/* Enable USART1 $ USART2 clock */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

/* Enable GPIOs clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC |
RCC_APB2Periph_GPIOD |
RCC_APB2Periph_GPIOE |
RCC_APB2Periph_AFIO, ENABLE);

/* Enable DMA1 and DMA2 clocks */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);

/* Enable ADC1, ADC2, ADC3 clocks */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2,ENABLE);

/* Enable TIM1 clock */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_TIM1, ENABLE);
}

/*------------------------------- GPIO Function ------------------------------------*/

void GPIO_setup(void)
{

GPIO_InitTypeDef GPIO_InitStructure;

// Configure PC6,PC7,PC8,PC9,PC10,PC11,PC12,PC13,PC14,PC15 LED On/Off

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 |
GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOE,&GPIO_InitStructure);

/* Configure PC.02, PC.03 and PC.04 (ADC Channel12, ADC Channel13 and
ADC Channel14) as analog inputs */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);

/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO, ENABLE);

/* Enable the USART1 Pins Software Remapping */
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);

/* Enable USART */
USART_Cmd(USART1, ENABLE);

/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);

/* Enable the USART2 Pins Software Remapping */
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);

/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);

/* Enable USART */
USART_Cmd(USART2, ENABLE);

}
/*------------------------------- DMA Function ------------------------------------*/

void DMA_setup(void)
{ DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC_RegularConvertedValueTab;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 2; //Buffer size (3 because we using 3 channels)
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_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);

/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);

}

/*------------------------------- ADC Function ------------------------------------*/

void ADC_setup(void)
{ ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 2;
ADC_Init(ADC1, &ADC_InitStructure);

/* ADC1 regular channel14 configuration */

ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 2, ADC_SampleTime_55Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);

/* Enable ADC1 external trigger */
ADC_ExternalTrigConvCmd(ADC1, 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));

/* TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE);

/* TIM1 main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);


/* Test on channel1 transfer complete flag */
while(!DMA_GetFlagStatus(DMA1_FLAG_TC1));

/* Clear channel1 transfer complete flag */
DMA_ClearFlag(DMA1_FLAG_TC1);

/* TIM1 counter disable */
TIM_Cmd(TIM1, DISABLE);

// /* Start ADC1 Software Conversion */
// ADC_SoftwareStartConvCmd(ADC1, ENABLE);

}

/*------------------------------- TIM1 Function ------------------------------------*/
void TIM1_setup(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* TIM1 channel1 configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0x7F;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
}

/*------------------------------- NVIC Function ------------------------------------*/
void NVIC_setup(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

/* Enable ADC1_2 IRQChannel */
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);
}

/*------------------------------- USART1 Function ------------------------------------*/

void USART1_setup(void)
{
USART_InitTypeDef USART_InitStructure;
// Enable USART1 clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
// USART1 configured
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART1, &USART_InitStructure); // Configure the USART1
USART_Cmd(USART1, ENABLE); // Enable USART1

}

//------------------------------ Function USART1 send 1 character ------------------------------------//
void usart1_putc(unsigned char c)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET); // Wait until transmition ready
USART_SendData(USART1,(int)c); // Send character
}

//------------------------------ Function USART1 send string -----------------------------------------//
void usart1_puts(unsigned char *s)
{
while(*s) // Check end of string
{
usart1_putc(*s++); // Send charracter 1 time
}
}

//------------------------------ Function USART1 wait character --------------------------------------//
int usart1_getc()
{
while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==RESET); // Wait until receive data
return(USART_ReceiveData(USART1)); // Return character
}

//------------------------------ Function USART2 Configuration -----------------------------------------//

void USART2_setup(void)
{
USART_InitTypeDef USART_InitStructure;
// Enable USART1 clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
// USART1 configured
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART2, &USART_InitStructure); // Configure the USART1
USART_Cmd(USART2, ENABLE); // Enable USART1

}

//------------------------------ Function USART1 send 1 character ------------------------------------//
void usart2_putc(unsigned char c)
{
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET); // Wait until transmition ready
USART_SendData(USART2,(int)c); // Send character
}

//------------------------------ Function USART1 send string -----------------------------------------//
void usart2_puts(unsigned char *s)
{
while(*s) // Check end of string
{
usart2_putc(*s++); // Send charracter 1 time
}
}

//------------------------------ Function USART1 wait character --------------------------------------//
int usart2_getc()
{
while(USART_GetFlagStatus(USART2,USART_FLAG_RXNE)==RESET); // Wait until receive data
return(USART_ReceiveData(USART2)); // Return character
}
/*-------------------------------- Main Program -----------------------------------*/

int main()
{
unsigned long adc_val=0,percentage,percentage1,percentage2;

RCC_setup();
NVIC_setup();
GPIO_setup();
TIM1_setup();
DMA_setup();
ADC_setup();

USART1_setup();
USART2_setup();
usart1_puts("ADC Read \r\n");
usart2_puts("ADC Read \r\n");
while(1)
{
GPIO_WriteBit(GPIOE,GPIO_Pin_8,1);
GPIO_WriteBit(GPIOE,GPIO_Pin_9,1);
GPIO_WriteBit(GPIOE,GPIO_Pin_10,1);
GPIO_WriteBit(GPIOE,GPIO_Pin_11,1);
GPIO_WriteBit(GPIOE,GPIO_Pin_12,1);
GPIO_WriteBit(GPIOE,GPIO_Pin_13,1);
GPIO_WriteBit(GPIOE,GPIO_Pin_14,1);
GPIO_WriteBit(GPIOE,GPIO_Pin_15,1);
delay(500);
GPIO_WriteBit(GPIOE,GPIO_Pin_8,0);
GPIO_WriteBit(GPIOE,GPIO_Pin_9,0);
GPIO_WriteBit(GPIOE,GPIO_Pin_10,0);
GPIO_WriteBit(GPIOE,GPIO_Pin_11,0);
GPIO_WriteBit(GPIOE,GPIO_Pin_12,0);
GPIO_WriteBit(GPIOE,GPIO_Pin_13,0);
GPIO_WriteBit(GPIOE,GPIO_Pin_14,0);
GPIO_WriteBit(GPIOE,GPIO_Pin_15,0);
delay(500);

// ADC_DMACmd(ADC1,ENABLE);
// while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==RESET);
// adc_val = ADC_GetConversionValue(ADC1);
// percentage = (adc_val);


percentage = ADC_RegularConvertedValueTab[0];
percentage1 = ADC_RegularConvertedValueTab[1];

//sprintf(str,"%d%",adc_val); // Convert percentage value to string
sprintf(str,"%d%",percentage);

sprintf(str1,"%d%",percentage1);

usart2_puts(str); // Send character from buffer
usart2_puts(",");
usart2_puts(str1);
usart2_puts("\r\n");
}

}