Thread: ADC Conversion to Voltage Value

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    6

    ADC Conversion to Voltage Value

    Good Morning, I am currently programming a ARM Nucleo board to read a ADC value and convert the value to decimal.

    I am using a 12 bit ADC which is then (hopefully) being converted into a float for me to eventually output. However this conversion does not seem to be working very well.

    Code:
    #include <stm32f4xx.h>
    #include "PLL_Config.c"
    #include "ADC.h"
    #include "DAC.h"
    #include "lcd.h"
    
    
    
    
    unsigned short ADC_DATA;
    int main(void)
    
    {
        PLL_Config();
        SystemCoreClockUpdate();
        
        
        initLCD();
    
    
        
        init_ADC();        //config ADC
        init_DAC();        //config DAC
        
        
        while (1){
    
    
            ADC_DATA=read_adc();            //read value from ADC
            output_dac(ADC_DATA);    //send straight to DAC (DAC pin should replicate ADC pin)    
            lcd_delayus(200000);
        }
        
    }
    void init_ADC(void)
    {
        RCC->AHB1ENR|=RCC_AHB1ENR_GPIOCEN;    //GPIOC clock enable
        ADC_input_port->MODER|=(3u<<(2*ADC_input_pin));    //ADC input pin is analogue mode
        
        RCC->APB2ENR|=RCC_APB2ENR_ADC1EN;        //ADC clock enable
        ADC1->SQR1&=~ADC_SQR1_L;                        //set number of conversions per sequence to 1
        ADC1->SQR3&=~ADC_SQR3_SQ1;                    //clear channel select bits
        ADC1->SQR3|=ADC_Channel;                        //set channel
        ADC1->CR2|=ADC_CR2_ADON;                        //enable ADC
        
    }
    
    
    
    
    unsigned short read_adc(void)
    {
        ADC1->CR2|=ADC_CR2_SWSTART;                //start ADC conversion
        while((ADC1->SR&ADC_SR_EOC)==0){__NOP();}    //wait for ADC conversion complete
        return ADC1->DR;                                    //return converted value
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,687
    Since, you did not post any float conversion code, it will be hard to help you. Even if I wanted to help you.

    I suggest you state what is failing to work! As in this is the output but I expected this!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A/D Voltage, write the value on a serial port
    By Enrico in forum C Programming
    Replies: 2
    Last Post: 03-24-2015, 10:16 AM
  2. What is needed to output voltage signals from PC
    By Lesshardtofind in forum Networking/Device Communication
    Replies: 6
    Last Post: 07-18-2014, 01:16 AM
  3. Convert bits to voltage levels...
    By Dark_Soul in forum C Programming
    Replies: 4
    Last Post: 09-22-2009, 08:28 PM
  4. Intel processors voltage
    By Mario F. in forum Tech Board
    Replies: 32
    Last Post: 06-12-2008, 11:31 AM
  5. Increasing voltage
    By Liger86 in forum Tech Board
    Replies: 3
    Last Post: 04-17-2006, 02:39 AM

Tags for this Thread