0
\$\begingroup\$

It has been quite a while since I haven't done PIC programming.

While trying to run a simple example I have ran into a problem.

#include <pic.h>

int main()
{
    TRISB = 0; // PORTB is output

    PORTB = 0xFF; // All leds are on

    while (1) {} // Loop forever

}

I use PICKit2 command line interface to load to program into the microcontroller.

pk2cmd -PPIC16F877A -Fhelloworld.hex -M PICkit 2 Program Report 1-10-2020, 21:19:44 Device Type: PIC16F877A

Program Succeeded.

You can see the schematics of the circuit here.

pic16f887a

I meausured the voltage at the output of RB5 with a multimeter. It was about 300 mV.

Are there any ideas on how to fix this problem. Thanks.

\$\endgroup\$
5
  • 2
    \$\begingroup\$ Have you set the clock configuration i.e. HS oscillator? \$\endgroup\$ Oct 1 '20 at 19:46
  • \$\begingroup\$ In this example I didn't. But before that I've tried clock settings that way. Also the crystal has a K16.000 label on it. \$\endgroup\$
    – Erdem
    Oct 1 '20 at 19:53
  • \$\begingroup\$ I do not expect the LED to blink. But in my previous attempt I was measuring about 300 milivolts from the output of RB5. \$\endgroup\$
    – Erdem
    Oct 1 '20 at 20:25
  • 1
    \$\begingroup\$ never assume that PORTB = 0xFF; will output Vcc ... always try PORTB = 0; also ... if both generate the same output, then start assuming that there is a problem \$\endgroup\$
    – jsotola
    Oct 1 '20 at 20:37
  • 1
    \$\begingroup\$ Well, SE/EE is not a forum, and it works this way: If you have an answer (or solution) to your question, please post it as such. Then mark it, you might need to let pass some time for this. Please do not edit your question with a solution, because others looking for help will just scan over the list of questions and commonly "filter" the ones with (marked) answers. \$\endgroup\$ Oct 2 '20 at 8:26
0
\$\begingroup\$

I solved the problem with the help of @rohat-kılıç.

It seems that some of my LED's were broken. I set the external clock frequency to high speed (HS). This is the updated code.

#include <pic.h>

#ifndef _XTAL_FREQ
// The clock frequency is 16 Mhz
#define _XTAL_FREQ 16000000
#endif

__CONFIG (FOSC_HS & WDTE_OFF & LVP_OFF & CPD_OFF & CP_OFF);

int main()
{
    TRISB = 0; // PORTB is output

    while(1)
    {
        /* RB5 PORTB6 is on */
        PORTB = 0x20;  // b0010 0000

        /* wait for a second */
        __delay_ms(1000);

        /* all leds on PORTB is off */
        PORTB = 0;

        /* wait for a second */
        __delay_ms(1000);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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