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.

Home Security alarm project

Status
Not open for further replies.
No when you define DATA_PORT PORTA you are actually setting the whole port and if you write 0xff to DATA_PORT then RA6 and RA7 i.e., RS and E becomes 1.
 

put urls in url tags and not code tags.

Datasheet says it uses KS0066 controller. I don't know wheather it is compatible with HD44780 controller or not. I have to check.

On the net it is mentioned that KS0066 is compatible with HD44780 controller.

SetAddr (0x80); and SetAddr (0xC0); is right for Row1 and Row2. I checked about it.

Can you post your LCD connection circuit?

Try this code

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


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;
}

Change this
Code:
        #define DATA_PORT	PORTA
	#define RS_PIN     	PORTAbits.RA6
	#define E_PIN      	PORTAbits.RA7
to
Code:
#define DATA_PORT	LATA
	#define RS_PIN     	LATAbits.RA6
	#define E_PIN      	LATAbits.RA7
When i change this
Code:
#define RS_PIN     	LATAbits.RA6
	#define E_PIN      	LATAbits.RA7
i have error
 

Last edited:

see c18 pic18 examples on the not on how to use LATx registers.
 

I dont know, but i think the problem is with the delays.
The delays for pic18f2520 is much faster than those who did the pic18f1220.
 

I dont know, but i think the problem is with the delays.
The delays for pic18f2520 is much faster than those who did the pic18f1220.
Then why you don't increase the delays? Insert big delays whether this is the problem or not, and when you find the solution restore proper delay values.

Also after reviewing the code, you have no delays after power on:

Code:
void main (void)
{
//SET UP
// OSCCON defaults to 31kHz. So no need to alter it.
ADCON1 = 0x0F;
CMCON = 0x07;
CVRCONbits.CVROE = 0;
TRISA = 0b00100000;;	//sets PORTA 
PORTA = 0b00000000;	//turns off PORTA outputs    	
// 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 (0x80);
WriteString("Hello World");
Delay1KTCYx(8); //delay 1s
WriteCmd(CLEAR_SCREEN);
SetAddr (0xC0);
WriteString("Hello again");
while(1);  
}

Moreover, you are missing a part of the initialize sequence. I don't know if this is the problem, but datasheet guidance is not followed. Please refer to "Display Initialization" section.

Furthermore, add a delay of let's say 2sec between those lines:

WriteCmd(CLEAR_SCREEN);
SetAddr (0xC0);


This will not fix the problem, but it will help you understand what is going on, before the second print.

Finally, please post the code you are using right now. After so many posts, I couldn't understand what your final code is.
 
Last edited:

Hello alexxx
My final code is the following:
Code:
#include <p18f2520.h>
#pragma config WDT=OFF , OSC=INTIO67 , 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	LATA
#define RS_PIN     	LATCbits.LATC1
#define E_PIN      	LATCbits.LATC2
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 = 0x0F;
CMCON = 0x07;
CVRCONbits.CVROE = 0;
TRISA =0b00000000;	//sets PORTA 
LATA = 0b00000000;	//turns off PORTA outputs 
TRISC =0b00000000;	//sets PORTA 3
LATC = 0b00000000;	//turns off PORTA outputs    	
// 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 (0x80);
WriteString("Hello World");
Delay10KTCYx(100);
WriteCmd(CLEAR_SCREEN);
SetAddr (0xC0);
WriteString("Hello again");
while(1);  
}
 

My final code is the following:

Again: :smile:

Before and after clear screen command add a delay over 1 sec to understand what is going on. Also fix the initialize sequence according to datasheet.
 

I have done this and it working fine!
 

What is your clock frequency?
The clock frequency is 40 MHz

- - - Updated - - -

Good that you problem got solved? Did you just add delay?
Yes i added delay before and after screen command and its working but alexx said that "This will not fix the problem, but it will help you understand what is going on, before the second print."

- - - Updated - - -

Again: :smile:

Before and after clear screen command add a delay over 1 sec to understand what is going on. Also fix the initialize sequence according to datasheet.
The initialize is it this one?:
initialize.png

- - - Updated - - -

I think the problem is this: WriteCmd(CLEAR_SCREEN)
 

The initialize is it this one?:
This is the initialization algorithm, 4 bit mode:




Yes i added delay before and after screen command and its working but alexx said that "This will not fix the problem, but it will help you understand what is going on, before the second print."

This delay pointed out the suspicious point, but it is just a debugging technique. It cannot be used as a real world solution, because the lcd will be left blank for a long period of time.
So after that and by reviewing the datasheet, someone can observe that the execution time of clear screen command in 4-bit mode, is 1.64ms after the second nibble.


The clock frequency is 40 MHz

Code:
void Delay20TCY()	//20 clock cycles, 2.5mS
40MHz means 25ns. 25*20 = 500ns. This doesn't meet the requirement of 1.64ms. I'm not aware of PIC or the compiler you are using, but I believe that it is time you gave a second look into your delay functions.

If you need an opinion, clear screen command is not necessary after initialization. Just write over previous screen and that's it!


PS: Could someone please explain why the title in the uploaded image is in Polish? :)
 
  • Like
Reactions: kle0ps

    kle0ps

    Points: 2
    Helpful Answer Positive Rating
I changed this code:
Code:
void Delay20TCY()	//20 clock cycles, 2.5mS
{
Delay10TCYx(2);
}

to

Code:
void Delay20TCY()
{
Delay10TCYx(50);
}
i don't know if its correct but its now working fine with the Clear Screen! :smile:
I found this delay generator but i don't understand the code.
http://www.sxlist.com/techref/piclist/codegen/delay.htm
 
Last edited:

Last edited:

RA0-RA3 is connected to D4-D7 of LCD and RA6-RA7 is connected to RS and E pins of LCD.
 

Finally i fixed the code. Thank you for your help!:)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top