PIC16F887 and PIC16F877 are the members of PIC16F88X and PIC16F87X families respectively. PIC16F887 is the new version launched by microchip to take place of PIC16F877.For detail information please refer to their datasheets.
In this tutorial i will show you how to program PIC16F887/877 in C.
So first of all download mikroc pro for pic compiler also download its user manual. After this create a new project choose P16F887 or 877 whatever you have, and 4MHz crystal etc. If you dont know how to create a new project refer to its manual or download document 'how to create first project' from its site.
Note that i will use PIC16F887 and 4MHz external crystal (although PIC16F887 also have internal crystal) you can use 8MHz and what so ever. For 4MHz oscillator setting should be XT where as for 8MHz or above it should be HS (see fig below).
Lets start with a simple program 'LED Chaser', before we start this lets check out the PIC16F887 pin out.
pinout of pic16f887 |
As you can see there are five multiplex i/o ports, in PIC16F887, from portA to portE. For our first program we need only one port let say portD and 5v at pins 11 & 32, ground pins 12 & 31, 4MHz xtal at pins 13 & 14. Dont be panic to see more than one name of pins, i will tell you the pin functions when we use them.
After creating the project just write the below code in mikroc ide and build it(CTRL + F9), if you want to change the project setting, fuse bits, clock etc, goto>>project>>edit project.
Code:
void main() {
int x[]={1,2,4,8,16,32,64,128,256}; // int array
int i;
portc=0; //to clear port
trisc=0; //as output
int x[]={1,2,4,8,16,32,64,128,256}; // int array
int i;
portc=0; //to clear port
trisc=0; //as output
///////////////////// comment this block if you are using pic16f877a/////////
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
////////////////////////////////////////////////////////////////////
while(1){
for(i=0; i<=8; i++){
portc=x[i];
delay_ms(100);
} //for bracket
for(i=0; i<=8; i++){
portc=x[i];
delay_ms(100);
} //for bracket
} //while bracket
} //main bracket
Now come towards code; firt select the port you want to use, i this case it is portc. Then clear the port by assigning it 0. Trisx, the SFR is used to set the port as input or output, where x can be a,b,c,d, and e; the port which you are using. Trisc=0 means the portc is selected for output. For input change it to Trisc=255; means all pins of portc as ready for input. If you want take i/p at single pin instead of whole then use trisxy_bit=1, where y could be from 0 to 7; the pin you at want to take i/p and y is the respective port.
There are two comparators in 887, to disable them assign 0 to c1 and c2 bits. Also to use pins as digital i/o assign 0 to ansel and anselh; dont be panic when we do ADC i will tell you about this. Note that for 877a these two thing is not required.
Now for loop; as for led chaser we want to on led at power of 2 sequence that is 1,2,4 till 256 as port is 8-bit so 2^8=256; that is why have save these values in a array.
To get visual we have to add some delay, to do this just write delay_ms(100); for 100 ms delay. You can increase it if you want.
As we want to see our o/p continuously not only for a single time, we use while loop; while(1) means this loop will run forever.
project setting |
Schematic:
Give the hex file path, of this project, to isis and simulate it. Dont forget to attach reset circuitry.
Reset Circuit:
Comments
Post a Comment