Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC Newcomers, please read! Upd. 0xD1

Status
Not open for further replies.

Jay.slovak

Active Member
Every time somebody new to PICs comes to this discussion forum, he/she asks the same questions about PICs. This post is intended for answering such questions.

Most of the answers are in the Datasheets so please download some of them and read them carefully!

1. Beginner guide to a PIC
The following link is a tutorial series on the 14-bit core PIC family made by Microchip. It covers common PICs like 16F84, 16F628, 16F87x etc...It tells you nearly everything about its architectures like program code bus and data bus and many other aspects like Reset, Oscillators etc. It is much easier to learn while someone is talking to you via diagram and animated drawings. At this moment, the following areas are covered:

Architecture, Instruction Set, Development Tools Overview, Device Configuration, Resets and Oscillator

Each downloadable module is an animated movie with audio. You can get them at the following link and save them to your hard drive for later viewing. PICmicro x14 Basic Training Modules

I also strongly recommend reading PICmicro™ Mid-Range MCU Family Reference Manual as it contains important information of all 16F PIC features. Example topics covered includes Device Structure and Varieties, explaining the Oscillator, Memory Organizations and EEPROM, Interrupts, Ports, Timers, A/D, LCD, CCP, Watchdog, the Instruction Set, and so on...

2. Which PIC to choose?
For hobbyist, usually we would like those PICs that allow us to program it many many times. So this rules out PIC that has a "C" in its part number which usually is of the type one-time programmable. The "F" in its part number generally means "Flash technology" and this type of PIC are what we often used for experimenting.

Many examples and projects we found on the Internet built around 16F84, but please keep in mind that this PIC is old, more expensive, less powerful and now obsoleted, so don't use it for any new design, unless you don't have the source code for your project but stuck with just the HEX object code.

If you are new to PICs, you'd probably buy the 16F628A or 16F88. There are many tutorials and schematics that use them. It is not difficult to adapt 16F84 source codes for use on newer PICs. For experienced users, it is even possible to disassemble the object code for 16F84 and use it with newer PICs.

16F628A-18Pin, Flash based,Internal Oscillator, PWM module, ICSP, USART
16F88-18Pin, as 16F628A but with added A/D converter

If you are looking for bigger PICs to have more I/O pins, try 16F876-28 pin and 16F877-40pin or their newer A version.

Also small PIN count PIC: 12F675-8pin or 10F206-6pin!

Later you may want to advance to 18F PIC series, which are called "High End", because they are more powerful and have better support for higher level languages (mostly C optimized). Just for information, there is also a DsPIC series from Microchip, which offer 16-bit core and DSP capabilities and with clock frequency up to 160Mhz brings you absolute top power solution... These CPU's are really powerful. (But absolutely not appropriate for beginners :D )

3. Where to find tutorials?

The tutorials by Microchip as detailed above and many datasheets and application notes from the same Microchip website.

For practical hand on experiment, try Nigel Goodwins tutorials (they use 16F628,876 & 877) and have different "module boards" to play with which also includes the newly added wireless interfacing tutorial:

http://www.winpicprog.co.uk

And some other that include tutorials & projects:
**broken link removed**
**broken link removed**
**broken link removed**
http://www.to-pic.com/
http://www.devrs.com/pic/
http://www.rentron.com/pic.htm
**broken link removed**
**broken link removed**
**broken link removed**

http://www.dontronics.com/piclinks.html
http://ee.cleversoul.com/pic.html
**broken link removed**
**broken link removed**
http://www.voti.nl/swp/
http://www.botkin.org/dale/pic_tutorial.htm
**broken link removed**
http://www.siriusmicro.com/projects.html
**broken link removed**

Other stuff
http://www.gnupic.org/

One of his many projects was posted here a while ago, but this guy simply comes up with such incredibly useful ideas that I had to post this :
**broken link removed**


4. What is involved in using a PIC

A PIC is piece of plastic with pins sticking out. It will do nothing. Internally it has memory which can store binary instructions that it understands. So we need to translate what we wanted it to do into binary instructions stored inside the PIC. The whole process involve two steps.

Human instructions can be in the form of computer languages like C or JAL or assembly. We write the instructions in a text file and a software translate/compile/assembly it into another text file containing HEX codes representing binary instructions that the PIC understands. This is called the object file.

Then a piece of hardware(called Hardware device programmer) which sits between the Personal Computer and the PIC whose job is to read the object file and transfer it into the PIC, more often with other functions like reading codes back out from the PIC or verify the codes with the object file. Another piece of software(programming software) is required to control this piece of hardware to perform these functions. Don't confuse this software with that for compiling the human instructions into object form.

When the HEX file is ready, there are two ways to download it into the flash memory of the PIC. One can either use a hardware device programmer mentioned above or a bootloader via serial port of the PC. A bootloader is some codes inside the PIC which read data from its serial RX/TX pin and program the data into the flash memory. This allow very easy operation when one wants to develop codes without the need of a hardware programmer. However, there are some catches.

a. Not all PICs can support bootloader operation as only those that can program their own flash memory will work
b. The bootloader code must be loaded into the PIC first by a hardware programmer
c. Bootloader takes up some code memory spaces
d. The PIC will have to be running at the time of programming
e. A serial port on the PC is required

Despite all of these limitations, it is still a very useful alternative for PIC hobbyist.

More info: bootloader for the Microchip PIC ()

4a. What compiling/assembling Software to use
Download free MPLAB (Developing environment) from www.Microchip.com. It is an integrated development environment with an Assembler editor, simulator, and debugger functions built-in. When you are familiar with PICs, you can then choose many other compilers to generate the object file.

1) If you are starting with PICs, I strongly recommend learning Assembly first. Most programs on Internet are written in it. Also keep in mind that Assembly code is compact and fast. Microchips Application Notes are also written in it.

2) If you know Assembly well, You might try C based programming languages. C will simplify your program (but will take more resources). Many complex projects are made in C (mostly 18F based, but also available for 12F/16F).

Microchip offers lite versions of C18, C24, and Hi-Tech compilers free for personal use. They are only restricted by the level of optimization available. The Hi-Tech compiler includes versions for PIC10/12/16, and PIC18 MCUs.

Most C compilers from other companies are not free(and relatively expensive) but some offer trial versions with code size or other limitations.

The BoostC compiler is reasonably priced so it is an option to look at.

3) Or try Basic, If you know PC BASIC this is really similar: **broken link removed**

4b. What hardware device programmer to use
Programmer circuits:

Microchip sells the PICKit2 and PICkit3 programmers for a reasonable price from MicrochipDirect and most major electronic parts suppliers. The PICkit2 is no longer being updated to support new chips but it includes a Logic Tool (simple logic analyizer) and a Serial Tool for $35 USD. The PICkit3 is the currently supported programmer and can be had for $45 USD. Both of these programmers work with all PIC families with the exception that you must have the PICkit3 to work with the 32 bit (MIPs) based PICs.

Plans for PICkit2 and ICD2 programmer clones can be found on the web.
ICD2 plans: **broken link removed**


All of the Microchip programmers mentioned support In Circuit Serial Programming, ICSP, which allows you to program your PIC while it is on your board. They also support In Circuit Debugging, ICD, which providers run control using the same debugger interface as the MPLAB simulator. The PICkit and ICD programmers are supported directly by MPLAB 8 and MPLAB X so you do not need external programming software.

If you can not obtain a PICkit2, PICkit3, ICD2, or ICD3 we recommend the parallel programmer P16Pro40 as many had been built and works perfectly.

A warning: Serial programmer designs like JDM or RCD relies heavily on the current and voltage capability of the serial port on your PC/laptop. If it works, congratulation. If it doesn't, bad luck. There is nothing you can do to make it work.

a) JDM: **broken link removed**
b) RCD: **broken link removed**
c) Parallel: **broken link removed**
Or try Nigels page for schematic of P16Pro40...
d) Linux is also supported, check this out
e) After you have learned the basics of PIC, I highly recommend to build yourself (or buy original) ICD2 - In Circuit Debugger 2. It's a nice tool from Microchip, which acts as Universal PIC programmer (uses MPLAB directly) and as PIC Debugger. Debugger feature allows you to run/step program inside PIC and even set breakpoints and directly read/write all registers status! More info at **broken link removed**

5. PIC Programming softwares:
There are many choices and are listed below in no particular oder. There is no limit on how many you can use as different software features different PIC types support.

Check the software to see if it actually supports the PIC that you use.

IC-Prog
**broken link removed**
**broken link removed**
WinPic
**broken link removed**


6. Some of my PIC output PINs are not working!
This is a common problem. Usually people try to output logic 1(HIGH) to PINs which are only inputs (aka PORTA,5 or PORTE,3 are usually Input only), or you are trying to get 5V from a PIN which can only pull down to 0V GND. Such PIN is called "Open drain Output", similar to open-collector in TTL logic. Such PIN is "missing" a MOSFET transistor from VDD to PIN, therefore PIN can't go HIGH. And of course the MOSFET transistor which is there connecting PIN to VSS can't output high but only 0V. If you want to use it as a general TTL compatible output, just add a Pull-up resistor from that PIN to +5V (4K7 is good). But keep in mind this logic 1(HIGH) wont be "strong" enough to drive a LED! (If you want to drive LED with it, connect anode of LED to +5V via a current limiting resistor and its Cathode to PIN, this will allow you to turn on the LED by putting logic 0 on the PIN.

Beginners usually don't like Open Drain/Collector PINs, but they will find it useful latter, when they advance into "self-designing" level.

Make sure that any internal PERIPHERAL is NOT taking over desired PIN, For example if you activates TIMER1 oscillator or ICD support, you can't use PORTB6,7 for ANY other purpose! This applies to both Input and Output problems.
List of peripherals (and features) that may cause problems:
MCLR - may prevent PINs PORTA,5 OR PORTE,3 to be used as general purpose input, disable it by clearing Master Clear Enable bit in configuration register.
Comparator Module - pins PORTA 0-3 are defaulted to Analog mode with comparators enabled. Writing a correct value to CMCON register (read Datasheet) will disable them. (Affected devices: 16F628, 16F628A, 16F876A, 16F877A, 16F88, 12F629, 12F675, 12F683... and more)
LVP - this is a special way of programming PIC with only +5V on MCLR and LVP pin PORTB3 or PORTB4 will control if PIC is on normal or in program mode. because this "feature" will takeover/block one PIN in the middle of an eight bit port, I highly recommend to DISABLE it by clearing LVP bit in config register.
ICD support will allow you to debug the PIC, but if you don't use ICD, under no circumstances enable DUBUG bit in config register, this would reserve PORTB6 & PORTB7 pins and your program won't be able to use them.

7. Some of my input PINs are not working!
First check the datasheet if the default state of the PIN is Digital Input, if it says that the Reset state is analog (comparator or ADC), you need to disable analog feature at the beginning of your program. This is a common problem, when you switch from 16F84 to 16F628 without rewriting the program (16F628 has Comparators turned on by default, whereas 16F84 doesn't have them at all). Also if you plan to use PORTA,5 or PORTE,3 as general inputs, you have to disable MCLR function first because they share the same PIN.

8. What are Watchdog, CCP, Timer etc ...
Watchdog timer (WDT) is an Independent oscillator+timer, that will reset (or wake-up) the PIC if you don't clear the timer value periodically. This clearing has to be done regularly in your PIC software by calling CLRWDT instruction. Watchdog prevents your program to be locked in infinite loop, because if it get stuck, the CLRWDT instruction wouldn't get executed and the PIC will reset and get out from the loop. Or you can use Watchdog to periodically wake the PIC up from sleep, and thus saves one precious Timer (that can be used elsewhere). The WDT is a crude RC oscillator, it's accuracy is very poor, and highly dependent on external factors such as temperature. However, this doesn't matter for it's designed purpose as a watchdog.

CCP is a unit that has three functions:
a. In Capture mode, CCPR register captures the 16-bit value of the TMR1 register (Timer1) when an event occurs on pin RB3/CCP1.
b. In Compare mode, the 16-bit CCPR register value is constantly compared against the TMR1 register pair value. When a match occurs, the RB3/CCP1 pin is altered (see datasheet)
c. In Pulse Width Modulation (PWM) mode, the CCP1 pin produces up to a 10-bit resolution PWM output. Since the CCP1 pin is multiplexed with the PORTB data latch, the TRISB<3> bit must be cleared to make the CCP1 pin an output.
Timer unit has to modes of operation:
d. Counter mode, is counting impulses from a dedicated input pin. This can serve also as a debounced input (Timer overflow will indicate that a button was pressed)
e. Timer mode, is counting oscillator pulses (Primary or Secondary), this will allow your program to know how much real time has passed (to construct delays, software serial buses etc)

9. Here are sample programs that will help you start.
These programs Flash LEDs on all their ports, useful for beginners if they are not sure where is the problem (Programmer,test board , program itself or MPLAB).
PICmcu Sample source files
Thanks to Nigel Goodwin for file hosting.

Don't forget to download newest Datasheet for PIC you are working with from www.Microchip.com. Good Luck! :D

PS: I thank Lim Chung <eblc1388> for grammar corrections and content remake,
0xD1 Update by 3v0 To include new compiler and programmer information.
 
Last edited by a moderator:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top