Sunday, May 17, 2020

Programming the PIC16F887 with MPLABX XC8

PIC16F887 was released during 2009. It is a mid range 8-bit PIC architecture, designed to replace the popular older one PIC16F887A. However, PIC16F877A still useful in some educations, due to its rich of learning resources.

PIC16F887 40-pin DIP package
PIC16F887 40-pin DIP package


I saw some major surpluses of PIC16F887 over PIC16F877A:
  1. Internal RC oscillator up to 8 MHz
  2. MCLR pin could be disable, and could be used as a digital input
  3. The 10-bit ADC module has up to 14 channels.
  4. Digital I/O has up to 35 etc.
For programming migration, there are some special function register differences. We can check them in the device's datasheet.

There are a lot of programming tools for PIC. The programming language could be Assembly, C, Basic, and Pascal etc. C is very popular that I mention here. Some widely used C compilers in the market are:
  1. MikroC
  2. CCS PICC
  3. Hi-Tech PICC
  4. Microchip XC8
All of them are paid. Hitech PICC was acquired by Microchip Technology, as a result of XC8 C compiler.
XC8 compiler provide three modes:
  1. free version
  2. standard version
  3. pro version
As a hobbyist or a student, we can use XC8 free version with no limit. But the free version doesn't have code optimization and technical support from the manufacturer. In the free version, the generated binary file could be larger.

MPLABX is the IDE for developing the project. In this tutorial I use MPLABX v1.51, and XC8 v1.31.

You can download them for free from Microchip website.

After the installation of MPLABX and XC8 C compiler, open the IDE.

main IDE of MPLABX
MPLABX when opened

Now start a new project. Click File->New Project


Click Next >, In the device type PIC16F887


Click Next > , In the Select Tool, select click Pickit2



Click Next >, select XC8(v1.31)


Click Next > , Type the project name and click Finish.


Now right click on Source Files of the project folder, and click on Empty File


In the File Name, type main.c and click Finish


Click on Window->PIC Memory View -> Configuration Bits


Select the options we need, and click Generate Source Code To Output


In the source code outputs below is the configure bits of the MCU, you can copy them into c source file, or type by hands.


Now, in the main.c file write your file embedded C program:

#include<xc.h>
// CONFIG1
#pragma config FOSC = XT
#pragma config WDTE = ON
#pragma config PWRTE = OFF
#pragma config MCLRE = ON
#pragma config CP = OFF
#pragma config CPD = OFF
#pragma config BOREN = ON
#pragma config IESO = ON
#pragma config FCMEN = ON
#pragma config LVP = ON
// CONFIG2
#pragma config BOR4V = BOR40V
#pragma config WRT = OFF
#define _XTAL_FREQ 40000000
void main(){
//Clear PortD
PORTD=0x00;
//Set PortD to digital output
TRISD=0x00;
//main program loop
while(1){
PORTD=0x00;
__delay_ms(50
0);
PORTD=0xFF;
__delay_ms(500);
}
}
After the completion of coding, press on Build Main Project button.

I use Proteus VSM 8 to simulate the program.

PIC16F887
Simulation Schematic,
PIC16F887 run at +5 V DC, clocks at 4 MHz.
PORTD connects to LED bar graph.

Simulation screen shot
A screen shot of PIC16F887 Proteus simulation
Back to main tutorials page.

No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (54) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)