LCD code_HTC

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
Rich (BB code):
/*-------------------------- BEGIN ----------------------------*/
#include<pic.h>
__CONFIG(0x3F39); 
#define _XTAL_FREQ 4000000
#define RS RB2
#define EN RB1
#define databits PORTD
 
/*----------------PORT SETTINGS-------------------*/
void pic_init()
    {
     TRISB2=0;
     TRISB1=0;
     TRISD=0;
    }
 
/*-----------------LCD FUNCTIONS ----------------------*/
void LCD_STROBE(void)
    {
    EN = 1;
    __delay_us(0.5);
    EN = 0;
    }
void data(unsigned char c)
    { 
    RS=1;
    __delay_us(40);
     databits = ( c >> 4 );
    LCD_STROBE();
    databits = ( c );
    LCD_STROBE(); 
    }
void cmd(unsigned char c)
    {
     RS=0;
    __delay_us(40); 
    databits = ( c >> 4 );
    LCD_STROBE();
    databits = ( c  );
    LCD_STROBE();
    }
void clear(void)
    {
     cmd(0x01);
    __delay_ms(2);
    }
void lcd_init()
    { 
    __delay_ms(20);
    cmd(0x30 );
    __delay_ms(1);
    cmd(0x30 );
    __delay_ms(1);
    cmd(0x30 );
    cmd(0x28 ); // Function set (4-bit interface, 2 lines, 5*7Pixels)
    cmd(0x0c); // Make cursorinvisible
    clear(); // Clear screen 
    cmd(0x6); // Set entry Mode(auto increment of cursor)
    }
void string(const char *q)
    {
    while(*q) {data(*q++);}
    }
 
/*------------------MAIN FUNCTION-----------------------*/
main()
    {
    __delay_ms(50);
    pic_init();
    lcd_init();
    while(1)
        {
        cmd(0x80);
        string("Hello World");
        cmd(0xc0);
        string("My 16x2 LCD");
        while(1); 
        }
    } 
 /*---------------- END --------------------------*/


Can you explain to me how actually this code function to activate the lcd?I dont really clear what is the cmd() use for?cmd is an operator?what is the databits=(c>>4)?
 
Last edited:

t06afre

Joined May 11, 2009
5,934
cmd is a function name. Perhaps no the best but still cmd is not reserved word in HI-Tech V so its OK
The databits=(c>>4) is the shift operator. Read more about it here http://en.wikipedia.org/wiki/Bitwise_operation
And also do you remember our talk about magic numbers. WHY are you still using them.
Do you have anhy book on basic C programming? I think you need it. Take a quick lookup in book may take few minutes. Asking in this forum is fine. But it may take days to get an answer.
Here are some free links.
http://www.oopweb.com/CPP/Documents/CProgramming/VolumeFrames.html
http://www.oopweb.com/CPP/Documents/CPointers/VolumeFrames.html
Found here
http://www.oopweb.com/CPP/Documents/CPointers/VolumeFrames.html
Oh yes here is some info on LCDs in 4 bit mode
http://esd.cs.ucr.edu/labs/interface/interface.html
http://www.taoli.ece.ufl.edu/teaching/4744/labs/lab7/LCD_V1.pdf
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
cmd is a function name. Perhaps no the best but still cmd is not reserved word in HI-Tech V so its OK
The databits=(c>>4) is the shift operator. Read more about it here http://en.wikipedia.org/wiki/Bitwise_operation
And also do you remember our talk about magic numbers. WHY are you still using them.
Do you have anhy book on basic C programming? I think you need it. Take a quick lookup in book may take few minutes. Asking in this forum is fine. But it may take days to get an answer.
Here are some free links.
http://www.oopweb.com/CPP/Documents/CProgramming/VolumeFrames.html
http://www.oopweb.com/CPP/Documents/CPointers/VolumeFrames.html
Found here
http://www.oopweb.com/CPP/Documents/CPointers/VolumeFrames.html
Oh yes here is some info on LCDs in 4 bit mode
http://esd.cs.ucr.edu/labs/interface/interface.html
http://www.taoli.ece.ufl.edu/teaching/4744/labs/lab7/LCD_V1.pdf

@t06afre
I started to use MPLAB SIM.thank you for reminding me we can check by using MPLAB SIM and watch how our program is ran.I have learnt this in my diploma but totally have forgotten.thank for your reminded.btw,i just want to check with you,now i try to use MPLAB SIM to view how my program run but i stuck in the (c>>4)
actually i want to check what is the variable of c through MPLAB SIM but which toolbar is for me to view the c?i mean we can view all the SFR from the SFR toolbar but what about the c?
hope you can understand what i'm trying to say!
 

debjit625

Joined Apr 17, 2010
790
@t06afre
actually i want to check what is the variable of c through MPLAB SIM but which toolbar is for me to view the c?i mean we can view all the SFR from the SFR toolbar but what about the c?
hope you can understand what i'm trying to say!
You will find it in File registers i.e.. From menu select "View" then "File Registers".In File Registers window you will find 4 columns .....
1) Address - it will show you the address of the variable in memory.
2) Hex - it will show you the value of the variable in hex format.
3) Decimal - it will show you the value of the variable in decimal format.
4) Symbol Name - it will show you the name of the variable and its what you need....:D

One more thing in File Registers window their are two tabs at bottom left ,Hex and Symbolic be sure to select the symbolic to see symbolic names

Good Luck
 
Last edited:

t06afre

Joined May 11, 2009
5,934
@t06afre
I started to use MPLAB SIM.thank you for reminding me we can check by using MPLAB SIM and watch how our program is ran.I have learnt this in my diploma but totally have forgotten.thank for your reminded.btw,i just want to check with you,now i try to use MPLAB SIM to view how my program run but i stuck in the (c>>4)
actually i want to check what is the variable of c through MPLAB SIM but which toolbar is for me to view the c?i mean we can view all the SFR from the SFR toolbar but what about the c?
hope you can understand what i'm trying to say!
Yes take a look at the recommened webseminars here
http://forum.allaboutcircuits.com/showthread.php?t=44852
 

debjit625

Joined Apr 17, 2010
790
Opps I didn't go through your code and just by your post I assumed that you want to view variables, but in your code "c" is a temporary variable used to pass value to functions, so with the above method you can't check it. Rather use this method. From menu select "View" and then "Watch”. Watch window will have a button named "Add Symbol" and with it a combo list box ,expand the list box and select the variable's name "c" and click the button "Add Symbol”. Now you are ready to watch it.

Good Luck
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
Rich (BB code):
/*-------------------------- BEGIN ----------------------------*/
#include<htc.h>
__CONFIG(0x3F39); 
#define _XTAL_FREQ 4000000
#define RS RB2
#define EN RB1
#define databits PORTD
 
/*----------------PORT SETTINGS-------------------*/
void pic_init()
{
TRISB2=0;
TRISB1=0;
TRISD=0;
}
 
/*-----------------LCD FUNCTIONS ----------------------*/
void LCD_STROBE(void)
{
EN = 1;
__delay_us(0.5);
EN = 0;
}
void data(unsigned char c)
{ 
RS=1;
__delay_us(40);
databits = ( c >> 4 );
LCD_STROBE();
databits = ( c );
LCD_STROBE(); 
}
void cmd(unsigned char c)
{
RS=0;
__delay_us(40); 
databits = ( c >> 4 );
LCD_STROBE();
databits = ( c );
LCD_STROBE();
}
void clear(void)
{
cmd(0x01);
__delay_ms(2);
}
void lcd_init()
{ 
__delay_ms(20);
cmd(0x30 );
__delay_ms(5);
cmd(0x30 );
__delay_us(100);
cmd(0x30 );
cmd(0x28 ); // Function set (4-bit interface, 2 lines, 5*7Pixels)
cmd(0x0c); // Make cursorinvisible
clear(); // Clear screen 
cmd(0x6); // Set entry Mode(auto increment of cursor)
}
void string(const char *q)
{
while(*q) {data(*q++);}
}
 
/*------------------MAIN FUNCTION-----------------------*/
main()
{
__delay_ms(50);
pic_init();
lcd_init();
while(1)
{
cmd(0x80);
string("Hello World");
cmd(0xc0);
string("My 16x2 LCD");
while(1); 
}
}



This code cannot function in my lcd.what is the problem.below show the pic of my lcd response with this code.
http://1.bp.blogspot.com/-89sp4O7VVmg/Tn1ULBd1oEI/AAAAAAAAAHY/VkaxLzD_zHY/s1600/23092011325.jpg


OR CAN YOU GIVE ME ANOTHER CODE THAT can be used in HTC to TRY?I just want to see the output and study the code.
I'm using
4MHZ crystal
PIC16f877a
2*16 LCD
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
Opps I didn't go through your code and just by your post I assumed that you want to view variables, but in your code "c" is a temporary variable used to pass value to functions, so with the above method you can't check it. Rather use this method. From menu select "View" and then "Watch”. Watch window will have a button named "Add Symbol" and with it a combo list box ,expand the list box and select the variable's name "c" and click the button "Add Symbol”. Now you are ready to watch it.

Good Luck

@debjit625
i tried to open the "Watch"but in the combo list of add symbol there is no "c"for me to add.i posted the picture on how my combo list look like.
http://3.bp.blogspot.com/-ELdVVYtoQD0/Tn1XDmWNIKI/AAAAAAAAAHc/waU2IWUFllY/s1600/Untitled.png
 

t06afre

Joined May 11, 2009
5,934
You may also put a watch on a memory address. In your MPLAB project folder locate the map file and look for the address allocated for the c variable. It should be named something like
Rich (BB code):
 

debjit625

Joined Apr 17, 2010
790
@debjit625
i tried to open the "Watch"but in the combo list of add symbol there is no "c"for me to add.i posted the picture on how my combo list look like.
http://3.bp.blogspot.com/-ELdVVYtoQD0/Tn1XDmWNIKI/AAAAAAAAAHc/waU2IWUFllY/s1600/Untitled.png
Yes,I missed that part in your code i.e.. the Watch window will only show you the variables which will use general memory(GPR),but in case of your function's temporary variable "c" ,it will use the working register as it is 8 bits in size.So to watch it watch W register in SFR.From menu "View" select "Special Function Registers".
Here "c" is defined as unsigned char which is 8 bits but if you define it as int or any other type which is larger than 8 bits then you can watch it from Watch window....actually you can also watch SFR in Watch window.
OR CAN YOU GIVE ME ANOTHER CODE THAT can be used in HTC to TRY?I just want to see the output and study the code.
I'm using

4MHZ crystal
PIC16f877a
2*16 LCD
Their are many type of 2*16 LCD and their initialization differs so it will be better if you can provide us with the datasheet for your LCD or you can also name the controller used in your LCD.

Good Luck
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69

debjit625

Joined Apr 17, 2010
790
Your controller, here you will find all the details i.e.. how your LCD works
http://andilcd.de/medien/de/download/controller/andilcd_conchar_sheet_de_splc780d1.pdf

I have not used this controller ,but it have similarities with Samsungs KS0070B which I use. This program I have written long ago to test my LCD first time ,so its the most easiest code but not accurate still it works. It was for PIC16F84A but it will work for PIC16F877A just modified.

Rich (BB code):
/*
Author : Debjit Roy
Info.. : 16x2 LCD Initialization testing
       PORTB = Data line
       RC0 = RS line
       RC1 = R/W line
       RC2 = Enable (E) line
Date : 25,Sept,2011
*/
#include "htc.h"
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif
void command();
void data(); 
int i;
char a[]={" WRITEN IN CBY DEBJIT ROY  A CHAR LCD DEMO"};
void main()
{
TRISB=0x00; //Made Port B out put port 
TRISC=0x00; //Made Port C out put port
//Initialization of LCD
__delay_ms(10);
__delay_ms(1);
PORTB=0x38; //to select the mode of the LED 0X38 - 8 bit 2 lines
command(); // Call command function, shows that the given values are commands and not datas
__delay_ms(1);
PORTB=0x0E; //to select the mode of the LED 0X38 - 8 bit 2 lines
command(); // Call command function
__delay_ms(1);
PORTB=0x06; // to activate entry mode
command();
__delay_ms(1);
//PORTB=0X0F; //to get a blinking cursor display
//command();
//__delay_ms(1);
//Initialization completed
__delay_ms(10);
for(i=0;i<(int)sizeof(a);i++)
{
if(i == 12)
{
PORTB = 192;
command();
__delay_us(40);
}
PORTB = a;
data(); //To show that the given values are data
__delay_us(40);
}
__delay_ms(30);
for(;;)
{
PORTB = 24;
command();
for(i = 0;i<10;i++)
{
__delay_ms(100);
}
}
}
void command()
{
 RC0=0;
 RC1=0;
 RC2=1;
 __delay_us(2);
 RC2=0;
}
void data() 
{
 RC0=1;
 RC1=0;
 RC2=1;
 __delay_us(2);
 RC2=0;
}

It has a very big mistake, I have used WRITEN rather WRITTEN because 16F84A really have very small space to store the characters, but in case 16F877A you should correct it.

I have commented out one of the command at initialization, its command for blinking cursor if you want it you can use it just remove the comment.

The LCD will be interfaced using 8 bits interface and PORTB is used for the data line and PORTC is used for command...RC0 is used for RS line,RC1 is used for R/W line and RC2 is used for Enable (E) line. Use 4 MHz for clock....A old photo of my first LCD test..

And I have also attached the MPLAB Project files...
Good Luck
 

Attachments

Last edited:

t06afre

Joined May 11, 2009
5,934
It has a very big mistake, I have used WRITEN rather WRITTEN because 16F84A really have very small space to store the characters, but in case 16F877A you should correct it
You can store the welcome screen in ROM. So you do not have to use up the RAM
 

debjit625

Joined Apr 17, 2010
790
t06afre said:
Also how do you connect your LCD to your PIC? do you have any schematics
Yes I have a schematic but its for 16F84A.....
I think its not needed,as its so simple and the data and command lines are already described on my post.But anyway its good thinking so I will try to make one schematic for 16F877A and add it later.

t06afre said:
You can store the welcome screen in ROM. So you do not have to use up the RAM
It is a very old program,that time I was just starting with mcu stuff and more it was just a testing code to understand and learn about interfacing char.. LCD with mcu.I thought it might help Jaden to understand initialization of LCD as its very easy.

I use my own library to do LCD jobs but that might be complex for the first time for Jaden.

Thanks and Good Luck
 

t06afre

Joined May 11, 2009
5,934
Yes I have a schematic but its for 16F84A.....
I think its not needed,as its so simple and the data and command lines are already described on my post.But anyway its good thinking so I will try to make one schematic for 16F877A and add it later.
Oh sorry I asked Jaden5165 for the schematics, not you. But feel free to post it. If you think it may help Jaden5165
 
Top