LCD PIC16F84 MPLAB - Page 1
Forum section: electronics / beginners

author: neko efecktz
author_rank: Regular Contributor
timestamp: June 13, 2016, 12:10:48 am
content: 20 May 16originally posted about two weeks ago MORE QUESTIONS.Please help me if anyone can. This may get lengthy.I think I have bitten off more than I can chew.About me...I'm 63 years old and not getting any younger.Total newbie to pics.A few weeks ago I decided to look into programming a few Pics to drive a 2 x 16 LCD.I have done some visual basic programming (many years ago) but decided to go the full hog and buy a Pickit 3 clone and learn C.How many versions of C are there?While I waited for it to arrive I downloaded MPLAB X IDE.I found that it has no or very little support for 2 x 16 LCD so I uninstalled it and found it didn't uninstall completely. It left a few folders and files behind. These are outlined in a previous post.I installed Microchip XC8 v1.37 and with a little help managed to get it running.I have so far been unable to get a program to work.I have downloaded complete projects including header files etc and still get errors.I have watched videos and using the Windows Snipping Tool (very handy tool) pause, copy / past to word and then printed it so I could type the code in.Just about every line in the code has an error mark in the line number column.Most of them have a message when I hover the mouse over the error flag likeUnable to resolve identifier Nop. and nop and NOPUnable to resolve identifier SetDDRamAddr.When I try to build the program I always seem to get 2 errors that always seem to be the same.A snap shot included.I have also copy/pasted displayed files with the same results.My main idea is to have the Pic monitor various parts of my power supply.It comprises of two power supply boards running from 2 separate transformers i.e.two completely separate Supplies.MonitoringOver current in both positive and negative supplies taken from the current limit led circuit.Variation of over say .25 volts between supplies using a window comparatorHeat sink temperature etc. Or at least over temperature.Every input will have an Opto Coupler for full isolation.The chips I have had for a few years bought in a bulk lot of components are PIC 16F84 20meg. NOT PIC16F84A...Once I get a couple of projects working I will buy bigger and more versatile chips and try to do silly things like read the voltages and temperature and have this displayed and perhaps even a tiny speaker as an alarm just in case I don't see there is an alarm condition.13 June 16have had a couple of clean compiles but so far all I have been able to do is get a program into a chip but it doesn't run or even oscillate.I am able to upload hex files to the chip and download from the chip.Silly question time.If I downloaded an earlier version than XC8 v1.37 would this help me get all of the required header files etc ?If so what would be a good version to try?Do software creators get their jollies by only issuing 3/4 of the header files or something.Will pickit3 work with other software other than MPLAB.Is there free version of Hitec C that won't stop working after 14 days.There is a full version of the LCD driver I am trying to create as a demo in HiTec but this will no longer compile as HiTec has stopped working after 2 weeks.I get a clean compile and am able to upload it into a chip but it does nothing.I have tried to find a simple blinking light program for pic16f84 / mplab, to make sure I am doing everything right, but even they seem to be hard to find.How the hell does anyone learn this language.Many sample programs I have found for blinking lights are in assembly. Don't want to try to learn another language at my time of life.I eagerly await any help I can get.

author: Ian.M
author_rank: Super Contributor
timestamp: June 13, 2016, 01:23:41 am
content: The PIC16F84 was released twenty years ago, back in 1996. Back when the 'F84 was popular, the available C compilers *SUCKED* and/or were very expensive so most 'F84 sample code is in assembler and, even if written for Microchip MPASM, with 20 years of development in-between, may not even build with the current MPASMX without quite a bit of fiddling.Not surprisingly, back in 2012, when XC8 v1.00 was first released, Microchip's XC8 compiler team had better things to do than writing demo code for a (then) 16 year old chip. Instead they wrote code for recently released chips there was still an active marketing budget for.However Microchip bought HiTech lock stock and barrel and the core XC8 development team started out as the same people who had been the HiTech C for PIC10/12/16 and for PIC18 team, still has many of the same staff, and XC8 was developed from the HiTech C code-base so there is a very high level of compatibility. There is a special #define needed to enable compatibility mode with HiTech C for PIC10/12/16 v9.80 or earlier. Unfortunately documentation for this was dropped, but if your compiler has a folder 'legacy' under the folder 'include', it should still be supported. Here's what the HiTech C for PIC10/12/16 v9.83 readme had to say about it:QuoteWith _LEGACY_HEADERS defined, XC8 will compile *most* older HiTech C code.A PIC16F84 has to have either an external clock*, or a RC circuit on OSC1 for RC mode or a crystal*+ appropriate load caps on OSC1 and OSC2 for LP, XT or HS modes. If your chip isn't even running, the most probable cause is failure to correctly define the CONFIG word that sets up stuff like oscillator modes. The next most probable cause is a bad oscillator circuit.* The clock or crystal must be less than or equal to the PIC's maximum frequency rating (two digits between '-' and '/' in the part no. give frequency in MHz).N.B. if your PIC is marked PIC16F84-20/... it is almost certainly fake as Microchip didn't introduce a 20MHz version till the PIC16F84A. Valid speed codes are -04 and -10.Microchip recently dropped the legacy PIC18 PLIB from the XC8 install, but that shouldn't affect you as it never supported PIC16. Its still available as a separate download if you have an old PIC18 project that needs it.Post the code you are trying to compile (in code tags please!), and the compiler errors from the MPLAB 8 or X output window, then we've got some chance of helping you.I suggest starting with something simple like a 'hello world' blinky LED program. Make sure the hardware is adequately documented - either show a schematic or list the exact pin connections in comments in the code. e.g.:Code:// /MCLR - 10K - Vdd// OSC1 - (22pF - Vss), - (1) 4MHz Xtal// OSC2 - (22pF - Vss), - (2) ''//// RB0 - (k) Red LED (a) - 330R - VddOnce you have the absolute basics mastered, there is considerable benefit in switching to a more modern PIC. Many of the newer ones support in-circuit debugging, its considerably easier to find sample code that builds with current tools, and they generally offer far more processing power, memory, and built in peripherals at a lower cost.

author: ez24
author_rank: Super Contributor
timestamp: June 13, 2016, 02:04:36 am
content: If you are open to other ideas take a look here for materialhttps://www.eevblog.com/forum/beginners/electronics-primers-course-material-and-books/and if STM32 is worth looking athttps://leanpub.com/mastering-stm32I bought this and it currently is at 587 pages at 60% with free updates. The author is a member here. The board he uses is less than $20. There is a sample of the book.It is not easy to find beginner material on MCUs, 99.999% of the members here are experts (I am part of the .001 %)

author: Skimask
author_rank: Super Contributor
timestamp: June 13, 2016, 02:19:56 am
content: ...or just buy one of the various flavors or Arduino boards and be done with it...

author: MarkF
author_rank: Super Contributor
timestamp: June 13, 2016, 03:08:34 am
content: It's not clear to me that you've properly install you development environment.1) Install the latest version of MPLAB X2) Install the latest version of XC8 complierI've been doing all my programming with a PICkit 2. Recently I bought a PICkit 3 and found that for most 5V PIC chips it doesn't have enough power to drive the PIC. You need to supply the 5V externally. I've been using the pic16f877a, pic16f876a and th pic18f2550 without problems.Your first program should be something simple. Try to just toggle one bit. Also, confirm your config bits, oscillator setup, TRIS direction bits and whether your using LAT or PORT for output. For most chips, you want to use LAT for output and PORT for input. However, on the pic16f87x chips there is no LAT register and you must use PORT for both input and output.Post your code please.

author: MarkF
author_rank: Super Contributor
timestamp: June 13, 2016, 03:14:50 am
content: Here's a blinking light example. Make sure you tie MCLR pin high through a 10K resistor.Code:/** File: main.c* Author: Mark** Created on June 12, 2016, 11:45 PM*/#include <xc.h>#include <stdint.h>// CONFIG#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)#pragma config WDTE = OFF // Watchdog Timer (WDT disabled)#pragma config PWRTE = ON // Power-up Timer Enable bit (Power-up Timer is enabled)#pragma config CP = OFF // Code Protection bit (Code protection disabled)#define _XTAL_FREQ 10000000L // 10MHz clock#define delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000000.0)))#define delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))void main(void) {uint8_t led=0;// Configure I/O pinTRISAbits.TRISA0 = 0; // Select RA0 pin as outputwhile (1) {PORTAbits.RA0 = led; // Set I/O bitif (++led > 1) led=0; // Toggle LED valuedelay_ms(500); // Wait 500ms}}

author: batteksystem
author_rank: Regular Contributor
timestamp: June 13, 2016, 05:49:34 am
content: Quote from: MarkF on June 13, 2016, 03:14:50 amI would change this to use internal RC Oscillator to avoid any hardware problem with the external crystal circuit.

author: JPortici
author_rank: Super Contributor
timestamp: June 13, 2016, 06:26:57 am
content: Quote from: batteksystem on June 13, 2016, 05:49:34 amI may have missed something but the OP says he is using a 16F84to the OP, if you want it "easier" to begin with, you might want to try MikroC from MikroElektronika. It has a lot of libraries for handling the peripherals and external hardware (for example, there is a library for alphanumeric LCDs). Free to use for ehr.. under 2k of code IIRCThe pickit 3 can be used with the MPLAB X IDE (of course), MPLAB X IPE (to only upload/flash/erase the chip memory), MPLAB 8. I'm not sure about other software to control it, so for example, you write your code into mikroc, compile, then open the ipe and flash the chip.Perople who learn this try not to use a 20 year old chipbut even if they do, they read the datasheet as everything is in there. especially for these old parts that have little more than the core, the IOs and a couple of timers you can read the datasheet back to back in 30 minutes or lessPeople who fail at this usually go grab an arduino.. or go back at it

author: neko efecktz
author_rank: Regular Contributor
timestamp: June 13, 2016, 07:25:00 am
content: THANKYOU EVERYONE FOR YOUR HELPTHE ONLY REASON I AM USING THE PIC16F84 IS I HAPPENED TO HAVE THEM BOUGHT IN A BULK LOT OF COMPONENTS FROM A SHOP THAT WAS CLOSING DOWN.TURNS OUT THE SPECK SHEET THAT CAME WITH THEM WAS WRONGIT STATED PIC16F84-20THEY ARE (GETS OUT MAGNIFYING GLASS AND WETS THE CHIP, TURNS TO THE RIGHT ANGLE TO CATCH THE LIGHT) PIC 16F84A-20/PI MANAGED TO GET A FLASHING LED GOING FROM THE CODE UPLOADED BY MARKFON THE FIRST ATTEMPT IT WOULDN'T WORK.CHECKED THE LAYOUT AND FOUND THE LED BACK TO FRONT.STILL DIDN'T WORKI HAD A CRAZY IDEA. WHAT IF IS MY BREADBOARD.BRAND NEW BUT YOU NEVER KNOW.REBUILT IT ON ONE I'VE HAD FOR OVER 30 YEARSBINGO, ONE BLINKING LED.2 BRAND NEW BREADBOARDS FROM CHINA WORTHLESS.LESSONS LEARNED NOT TO TRUST BREADBOARDS FROM CHINA, PRICELESS.I WILL DRAG OUT A LARGER DOUBLE BREADBOARD THAT STILL HAS AN OLD GUITAR EFFECT PROJECT ON IT FROM A COUPLE OF YEARS AGOREBUILD THE LCD PROJECT AND HOPEFULLY MAY GET SOMEWHERE.

author: Ian.M
author_rank: Super Contributor
timestamp: June 13, 2016, 07:53:08 am
content: Athough the PIC16F84A is 100% binary code compatible with the PIC16F84, there are some subtle differences in the programming algorithm, so it is critically important that you have the correct PIC selected in MPLAB.If you have problems building the LCD demo, post the code (in code tags or a link to it if its already on the WWW) and the error messages you are getting.If you've got questionable solderless breadboards it is always worth powering off and double-checking continuity for any suspect pins between the chip or other component pin and a wire inserted in a spare hole in the same strip using a DMM. Beware of longer breadboards that have the power rails at the long edges split into two sections, each half the length of the board!

author: neko efecktz
author_rank: Regular Contributor
timestamp: June 13, 2016, 12:24:50 pm
content: GIDDAY IAN PLEASE EXCUSE THE CAPS, NOT SHOUTING, JUST A LAZY TYPIST BUT I TRY TO MAKE SURE THAT THE CODE IS CORRECT..HAVE BUILT UP THE PROJECT ON THE OLD BREAD BOARD BUT IT STILL ISN'T WORKING.THE DISPLAY IS BLANK AND I SUSPECT THE OSCILLATOR IS ALSO POT WORKING.THE CODE IS THE ONE I FOUND IN THE HITECH DEMOS.I HAD TO CHANGE THE PORT DETAILS TO SUIT THE PIC16F84A AND I'M SURE I GOT IT ALL.I WILL HAVE ANOTHER GO TOMORROW MORNING AND IF UNSUCCESSFUL WILL UPLOAD THE CODE AND ANOTHER ONE THAT IS SUPPOSED TO BE MPLAB, IF I CAN FIND IT.I'M A SHIFT WORKER 2:30pm TILL 12:20am SO YOU MAY NOT HEAR FROM ME UNTIL WEDNESDAY.PUBLIC HOLIDAY HERE IN AUSTRALIA TODAY.HOW DO I SET UP A CODE TAG?

author: JPortici
author_rank: Super Contributor
timestamp: June 13, 2016, 12:34:02 pm
content: [ code ] insert code here [ /code ]but without the spaces between brackets it becomes thisCode:insert code here

author: Ian.M
author_rank: Super Contributor
timestamp: June 13, 2016, 03:35:07 pm
content: Quote from: neko efecktz on June 13, 2016, 12:24:50 pmIts taken you two weeks to get your questions an active audience, so don't make your posts harder to read, lest your audience deserts you. Unjam your Caps Lock key. C is case sensitive, so are we . . . . .How To Ask Questions The Smart Way

author: ez24
author_rank: Super Contributor
timestamp: June 13, 2016, 05:21:59 pm
content: Quote from: neko efecktz on June 13, 2016, 12:24:50 pmToo hard to read

author: neko efecktz
author_rank: Regular Contributor
timestamp: June 13, 2016, 10:43:35 pm
content: Gidday Ian, sorry about the capsI Have built up the project on the old bread board but it still isn't working.The display is blank and I suspect the oscillator is also not working.The code is the one I found in the Hitech demos.I had to change the port details to suit the PIC16F84A and I'm sure I got it all.There are a lot of #include which lead to more #includeI will have another go tomorrow morning and if unsuccessful will upload the code and another one that is supposed to be Mplab, if I can find it.I'm a shift worker 2:30pm till 12:30am so you may not hear from me until Wednesday.How do I set up a code tag?thankyouBill

author: Skimask
author_rank: Super Contributor
timestamp: June 13, 2016, 11:05:16 pm
content: Quote from: neko efecktz on June 13, 2016, 10:43:35 pmOnly one way to find out...measure it.Start with a blinky LED and go from there.If you can't make a blinky LED, you sure can't go much farther...QuoteSee reply #11.

author: Ian.M
author_rank: Super Contributor
timestamp: June 13, 2016, 11:12:02 pm
content: Was the LCD code bundled with the HiTech C install (if so, which version) or did you find it on the web? I'd add a LED on a spare pin, and a function to flash it once, say 200ms On, 300ms Off. Call the LED flash at the beginning of main() and after every LCD function that main() calls. You'll soon see if its running at all or getting hung up somewhere just by counting flashes.If you are using the full forum editor (not the quick reply) simply click thebutton which insert an empty pair of code tags for you then copy/paste your source code where it left your cursor, between ][. You can also attach the source file as this forum accepts .c and .h files. For larger programs, and more obscure bugs it can be helpful to package the project (as a .zip file) from within MPLAB and attach that so that all MPLAB/XC8 users here can build your project, but as we then have to unzip the project before we can look at the source, its less appropriate for quick questions. Often the best bet is to post the function or section of the code you are asking about in code tags with the full source attached either as individual .c and .h files for a small project or as a packaged project .zip file for larger multi-file projects.

author: MarkF
author_rank: Super Contributor
timestamp: June 14, 2016, 01:09:56 am
content: Quote from: batteksystem on June 13, 2016, 05:49:34 amI don't use a crystal. I always use aStandard Clock Oscillator.neko efecktz:The pic16f84 doesn't have any analog inputs. At a minimum, you will want a PIC with analog inputs and create a voltage divider with 2 resistors to limit the PIC input to 0-5 volts from whatever the power supply voltage is.

author: batteksystem
author_rank: Regular Contributor
timestamp: June 14, 2016, 01:17:15 am
content: Quote from: MarkF on June 14, 2016, 01:09:56 am

author: CJay
author_rank: Super Contributor
timestamp: June 14, 2016, 10:27:52 am
content: Basically, what all the rest of the replies have said, get a basic circuit working first, doesn't matter if you write the code or not, just make sure your hardware works.But, my opinion, for a beginner who is new to the IDE, new to the programmer, new to the hardware, I would always recommend buying a built dev board of some description, it removes so much of the doubt when you have one definite, it's a handy sanity check.Microchip have some cute little demo boards intended to be used with PICKit3 and they're not too expensive (they're not cheap compared to some) considering you can rely on them to work out of the box (they usually come with demo code to chase or blink LEDs programmed into them)I'd also recommend ditching the 'F84 and getting something that's got much better support for debugging, C and the newer libraries but whatever you choose will have many more peripherals so it will require more 'setting up' in code to make the ports do sensible things.

author: neko efecktz
author_rank: Regular Contributor
timestamp: June 14, 2016, 11:43:31 pm
content: 15 JUNE 16Hello all,Thank you for your responses regarding my problem.Thank you MarkF for your code.Ian.M asked for the Hi-Tech code I've been trying to use.I am attempting to link in a RAR file of the original project folder supplied with hi-tech version 9.60I have also included the mplab project folder I have created.There are as few places I have had to change the ports from portd to portbAnd ADCON1 = 0x06; // Disable analog pins on PORTA.rar file was disallowed trying to find out what compressed file types are allowed.I am sending a zip file instead.Tried to send the file yesterdays but I didn't get to as I had to go to work.

author: Ian.M
author_rank: Super Contributor
timestamp: June 15, 2016, 12:13:56 am
content: The PIC16F84A doesn't have an ADCON1 SFR. Delay routines from HiTech C for PIC10/12/16 v9.65 or earlier that don't use the _delay() psuedofunction aren't to be trusted under HiTech C for PIC10/12/16 v9.81 or later or any version of XC8.Also I cant see the PIC's CONFIG setting anywhere in the source. Without proper CONFIG settings the 'F84A will default to external RC oscillator with the WDT enabled which will either result in it not running or repeatedly resetting.

author: Skimask
author_rank: Super Contributor
timestamp: June 15, 2016, 12:58:45 am
content: Time must be worth nothing.Could've bought some sort of cheap Arduino knockoff and been miles and miles ahead of the game by now...in only a couple of weeks.As much as I love my PICs and straight assembly programming for those PICs, I've all but switched camps. Haven't looked back and got more done in far less time since doing so.Got a bunch of PICs that'll be going on ebay sooner or later too.

author: neko efecktz
author_rank: Regular Contributor
timestamp: June 15, 2016, 01:12:39 am
content: EUREKA...It's workingThank you so much.I just pasted the code sent by MarkF I think it was into main.c..once again I'd like to thank everyone for their help.I may start to look at Arduino or Cherry pi in the future but then that's another code to learn, bu isn't that what life's forAll The Best.BILL.// CONFIG#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)#pragma config WDTE = OFF // Watchdog Timer (WDT disabled)#pragma config PWRTE = ON // Power-up Timer Enable bit (Power-up Timer is enabled)#pragma config CP = OFF // Code Protection bit (Code protection disabled)#define _XTAL_FREQ 10000000L // 10MHz clock#define delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000000.0)))#define delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))

author: Skimask
author_rank: Super Contributor
timestamp: June 15, 2016, 02:13:45 am
content: [quote author=neko efecktz link=topic=69626.msg962290#msg962290 date=1465953159I may start to look at Arduino or Cherry pi in the future but then that's another code to learn, bu isn't that what life's forTrue, but whatever code you'D write with the Arduino IDE is a helluva lot more human readable than the mess required to keep anything in the PIC world happy.I only know what it is because I've been using it for almost a couple decades. If I had to do it again, it would be via an Arduino type of thing.

author: Ian.M
author_rank: Super Contributor
timestamp: June 15, 2016, 02:17:33 am
content: That would fix it. The default RC mode doesn't work with crystals!For future reference: MPLAB X has a GUI wizard that lets you pick all the CONFIG options you want for the PIC type in the active project, then you can click a button to generate CONFIG source code to paste into your main C file (outside of any functions).http://microchip.wikidot.com/mplabx:view-and-set-configuration-bitsIt makes it a *lot* easier to get a valid CONFIG, especially on the more complex chips with many CONFIG registers.If Arduino code looks prettier, its only because of the complex hardware abstraction layers hidden away in the standard wiring libraries. If you are hitting the bare ATmega metal directly, it can look just as complex as PIC HiTech or XC8 code.

author: neko efecktz
author_rank: Regular Contributor
timestamp: June 15, 2016, 02:25:26 am
content: I will look into arduino in the near futureTomorrow i am going shopping to get some 16F628 at a reasonable price $7.45and maybe 16c711 @ $12.95 or just look online.Would like a few more ports. 28 or 40 pin the worlds my oyster.Once again.Thank you to everyone for you help in teaching an old dog new tricks.BILL.

author: neko efecktz
author_rank: Regular Contributor
timestamp: June 15, 2016, 02:35:53 am
content: BTY You might like to look at this schematic i found online.the contributer had built an addon for his GPS.pretty shonkynote the pot wiring and values.BILL.

author: MarkF
author_rank: Super Contributor
timestamp: June 15, 2016, 02:37:25 am
content: Quote from: Skimask on June 15, 2016, 02:13:45 amC code is C code. You can make it as readable or unreadable as you like no matter what platform is running it.Take it from someone who has been writing code in various languages for 35+ years.Quote from: neko efecktz on June 15, 2016, 02:25:26 amI would suggest apic16f886for $2.46 or apic18f2550for $5.16 from Mouser. Both are 28 pin packages. The pic18f2550 is a little more difficult to program but would be an easy next step in difficultly and is used in the PICkit 2.

author: Ian.M
author_rank: Super Contributor
timestamp: June 15, 2016, 02:53:55 am
content: The PIC18F2550 is a USB PIC. If you are not using its USB device interface, that limits your clock options and the USB hardware takes up pins. Also the Microchip USB stack has a fairly steep learning curve. For general non-USB use, you'd be better off with the 28 pin PIC18F2520, or possibly the PIC18F25K22, although that one's a bit more feature rich and complex.Also the PIC16F88 is well worth a look if you want a standard midrange 18 pin PIC, that in many applications is pin compatible with a PIC16F84(A) with only minor code changes, but has most of the newer features (including debugging) of the PIC16F886 in a lower pin-count.For all the PICs mentioned above by myself and Mark, if you want in-circuit debugging, your target board must not use the RB6, RB7 and /MCLR pins.