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.

Display Value Of ADC on Uart

Status
Not open for further replies.

makwanamilan96

Newbie level 4
Joined
Apr 3, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,315
My problem is similar to the problem listed below.

https://www.edaboard.com/threads/277534/

I can already display the value on LCD :)

But i want to display the value of adc on UART ,:roll:

Now let me tell my hardware info so that you can help.

i am using 8051 microcontroller (P89V51RD2)
interfaced with lcd and adc.

Adc gives digital value which is being displayed on LCD.
now i want to display this value on UART..

my current program is listed below

Code:
//Adc_potentiometer
#include <p89v51Rx2.h>
#include <stdio.h> 
#define lcd_port P2				// Data port for LCD

//sfr SPCTL = 0xD5;			  //SPI CONTROL REG; SPI INTERRUPT ENABLE=1,SPI SYSTEM ENABLE=1, SPI MASTER MODE=1
//sfr SPCFG = 0XAA;			 //	SPI STATUS REG;	SPIF Interrupt Flag=1	 ,SPI Transmit Empty Interrupt Flag=1	,MODF=0
//sfr SPDAT = 0X86;			 //SPI Data Register'S sfr ADDRESS
sbit SS   = P1^4;
sbit rs = P3^6;
sbit en = P3^7;
rw=0;

void delay(unsigned int msec)		// Function for delay
{
	int i,j;
	for(i=0;i<msec;i++)
	    for(j=0; j<110; j++);
}

							//CPHA - SPI Clock Phase Bi, SSOE - Slave Select Output Enable,LSBFE - LSB-First Enable
void lcd_cmd(unsigned char item)		// Function to send command on LCD
{
	lcd_port = item;
	rs= 0;
	rw=0;
	en=1;
	delay(1);
	en=0;
	return;
} 

void lcd_data(unsigned char item)		// Function to display character on LCD
{
	lcd_port = item;
	rs= 1;
	rw=0;
	en=1;
	delay(1);
	en=0;
	return;
}

void lcd_init()
{
	lcd_cmd(0x38);		  				// For using 8-bit 2 row LCD 
	delay(5);			  						
	lcd_cmd(0x0F);        				// For display on cursor blinking
	delay(5);			 						
	lcd_cmd(0x80);		  				// Set the cursor on first position of LCD 
	delay(5);		
	lcd_cmd(0x01);	  					//clear the screen	
}

void lcd_print(unsigned char x[],unsigned int l)
{
unsigned int i;
	if(l==1)
	{lcd_cmd(0x80);				//start from location 0 line 1
		delay(200);}

	if(l==2)					//force cursor to begin from 2nd line
	{lcd_cmd(0xC0);				//start from location 0 line 1
		delay(200);}

	for(i=0;x[i]!='\0';i++)		
	{
		lcd_data(x[i]);
		delay(20);
	}	
}

void  spi_init()
{	
	SPCTL=0X5F;		   //SPI System Enable Bit	,SPI Master/Slave Mode Select Bit(Here master mode is selected),CPOL - SPI Clock Polarity Bit,
}

unsigned int ADC_Read()
{						
	union
	{
		unsigned int adc_int;
		unsigned char adc_char[2];
	}adc_value;

	adc_value.adc_int = 0;

//	P2 = 0x00;		
	SS = 0;				//chip select is  made low to initiate SPI 
	SPCFG=0X00; 	   

	SPDAT=0X00;		//CHANNEL I/P FOR spi
	while(SPCFG!= 0x80);
	SPCFG=0;
		
	SPDAT = 0XC0;		  
	while(SPCFG!=0x80);
	adc_value.adc_char[0] = SPDAT & 0x0F;
	SPCFG = 0;
		
	SPDAT = 0X00;		//data is send to generate SCK signal
	while(SPCFG!=0x80);
	adc_value.adc_char[1] = SPDAT;
	SPCFG = 0;
		
	SS = 1;

//	P2 = 0x01;

	return(adc_value.adc_int);
}

void main ()
{
	unsigned int adc_cnt;
	char lcd_buf[16];
	lcd_init();
	spi_init();
	
	while(1)
	{
		adc_cnt = ADC_Read();
		sprintf(lcd_buf,"adc=%4d",adc_cnt);
		lcd_print(lcd_buf,1);
	}
}
 
Last edited:

You have to convert adc value to string and send it byte by byte to serial port Tx. You have to do UART initialization.

Code C - [expand]
1
2
3
4
5
6
7
8
void send_string_usart(unsigned char *str_adc_val){
            while(*str_adc_val){
                       TI = *str_adc_val;
                       while(TI == 0);
                       TI = 0;
                       str_adc_val++;
            }
}

 
thanks jayanth .

How can i convert adc value to string ?
and
the code you have written is (UART initialization) ????
 

See code.txt here https://www.edaboard.com/threads/254723/ I have written float to string routine in it. Use it. No, that is Serial transmit code. floattostring() converts float to string. send the string variable to send_string_usart(string); For Serial initialization refer book "The 8051 Microcontroller and Embedded Systems Using Assembly and C" by Muhammed Ali Mazidi.
 
ok i will modify c program and then tell you what is the result :!:
 

Actually i am newbie ,
the value which is being displayed is (adc=xxx)

is that due to this line sprintf(lcd_buf,"adc=%4d",adc_cnt); ??

and then to convert to string ..

how many portion from this code is required
below is the program related to the float to string

Code:
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

//Global Variables
double volt, amp, power, rload, FP_NUM;
char string[8];
bit b_mode1, b_mode2;

const char character[] = {0,14,17,17,10,27,0,0};

void CustomChar(char pos_row, char pos_char) {
  char i;
    Lcd_Cmd(64);
    for (i = 0; i<=7; i++) Lcd_Chr_CP(character[i]);
    Lcd_Cmd(_LCD_RETURN_HOME);
    Lcd_Chr(pos_row, pos_char, 0);
}

//Function Prototypes
void floattostring(double FP_NUM);

//Sub Function
void floattostring(double FP_NUM) {
        double fpnumber;
        long int befdec, aftdec;

        fpnumber = FP_NUM;

        befdec = fpnumber;                      // Fractional part is truncated
                                                // 12.163456 becomes 12
        aftdec = fpnumber * 100;            // 12.163456 becomes 1216
        aftdec = aftdec - (befdec * 100);   // 1216 - 1200 = 16


        if (fpnumber < 1) {
                string[0] = '0';
                string[1] = '.';
                string[2] = (aftdec/10) + 48;
                string[3] = (befdec/1)%10 + 48;
                string[4] = ' ';
                string[5] = ' ';
                string[6] = ' ';
                string[7] = '\0';

        }

        else if ((fpnumber >= 1) && (fpnumber < 10)) {
                string[0] = (befdec/1)%10 + 48;
                string[1] = '.';
                string[2] = (aftdec/10) + 48;
                string[3] = (befdec/1)%10 + 48;
                string[4] = ' ';
                string[5] = ' ';
                string[6] = ' ';
                string[7] = '\0';

        }

        else if ((fpnumber >= 10) && (fpnumber < 100)) {
                string[0] = (befdec/10) + 48;
                string[1] = (befdec/1)%10 + 48;
                string[2] = '.';
                string[3] = (aftdec/10) + 48;
                string[4] = (befdec/1)%10 + 48;
                string[5] = ' ';
                string[6] = ' ';
                string[7] = '\0';

        }

        else if ((fpnumber >= 100) && (fpnumber < 1000)) {
                string[0] = (befdec/100) + 48;
                string[1] = (befdec/10)%10 + 48;
                string[2] = (befdec/1)%10 + 48;
                string[3] = '.';
                string[4] = (aftdec/10) + 48;
                string[5] = ' ';
                string[6] = ' ';
                string[7] = '\0';

        }
        
        else if ((fpnumber >= 1000) && (fpnumber < 10000)) {
                string[0] = (befdec/1000) + 48;
                string[1] = (befdec/100)%10 + 48;
                string[2] = (befdec/10)%10 + 48;
                string[3] = (befdec/1)%10 + 48;
                string[4] = '.';
                string[5] = (aftdec/10) + 48;
                string[6] = ' ';
                string[7] = '\0';

        }

        else if ((fpnumber >= 10000) && (fpnumber < 100000)) {
                string[0] = (befdec/10000) + 48;
                string[1] = (befdec/1000)%10 + 48;
                string[2] = (befdec/100)%10 + 48;
                string[3] = (befdec/10)%10 + 48;
                string[4] = (befdec/1)%10 + 48;
                string[5] = '.';
                string[6] = (aftdec/10) + 48;
                string[7] = '\0';

        }
        

}
void main() {
      TRISA = 0b00000011;
      PORTA = 0x00;
      TRISB = 0x00;
      PORTB = 0x00;
      TRISC = 0b00001110;
      PORTC = 0x00;
      ADCON1 = 0b10000010;
      CMCON.CM2 = 0;
      CMCON.CM1 = 0;
      CMCON.CM0 = 0;
      
      LCD_Init();
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1,3,"Welcome Vlad");
      Delay_ms(3000);
      Lcd_Cmd(_LCD_CLEAR);

      while(1) {

           volt = ADC_Read(0);
           amp = ADC_Read(1);

           /*
           if ((PORTC.F1 == 1) || (b_mode1 == 1) && (PORTC.F2 == 0)) {
              Delay_ms(20);
              if ((PORTC.F1 == 1) || (b_mode1 == 1) && (PORTC.F2 == 0)) {
                 volt = volt * 0.0725388601036269;
                 amp = amp * 0.0103626943005181;
                 b_mode1 = 1;
                 b_mode2 = 0;
              }
           }
           else if ((PORTC.F2 == 1) || (b_mode2 == 1) && (PORTC.F1 == 0)) {
                Delay_ms(20);
                if ((PORTC.F2 == 1) || (b_mode2 == 1) && (PORTC.F1 == 0)) {
                  volt = volt * 0.5181347150259067;
                  amp = amp * 0.05181347150259067;
                  b_mode2 = 1;
                  b_mode1 = 0;
                }
           }
           */
           volt = volt * 0.0725388601036269;
           amp = amp * 0.0310880829015544;
                 
           power = volt * amp;
           rload = volt / amp;

           floattostring(volt);
           Lcd_Out(1,1,string);
           Lcd_Out(1,8,"V");

           floattostring(amp);
           Lcd_Out(1,9,string);
           Lcd_Out(1,16,"A");

           floattostring(power);
           Lcd_Out(2,1,string);
           Lcd_Out(2,8,"W");

           floattostring(rload);
           Lcd_Out(2,9,string);
           CustomChar(2,16);
           

      }
      
}

and i request to replay
 

Pass your adcval.adcint to float2string function and the string will be in the string variable. pass string variable to lcd print function.
 

and will that value on lcd willl be displayed on UART ?
 

Passing float value to floattostring() function is for converting float to string. Use send_string_usart(string); to send data to usart.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top