Monday, October 18, 2021

Programming PIC16F877A Using XC8

 

Introduction

PIC16F877A was a popular 8-bit microcontroller for university students and hobbyists. It still useful for novice programmers learning to program microcontroller.

Programming PIC16F877A Using XC8

This device has digital I/O, analog input, Timer, PWM output, Communication, and other features.

Programming PIC16F877A Using XC8
PIC16F87XA devices Table


I select PIC16F877A-I/P in DIP package that ease of prototyping.

Programming PIC16F877A Using XC8
PIC16F877A 40-Pin DIP Package Pin Diagram

 
Programming PIC16F877A Using XC8
PIC16F877A 40 pins DIP

Architecture

It’s designed using Harvard architecture, separating between program and data memory. CPU fetches both data and program memory concurrently. Its pipe-lining technique make instruction execution archive at one CPU cycle, but it excepts some instructions that require more than one cycle.

Memory

Its program counter is 13-bit wide. Instruction length is 14-bit (called one word). It can address program memory of 8K x 14 program space. Reset vector locate at address 0000h. It has only one interrupt vector which locate at address 0004h.

Programming PIC16F877A Using XC8
PIC16F877A memory map and stack

Stack used for calling and returning sub routine. It’s 8 levels, allowing up to 8 nested sub routine calling. Nested calling more than this value, making program unpredictable.

Coding PIC16F877A

Assembly language is core programming for microcontroller. It’s preferred for most of university program. It’s essential for programmer to understand overall operation of microcontroller before allowing to program in Assembly language.

Assembly language is free offered by device’s manufacturer. Programming made from this language composes of a lot of lines. It consumes a very long time to complete a program using this language. Updating and modifying program is very hard.

However microcontroller program written in Assembly language is very effective in program memory. Program execution is very fast.

Modern embedded C compilers for PIC microcontroller is very popular now. Even small 8-bit microcontroller implemented using C regardless of its program memory space – for example PIC microcontroller with 1KB of program memory. I see only a few of C compilers in market,

  • MikroC
  • CCS PICC
  • XC8

XC8 integrated in MPLABX IDE from Microchip Technology. It has a free version without limitation.

Getting Started With C Using XC8

Toggling digital I/O is a good getting-started section for most of microcontroller new learner. It allow us to test its normal operation.

Hardware

PIC16F877A supplies at +5V DC with its clocking input of 20MHz, from external crystal oscillator. Port C connects to 8 LED outputs via a resistor network.

Programming PIC16F877A Using XC8
Schematic Diagram

In Proteus, power supply pins are hidden but they internally wired by this simulator program.

C Programmer

This getting started program made of a little lines of code. Device configuration section seem very long due XC8 compiler directives. Port C toggles its output with a delay of one second.

  1. /*
  2.  * Blinking PORTC of PIC16F877A
  3.  * using XC8 in MPLABX IDE
  4.  */
  5. #include <xc.h>
  6.  
  7. // CONFIG
  8. #pragma config FOSC = HS
  9. // Oscillator Selection bits (HS oscillator)
  10. #pragma config WDTE = OFF
  11. // Watchdog Timer Enable bit (WDT disabled)
  12. #pragma config PWRTE = OFF
  13. // Power-up Timer Enable bit (PWRT disabled)
  14. #pragma config BOREN = ON
  15. // Brown-out Reset Enable bit (BOR enabled)
  16. #pragma config LVP = OFF
  17. // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit
  18. //(RB3 is digital I/O, HV on MCLR must be used for programming)
  19. #pragma config CPD = OFF
  20. // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
  21. #pragma config WRT = OFF
  22. // Flash Program Memory Write Enable bits (Write protection off;
  23. //all program memory may be written to by EECON control)
  24. #pragma config CP = OFF
  25. // Flash Program Memory Code Protection bit (Code protection off)
  26.  
  27. /*Clocking Parameter for Delay Function*/
  28. #define _XTAL_FREQ 20000000
  29.  
  30. void main(void){
  31. /*Clear Port C*/
  32. PORTC=0x00;
  33. /*Set Port C To Output*/
  34. TRISC=0x00;
  35. while(1){
  36. PORTC=0x00;
  37. __delay_ms(1000);
  38. PORTC=0xFF;
  39. __delay_ms(1000);
  40. }
  41. }

 This program consumes about 1% of memory resource.

Programming PIC16F877A Using XC8
Statistic in MPLABX IDE dashboard

Click here to download this example package.



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 (56) 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 (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) 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 (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)