Microchip PIC Microcontroller Files

 

 

What is the PIC?

 

The PIC is an 8 - 16bit microcontroller. It has everything it needs to run a set of instructions in one chip, you only need to supply 5v power and a time base (crystal). You can program the chip in various languages and compile it using various free compilers, so it makes it a versitile and space saving alternative to logic circuits. There are so many benefits to using microcontrollers now it isn't funny! You wouldn't even consider design a complex system using PLC logic units or logic gates anymore.

 

How to learn the basics:

 

Once you get your head around how the device works and indeed how logical instruction work, you'll find it so easy to do complex tasks with just the 1 microcontroller. For starters, I would get a copy of "The PIC Microcontroller" by John Morton. The straight forward text with small projects are the easiest way to get to grips with the world of Microcontrollers. After that, you can teach yourself pretty much everything through little projects, like me.

 

I write all my programs in Assembler, the same program supplied by Microchip called MPLAB. I find it a really straight forward and logical way to write programs, but many people use PICBASIC, which is a compiler for the BASIC language and even C++. Whatever you use, you'll find it rewarding to see that the code you write on the screen, actually does something in the real world!

 

Helpful code snippets:

 

The PICs I use are commonly PIC16F876, PIC16F627 and PIC16F676. Others may use these codes, but I can't confirm this, so experiment if it doesn't work.

 

Common setting up ports and declaring names:

 

 

Reading a LDR Light sensor using capacitor without ADC:

 

One end of the LDR is connected to I/O pin RA3 and the other to ground, a capacitor of appropriate value (around 100uF) is connected in parrallel with the LDR. The idea is, that you charge up the cap with RA3 as an output and then quickly chage to an input, test the input, RA3, and count how long it stays as a 1 (until it drops below 3v). The amount of light will effect the LDR resistance and change how fast the capacitor discharges. Simple hey!

 

 

LIGHT and READSTOP are registers, SENSOR is RA3. When it has the value, after READSTOP has reached 0, it returns to the main program and you then write that value to a display or whatever!

 

Generating Sound:

 

This is particularly fun! Sound is produced with the Micro in much the same way as the old Commodore 64 did. Basically we use loops of a set length to turn on and off an output that is conected to a piezo speaker to generate a frequency that corresponds to a note.

 

The piezo speaker can be anything that doesn't draw too much current. The speaker is connected in series from ground to a capacitor (0.1uF) to an I/O pin. The cap is there to isolate the Micro from the AC transient.

 

 

Call as many notes as you want to play a tune. NOTELENG defines how long the note is played, so if you want a short note, change to a lower value, longer note higher value up to 255.

 

The function for the notes is here (only A and B are shown, but the rest are similar, just change the time delay numbers);

 

 

The delay loops for the program are here;

 

 

BREAKl and BREAKS and pauses between notes, timedelay is a lookup table that was implemented in an effort to reduce the amount of program memory used, it simply returns a value for the TIMER1 and TIMER2 values.

 

The tune I created was the SuperGridder theme tune (an old Commodore 64 game), to see how close I could get the sounds. It was near enough and very good! Was used in a card I made for someone's birthday!

 

Using the Hitachi based HD44780 LCD module:

 

Wiring of the LCD module to the PIC using 8 bit instructions;

 

 

I use 8 bit instructions, but 4bit (2 nibbles) is also possible, but more complicated. It is important that the data lines (D0 to D7) are all connected to one port (PORTB) as it makes it a lot easier. The Vee connection is there to ajust the contrast of the display. There is also a backlight on most modules, but not shown on this picture. Just connected to supply voltages.

 

Program to write "Hello & welcome to LCD" text to display;

 

 

Thats enough to get you started. More complex programs to come.

 

You should also take a look at the HD44780 datasheet and also an article that was printed in Everyday Practical Electronics about using the display.

 

EPE files: no1 no2

 

HD44780 datasheet: pdf

 

Compiled HEX and Source code of above PIC LCD project. Modify it as you like: Zip

 

 

 

ICSP crosstalk fix and Wiring:

 

ICSP (In Circuit Serial Programming) is another area where documentation is non-existant! This form of programming is very useful, as you can make your project, connect it all up and then write or update the program to the Microcontroller. There is however a small problem with the wiring. Crosstalk... This is where data lines will induce currents in each other and create errors in the data stream. A good way I use to reduce this is shown here:

 

 

Config Words:

 

This area of the Micro I find is poorly documented. A config word is used for setting the "fuses" in the PIC when you write a program to it. They can do things such as, tell the pic which oscillator source to use, code protection so as to stop it being overwritten if you market something, brown out detection and so on. I have compiled a list of the basic config words, so as to help people understand what each of the functions do:

 

________________________________________

 

A typical config word, written at the top of your program and used by the programmer:

 

__CONFIG _BODEN_ON & _CP_OFF & _DATA_CP_OFF & _PWRTE_ON & _WDT_OFF & _LVP_OFF & _MCLRE_ON & _XT_OSC

 

________________________________________

 

CP1,CP0 : Code Protection bits
All of the CP1:CP0 pairs have to be given the same value to enable the code protection scheme listed.



11 : Program memory code protection off
10 : 0400h-07FFh code protected
01 : 0200h-07FFh code protected
00 : 0000h-07FFhcode protected

 

________________________________________


CPD : Data Code Protection bit, careful with this function, as I have found that once it's on, it doesn't turn off!
1 : Data memory code protection off
0 : Data memory code protected

 

________________________________________


LVP : Low Voltage Programming Enable
1 : RB4/PGM pin has PGM function, low voltage programming enabled
0 : RB4/PGM is digital I/O, HV on MCLR must be used for programming

 

________________________________________


BODEN : Brown-out Detect , resets PIC if power drops to Ov
1 : BOD Reset enabled
0 : BOD Reset disabled

 

________________________________________


MCLRE : RA5/MCLR pin function select
1 : RA5/MCLR pin function is MCLR
0 : RA5/MCLR pin function is digital Input, MCLR internally tied to VDD

 

________________________________________


PWRTE : Power-up Timer Enable bit
1 : PWRT disabled
0 : PWRT enabled

 

________________________________________


WDTE : Watchdog Timer Enable bit, if used in the program, will reset PIC if it gets stuck
1 : WDT enabled
0 : WDT disabled

 

________________________________________


FOSC2,FOSC1,FOSC0 : Oscillator Selection bits ( Functions are added )
111 : ER(External Resistor) oscillator:
CLKOUT function on RA6/OSC2/CLKOUT pin, Resistor on RA7/OSC1/CLKIN
110 : ER(External Resistor) oscillator:
I/O function on RA6/OSC2/CLKOUT pin, Resistor on RA7/OSC1/CLKIN
101 : INTRC(Internal 4MHz OSC) oscillator:
CLKOUT function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
100 : INTRC(Internal 4MHz OSC) oscillator:
I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
011 : EC(External Clock In):
I/O function on RA6/OSC2/CLKOUT pin, CLKIN on RA7/OSC1/CLKIN
010 : HS oscillator: High speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
001 : XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN
000 : LP oscillator: Low power crystal on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN

 

________________________________________

 

Links:

 

Microchip

 

 

 

 

 

Please do not reproduce anything contained within my website, as it maybe hazardous to your health unless you fully understand what you are doing.

I cannot be held responsible. This website is copyright.

© Oliver Hunt 2006-2008