EEVblog Electronics Community Forum

Electronics => Microcontrollers => Topic started by: donthack22 on March 06, 2019, 09:28:55 pm

Title: PIC F873A
Post by: donthack22 on March 06, 2019, 09:28:55 pm
Hello ! I am trying to implement a "Hello World" program on my picf873a. I did it in proteus and it works, but when i use it in reality it shows nothing. I use a 20mhz crystal quartz on pins 9 10, . I connected pins 8 and 19 to GND,pins 28 to 21 to DB7 to DB0. I also connected pin 12 to E and pin 11 to RS. RW is connected to ground, same as VSS. VDD is at 5v and i control V0 using a pot. A is on 5v and k is on ground aswell. I attached the program, maybe anyone could point me on what i am doing wrong.
Title: Re: PIC F873A
Post by: Andy Watson on March 07, 2019, 01:27:51 am
Schematic ? Or even some indication of what the PIC is driving ?

From your description I am guessing that you are using a 1620 type display, or one of its derivatives. These are slow devices - especially if of dubious provinence  - study the datasheet for the display and ensure that all your timings are very generous. In particular these displays can take considerable time to complete their reset cycle. Also, your "Enable" function appears to strobe the eneable signal in two instruction cycles of the PIC - with a 20MHz clock this is probably much too fast for the display.
Title: Re: PIC F873A
Post by: donthack22 on March 07, 2019, 09:10:27 am
I am driving a 1602A QAPASS. I ll try to make the enable function take longer.
Title: Re: PIC F873A
Post by: picandmix on March 07, 2019, 09:38:30 am
Have you got your VEE connected like this pic shows ?

When you rotate the 10k trimmer can you produce the 'blocks' as in the second pic or does the screen remain blank at all times ?

What are you programming your Pic chip with , and once programmed have you Read it back by doing a Verify to ensure its programmed in ok ?

What have you built the circuit on, a breadboard ?
 If so a common problem is that leads of the crystal and its caps are so thin they do not make good contact in the breadboard.
If so, solder then together onto some thicker wire and plug those into the board.
Always keep those wires as short as possible and close to the pics pins, not on long leads etc.

Title: Re: PIC F873A
Post by: donthack22 on March 07, 2019, 09:43:15 am
I use a pickit3 to rpogram, if i verify after i program it says "Verification succsesful". I do not see the black boxes no matter how i switch the pot, i only see the LCD turned on, with a blue screen.
Title: Re: PIC F873A
Post by: StillTrying on March 07, 2019, 10:14:42 am
It looks like there's only a 5ms delay between power up and sending commands to the LCD, 15ms to 100ms is recommended.
Title: Re: PIC F873A
Post by: picandmix on March 07, 2019, 10:15:31 am
If you are not getting those blocks then the screen is not working correctly.

Connections between the pic and lcd are a very common problem area for a first circuit,  so check your wiring very carefully  including shorts between the lcd pins etc.

To ensure your Pic circuit  is also working have you run a LED flasher program ?
Title: Re: PIC F873A
Post by: donthack22 on March 07, 2019, 11:51:43 am
I redid my code using the steps from here ( . I increased the delay at the begining. I also get a black boxes row, there was a faulty wire between pot and VEE.
Title: Re: PIC F873A
Post by: mikerj on March 07, 2019, 12:55:47 pm
Are you disabling the PGM pin in the config settings?
Title: Re: PIC F873A
Post by: picandmix on March 07, 2019, 01:13:26 pm
Ok,  good you have found the lcd fault.  :)

Best if you can load up this flasher code, it is what I have used before as a 4 second flasher, but using a 4mhz crystal, so have changed the OSC to HS for your 20mhz crystal which should flash about once per second.

You can measure the output on any port B pin with a DVM or led and resistor etc.

Title: Re: PIC F873A
Post by: donthack22 on March 07, 2019, 02:58:40 pm
Sadly the program doesnt work, so certainly something that i do is wrong. I uploaded the program in a new MPLAB IDE project and i CLEAN & BUILD it and i uploaded the hex file to the PIC through PICKIT3. I used a led that doesnt light up also i measured the outputs using a digital multimeter, and they are about 0.04 cosntant.
Title: Re: PIC F873A
Post by: donthack22 on March 07, 2019, 03:00:13 pm
No i did not. Can you explain me what it is and how to do that please. 
Title: Re: PIC F873A
Post by: donthack22 on March 07, 2019, 03:12:22 pm
My fault again, i forgot to connect the VDD pin. The program works, the led is flashing and the output is from about 0.30V to 4.9 V.
Title: Re: PIC F873A
Post by: donthack22 on March 07, 2019, 03:51:38 pm
I finally managed to do it guys. Thank you everybody, i listened to all of your advices and i fixed every issue. For future newbies as myself i will leave here a lsit of what i have changed hardware and software:

1) Check all connections and faulty wires (one of problems was a faulty wire, another one was forgetting the vdd connection)
2) Check that your microcontroller is working with a led flash program, like the one @picandmix provided, THANKS.
3) The pins of the Crystal were indeed too tiny for a breadboard, so i soldered them with some wire.
4) The enable sequence in my code was indeed too short for a 20MHz frequency, so i added a delay of 500ns routine that i called inside the enable routine (the enable should take above 450ns)
Title: Re: PIC F873A
Post by: picandmix on March 07, 2019, 04:43:50 pm
Well done !  :)

A couple of points,  the 16F873A is the smallest of that family so most small programs for the 874/876/877 will run on it ok, depending on how many i/o pins are needed.

You will find lots of example code for these old, but popular chips, though a 4mhz crystal is often needed for them, unless you modify the code.

The code you use is running in lcd 8 bit mode, many other lcd routines just use the 4 bit mode , so saving i/o usage.

This site l use 4 and 20mhz  examples and a 4 bit lcd routine, the author is still around for help. (

As you advance you my find some of the more modern  16F chips with internal oscillators more handy.
Title: Re: PIC F873A
Post by: macboy on March 08, 2019, 03:46:30 pm
4 bit mode is definitely worth implementing, to save 4 I/O pins. The two nibbles can be written back-to-back quickly so that the overall speed is hardly affected.

I also always like to implement R/W capability so that I can read the busy flag. This can substantially speed up writing to the display, since you can simply check for busy before writing each byte/command, rather than hard-coding a delay after writing. This allows the PIC to run other code (like determining the next character to display) while the display is busy, rather than just spinning it wheels burning away a fixed worst-case wait time.
Title: Re: PIC F873A
Post by: donthack22 on March 15, 2019, 11:13:31 pm
Hi again guy !  I am trying to implement the 4 bit connection and until now i just succeded to get weird characters on my lcd. Can u tell me please how can i send the letter which are writen with 8 bits, on a 4 bit connection?
Title: Re: PIC F873A
Post by: picandmix on March 15, 2019, 11:56:27 pm

Its all down to the lcd output routine you use,  with 8 bit routine the character is sent out in one 8 bit operation,  with a 4 bit routine the upper and lower 4 bits  are sent one after another and are reconnected by the displays internal program.

I have such a 4 bit lcd routine but its geared for a 4 mhz crystal, so unless you have one then look at that site I mentioned earlier which I think has a 4 bit 20mhz routine or search the web for a suitable one.
Title: Re: PIC F873A
Post by: mariush on March 16, 2019, 12:02:39 am
You also have to init the display differently

see page 10 and 11 for example code to init and send data: (
Note though that technically the 4bit code works, but it's not great.
What I mean is that they still use a full 8 bit port as a variable, so if you have only 4 of the 8 pins of that port connected to the LCD, the code would overwrite the data on the other 4 pins instead of just modifying only the pins connected to lcd.
You can change the code to get individual bits and put them on pins for example something like this 
Code: [Select]
// in a header file h file

    #define LCD_RS PORTEbits.RE0
    #define LCD_RW PORTEbits.RE1
    #define     LCD_EN PORTEbits.RE2

    // for 4 bit mode (can be on different ports)

    #define LCD_DATA0 PORTAbits.RA4
    #define LCD_DATA1 PORTAbits.RA5
    #define LCD_DATA2 PORTAbits.RA6
    #define LCD_DATA3 PORTAbits.RA7

   // second lcd_en = 1 is just a delay, to keep LCD_EN =1 for 500ns when clock is 16Mhz
   // may want to change this to a function to add a proper delay if running at different frequencies

    #define LCD_STROBE() ((LCD_EN = 1),(LCD_EN = 1), (LCD_EN=0))

// in a c file :

// moves the 4bit values into the 4 individual pins (when lcd in 4bit mode)  - not the most efficient but it does the job
void lcd_bits(unsigned char data) {
    LCD_DATA0 = data & 1;
    LCD_DATA1 = (data >> 1) & 1;
    LCD_DATA2 = (data >> 2) & 1;
    LCD_DATA3 = (data >> 3) & 1;
void lcd_write_4bit(unsigned char c) {
    LCD_RS = 1;
    LCD_RW = 0;
    lcd_bits(c >> 4);
    lcd_bits(c );
    LCD_RS = 0;
    delay_50us(); // custom delay function i wrote for 16 mhz, you can use your own or you can wait until lcd says it's no longer busy.

may find more useful data in controller datasheet (these displays use various chips that clone a hitachi chip,
so command set is same, timings more or less same, but it's good idea to be conservative)

Link: (
See pages 23-28 for detailed  4bit and 8 bit initialization and sending data info.
Title: Re: PIC F873A
Post by: picandmix on March 16, 2019, 04:29:44 pm
If you have not sorted things, this works fine with my 873a and 20mhz xtal.

Might seem a bit different to the code you already have, but lots of slightly different ways of handling the users data etc.

Title: Re: PIC F873A
Post by: donthack22 on March 16, 2019, 08:44:58 pm
Thank you guys !
Title: Re: PIC F873A
Post by: picandmix on March 18, 2019, 08:45:13 pm
Did that 4 bit code work for you ?
Title: Re: PIC F873A
Post by: donthack22 on March 19, 2019, 11:43:13 am
Sorry, i didnțt try your code. I am trying to do a project i found on the internet and it uses a 4bit LCD. In this project there is also a code provided, but there was a different LCD used. So i read your code and changed the project code, and it seems to work, but i didn't try your code separately.
Title: Re: PIC F873A
Post by: picandmix on March 19, 2019, 04:31:30 pm
No problem... :)