Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem Processing the adc value in 8051

Status
Not open for further replies.
It only displays integer values. If you need to display floating point values then you have to write float to string conversion code.

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while(1){  
            
            lcd_cmd(0xC0);
    
            read(); //Read ADC
            
            adc_val = adc_val * 5 / 255;
            
            str[0] = adc_val / 100 + 48;
            str[1] = (adc_val / 10) % 10 + 48;
            str[2] = adc_val % 10 + 48;
            str[3] = '\0';
            
            string(str);
 
            P3 = adc_val;
 
    }

 
sorry for the delay ..
I googled a lot of confused can you give some logics?
 

The most straight forward method is to employ the sprintf() routine, if the utilized compiler provides an appropriate version which handles floats.

The economy of system resources depends largely on compiler and the the routines specific implementation and frequency and variation of utilization.

Example reference: Cx51 User's Guide - sprintf
Code:
#include <stdio.h>

void tst_sprintf (void) {
  char buf [100];
  int n;
  int a,b;
  float pi;

  a = 123;
  b = 456;
  pi = 3.14159;

  n = sprintf (buf, "%f\n", 1.1);
  n += sprintf (buf+n, "%d\n", a);
  n += sprintf (buf+n, "%d %s %g", b, "---", pi);

  puts (buf);
}



If system resources are particularly limited as well as frequency of utilization, then writing a specific routine for the task maybe for appropriate.

There are a number of thread within this forum which discuss and offer possible routines, the majority support set number digits in decimal fraction :

Converting From Float to String



Inspiration for writing your own routine can be gleaned from examining sprinf() routine source code, there are numerous open source versions available.


BigDog
 
in that exapmle int a , b also giving ,
i need to give only one floating value ..
How to give that ? that is adc_value ?
 

The beauty of the sprintf() routine is that it allows you to customize a string to your requirements, you can incorporate as few or as many parameters into the final string as required.

Which is why the function prototype includes an ellipsis at the end of the parameter list:

Code:
int sprintf(char *str, const char *format, ...)

The routine also allows you to concatenate text along with the formatted numerical values.


C library function - sprintf()

C Language: sprintf function

Although the following demonstrates the C++ STD sprintf() equivalent, the format specifier examples a typically valid for the C sprintf() routine:

std::printf, std::fprintf, std::sprintf, std::snprintf

The trick is utilizing the proper format specifier, which determines the numerical format, digit width, fixed decimal, etc.

Typically the format specifiers are the same as those used by the printf() routine.

Consult your compiler documentation as to what specific format specifiers are available and the specific numerical types supported.


BigDog
 

Re: ADC is working but cant processes the adc value in 8051

If you want to rectify the voltage then slightly pull up the adc output port with a resistor of about 27-68 K ohms.....
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top