Skip to main content
sumalatha
Associate II
October 3, 2010
Question

how to count the 12 bit ADC count

  • October 3, 2010
  • 11 replies
  • 2577 views
Posted on October 03, 2010 at 11:41

how to count the 12 bit ADC count

    This topic has been closed for replies.

    11 replies

    Andrew Neil
    Super User
    May 17, 2011
    Posted on May 17, 2011 at 14:09

    and sending it to Hyperterminal (or any other application) is a matter of using the USART - again, see examples provided with the library...

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    raptorhal2
    Lead
    May 17, 2011
    Posted on May 17, 2011 at 14:09

    Unfortunately ''count'' is not a very helpful term. When the ADC converts an input analog channel, it returns an unsigned integer value. For zero or negative input volts, it returns a value of zero. For 3.3 or higher volts, it returns a value of 4095. (Don't go too negative or too high to avoid damaging the ADC). In between zero and 3.3 volts, the ADC returns a corresponding ratioed value.

    No ''counting'' is required, just a simple read.

    Look at the examples in the Library for further information on how to convert and read a channel.

    If I have misunderstood your question, please restate.

    Cheers, Hal

    Andrew Neil
    Super User
    May 17, 2011
    Posted on May 17, 2011 at 14:09

    ''i didnt see the count value in hyperterminal''

    Nothing in the code you've shown writes anything to the USART!

    Remember: to the STM32, Hyperterminal is irrelevant!

    All the STM32 knows is that it transmits bytes through the USART - where those bytes go from there is entirely immaterial to the STM32.

    See:

    [DEAD LINK /public/STe2ecommunities/mcu/Lists/ARM%20CortexM3%20STM32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/ARM CortexM3 STM32/Hyperterminal is not the only terminal program!!&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000626BE2B829C32145B9EB5739142DC17E]https://my.st.com/public/STe2ecommunities/mcu/Lists/ARM%20CortexM3%20STM32/Flat.aspx?RootFolder=%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fARM%20CortexM3%20STM32%2fHyperterminal%20is%20not%20the%20only%20terminal%20program%21%21&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000626BE2B829C32145B9EB5739142DC17E

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    sumalatha
    sumalathaAuthor
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 14:09

    Thanx for replying ...

    actualy in my application my data (Analog temperature value ) i want read this so i am confg here   

    RCC_Configuration();           

     SYSTICKS_Configuration();

      GPIO_Config();

        TIM2_Configuration();

        NVIC_Configuration();

        USART_Configuration(DBG_USART,115200);

       ADC_Config();

    for read this channel....

    u16 readADC1(u8 channel)

    {

      ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_55Cycles5);

      // Start the conversion

      ADC_SoftwareStartConvCmd(ADC1, ENABLE);

      // Wait until conversion completion

      while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);

      // Get the conversion value

      return ADC_GetConversionValue(ADC1);

    }

    but i didnt see the count value in hyperterminal so plz tel me what is the thing is wrong

    thanx

    Andrew Neil
    Super User
    May 17, 2011
    Posted on May 17, 2011 at 14:09

    How have you verified that your WriteSerial() routine actually works?

    eg, have you tried it with a simple ''Hello, world'' program?

    Remember that serial comms is very, very slow relative the CPU execution - how do you ensure that the USART is ready before you call WriteSerial() again...?

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    sumalatha
    sumalathaAuthor
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 14:09

    thanx andrew  in main ()

    while(1)

           {

          //u16 readADC1(u8 channel);

            ADC_val=ADC_GetConversionValue(ADC1);

            WriteSerial(DBG_USART,''count '');

           Second_Delay(3);

            WriteSerial(DBG_USART,ADC_val);

            WriteSerial(DBG_USART,''\r\n'');

    Andrew Neil
    Super User
    May 17, 2011
    Posted on May 17, 2011 at 14:09

    You still haven't showed the code of your WriteSerial() function!

    And you still haven't said how you know that it actually works at all!

    But this doesn't look right:

    Here, you are passing a string to it: 

        WriteSerial( DBG_USART, '' hello \r\n'' );

     

    But here, you are passing a u16 integer value:

           

    WriteSerial( DBG_USART, ADC_val );

    That can't be right, surely??!

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    sumalatha
    sumalathaAuthor
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 14:09

    Dear Andrew….Thanx for reply…

    Actually I want to disply the ADC count on USART

     

    and this my code

    int main(void)

    {

    u16 ADC_val;

     

       

    RCC_Configuration();

                                     

       

    SYSTICKS_Configuration();

       

    GPIO_Config();

       

    Delay_Ms(500);

       

    TIM2_Configuration();

       

    NVIC_Configuration();

       

    USART_Configuration(DBG_USART,115200);

       

    Delay_Ms(500);

       

    WriteSerial(DBG_USART,'' hello \r\n'');

     

       

    Delay_Ms(500);

       

    ADC_Config();

       

    u16 readADC1(u8 channel);

     

    while(1)

          

    {

         

    //u16 readADC1(u8 channel);

           

    ADC_val=ADC1ConvertedValue;

           

    //strcat(ADC_val,'':'');

           

    //sprif(ADC_val,ADC_val);

           

    WriteSerial(DBG_USART,''SJA '');

          

    Second_Delay(3);

           

    WriteSerial(DBG_USART,ADC_val);

           

    WriteSerial(DBG_USART,''\r\n'');

          

    //WriteSerial(DBG_USART,''ADC_val'');

           

    }

           

    }

    And ADC config

    void ADC_Config(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_None;

     

    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

     

    ADC_InitStructure.ADC_NbrOfChannel = 1;

     

    ADC_Init(ADC1, &ADC_InitStructure);

     

    /* ADC1 regular channel5configuration */

     

    ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_55Cycles5);

     

    /* Enable ADC1 DMA */

     

    //ADC_DMACmd(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));

        

     

    /* Start ADC1 Software Conversion */

     

    ADC_SoftwareStartConvCmd(ADC1, ENABLE);

     

    while (1)

     

    {

     

    }

     

    }

     

    u16 readADC1(u8 channel)

    {

     

    ADC_RegularChannelConfig(ADC1, 5, 1, ADC_SampleTime_55Cycles5);

     

    // Start the conversion

     

    ADC_SoftwareStartConvCmd(ADC1, ENABLE);

     

    // Wait until conversion completion

     

    while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);

     

    // Get the conversion value

     

    //return ADC_GetConversionValue(ADC1);

     

    ADC1ConvertedValue = ADC_GetConversionValue(ADC1);

    }

    // -------------- The ADC IRQ handler

     

    --------------

    /*

    void ADC1_2_IRQHandler(void)

    {

       

    // Clear ADC1 EOC pending interrupt bit

         

    ADC_ClearITPendingBit(ADC1, ADC_IT_EOC);

       

    // Get converted value of Channelxx converted by ADC1

         

    ADC1ConvertedValue = ADC_GetConversionValue(ADC1);

       

    cADC1_BUF1[cnt++]=ADC1ConvertedValue;

       

    WriteSerial(DBG_USART,cADC1_BUF1);

       

    if(cnt==128)

           

    {

           

    cnt=0;

           

    }*/

    How have you verified that your WriteSerial() routine actually works?

    In hyper

    only hello is displayed nt ADC_val

    Y it is not displayed? Iam confusing plz help me.:-).

    Andrew Neil
    Super User
    May 17, 2011
    Posted on May 17, 2011 at 14:09

    Sorry - stupid forum playing up again!

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Andrew Neil
    Super User
    May 17, 2011
    Posted on May 17, 2011 at 14:09

    ''Perhaps you should convert the number into ASCII before pushing it out to the serial port?''

     

    Indeed!

    The commented-out lines suggest that she might have been trying to do that:

           //strcat(ADC_val,'':'');

           

    //sprif(ADC_val,ADC_val);

    But there seem to be some basic problems there with (lack of) understanding how strings work in the 'C' programming language...?

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.