I can't seem to get my PIC16F877A to work. - Page 1
Forum section: electronics / beginners

author: Cagruntas
author_rank: Newbie
timestamp: September 21, 2021, 12:37:35 am
content: Hello good day, I am new to the world of electronics and micro-controllers however I am very eager to learn so I bought myself some equipment and I am trying to learn how to use a PIC16F877A but I can't seem to get it to work at all. I am using MPLABX as my IDE. I am just trying to get an LED to blink before I try to interface things like sensors. I have set up a simple circuit on my breadboard and I have written some code and successfully flashed it into the PIC16F877A according to MPLAB IPE however, it doesn't work and I need some help finding out why.This is also the code I have written in MPLAB.// CONFIG#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)#include <xc.h>#define _XTAL_FREQ 20000000void main(void) {TRISB1 = 0;while (1){RB1 = 1;__delay_ms(5000);RB1 = 0;__delay_ms(5000);}return;}Any assistance would be great in helping me find and understand what I am doing wrong. I do not have any experience doing schematics either, I am just learning so please bare with me if it's incredibly wrong, I am trying to learn this all by myself. Much thanks in advance to anyone who is willing to help.

author: StillTrying
author_rank: Super Contributor
timestamp: September 21, 2021, 03:30:05 am
content: The LED's leads are shorted because they're both in the same row.It might need a ~100nF near to ICs supply pins.Itlookslike the 470uF's polarity is the wrong way around.I didn't notice much else.

author: Cagruntas
author_rank: Newbie
timestamp: September 21, 2021, 01:24:02 pm
content: Hey man, thanks alot ! It was actually the first problem you said, the both pins of the LED was in the same row, it works now. Thanks alot for you help because I think I was going crazy trying to figure out how to make an LED blink lol.

author: NorthernWing
author_rank: Contributor
timestamp: September 21, 2021, 06:40:35 pm
content: Unrelated to your question now that it's solved but maybe useful to someone starting with PICs, have you tried the MPLAB Code Configurator ("MCC") plugin yet? It's a little tool that will generate a .C file with basic initialization functions for the peripherals on pretty much any PIC MCU up to the PIC32MM. Super useful, you can go through and just set up things like your clock speed/source, baud rates, etc through a dropdown menu in the MCC instead of cross-referencing the datasheet for every single configuration register. And if you really don't like the extra source files, it at least gives you all of the #pragma/config bits to set and you can just move them wherever.I was just doing a project using the USB on a PIC32MM and it's a nightmare without this, there are a boatload of register bits to set before it will even power up the interface. Unfortunately any of their larger MCUs (PIC32MX/MZ and up) run on their new toolchain which is so much of an abomination that I just pretend they don't exist and use NXP or TI parts instead.

author: Cagruntas
author_rank: Newbie
timestamp: September 21, 2021, 10:35:18 pm
content: Hello, thanks for the advice, I'm very much of a novice to say, I understand basic things in C so I am just going through configuration drivers for the I2C protocol right now by a guy named Deepblue to understand how it's set up from the ground up and why it is important. Most of the peripherals I bought are either I2C or SPI so I am learning those protocols. I am trying my best to learn what everything in a datasheet means as well. Thanks again for the advice, I will install the plug in and try it out.If you have any other resources where you learn please feel free to link them, I am very eager to learn.

author: DavidAlfa
author_rank: Super Contributor
timestamp: September 21, 2021, 11:37:06 pm
content: These stupid mistakes are hilarious. You keep checking the code like crazy, whyyyy doesn't this work?Do you have any literature to learn from? I got few decent books