2 Replies - 353 Views - Last Post: 12 February 2014 - 08:34 AM Rate Topic: -----

#1 TIARA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-February 14

Print to LCD: Count up to 100 then down to 1

Posted 12 February 2014 - 07:07 AM

I have the following code to print a string to the LCD using my PIC32 Starter Kit; I have made this code work with success. I cannot, however, make the LCD print numbers.

#include<p32xxxx.h>
#define LCD_PRT PORTE //LCD DATA PORT
//#define LCD_DDR DDRE	//LCD DDR
//#define LCD_PIN PINE	//LCD PIN

#define LCD_RS 0	//LCD RS
#define LCD_RW 1	//LCD RW
#define LCD_EN 2	//LCD EN

#define DELAY 2	

void delay_ms(int d)
{
	TMR1 = 0;
	while (TMR1 < d)
	{
		//wait
	}
}

void lcdCommand(unsigned char cmnd)
{
	LCD_PRT = (LCD_PRT & 0x0F) | (cmnd & 0xF0);
	LCD_PRT &= ~(1<<LCD_RS);	 //RS = 0 for command
	LCD_PRT &= ~(1<<LCD_RW);	 //RW = 0 for write
	LCD_PRT |= (1<<LCD_EN); 	 //EN = 1 for H-to-L
	delay_ms(300);				//wait to make EN wider
	LCD_PRT &= ~ (1<<LCD_EN); 	//EN = 0 for H-to-L

	delay_ms(400);	//wait to make EN wider
	
	LCD_PRT = (LCD_PRT & 0x0F) | (cmnd << 4);
	LCD_PRT |= (1<<LCD_EN);		 //EN = 1 for H-to-L
	delay_ms(300); 				//wait to make EN wider
	LCD_PRT &= ~(1<<LCD_EN);	//EN = 0 for H-to-L
}

void lcdData(unsigned char data)
{
	LCD_PRT = (LCD_PRT & 0x0F) |(data & 0xF0);
	LCD_PRT |= (1<<LCD_RS); 	 //RS = 1 for data
	LCD_PRT &= ~(1<<LCD_RW); 	//RW = 0 for write
	LCD_PRT |= (1<<LCD_EN); 	//EN = 1 for H-to-L

	delay_ms(300); //wait to make EN wider
	LCD_PRT &= ~(1<<LCD_EN); 	//EN = 0 for H-to-L

	LCD_PRT = (LCD_PRT & 0x0F) | (data <<4);
	LCD_PRT |= (1<<LCD_EN); 	//EN = 1 for H-to-L
	delay_ms(300); 				//wait to make EN wider
	LCD_PRT &= ~ (1<<LCD_EN); 	//EN = 0 for H-to-L
}

void lcd_init()
{ 	LCD_PRT = 0xFF; //LCD port is output

	LCD_PRT &=~(1<<LCD_EN); //LCD_EN = 0
	delay_ms(2000);			//wait for stable power
	lcdCommand(0x33);		//$33 for 4-bit mode
	delay_ms(300);			//wait
	lcdCommand(0x32);		//$32 for 4-bit mode
	delay_ms(300);			//wait
	lcdCommand(0x28);		//$28 for 4-bit mode
	delay_ms(300);			//wait
	lcdCommand(0x0e);		//display on, cursor on
	delay_ms(300);			//wiat
	lcdCommand(0x01);		//clear LCD
	delay_ms(3000);			//wait
	lcdCommand(0x06);		//shift cursor right
	delay_ms(300);			//wait
}

void lcd_gotoxy(unsigned char x, unsigned char y)
{
	unsigned char firstCharAdr[]={0x80, 0xC0, 0x94,0xD4};
	lcdCommand(firstCharAdr[y-1] + x-1);
	delay_ms(300);
}

void lcd_print(char * str) //recieve and print string to LCD
{
	unsigned char i=0;
	while(str[i] != 0)
	{
		lcdData(str[i]);
		i++;
	}
}

int main(void)
{
	//initialize 
	TRISE = 0xff00;
	T1CON = 0x8030;
	PR1 = 0xFFFF;
	
	lcd_init();

//print hello
	while(1)
	{
		lcd_gotoxy(1,1);
		lcd_print("hello");
	}

	return 0;
}


I want to replace the loop in print with a for loop, but I just get garbage printing to the LCD. Here is my latest attempt. What am I missing?

void lcd_print_up(char * num) //recieve and print num to LCD
{
	unsigned char i=0;
	while(num[i] != 0)
	{
		lcdData(num[i]);
		i++;
	}	
}
int main(void)
{
	//initialize 
	TRISD = 0xff00;
	T1CON = 0x8030;
	PR1 = 0xFFFF;	

	lcd_init();
	while(1)
	{
		lcd_gotoxy(1,1);
		lcd_print_up("1");
	}

return 0;
}


I want to change the original code as little as possible, don't worry about my header files - they are fine.

Thank you for your useful input!

Is This A Good Question/Topic? 0
  • +

Replies To: Print to LCD: Count up to 100 then down to 1

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,402
  • Joined: 25-December 09

Re: Print to LCD: Count up to 100 then down to 1

Posted 12 February 2014 - 08:09 AM

Leave the LCD printing alone and convert the numbers to strings for printing to the LCD. I recommend you investigate sprintf() for this purpose.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 TIARA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-February 14

Re: Print to LCD: Count up to 100 then down to 1

Posted 12 February 2014 - 08:34 AM

I had not considered this option, thank you for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1