Convert lcd code for pic18f1220 to pic18f2520

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
kle0ps
Posts: 2
Joined: Wed Nov 21, 2012 10:16 pm
Has thanked: 1 time
Contact:

Convert lcd code for pic18f1220 to pic18f2520

Post by kle0ps »

Hello I use this code to connect pic18f1220 and with lcd (HITACHI HD44780 ) and its working fine.

Code: Select all

       //E on A7, RS on A6,  D4-7 on A0-3.
        #include <p18f1220.h>
	#pragma config WDT=OFF , OSC=INTIO2 , PWRT = ON, LVP=OFF, MCLRE = OFF

	#include <delays.h>
	#define CLEAR_SCREEN  	0b00000001
	#define FOUR_BIT  		0b00101100
	#define LINES_5X7  		0b00111000
	#define CURSOR_BLINK  	0b00001111
	#define CURSOR_RIGHT  	0b00000110
	#define DATA_PORT	PORTA
	#define RS_PIN     	PORTAbits.RA6
	#define E_PIN      	PORTAbits.RA7


	void Delay20TCY()	//20 clock cycles, 2.5mS
{
	Delay10TCYx(2);
}

	void SetAddr(unsigned char DDaddr)
	{
	DATA_PORT &= 0xf0;                      // Write upper nibble
	DATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f);
	RS_PIN = 0;                   	// Set control bit
	Delay20TCY();
	E_PIN = 1;                      	// Clock the cmd and address in
	Delay20TCY();
	E_PIN = 0;
	DATA_PORT &= 0xf0;                      // Write lower nibble
	DATA_PORT |= (DDaddr&0x0f);
	Delay20TCY();
	E_PIN = 1;                      	// Clock the cmd and address in
	Delay20TCY();
	E_PIN = 0;
	}
	void WriteCmd(unsigned char cmd)
	{
	DATA_PORT &= 0xf0;
	DATA_PORT |= (cmd>>4)&0x0f;           
	RS_PIN = 0;          	// Set control signals for command
	Delay20TCY();
	E_PIN = 1;                      	// Clock command in
	Delay20TCY();
	E_PIN = 0;
	// Lower nibble interface
	DATA_PORT &= 0xf0;
	DATA_PORT |= cmd&0x0f;
	Delay20TCY();
	E_PIN = 1;                      	// Clock command in
	Delay20TCY();
	E_PIN = 0;
	}

	void WriteChar(char data)
	{
	DATA_PORT &= 0xf0;
	DATA_PORT |= ((data>>4)&0x0f);
	
	RS_PIN = 1;                     	// Set control bits
	Delay20TCY();
	E_PIN = 1;                      	// Clock nibble into LCD
	Delay20TCY();
	E_PIN = 0;


	DATA_PORT &= 0xf0;              // Lower nibble interface
	DATA_PORT |= (data&0x0f);
	Delay20TCY();
	E_PIN = 1;                      	// Clock nibble into LCD
	Delay20TCY();
	E_PIN = 0;
	}

	void WriteString(const rom char *buffer)    
	{		 
	while(*buffer)     		// Write data to LCD up to null
	{
	Delay20TCY();
	WriteChar( *buffer); 	// Write character to LCD
	buffer++;               	// Increment buffer
	}
	return;
	}       

	void main (void)
	{
	//SET UP
	// OSCCON defaults to 31kHz. So no need to alter it.
	ADCON1 = 0x7F;  //all IO are digital  or 0b01111111 in binary
	TRISA = 0b00100000;	//sets PORTA 
	PORTA = 0b00000000;	//turns off PORTA outputs    	
	TRISB = 0b01000000;	//sets PORTB as all outputs
	PORTB = 0b00000000;	//turns off PORTB outputs, good start position


	// this code configures the display   	
	WriteCmd ( 0x02 );				// sets 4bit operation
	WriteCmd ( FOUR_BIT & LINES_5X7 );	// sets 5x7 font and multiline operation.
	WriteCmd ( CURSOR_BLINK );			// blinks cursor
	WriteCmd ( CURSOR_RIGHT  );			// moves cursor right	


	// Start of user program.
       SetAddr  (0x83);
       WriteSting("Hello World");	
        while(1);
   	}
but i want to convert this code for the pic18f2520.
Does anyone know what to do?
I use Mplaba and c18 compiler
Thank you

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Convert lcd code for pic18f1220 to pic18f2520

Post by Enamul »

Hi
Actually only you need to change the following bit..rest should be same.
#include <p18f1220.h>
#pragma config WDT=OFF , OSC=INTIO2 , PWRT = ON, LVP=OFF, MCLRE = OFF
Need to change above two
Enamul
University of Nottingham
enamul4mm@gmail.com

kle0ps
Posts: 2
Joined: Wed Nov 21, 2012 10:16 pm
Has thanked: 1 time
Contact:

Re: Convert lcd code for pic18f1220 to pic18f2520

Post by kle0ps »

Thank you very much. I changed this and its working fine!

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Convert lcd code for pic18f1220 to pic18f2520

Post by Enamul »

Glad to know that it works :)
Enamul
University of Nottingham
enamul4mm@gmail.com

Post Reply