LCD code_HTC

Discussion in 'Programmer's Corner' started by Jaden5165, Sep 22, 2011.

  1. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    Code ( (Unknown Language)):
    1.  
    2. /*-------------------------- BEGIN ----------------------------*/
    3. #include<pic.h>
    4. __CONFIG(0x3F39);
    5. #define _XTAL_FREQ 4000000
    6. #define RS RB2
    7. #define EN RB1
    8. #define databits PORTD
    9.  
    10. /*----------------PORT SETTINGS-------------------*/
    11. void pic_init()
    12.     {
    13.      TRISB2=0;
    14.      TRISB1=0;
    15.      TRISD=0;
    16.     }
    17.  
    18. /*-----------------LCD FUNCTIONS ----------------------*/
    19. void LCD_STROBE(void)
    20.     {
    21.     EN = 1;
    22.     __delay_us(0.5);
    23.     EN = 0;
    24.     }
    25. void data(unsigned char c)
    26.     {
    27.     RS=1;
    28.     __delay_us(40);
    29.      databits = ( c >> 4 );
    30.     LCD_STROBE();
    31.     databits = ( c );
    32.     LCD_STROBE();
    33.     }
    34. void cmd(unsigned char c)
    35.     {
    36.      RS=0;
    37.     __delay_us(40);
    38.     databits = ( c >> 4 );
    39.     LCD_STROBE();
    40.     databits = ( c  );
    41.     LCD_STROBE();
    42.     }
    43. void clear(void)
    44.     {
    45.      cmd(0x01);
    46.     __delay_ms(2);
    47.     }
    48. void lcd_init()
    49.     {
    50.     __delay_ms(20);
    51.     cmd(0x30 );
    52.     __delay_ms(1);
    53.     cmd(0x30 );
    54.     __delay_ms(1);
    55.     cmd(0x30 );
    56.     cmd(0x28 ); // Function set (4-bit interface, 2 lines, 5*7Pixels)
    57.     cmd(0x0c); // Make cursorinvisible
    58.     clear(); // Clear screen
    59.     cmd(0x6); // Set entry Mode(auto increment of cursor)
    60.     }
    61. void string(const char *q)
    62.     {
    63.     while(*q) {data(*q++);}
    64.     }
    65.  
    66. /*------------------MAIN FUNCTION-----------------------*/
    67. main()
    68.     {
    69.     __delay_ms(50);
    70.     pic_init();
    71.     lcd_init();
    72.     while(1)
    73.         {
    74.         cmd(0x80);
    75.         string("Hello World");
    76.         cmd(0xc0);
    77.         string("My 16x2 LCD");
    78.         while(1);
    79.         }
    80.     }
    81.  /*---------------- END --------------------------*/
    82.  


    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: Sep 22, 2011
  2. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Jaden5165 likes this.
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
    Jaden5165 likes this.
  4. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0

    @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!
     
  5. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    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: Sep 23, 2011
    Jaden5165 likes this.
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Yes take a look at the recommened webseminars here
    http://forum.allaboutcircuits.com/showthread.php?t=44852
     
    Jaden5165 likes this.
  7. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    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
     
    Jaden5165 likes this.
  8. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    Code ( (Unknown Language)):
    1.  
    2. /*-------------------------- BEGIN ----------------------------*/
    3. #include<htc.h>
    4. __CONFIG(0x3F39);
    5. #define _XTAL_FREQ 4000000
    6. #define RS RB2
    7. #define EN RB1
    8. #define databits PORTD
    9.  
    10. /*----------------PORT SETTINGS-------------------*/
    11. void pic_init()
    12. {
    13. TRISB2=0;
    14. TRISB1=0;
    15. TRISD=0;
    16. }
    17.  
    18. /*-----------------LCD FUNCTIONS ----------------------*/
    19. void LCD_STROBE(void)
    20. {
    21. EN = 1;
    22. __delay_us(0.5);
    23. EN = 0;
    24. }
    25. void data(unsigned char c)
    26. {
    27. RS=1;
    28. __delay_us(40);
    29. databits = ( c >> 4 );
    30. LCD_STROBE();
    31. databits = ( c );
    32. LCD_STROBE();
    33. }
    34. void cmd(unsigned char c)
    35. {
    36. RS=0;
    37. __delay_us(40);
    38. databits = ( c >> 4 );
    39. LCD_STROBE();
    40. databits = ( c );
    41. LCD_STROBE();
    42. }
    43. void clear(void)
    44. {
    45. cmd(0x01);
    46. __delay_ms(2);
    47. }
    48. void lcd_init()
    49. {
    50. __delay_ms(20);
    51. cmd(0x30 );
    52. __delay_ms(5);
    53. cmd(0x30 );
    54. __delay_us(100);
    55. cmd(0x30 );
    56. cmd(0x28 ); // Function set (4-bit interface, 2 lines, 5*7Pixels)
    57. cmd(0x0c); // Make cursorinvisible
    58. clear(); // Clear screen
    59. cmd(0x6); // Set entry Mode(auto increment of cursor)
    60. }
    61. void string(const char *q)
    62. {
    63. while(*q) {data(*q++);}
    64. }
    65.  
    66. /*------------------MAIN FUNCTION-----------------------*/
    67. main()
    68. {
    69. __delay_ms(50);
    70. pic_init();
    71. lcd_init();
    72. while(1)
    73. {
    74. cmd(0x80);
    75. string("Hello World");
    76. cmd(0xc0);
    77. string("My 16x2 LCD");
    78. while(1);
    79. }
    80. }
    81.  



    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
     
  9. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0

    @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
     
  10. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
    Code ( (Unknown Language)):
    1.  
    2. [EMAIL="cmd@c"]cmd@c[/EMAIL]
    3.  
     
  11. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    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.
    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
     
    Jaden5165 likes this.
  12. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
  13. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    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.

    Code ( (Unknown Language)):
    1.  
    2. /*
    3. Author : Debjit Roy
    4. Info.. : 16x2 LCD Initialization testing
    5.        PORTB = Data line
    6.        RC0 = RS line
    7.        RC1 = R/W line
    8.        RC2 = Enable (E) line
    9. Date : 25,Sept,2011
    10. */
    11. [COLOR=black][FONT=Verdana]#include "htc.h"[/FONT][/COLOR]
    12. [COLOR=black][FONT=Verdana]#ifndef _XTAL_FREQ[/FONT][/COLOR]
    13. [FONT=Verdana][COLOR=black]#define _XTAL_FREQ 4000000[/COLOR][/FONT]
    14. [FONT=Verdana][COLOR=black]#endif[/COLOR][/FONT]
    15. [COLOR=black][FONT=Verdana]void command();[/FONT][/COLOR]
    16. [FONT=Verdana][COLOR=black]void data(); [/COLOR][/FONT]
    17. [COLOR=black][FONT=Verdana]int i;[/FONT][/COLOR]
    18. [FONT=Verdana][COLOR=black]char a[]={" WRITEN IN CBY DEBJIT ROY  A CHAR LCD DEMO"};[/COLOR][/FONT]
    19. [COLOR=black][FONT=Verdana]void main()[/FONT][/COLOR]
    20. [FONT=Verdana][COLOR=black]{[/COLOR][/FONT]
    21. [FONT=Verdana][COLOR=black]TRISB=0x00; //Made Port B out put port [/COLOR][/FONT]
    22. [FONT=Verdana][COLOR=black]TRISC=0x00; //Made Port C out put port[/COLOR][/FONT]
    23. [COLOR=black][FONT=Verdana]//Initialization of LCD[/FONT][/COLOR]
    24. [COLOR=black][FONT=Verdana]__delay_ms(10);[/FONT][/COLOR]
    25. [COLOR=black][FONT=Verdana]__delay_ms(1);[/FONT][/COLOR]
    26. [FONT=Verdana][COLOR=black]PORTB=0x38; //to select the mode of the LED 0X38 - 8 bit 2 lines[/COLOR][/FONT]
    27. [FONT=Verdana][COLOR=black]command(); // Call command function, shows that the given values are commands and not datas[/COLOR][/FONT]
    28. [FONT=Verdana][COLOR=black]__delay_ms(1);[/COLOR][/FONT]
    29. [FONT=Verdana][COLOR=black]PORTB=0x0E; //to select the mode of the LED 0X38 - 8 bit 2 lines[/COLOR][/FONT]
    30. [FONT=Verdana][COLOR=black]command(); // Call command function[/COLOR][/FONT]
    31. [FONT=Verdana][COLOR=black]__delay_ms(1);[/COLOR][/FONT]
    32. [FONT=Verdana][COLOR=black]PORTB=0x06; // to activate entry mode[/COLOR][/FONT]
    33. [FONT=Verdana][COLOR=black]command();[/COLOR][/FONT]
    34. [FONT=Verdana][COLOR=black]__delay_ms(1);[/COLOR][/FONT]
    35. [COLOR=black][FONT=Verdana]//PORTB=0X0F; //to get a blinking cursor display[/FONT][/COLOR]
    36. [FONT=Verdana][COLOR=black]//command();[/COLOR][/FONT]
    37. [FONT=Verdana][COLOR=black]//__delay_ms(1);[/COLOR][/FONT]
    38. [COLOR=black][FONT=Verdana]//Initialization completed[/FONT][/COLOR]
    39. [COLOR=black][FONT=Verdana]__delay_ms(10);[/FONT][/COLOR]
    40. [COLOR=black][FONT=Verdana]for(i=0;i<(int)sizeof(a);i++)[/FONT][/COLOR]
    41. [FONT=Verdana][COLOR=black]{[/COLOR][/FONT]
    42. [FONT=Verdana][COLOR=black]if(i == 12)[/COLOR][/FONT]
    43. [FONT=Verdana][COLOR=black]{[/COLOR][/FONT]
    44. [FONT=Verdana][COLOR=black]PORTB = 192;[/COLOR][/FONT]
    45. [FONT=Verdana][COLOR=black]command();[/COLOR][/FONT]
    46. [FONT=Verdana][COLOR=black]__delay_us(40);[/COLOR][/FONT]
    47. [FONT=Verdana][COLOR=black]}[/COLOR][/FONT]
    48. [FONT=Verdana][COLOR=black]PORTB = a[i];[/i][/COLOR][i][/i][/FONT][i]
    49. [FONT=Verdana][COLOR=black]data(); //To show that the given values are data[/COLOR][/FONT]
    50. [FONT=Verdana][COLOR=black]__delay_us(40);[/COLOR][/FONT]
    51. [FONT=Verdana][COLOR=black]}[/COLOR][/FONT]
    52. [COLOR=black][FONT=Verdana]__delay_ms(30);[/FONT][/COLOR]
    53. [COLOR=black][FONT=Verdana]for(;;)[/FONT][/COLOR]
    54. [FONT=Verdana][COLOR=black]{[/COLOR][/FONT]
    55. [FONT=Verdana][COLOR=black]PORTB = 24;[/COLOR][/FONT]
    56. [FONT=Verdana][COLOR=black]command();[/COLOR][/FONT]
    57. [COLOR=black][FONT=Verdana]for(i = 0;i<10;i++)[/FONT][/COLOR]
    58. [FONT=Verdana][COLOR=black]{[/COLOR][/FONT]
    59. [FONT=Verdana][COLOR=black]__delay_ms(100);[/COLOR][/FONT]
    60. [FONT=Verdana][COLOR=black]}[/COLOR][/FONT]
    61. [COLOR=black][FONT=Verdana]}[/FONT][/COLOR]
    62. [COLOR=black][FONT=Verdana]}[/FONT][/COLOR]
    63. [COLOR=black][FONT=Verdana]void command()[/FONT][/COLOR]
    64. [FONT=Verdana][COLOR=black]{[/COLOR][/FONT]
    65. [FONT=Verdana][COLOR=black] RC0=0;[/COLOR][/FONT]
    66. [FONT=Verdana][COLOR=black] RC1=0;[/COLOR][/FONT]
    67. [FONT=Verdana][COLOR=black] RC2=1;[/COLOR][/FONT]
    68. [FONT=Verdana][COLOR=black] __delay_us(2);[/COLOR][/FONT]
    69. [FONT=Verdana][COLOR=black] RC2=0;[/COLOR][/FONT]
    70. [FONT=Verdana][COLOR=black]}[/COLOR][/FONT]
    71. [COLOR=black][FONT=Verdana]void data() [/FONT][/COLOR]
    72. [FONT=Verdana][COLOR=black]{[/COLOR][/FONT]
    73. [FONT=Verdana][COLOR=black] RC0=1;[/COLOR][/FONT]
    74. [FONT=Verdana][COLOR=black] RC1=0;[/COLOR][/FONT]
    75. [FONT=Verdana][COLOR=black] RC2=1;[/COLOR][/FONT]
    76. [FONT=Verdana][COLOR=black] __delay_us(2);[/COLOR][/FONT]
    77. [FONT=Verdana][COLOR=black] RC2=0;[/COLOR][/FONT]
    78. [FONT=Verdana][COLOR=black]}[/COLOR][/FONT]
    79. [/i]

    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..
    [​IMG]
    And I have also attached the MPLAB Project files...
    Good Luck
     
    Last edited: Sep 25, 2011
  14. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Also how do you connect your LCD to your PIC? do you have any schematics
     
  15. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You can store the welcome screen in ROM. So you do not have to use up the RAM
     
  16. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    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.

    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
     
    Jaden5165 likes this.
  17. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Oh sorry I asked Jaden5165 for the schematics, not you. But feel free to post it. If you think it may help Jaden5165
     
    Jaden5165 likes this.
  18. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    Schematic....
    [​IMG]
     
    Jaden5165 likes this.
Loading...