Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pic and LCD question

Status
Not open for further replies.

timtalk

New Member
Hi there all..

I'm very new to electronics and still am trying to understand what is happening when a LCD is initalized. I'm using Nigels tutorials but since I don't fully understand ASM and normaly program in C I'm a bit lost. If I understand what i'm reading in Nigels code I'm seeing that all pins on PORT A are dropped to 0, then after a 100ms wait I see that it's giving the command "movlw 0x20" my question is does this mean that it's setting pin_A0 to 0, A1 to 0, A2 to 0, A3 to 0, A4 to 0 and A5 to 1 which would be the binary equilivent??? or is it actually sending the value 0X20 over one of the pins? if it's the second then which pin on the LCD is it sending to?
 
timtalk said:
Hi there all..

I'm very new to electronics and still am trying to understand what is happening when a LCD is initalized. I'm using Nigels tutorials but since I don't fully understand ASM and normaly program in C I'm a bit lost. If I understand what i'm reading in Nigels code I'm seeing that all pins on PORT A are dropped to 0, then after a 100ms wait I see that it's giving the command "movlw 0x20" my question is does this mean that it's setting pin_A0 to 0, A1 to 0, A2 to 0, A3 to 0, A4 to 0 and A5 to 1 which would be the binary equilivent??? or is it actually sending the value 0X20 over one of the pins? if it's the second then which pin on the LCD is it sending to?

MOVLW means MOV Literal to W, which means move the following value (in this case 20 HEX) to the W register. This is then written to an I/O port (by MOVWF Portx) - W is 8 bit, and so is the port register, the value is simply written to the port. So as you say, bit 0 is written to port pin 0, and bit 1 to port pin 1 etc.
 
Nigel thanks for the quick response. I thought that was the case but I guess I do not understand why you would be setting Pin_A5 high in this case. From what I can see of the schmatic of the LCD board Pin_A5 is not connected to anything and thus setting it high or low would not have any effect. If I understand what your saying then if it were shown visibly it would be

0 0 1 0 0 0 0 0

A A A A A A A A
7 6 5 4 3 2 1 0

Or am I still misunderstanding this???
 
timtalk said:
Nigel thanks for the quick response. I thought that was the case but I guess I do not understand why you would be setting Pin_A5 high in this case. From what I can see of the schmatic of the LCD board Pin_A5 is not connected to anything and thus setting it high or low would not have any effect. If I understand what your saying then if it were shown visibly it would be

0 0 1 0 0 0 0 0

| | | | | | | |
A A A A A A A A
7 6 5 4 3 2 1 0

Or am I still misunderstanding this???

Yes, I've been and checked the section you're talking about, the 0x20 isn't written to the port, it's used as a value passed to a subroutine - the 0x20 is a command code to the LCD module, as it's commented, it sets the LCD to 4 bit mode.

The subroutine does considerably more than just write to the port!.
 
OK so it does not set the pins of PORT_A then. That makes a bit more sense. Can you tell me what PIN it uses to send the value 0X02 to the LCD? I've read all the data sheets that I can find on the LCD and how it works and i've read several others walkthrus on this. All of them seem to say the same thing that "Write 0x02 to the LCD to Enable Four Bit Mode" but none of them specify what pin is being used to write that to the LCD. Is it being sent in on LCD data bit 4?
 
timtalk said:
OK so it does not set the pins of PORT_A then. That makes a bit more sense. Can you tell me what PIN it uses to send the value 0X02 to the LCD? I've read all the data sheets that I can find on the LCD and how it works and i've read several others walkthrus on this. All of them seem to say the same thing that "Write 0x02 to the LCD to Enable Four Bit Mode" but none of them specify what pin is being used to write that to the LCD. Is it being sent in on LCD data bit 4?

No, it's sent in parallel form, in eight bit mode as one 8 bit byte, in 4 bit mode as two 4 bit nibbles. It's clearly explained in the datasheets!.

Because the LCD defaults to 8 bit mode it's designed so it can accept this particular code in 4 bit mode regardless - otherwise you wouldn't be able to select 4 bit mode.

My tutorial includes a link to the EPE LCD Tutorial, have you downloaded that? - it's an excellent guide to LCD's.
 
Nigel, Thanks for pointing out that link. I had seen that there but never had followed the link. That page should be renamed to LCD for DUMMIES! hehehehe. I finally was able to initialize my LCD and get a flashing cursor. Now if I can just figure out how to get it to make letters. For some reason all I can get is a solid black block and then it skips to the next position. problem is that i'm not sending out 1111 1111 in my two nybles so it should not be producing the solid block. I'll provide the code that i'm using but I know that you do not program in C so I'll hope that someone who does will read this and point out where my mistake is.

Code:
#include <16F628A.h>
#fuses INTRC_IO,NOPROTECT,NOBROWNOUT,NOMCLR,NOLVP,NOWDT,PUT // must include this line !!
#use fixed_io(A_OUTPUTS=40,41,42,43,44,46,47)

#define D7  PIN_A3
#define D6  PIN_A2
#define D5  PIN_A1
#define D4  PIN_A0
#define RS  PIN_A4
#define RW  PIN_A6
#define E  PIN_A7

#byte port_A=5

Delay100()
{
delay_cycles(100);
}

Pulse_e()
{
output_high(E);
delay_cycles( 1 );
output_low(E);
}

LCD_Init()
{
OUTPUT_A(0x02);  // 1st of Set 4 bit mode
Pulse_e();
OUTPUT_A(0x00);  // 2nd of set 4 bit mode
Pulse_e();
delay_cycles( 5 );
OUTPUT_A(0x00); // 1st of turn on display
Pulse_e();
OUTPUT_A(0x0f); // 2nd of turn on display
Pulse_e();
delay_cycles( 5 );
OUTPUT_A(0x02); // 1st of 2 line mode
Pulse_e();
OUTPUT_A(0x08); // 2st of 2 line mode
Pulse_e();
delay_cycles(100);
}



main()
{
OUTPUT_A(0x00);  //make sure all pins are at 0
Delay100();
LCD_Init();
OUTPUT_A(0x14);  // send the first half of A with the RS pin high
Pulse_e();
OUTPUT_A(0x11);  // send the second half of A with the RS pin high
Pulse_e();
delay_cycles(5);
}
 
Well I sort of found my problem. I was not delaying enough cycles between the first and second nybles when I was sending out charcters. I guess this rasies another question. I'm using the PIC16F628A chip and i've noticed that if I use a #use delay(clock=32768) command and then instead of using the delay_cycles(xxx) command I have tried using the delay_ms(xxx) and if I use this combination the LCD won't initate. I guess i'm wondering what speed does the PIC run at if there is not a clock command used?
 
OK I can't figure this out. I've used the code shown above and when I first plug in the pic it produces a solid first block with a blinking cursor in the second location. I unplug the pic and wait maybe 5 seconds and plug it back in and then it produces a "A" as it should in the first block and a blinking cursor in the second block again as it should. Why is it tho that I have to unplug it and plug it back in to make it opperate properly???
 
As you're using a C compiler, doesn't the compiler have in-built LCD commands?.

My LCD routines took MANY months to perfect, and I'm EXTREMELY pleased with them, they offer reliabilty, speed, and ease of use. I didn't manage to download any code which actually worked 'as is', and my routines are derived from bits of many different programs, plus much of my own.

If you're writing your own in C, I don't expect you to find it any easier :lol:
 
Nigel, Yes I am trying to write my own code, but only because all of the LCD routines that I see written for my assembeler are for 16f877 type pics and not for the 16F628A style pic.
 
timtalk said:
Nigel, Yes I am trying to write my own code, but only because all of the LCD routines that I see written for my assembeler are for 16f877 type pics and not for the 16F628A style pic.

There should not much difference in the LCD specific code. Why would you have to rewrite code for two very similar devices from the same family?

I have reused assembly language code originally written for the PIC16F877 to run with minor changes on a PIC18F452. You should be able to the same on two more closely related devices.
 
Actually i'm doing ok so far with this. The problem with using the premade ones is that they all seem to rely on the use of a RS232 which then allows the putc command. If I can figure out a way to convert a letter into an ascii equilivent and then change the status of the upper 4 bits then i'd be set... I.e. the letter A is ascii value 001 I just need to convert/transform that to 0b00010001. I know I can make a chart of the entire alphabit to do this but there has to be a better way.
 
timtalk said:
Nigel, Yes I am trying to write my own code, but only because all of the LCD routines that I see written for my assembeler are for 16f877 type pics and not for the 16F628A style pic.

There's no difference in the code, both are 14 bit PIC's, the only possible changes required are the ports used - bit difficult to use PortC, PortD and PortE on a 16F628 :lol:

My tutorials give the changes required (in assembler) between the 16F628 and 16F876 at . As you will see, there's very little change required, the biggest is because I changed from 4MHz to 20MHz oscillator as well.

An important change is to make sure you disable the comparators, and you don't need to disable the analogue inputs (as there isn't any on the 628).
 
Hello there. I was playing around with LCDs a bit and I found this site very helpful.

http://www.geocities.com/dinceraydin/lcd/index.html

It has much information that will make you understand the operation of the LCD as well as examples.

There is also A VERY HELPFUL LCD simulator which is the best thing if you are just starting on LCDs. I suggest you visit that site and surely use the simulator.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top