Printing ADC value to LCD.

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm trying to print an ADC value taken from temperature(LM34) sensor, but i don't know what to do, because the value is in binary and i need to print it as string.

this is the init code for the LCD

void delay_us(unsigned int d)
{
	while(d) {
		_delay_us(1);
		d--;
	}
}

void lcdCommand(unsigned char cmnd)
{
	LCD_DPRT = cmnd;                //send cmnd to data port
	LCD_CPRT &= ~(1<<LCD_RS);       //RS = 0 for command
	LCD_CPRT &= ~(1<<LCD_RW);       //RW = 0 for write
	LCD_CPRT |= (1<<LCD_EN);        //EN = 1 for H-to-L pulse
	delay_us(1);                    //wait to make enable wide
	LCD_CPRT &= ~(1<<LCD_EN);       //EN = 0 for H-to-T pulse
	delay_us(100);                  //wait to make enable wide
}

void lcdData(unsigned char data)
{
	LCD_DPRT = data;                //send data to data port
	LCD_CPRT |= (1<<LCD_RS);        //RS = 1 for data
	LCD_CPRT &= ~(1<<LCD_RW);       //RW = 0 for write
	LCD_CPRT |= (1<<LCD_EN);        //EN = 1 for H-to-L pulse
	delay_us(1);                    //wait to make enable wide
	LCD_CPRT &= ~(1<<LCD_EN);       //EN = 0 for H-to-l pulse
	delay_us(100);                  //wait to make enable wide
}

void lcd_init()
{
	LCD_DDDR = 0xff;                //all pins in port C output
	LCD_CDDR = 0xff;                //all pins in port D output

	LCD_CPRT &= ~(1<<LCD_EN);       //LCD_EN = 0
	delay_us(2000);                 //wait for init
	lcdCommand(0x38);               //init. lcd 2 line, 5 x 7 matrix
	lcdCommand(0x0e);               //display on, cursor on
	lcdCommand(0x01);               //clear LCD
	delay_us(2000);                 //wait
	lcdCommand(0x06);               //shift cursor right
}

void lcd_gotoxy(unsigned char x, unsigned char y)
{
	unsigned char firstCharAdr[] = {0x80, 0xc0, 0x94, 0xd4};
	lcdCommand(firstCharAdr[y - 1] + x - 1);
	delay_us(100);
}

void lcd_print(char* str)
{
	unsigned char i  = 0;
	while (str[i] != 0)
	{
		lcdData(str[i]);
		i++;
	}
}

this how i convert and take the converted value

(i know that in this example he declared Port D as an output but he used PORTB instead of PORTD, it seems like a mistake from the writer of the book, but i'm using Port B.) 

PORTB will be in binary, so how i print PORTB.

this is the code i use to print.

lcd_init();
lcd_gotoxy(1, 1);
lcd_print();

as we see to print something on the LCD we need to pass it as string like this

lcd_print("Hello");

so what to do, to print an integer binary number to a screen that take strings.

This topic has a solution.
Last Edited: Tue. Jun 13, 2017 - 04:35 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1
char buffer[12];

itoa(ADC, buffer, 10);
lcd_print(buffer);

itoa() is the "cheap" function for this - but does not give you much control over the formatting of the value. For that push the boat out and use sprintf().

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you, it worked.