/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / read adc on primer2

Username:     
Password:     
             

Forum

# 1   2009-11-20 15:06:32 read adc on primer2

victor_passe
New member
Registered: 2009-04-13
Posts: 8

read adc on primer2

Hello, can anyone tell me how to read the adc value from the 2 channels that are connencted to the extention connector?

And please, dont just say look at the bat/temp measurements code, i dont get how it works.


How hard can it be to read the adc? Can you do it without dma?
It should be as easy as this:
my_value=adc_read(channel13);

Offline

 

# 2   2009-11-22 09:48:38 read adc on primer2

ntrf.zns
Member
From: Belgorod, Russia
Registered: 2009-11-01
Posts: 134

Re: read adc on primer2

I can't be that simple. ACD needs time to measure the exact value on input. This means you still need interrupt, event, direct memory access or simple wait.

Sorry, i'm not aware of the exact API.

Offline

 

# 3   2009-11-23 08:01:57 read adc on primer2

yrt
Administrator
From: Grenoble-France
Registered: 2008-06-11
Posts: 520
Website

Re: read adc on primer2

Hi,

For ADC measurement, there is two ways : continous conversion mode that needs DMA, and single conversion mode.
With the second solution, you have to configure and calibrate the ADC, and then to launch a conversion when needed, while with the first solution, the DMA updates automatically the memory with the converted value.
For the DMA solution, you can take a look to the "ADC1_DMA" example from the ST library examples.
Foth the single conversion solution, you can find hereafter a program example for the extension Primer "Cx_ADC1" signal acquisition.


Initialization :
/*******************************************************************************
* Function Name  : ADConverter_Init
* Description    : Initialize ADC for measuring.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void ADConverter_Init( void )
    {
    ADC_InitTypeDef  ADC_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
   
    // Enable PORT clock
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
   
    /* Configure pin as analog input ---------------*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init(GPIO_C, &GPIO_InitStructure);
   
    /* Enable ADC1 clock */
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1, ENABLE );

    /* ADC1 Configuration ------------------------------------------------------*/
    ADC_InitStructure.ADC_Mode                = ADC_Mode_Independent;
    ADC_InitStructure.ADC_ScanConvMode        = DISABLE;
    ADC_InitStructure.ADC_ContinuousConvMode  = DISABLE;
    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 Channel 14 */
    ADC_RegularChannelConfig( ADC1, ADC_Channel_14,  1, ADC_SampleTime_239Cycles5);

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

    }

Acquisition :

/*******************************************************************************
* Function Name  : Get_Measure
* Description    : Get the ADC value.
* Input          : None.
* Output         : Value in points from 0 to 4096.
* Return         : None.
*******************************************************************************/
u16 Get_Measure()
    {
    // Start ADC1 Software Conversion
    ADC_SoftwareStartConvCmd( ADC1, ENABLE );

     // Wait for end of conversion
    while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
       
    // Return the conversion value
    return (ADC_GetConversionValue(ADC1));
    }


Yves

Last edited by yrt (2009-11-23 08:04:24)

Offline

 

# 4   2009-12-22 21:53:42 read adc on primer2

Chr1s
New member
Registered: 2009-11-24
Posts: 1

Re: read adc on primer2

Shouldn't the code read:

/* Configure pin as analog input ---------------*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

not pin 14?

Also, I have succeeded in reading the ADC when using Pin 4 (ADC channel 14), supplying a signal via the extension connector, but I can't get it to work with Pin 5 (ADC channel 15), with almost identical code. Any suggestions gratefully received.

Offline

 

# 5   2009-12-25 16:36:30 read adc on primer2

yrt
Administrator
From: Grenoble-France
Registered: 2008-06-11
Posts: 520
Website

Re: read adc on primer2

Oups !

You're right about the pin number (PC4).

Concerning the PC5 (Cx_ADC2) there is no reasons for no work with the same type of program (no special alternative function other than ADC). Do you use the 2 channels at the same time, and with the same ADC ?

Yves

Offline

 

# 6   2010-03-10 13:29:28 read adc on primer2

thefly
New member
Registered: 2010-01-27
Posts: 8

Re: read adc on primer2

Hi Yves,

how can I implement your code for Primer 2? Where can I see the returned value of Get_Measure()?

Thanks

Last edited by thefly (2010-03-10 15:12:57)

Offline

 

# 7   2010-03-11 07:24:23 read adc on primer2

yrt
Administrator
From: Grenoble-France
Registered: 2008-06-11
Posts: 520
Website

Re: read adc on primer2

Hi,

Yes, as we said above, the pin is PC4.
What do you mean by "Implement your code for Primer2" ? Do you want to implement this code with the CircleOS ?
This example runs without CircleOS, which already uses ADC1 for battery voltage and touschscreen acquisition.
It should be adapted to be implemented into a CircleOS application (to add channels into DMA acquisition, or to use the ADC2).

Yves

Offline

 

# 8   2011-12-13 15:22:57 read adc on primer2

antoniocas
New member
Registered: 2010-12-10
Posts: 2

Re: read adc on primer2

Hi
This is my first proyect and i´m trying make an ADC adquisition like the example, but when i add the includes i have this message
" C:\Users\Usuario\Downloads\proyecto\interface\Application.c:13:23: fatal error: stm32f10x.h: No such file or directory"

I don´t know if the problem is that i need more includes or change another thing.

I had added this includes

#include "circle_api.h"
#include "time.h"
#include "stdio.h"
#include "signal.h"
#include "stm32f10x.h"
#include "stm32f10x_adc.h"
#include "stm32f10x_dma.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "misc.h"

Thanks

Antonio

Offline

 

Board footer