Electronic – PIC16F877A External Crystal giving 60Hz

crystalmicrocontrollerpicpic16fpic16f877a

I'm new around here. Usually I read a lot of stuff and find all info I need, but this once I googled my fingers till blood, yet to no avail.

Recently got my hands on PIC16F877A. It wasn't MCU of my choice, I have another PIC, which I couldn't get to do what I want (blinking was not a problem at all), so I decided to get a chip which has a ton of examples. Decided to downgrade myself to copy-pasting examples and running them just to make sure stuff works. Guess what: it still doesn't 😀

Platform: Windows 10

Software: MPLab with XC8

Hardware: PIC16F877A, Pickit 4 programmer

Available equipment: Multimeter, Oscilloscope

Experience: I feel comfortable reading datasheets and changing register values, I know what I'm doing usually, so things like "that register controls this and that, take a look at it" are totally clear to me. However, I lack general experience with PIC, so detailed answers are appreciated. After all, it's better to have too much info than lack it like I do right now.

Problem: 8Mhz Crystal with 20pf caps generates 60-63Hz 5Vp-p sine waveform worth the best geometry classes in its shape, thus the chip's not running. All power and all ground pins of the MCU are connected and delivering juice to my not-running puppy. I even bypassed the power supply with a 0.1uF cap, which I usually don't do during early stages of development.

Since you'll probably insist on seeing the code:


// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = ON         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#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)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#include <htc.h>
#include <stdio.h>
#include <stdint.h>
#define _XTAL_FREQ 8000000


void main(void) {
    TRISC = 0xff;
    PORTC = 0xff; //do anything program, I just want to see 5V on pins first
    while(1){
        PORTCbits.RC6 = 1;
        __delay_ms(500);
        PORTCbits.RC6 = 0;
        __delay_ms(500);    
    }
    return;
}

Things I tried:
1. Re-seating the crystal and caps. Still get perfect 5Vp-p 60Hz sine wave.
2. Trying XT crystal setting in configuration bits (datasheet says XT setting is up to 4Mhz, so it didn't work with 8Mhz crystal, no wonder)
3. Crying in desperation (most helpful so far, but the chip still doesn't run)

I would be very grateful for any response. Tbh, I couldn't register on microchip forum, the email for my account just doesn't come there and my account is unverified for several days already, so I abandoned that idea. It feels like Microchip and PIC are just signalling me like "you're not wanted here", and after a few days of all kinds of fights with PICs, I was determined at first, but I'm getting a massive demoralization. Hope once I get PIC16F877A running, the examples I "copy-paste" will run.

Best Answer

TRISC=0xFF sets the pins on PORTC to float. There may be other issues but that's a killer.

TRIS = Tri-State (ie. make it float) is the way to remember.