need help with simple use of pic18f2455

Discussion in 'Embedded Systems and Microcontrollers' started by leviitzhak, Apr 16, 2014.

  1. leviitzhak

    Thread Starter New Member

    Apr 16, 2014
    3
    0
    I am trying to use pic18f2455 and I could not manage to use it in a simple way.

    I am new to pic programming and any helpful advice would be greatly appreciated.

    Attached is the simplified schematic of my circuit.

    I also attached the configuration file config.h that I am using. Its main parts being :

    #pragma config FOSC = INTOSCIO_EC
    #pragma config PWRT = OFF
    #pragma config BOR = OFF
    #pragma config WDT = OFF
    #pragma config PBADEN = OFF
    #pragma config MCLRE = ON
    #pragma config STVREN = OFF

    Now a simple program like the following would not light my LED :

    #include<xc.h>
    #include "config.h"

    void main() {

    TRISBbits.TRISB0 = 0;
    LATBbits.LATB0 = 0;

    for(int i=0; i<50; ++i){}

    LATBbits.LATB0 = 1;
    for( ; ; ){}

    }

    I managed to isolate the problem and I suspect a constant Power-on-Reset which I think I can show with the following code, which lets the LED constantly lit :

    void main() {

    TRISBbits.TRISB0 = 0;

    if(RCONbits.POR == 0){
    LATBbits.LATB0 = 1;
    }else{
    LATBbits.LATB0 = 0;
    }

    RCONbits.POR = 1;

    for(int i=0; i<50; ++i){}

    LATBbits.LATB0 = 0;
    for( ; ; ){}

    }

    Any help will be appreciated. Thanks!!
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Yeah, this behavior is expected when switching Vdd and Vss. ;)

    You may (probably) have killed it...

    Did you program it out of circuit?

    On the plus side, you remembered MCLR, which a lot of people forget...
     
    leviitzhak likes this.
  3. leviitzhak

    Thread Starter New Member

    Apr 16, 2014
    3
    0
    Thanks for your answer it helps me a lot.
    Yes I programmed it out of circuit, probably switched once by mistake Vdd and Vss. It will take me a couple of days to get a new pic, is there a way to test if my pic is dead ?
     
    Last edited: Apr 16, 2014
  4. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    What is your power supply?

    Many times a PIC can withstand reverse connection pretty well, it depends a lot on your PSU and how much current it can source.

    Re testing the PIC, just try and use it.

    You have a number of code errors. That PIC (from memory) needs to configure the ADC pins etc to make them digital or the port won't work as expected.

    Then you have no while(1) loop to keep your code running, and you have no delay in there to slow down the flashing so your eye can see it.
    :)
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    One good test is to erase the PIC, program it, and verify the write.

    A dead PIC cannot be programmed.

    Meanwhile, this is a good time to try out the debugger, either the simulator built into MPLAB, or possibly thru your programmer to the hardware. PICkits can do that.
     
    leviitzhak likes this.
  6. leviitzhak

    Thread Starter New Member

    Apr 16, 2014
    3
    0
    Thanks THE_RB for your suggestions. I checked that I do supply 5V.

    As for the code errors you are talking about, I have configured the only pin that I am using as being a digital I/O (port b). Why should I care about the rest ?

    About the infinite loop, I did write a for( ; ; ), maybe you did not see it.

    Thanks for your help, but I think tshuck got the problem at first sight : it is apparently because I switched Vdd and Vss.
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Hey RB, actually this should do what he thinks it should do... granted his code is horribly formatted to the point of obscurity. (That’s a hint leviitzhak ;-) )

    The code checks if the power on reset flag is reset (normal after a power-on) and lights the LED, and after a time turns it off.

    Whatever happens before the “LATBbits.LATB0 = 0;” should leave the LED off.

    THEN an infinite loop is placed, so the code runs one and once only. That’s fine for a test.

    leviitzhak: I do hope you powered BOTH Vss pins, and it wouldn't hurt to put the cap on the Vusb pin the spec calls for.
     
    leviitzhak likes this.
  8. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Thanks Ernie, I only checked his text briefly and thought the goal was to flash a LED, and the code did not seem very good for that task... ;).
     
Loading...