3
\$\begingroup\$

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 :D

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.

\$\endgroup\$
6
  • 4
    \$\begingroup\$ If you're getting 60Hz from the xtal, it sounds like your scope is not propoerly grounded and you're seeing mains pickup. \$\endgroup\$
    – Neil_UK
    Commented May 30, 2020 at 11:45
  • 3
    \$\begingroup\$ 60Hz sine wave sounds like you are picking up the AC powerline frequency. You'll need an oscilloscope with very high input impedance to check the oscillator. Use a 10X probe if you have one. \$\endgroup\$
    – JRE
    Commented May 30, 2020 at 11:45
  • 2
    \$\begingroup\$ "Re-seating the crystal and caps" sounds like you are using a breadboard. Probably not the best idea when you are using 20pF capacitors on the crystal. The breadboard can easily have that much capacitance between rows. \$\endgroup\$
    – JRE
    Commented May 30, 2020 at 11:49
  • \$\begingroup\$ My main is 50hz Europe. And I get like 62Hz on crystal line with 0 to 5V there, which seems strange to me and doesn't look like mains pickup \$\endgroup\$
    – Ilya
    Commented May 30, 2020 at 11:53
  • 1
    \$\begingroup\$ Is not-MCLR (pin 1) pulled up to Vdd with a resistor? You should measure half-Vdd on OSC2 (pin 14) if the oscillator is not running. \$\endgroup\$
    – glen_geek
    Commented May 30, 2020 at 12:13

2 Answers 2

1
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ damn right, my bad about it, but it doesn't explain 60hz clock from 8mhz crystal \$\endgroup\$
    – Ilya
    Commented May 30, 2020 at 11:52
  • 2
    \$\begingroup\$ Some kind of measurement error I suspect. You can't necessarily read the crystal frequency. If you measure the oscillator OUTPUT pin and leave the oscillator input connected to the cap + crystal and use the x10 setting on the probe you should be able to get a reading. I disagree with my learned colleagues about capacitance of a breadboard being a factor with an 8MHz crystal and 20pF caps, however breadboards can be unreliable, and that could be a factor. \$\endgroup\$ Commented May 30, 2020 at 11:56
  • \$\begingroup\$ If your local mains frequency is 60Hz it pretty much does explain it, floating circuits pick up any interference going, most of which is AC mains. \$\endgroup\$
    – user16324
    Commented May 30, 2020 at 12:02
  • 1
    \$\begingroup\$ OSC1 is the input OSC2 is the output. Be sure to check your wiring. Lack of a ground on the load capacitors could cause weird things to happen. \$\endgroup\$ Commented May 30, 2020 at 12:03
  • 1
    \$\begingroup\$ By the way, a very good way of verifying your code and setup is halfway reasonable is to use the (free and built-in) MPLAB-X simulator. It isn't perfect, but it can help with a number of issues. \$\endgroup\$ Commented May 30, 2020 at 13:18
0
\$\begingroup\$

I have no idea what it was, maybe some loose connection somewhere, but the thing just randomly started to give perfect 8Mhz and the MCU is running. Nevertheless, I've learned a great deal from the comments of you, guys, in terms of what I should look at when troubleshooting. Thank you everyone for contribution and bits of knowledge you gave me. I'll go PIC my poison...

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.