LCD / Microchip Schematic

Thread Starter

beeson76

Joined Apr 19, 2010
211
Here is my schematic for my hooking up my LCD to my PIC16F886. The LCD is still not initializing correctly (Or I don't think it is anyway). It only does 1 line of black boxes. It is a 2 x 16 display. When it initializes correctly both lines should have black boxes, right? Could I get some advice on whether my schematic looks correct or am I missing anything on the hardware side of things. I really appreciate any help anyone can give, because I have been having problems getting this to work for a long time now. I finally got around to do this schematic. Again thanks for any help or advice.
 

Attachments

I don't understand MCLR connected to ground through a resistor. I'm use to seeing it pulled high through a pull up with (internal or external).

Aside from that, the connections look good.

The code is just important as the connection, so let's have a look at that.
 

Thread Starter

beeson76

Joined Apr 19, 2010
211
Thanks wannaBinventor for the reply. I will look into what you suggested. Here is what I have as code. I at first started out pretty complicated, but through suggestions from you guys, I went very simple and am trying just basic LCD code. I got this code mostly from the sample code from the Samples directory in the Hitech Compiler. Here is my LCDMAIN code:

Rich (BB code):
#include <htc.h>
#include "lcd.h"

void pause (unsigned short usvalue);

__CONFIG (INTIO & WDTDIS & MCLRDIS & UNPROTECT);

#define number 0x30

unsigned char b0;

void
main(void)

{
ANSEL = 0;
ANSELH = 0;

CM1CON0 = 0;
CM2CON0 = 0;


TRISA = 0;
TRISC = 0;

lcd_init();

while (1 == 1)
{
	lcd_clear();
	lcd_goto(0);
	lcd_puts("Hello World");
	pause(1000);
}
}
Sorry for not commenting, but its really basic. It is just simple code that displays "Hello World"--it should display after initialization.

Here is my LCD.C code:

Rich (BB code):
#include <htc.h>
#include "lcd.h"

void pause(unsigned short usvalue);

#define		LCD_RS		RA2
#define		LCD_RW		RA4
#define		LCD_EN		RA1
#define		LCD_DATA	PORTC
#define		LCD_STROBE()	((LCD_EN = 1),(LCD_EN = 0))

void
lcd_write (unsigned char c)
{
	pause (1);
	LCD_DATA = ((c >> 4) & 0x0F);
	LCD_STROBE();
	LCD_DATA = (c & 0x0F);
	LCD_STROBE();
}

void
lcd_clear(void)
{
	LCD_RS = 0;
	lcd_write (0x1);
	pause(2);
}

void 
lcd_puts(const char *s)
{
	LCD_RS = 1;
	while (*s)
		lcd_write(*s++);
}

void
lcd_putch(char c)
{
	LCD_RS = 1;
	lcd_write(c);
}

void
lcd_goto(unsigned char pos)
{
	LCD_RS = 0;
	lcd_write (0x80 + pos);
}

void
lcd_init()
{
	char init_value;

	ANSEL = 0;

	init_value = 0x3;
	TRISA = 0;
	TRISC = 0;
	LCD_RS = 0;
	LCD_EN = 0;
	LCD_RW = 0;

	pause(15);					
	LCD_DATA = init_value;
	LCD_STROBE();
	pause(10);					
	LCD_STROBE();
	pause(10);					
	LCD_STROBE();
	pause(10);					
	LCD_DATA = 2;			
	LCD_STROBE();

	lcd_write (0x28);
	lcd_write (0xF);
	lcd_clear();
	lcd_write(0x6);
}

void lcd_putint (int i)
{
	LCD_RS = i;
	lcd_write(i);
}
This code was mainly taken from again the Samples directory of the Hitech compiler.

And here is my LCD.h code:

Rich (BB code):
extern void lcd_write (unsigned char);
extern void lcd_clear (void);
extern void lcd_puts (const char *s);
extern void lcd_goto (unsigned char pos);
extern void lcd_init (void);
extern void lcd_putch (char);
extern void lcd_putint (int);
Hope this code can help you guys help me. This is driving me crazy. Thanks again for any help, and I certainly appreciate any replies and help received.
 

debjit625

Joined Apr 17, 2010
790
As wannaBinventor mentioned
You cant put MCLR low because it will reset the MCU,i.e. for the program to run you have to connect the MCLR pin to Vdd.We only pull MCLR low when we want a external reset.

Good Luck
 

eblc1388

Joined Nov 28, 2008
1,542
The LCD is still not initializing correctly (Or I don't think it is anyway). It only does 1 line of black boxes. It is a 2 x 16 display. When it initializes correctly both lines should have black boxes, right?
Nope.

An uninitialized LCD usually has only one row of black blocks.
 

t06afre

Joined May 11, 2009
5,934
As wannaBinventor mentioned
You cant put MCLR low because it will reset the MCU,i.e. for the program to run you have to connect the MCLR pin to Vdd.We only pull MCLR low when we want a external reset.
Good Luck
In this case it is wrong. Because the configuration setting has disabled the MLCR option. It is now tied to VDD inside the chip. A very good choice for the beginner. It is also important to sure that all configuration bits are set correct. Because if not set they default to 1. That could in some cases create trouble. PICC also has native functions for delay they are named __delay_ms() and __delay_us() use them instead of the pause() function. Remember to use the
Rich (BB code):
#define _XTAL_FREQ  4000000
Using 4000000 is correct in your case also. In the compiler install folder you will find a folder named docs. Here is the Hitech C manual.
In order to master programming you must also master the art of debugging. You can trace many errors by using the internal simulator in MPLAB. Here you will find some useful free websimenar http://techtrain.microchip.com/webseminars/QuickList.aspx
I would recommend these as a start
http://techtrain.microchip.com/webseminars/ArchivedDetail.aspx?Active=46
http://techtrain.microchip.com/webseminars/ArchivedDetail.aspx?Active=61
http://techtrain.microchip.com/webseminars/ArchivedDetail.aspx?Active=137
and perhaps this
http://techtrain.microchip.com/webseminars/ArchivedDetail.aspx?Active=153

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en542849
Here you can find the latest Hitech C version. 45 days in pro mode then it will only work in lite mode. But that is ok for the hobbyist. One last thing your chip share the include file with the 16f887 chip. All the deffintions are in the pic16f887.h file. The config word settings in the end
Example on Using the dealy function it should work for your chip, I tested it on 16f690 chip.
Rich (BB code):
#include <htc.h>
#define _XTAL_FREQ  500000
unsigned long slow_int=1000;
unsigned long fast_int=30;
int i=0xff;
__CONFIG(INTIO & WDTDIS & NONSENS &PWRTDIS & BORDIS & MCLRDIS & FCMEN  & IESODIS & UNPROTECT);
void fast_blink(void)
{
 PORTC = i;
  __delay_ms(fast_int);
PORTC = 0x0;
  __delay_ms(100);
}
void slow_blink(void)
{
PORTC = i;
 __delay_ms(50);
  PORTC = 0x0;
__delay_ms(1000);
}void main(void)
{
 TRISC = 0b00000000;// port directions: 1=input, 0=output
  OSCCON=0b00110000;//set internal OSC to 500KHz
   PORTC = 0x0;//Zero portC
    ANSEL=ANSELH=0;
while (1){
 fast_blink();
  fast_blink();
__delay_ms(slow_int-100);
 slow_blink();
}
}
 

Thread Starter

beeson76

Joined Apr 19, 2010
211
Thanks for the replies. Appreciate it very much. So after reading the replies, here is what I have come up with along with questions.

Because the MCLR is disabled in my configuration of the chip, I am pulling the pin low?? or high??

And because of MCLR being disabled, is my circuit right or wrong when it comes to pin 1 of the chip??

After getting the answers to these questions, it will help me to decide which way to proceed.

Thanks again, and I look forward to the replies.
 

t06afre

Joined May 11, 2009
5,934
Then MCLR is disabled, the "MCLR" pin become a general pin. Often (if not allways) on Microchips MCUs the pin used for MCLR is a read only pin(input only). So you do not have to worry about this pin with your current setting. Using internal osc, and MLCR disable is as have said before a good options for the beginner. Because you eliminate two potential sources for trouble.
 

Thread Starter

beeson76

Joined Apr 19, 2010
211
I know this is kinda a vague question, but in this situation, can someone please tell me how to use __delay_ms or __delay_us. I have see it used in #define statement? But then I read that if you #include it as delay.h and delay.c, but I cannot find them, so I don't know if this is the proper way. Any help is greatly appreciated.
 

t06afre

Joined May 11, 2009
5,934
I know this is kinda a vague question, but in this situation, can someone please tell me how to use __delay_ms or __delay_us. I have see it used in #define statement? But then I read that if you #include it as delay.h and delay.c, but I cannot find them, so I don't know if this is the proper way. Any help is greatly appreciated.
Just include htc.h In the HItech c install folder you will find a folder named doc here is the manual. but as an axample
Rich (BB code):
__dealy_ms(100);
will give you a dealy equal to 100 msec or 0.1 seconds if you have used the correct settings for _XTAL_FREQ
Rich (BB code):
#define _XTAL_FREQ xxxxxxx
Note this will only be used by the compiler. It will NOT set any clock frequncy settings on the MCU
 

Thread Starter

beeson76

Joined Apr 19, 2010
211
Thanks t06afre for replying. Here is what I have. Can you please check it to see if everything looks good.
Rich (BB code):
#include <htc.h>
#include "lcd.h"

///////////////////////////////////////////////////////////////////////////////////////////////////
//#Define Statements///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#define  _XTAL_FREQ		4000000  // **ADDED** //Needed for Internal Oscillator
#define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))

//  **CHANGED** void pause (unsigned short usvalue);

__CONFIG (MCLRDIS, INTIO, WDTDIS, PWRTEN, BORDIS, LVPDIS, UNPROTECT);
// **CHANGED**  __CONFIG (INTIO & WDTDIS & MCLRDIS & UNPROTECT);

main()

{
ANSEL = 0;
ANSELH = 0;

CM1CON0 = 0;
CM2CON0 = 0;


TRISA = 0;
TRISC = 0;

lcd_init();

while (1 == 1)
{
	
	lcd_clear();
	lcd_goto(0);
	lcd_puts("Hello World");
	__delay_ms(150);  // **ADDED**
	// **CHANGED** pause(1000);
}
}
 

t06afre

Joined May 11, 2009
5,934
Does it work?
I am not sure about this. Comparators are by default not active after power on/reset. I think it is best to drop doing anything to them in the start. Which chip are you using
Rich (BB code):
CM1CON0 = 0;
CM2CON0 = 0;
And you do not need this either
Rich (BB code):
#define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))
 

Thread Starter

beeson76

Joined Apr 19, 2010
211
It works. Yes!!!!!! Can't believe it. I've been working on this for about 2 months now. I made the changes you suggested T06afre and it works!! I really do appreciate the help. Here is the final code for this project. I am using the PIC16F886.

Rich (BB code):
#include <htc.h>
#include "lcd.h"

///////////////////////////////////////////////////////////////////////////////////////////////////
//#Define Statements///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#define  _XTAL_FREQ		4000000  // **ADDED** //Needed for Internal Oscillator
//  **CHANGED** #define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))

//  **CHANGED** void pause (unsigned short usvalue);

__CONFIG (MCLRDIS & INTIO & WDTDIS & PWRTEN & BORDIS & LVPDIS & UNPROTECT);
// **CHANGED**  __CONFIG (INTIO & WDTDIS & MCLRDIS & UNPROTECT);

main()

{
ANSEL = 0;
ANSELH = 0;

// **CHANGED**  CM1CON0 = 0;
// **CHANGED**  CM2CON0 = 0;


TRISA = 0;
TRISC = 0;

lcd_init();

while (1 == 1)
{
	
	lcd_clear();
	lcd_goto(0);
	lcd_puts("Hello World");
	__delay_ms(150);  // **ADDED**
	// **CHANGED** pause(1000);
}
}
Please notice the changes in the code. I have commented out the changes. This has been by far the hardest project I have done yet, and this was pretty basic stuff when it comes to what you do. Thanks again for all the help. I am noticing blinking in the LCD--its quick, so its not too noticeable. Could this have something to do with the way the program is written--it being is a while loop, or does it have to do with my frequency of my oscillator?
 

t06afre

Joined May 11, 2009
5,934
It works. Yes!!!!!! Can't believe it. I've been working on this for about 2 months now.
2 months? You should have asked for help a long time ago. A LCD display is not made for very dynamic display. It will perhaps help some if you put the LCD clear outside the while looop.
 
Top