PIC

From ScienceZero
Jump to: navigation, search
PIC16F84A BLOCK DIAGRAM

The Microchip PIC one the most popular embedded microcontrollers for amateurs. It is an old and quirky design but it is simple and in the hands of a master they can do amazing things at low cost.

Most of the chips have an architecture that is accumulator based with a banked memory were the peripherals are mapped in. It is a good starting point since there are large amounts of information available on the internet. For simple designs it is very suitable but for complex designs a more capable architecture like the ARM is recommended.

Several types are available from very low power chips in grain-sized transistor packages to high performance RISC DSP chips.

Hints and tips

If you are a beginner you might want to read this section as it may save you a bit of trouble later on.

Configuration

  • Always pay close attention to the Configuration bits. If the Watchdog timer is left on by accident or the wrong oscillator type is configured your project may behave erratic.
  • Low Voltage Programming function; PGM, PGC and PGD pins. The state of these pins must be correct when programming or you may lose the ability to program the chip with some programmers and/or lose the function of the pin marked as PGM as a general I/O pin. PGM should be at Gnd for HV programming.

Assembler

  • The default radix of MPASM is hexadecimal so if you type movlw 10 you will not end up with W = 10 but with W = 16 since 10 in hexadecimal is 16 in decimal. To get 10 into W you type movlw .10 or movlw A. You can also change the default radix by using radix <hex|dec|oct>.
  • Any path names for some versions of MPASM should not have any spaces in it or have a lenght of over 64 characters or you will get in all sorts of trouble. Care should also be taken with the CRLF sequences in the file you try to assemble. If it has been saved on a Unix machine or been converted when downloaded from the internet it might confuse the assembler terribly. Paste the program into a text editor and save it as a text file will usually fix the problem.
  • Use the CBLOCK <address> and ENDC structure to reserve variables.
  • Remember that sublw <n> does not do W = W - n but in fact does W = n - W. To do W = W - n you can use addwlw <-n> instead. Also keep in mind that the carry flag is inverted for subtractions.
  • If you want to execute some code 256 times using the decfsz <file> command you need to start the file with the value of 0.
  • Insert errorlevel -305 at the top of your program and leave out the default destination of ,F so you type incf <file> instead of incf <file>,F. Then you will see the code as it was intended by the designer and the less usual ,W destination stands out and is easy to spot.
  • Use the banksel <file> macro to change to a specific bank and use banksel 0 to return to the default bank.
  • Load-modify-store, this means that every time an output port is modified it will be read and modified before the final value is stored. Reading a pin will report the actual state of the pin, not what was written to it. So if a port is modified twice in a very short space of time there is no guarantee that the outcome will be as logically expected because a pin may take some time to settle to its final value.

External links