0
\$\begingroup\$

I'm just getting started with PIC programming and I'm giving up already. I just want to make an LED blink! I'm working with a PIC16F1773, MPLABX IDE v4.05 and Pickit3.

These are the configuration bits that I've defined in a header file:

// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit cannot be cleared once it is set by software)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR)
#pragma config PLLEN = ON       // Phase Lock Loop enable (4x PLL is always enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = ON

#define _XTAL_FREQ 8000000       // INTOSC 8MHz

And this is the actual code:

#include <xc.h>

void main(void) {

OSCCON = 0x72;

TRISB = 0x00;
PORTBbits.RB0 = 0;

while (1) {

    if (PORTBbits.RB0 == 0) {
        PORTBbits.RB0 = 1;
    }else {
        PORTBbits.RB0 = 0;
    }
    // Delay for 1 sec
    for (int count = 0; count < 20; count++) {
        __delay_ms(50);
    }

}

return;
} 

I've defined the OSCCON register as 0x72 following the datasheet but nothing lights up. I'm measuring a constant voltage of 0.42V in RB0.

EDIT: The datasheet:

I'm powering the PIC through the Pickit3. RB0 is connected to a resistor in series to the LED and to ground.

What am I doing wrong?

\$\endgroup\$
  • \$\begingroup\$ Please add a link to the datasheet so others could easily reference. \$\endgroup\$ – MaNyYaCk Jan 10 '18 at 18:14
  • \$\begingroup\$ No experience with PIC family, but don't you have to define the directions of the PIN? \$\endgroup\$ – MaNyYaCk Jan 10 '18 at 18:15
  • \$\begingroup\$ I believe TRISB = 0x00 defines all PortB pins as outputs. \$\endgroup\$ – Raúl Jan 10 '18 at 18:17
  • \$\begingroup\$ Ok. Hang around. Someone would surely be helpful to you! \$\endgroup\$ – MaNyYaCk Jan 10 '18 at 18:20
  • \$\begingroup\$ Is the LED right way round? (if you connect that resistor to +5V instead of RB0, does it light up?) And what happens if you start with PORTBbits.RB0 = 1;? \$\endgroup\$ – Brian Drummond Jan 10 '18 at 18:26
2
\$\begingroup\$

The difference between your first (non-working) and your second (working) code snippets is that the first relies on the existing value of RB0. If the external circuit is holding RB0 a particular way, even when the PIC is trying to drive it the other way, then the first won't work.

This could be the case, for example, if you don't have the LED connected correctly with the proper minimum resistance in series, for example.

\$\endgroup\$
1
\$\begingroup\$

Don't ask me what was wrong with that code, but I changed it to be:

 while (1) {

    LATBbits.RB0 = 0;

    __delay_ms(500);

    LATBbits.RB0 = 1;

    __delay_ms(500);

}

This works just fine. I don't get it.

\$\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.