0
\$\begingroup\$

I have been trying really hard to just get a PIC to turn on an LED, i know its super simple but for some reason i just cant figure out, its not turning ON!

I am using an old fashion PIC16F877, I am 100% sure its working because my programmer can read and write to it. I am connecting my Arduino 5v and GND to power my circuit. I am sure that has nothing to do with it but hey, you never know.

Anyway, I double and triple checked the wiring and made sure MCLR is connected to 5V, I am connecting:

PIN 11 Vdd to +5v

PIN 12 Vss to GND

PIN 13 CLKIN to a simple RC circuit

schematic

simulate this circuit – Schematic created using CircuitLab

Pin 37 (RB4) connected to the LED.

Following is my code in C:

// CONFIG1
#pragma config FOSC = EXTRC_NOCLKOUT// Oscillator Selection bits (RCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, RC on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

#include xc.h

#include stdio.h
#include stdlib.h

#define _XTAL_FREQ 40000;

void main(int argc, char** argv) {

    TRISA = 0xFF;
    TRISB = 0xFF;
    TRISC = 0xFF;

    ANSEL = 0x00;
    ANSELH = 0x00;

    TRISBbits.TRISB4=0;

while(1){

        PORTBbits.RB4=1; //led on
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Hi Karam! Your code didn't format correctly, so I fixed it. In the future, you can insert four spaces in front of each line of code to cause it to look right. Hope you get a good answer :) \$\endgroup\$ – bitsmack Dec 20 '18 at 19:40
  • \$\begingroup\$ @bitsmack, Thanks for the information. I did not know that, and thanks for your help (: \$\endgroup\$ – forthelulx Dec 20 '18 at 19:41
  • 1
    \$\begingroup\$ Glad to help! I recommend you draw an entire schematic, including the PIC, the oscillator, and the LED circuit. You'll get better responses that way. You can click on "edit" underneath your question... \$\endgroup\$ – bitsmack Dec 20 '18 at 19:44
  • \$\begingroup\$ If you're setting up your I/O using the PIC configurator, it will probably introduce initialization bugs that you'll have to find by hand. I've never seen their configurator produce the correct initialization code. \$\endgroup\$ – Cristobol Polychronopolis Dec 20 '18 at 20:50
2
\$\begingroup\$

The configuration bits are the key to PIC when i set BOREN and IESO bit to OFF it worked like magic.

So per multi line answer, the BOREN bit, toggles the brown out reset pin; which is when the voltage is too low, the lights dim so much that very few to none amps of current are flowing through which in my case can be too dim to even see in an LED, toggling this particular bit off, will always supply almost .3 to .5 volts to each output pin.

IESO bit, okay this is a little complicated but i will try to explain, so if you toggle this bit off, and your circuit works, that means your external oscillator is functioning and everything should be working fine (Specially including a sample test, which i was doing here). However, if you toggle this bit ON and your circuit does not work, that means your oscillator is not functioning properly and it needs some adjustment or modification, now if it works, after the sample test, you can toggle this bit ON and you'll have your Primary source oscillator functioning nicely.

my resistor is a 47K ohm resistor and a 56pF cap, i choose these values because based on the oscillation formula of 1 degree deep, it provides almost 38mHz which is around what i can work with, in my PIC16F877.

Thank you.

\$\endgroup\$
  • \$\begingroup\$ What actual values of R1 and C1 did you use? \$\endgroup\$ – Bruce Abbott Dec 20 '18 at 20:44
  • \$\begingroup\$ @KARAMJABER Single line answers are considered bad practice here. Please add some more information to increase the benefit to the community. \$\endgroup\$ – Ariser Dec 20 '18 at 21:34
  • \$\begingroup\$ @BruceAbbott please check the editted answer \$\endgroup\$ – forthelulx Dec 20 '18 at 23:11
  • 1
    \$\begingroup\$ @Ariser, Thank you for the clarification, i tried my best to answer my question like i understood it. I am new here so thanks for your help. \$\endgroup\$ – forthelulx Dec 20 '18 at 23:12
0
\$\begingroup\$

You will need to raise the impedance of the RC circuit (R1 between 3K and 100K and adjust C1 to give you the correct time constant).

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