Author Topic: PIC control LCD 1602A & programming with HI-tech C  (Read 3440 times)

0 Members and 1 Guest are viewing this topic.

Offline singlarrysongTopic starter

  • Contributor
  • Posts: 34
PIC control LCD 1602A & programming with HI-tech C
« on: January 03, 2017, 10:37:52 am »
I always try not to disturb you people here with my easy question by try to dig solutions by myself, but today I fed up again with LCD.
With almost 3 years of microcontroller experience I suppose to learn how to program LCD in C, unfortunately no as I learn flowcode in my school  :palm:
So I struggle every time exploring new modules, like last time I ever ask how to make alphabet words input into ASCII and sent to a vintage VFD.
Yes, this is my second experience on display but it is a LCD with newer tech and instruction compare to my VFD.
Skip my story here.....

My LCD model is 16 pins I/O(incl a LED anode and cathode) 1602A and I had research that it instructions are same as the commonly used Hitachi HD44780U 16x2 display.
I had attach the datasheet I refers to and comparing other 1602 datasheet I get similar display characteristic.
below is my code

Code: [Select]
#include <htc.h>
#include <pic.h>

#define _XTAL_FREQ 1000000

void write(unsigned char RS , unsigned char data)
{
RA7 = 0; //R/W
RA0 = RS; //RS
__delay_us(0.15); //RS and RW setup time
RA6 = 1; //EN
LATB = data;
__delay_us(0.3); //Tw, minimum EN rise time
__delay_us(0.01); //Th2, minimum wait time for data clear after EN fall
LATB = 0x00;
__delay_us(0.19); //Tc, sum from Tw for minimum cycle time of EN
}

void init_LCD(void)
{
TRISA = 0x00; //RA0 is RS, RA7 is R/W, RA6 is EN
TRISB = 0x00; //8 bit data port
LATA = 0x00;
LATB = 0x00;

//ASSUME INITIAL FUNCTION MODE IS 4 Bits
// write(0 , 0b00111000); //set functions, sent first 4 MSB bits     8bits bus mode, 2 lines display, 5*8 dots format
// write(0 , (0b00111000<<4)); //set functions, send last 4 LSB bits

//ASSUME INITIAL FUNCTION MODE IS 8 Bits
write(0 , 0b00111000); //set functions, 8bits bus mode, 2 lines display, 5*8 dots format

write(0 , 0b00000001); //clear display
write(0 , 0b00000010); //return cursor to home
write(0 , 0b00001110); //display on/off control, display on, cursor on, cursor blink off

}

void main(void)
{
OSCCON = 0b01011010; //1MHz Fosc, Int Osc module

__delay_ms(400);
init_LCD();

write(0, 0b01000101); //try to print a alphabet first, "E" in ASCII

for(;;)
{
}
}

PROBLEM is here
I try a few codes from Hi-Tech C sample code to code that I get from the internet(modify their I/O config to specific pic I use)...
The codes had a successful built and programmed into my PIC.
but then I still get a blank on my LCD(attached picture)

so I wonder anyone had try 1602A LCD and give my code correction or a sample display test code.
This is my first LCD and I really afraid that it is broken.

Picture LCD : when the power is off the screen appear dark... the bottom part show slightly poor contrast as my V0 are tuned by 10k ohm resistor, both device are power up by USB power.
Picture VFD : This VFD is about 27 years old, so the controller is still using NEC 8041 where lots of SIPO to multi task the matrix.
LCD command from website : http://mil.ufl.edu/3744/docs/lcdmanual/commands.html
 
 

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1719
  • Country: se
Re: PIC control LCD 1602A & programming with HI-tech C
« Reply #1 on: January 03, 2017, 10:54:59 am »
Code: [Select]
#include <htc.h>
#include <pic.h>

#define _XTAL_FREQ 1000000

void write(unsigned char RS , unsigned char data)
{
RA7 = 0; //R/W
RA0 = RS; //RS
__delay_us(0.15); //RS and RW setup time
RA6 = 1; //EN
LATB = data;
__delay_us(0.3); //Tw, minimum EN rise time
__delay_us(0.01); //Th2, minimum wait time for data clear after EN fall
LATB = 0x00;
__delay_us(0.19); //Tc, sum from Tw for minimum cycle time of EN
}


The first thing I notice is that the E line (RA6) is never reset to 0 in your write function, so the write will never be actually performed (the 44780 latches data on falling edge of E).
Consider also that E must stay high for at least 450ns: I don't do PICs so I'm not aware of the resolution and precision of the delay functions.
Nandemo wa shiranai wa yo, shitteru koto dake.
 
The following users thanked this post: singlarrysong

Offline void_error

  • Frequent Contributor
  • **
  • Posts: 673
  • Country: ro
  • I can transistor...
Re: PIC control LCD 1602A & programming with HI-tech C
« Reply #2 on: January 03, 2017, 11:09:22 am »
This is what I found when I started playing with those character LCDs: https://electrosome.com/lcd-pic-mplab-xc8/
It's using the XC8 compiler though. Shouldn't be too different from Hi-Tech C.
Trust me, I'm NOT an engineer.
 

Offline singlarrysongTopic starter

  • Contributor
  • Posts: 34
Re: PIC control LCD 1602A & programming with HI-tech C
« Reply #3 on: January 03, 2017, 01:56:10 pm »
I try to put RA6 = 0 into the section but it still the same
Code: [Select]
void write(unsigned char RS , unsigned char data)
{
RA7 = 0; //R/W
RA0 = RS; //RS
__delay_us(0.15);
RA6 = 1; //EN
LATB = data;
__delay_us(0.3); //Tw, minimum EN rise time
RA6 = 0;  //clear EN
__delay_us(0.2); //Th2, minimum wait time for data clear after EN fall
LATB = 0x00;
//9__delay_us(0.19); //Tc, sum from Tw for minimum cycle time of EN
}





https://electrosome.com/lcd-pic-mplab-xc8/
I had check this page out early, but the problem is the lcd.h. They said
Quote
But we solved this problem by creating a header file lcd.h which includes all the commonly used functions using 4 bit mode. Just include it and enjoy.

so the key is the LCD.h

 

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1719
  • Country: se
Re: PIC control LCD 1602A & programming with HI-tech C
« Reply #4 on: January 03, 2017, 02:57:59 pm »
I try to put RA6 = 0 into the section but it still the same

Ok, that was just the first comment...there are other issues with the code though.
  • To make sure the HD44780 is correctly initialized, the initial setup should be done according to the attached picture: the delays and function set [0b0011xxxx] repetitions are missing
  •    write(0, 0b01000101);    //try to print a alphabet first, "E" in ASCII
    RS should be set to 1, otherwise you are still writing to the control register, rather than the display RAM.
  • The timings in the write routine are not according the datasheet, assuming you can have such a fine control (I frankly doubt it, if the clock is actually 1MHz). For tests, you can have them as high as you fancy, the HD44780 won't care as long as they are longer than the minimum.

Of course, check and re-check the HW connections...

Hope this helps.
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline JanJansen

  • Frequent Contributor
  • **
  • Posts: 380
  • Country: nl
Re: PIC control LCD 1602A & programming with HI-tech C
« Reply #5 on: January 03, 2017, 05:42:40 pm »
Here is the first datasheet in google : https://www.openhacks.com/uploadsproductos/eone-1602a1.pdf
I suggest write all code yourself, dont copy.
Use write only no reading, start with 8 bit.

From my head :
First small delay.
Then set mode : 8 bit mode, datasheet command 0b110000.
Clear screen,  datasheet command 0b1, then wait long.
activate display,  datasheet command 0b1100.

for instruction : set dataport, then set enable, wait for 7us, clear enable, wait 27us.

Your screen aint broken, i thought the same, only bad code.
aliexpress parachute
 

Offline singlarrysongTopic starter

  • Contributor
  • Posts: 34
Re: PIC control LCD 1602A & programming with HI-tech C
« Reply #6 on: January 06, 2017, 03:02:03 pm »
Sorry for the late.

I had follow the flowchart and try the instruction but still get blank. The contrast of the display flash for less than a second and everything went blank.

From my head :
First small delay.
Then set mode : 8 bit mode, datasheet command 0b110000.
Clear screen,  datasheet command 0b1, then wait long.
activate display,  datasheet command 0b1100.


Here is the code. As I don't read busy flag, so I put longer on those delay, such as from datasheet min 37us, I give 50 etc etc
Code: [Select]
void write(unsigned char RS , unsigned char data)
{
RA7 = 0; //R/W
RA0 = RS; //RS
__delay_us(0.15);
RA6 = 1; //EN
LATB = data;
__delay_us(0.3); //Tw, minimum EN rise time
RA6 = 0;
__delay_us(1.2); //Th2, minimum wait time for data clear after EN fall
LATB = 0x00;

__delay_us(50); //Write data delay
}

void init_LCD(void)
{
__delay_ms(500); //Power up delay

TRISA = 0x00;
TRISB = 0x00;
LATA = 0x00;
LATB = 0x00;

write(0 , 0b00110000); //8 bits mode
__delay_ms(450);
write(0 , 0b00110000); //8 bits mode
__delay_us(100);
write(0 , 0b00110000); //8 bits mode
__delay_us(100);

write(0 , 0b00110000); //bits 3, 2 lines mode
__delay_us(50);

write(0 , 0b00001100); //entire dislay on, cursor on, cursor blinking
__delay_us(50);

write(0 , 0b00000001); //clear display
__delay_ms(2);

write(0 , 0b00000110); //entry mode
__delay_us(50);
}

void main(void)
{
OSCCON = 0b01111010; //16MHz Fosc, Int Osc module


init_LCD();

write(1, 0b01000101); //try to print "E" first

for(;;)
{
}
}
 

Offline Buriedcode

  • Super Contributor
  • ***
  • Posts: 1611
  • Country: gb
Re: PIC control LCD 1602A & programming with HI-tech C
« Reply #7 on: January 06, 2017, 10:18:23 pm »
Have you tried using a pot to control the contrast?  Some displays I've used are fine with 10K to 0V, but were hard to read, whilst others this was too much, and I had to use a pot, which is set quite close to 0V (hooked up to 5V and 0V).

Also, increase your enable line time to say 10us. Thats a lot, but yuo're makign Enable high, writing to your port, then waiting 1.2us, then pulling it low.  That sounds long enough to me, but it is something to check.

Set RS.
Write data to port.
Make E high.
wait 10us.
Make E low.

If that works then you know its a timing issue.

Also, what chip are you using?  Some PIC's have peripherals enabled by default, and may interfere with writing to PORTB.  In other words - post your entire code :)
 

Offline JanJansen

  • Frequent Contributor
  • **
  • Posts: 380
  • Country: nl
Re: PIC control LCD 1602A & programming with HI-tech C
« Reply #8 on: January 07, 2017, 04:41:24 pm »
Please define your pins also to make more readable.
aliexpress parachute
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2850
  • Country: se
  • Country: Broken Britain
Re: PIC control LCD 1602A & programming with HI-tech C
« Reply #9 on: January 07, 2017, 05:43:16 pm »
RA7 = 0;   //R/W
   RA0 = RS;   //RS
   __delay_us(0.15);
   RA6 = 1;   //EN
   LATB = data;


Although some LCDs say they latch RS and DATA on the different edges of EN, don't toggle EN until the after the DATA is there.
« Last Edit: January 07, 2017, 05:45:09 pm by StillTrying »
.  That took much longer than I thought it would.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf