Problem with programming PIC P18F452 , Help plz !

Discussion in 'Embedded Systems and Microcontrollers' started by k.a.docpp, Jan 2, 2011.

  1. k.a.docpp

    Thread Starter New Member

    Jan 2, 2011
    1
    0
    Hello
    I've written a C program using MicroC for PIC MCU P18F452. It is a simple blinker that will turn on and turn off LEDs on PORTC.F4 , PORTC.F5 and PORTC.F6 in a specified pattern. Pattern will change after 1 second and we have 4 patterns. Here is the program :

    void main()
    {
    TRISC = 0 ;

    PORTC = 0b00010000 ;

    Delay_ms( 1000 ) ;

    while( 1 )
    {
    PORTC = 0b01110000 ;

    Delay_ms( 1000 ) ;

    PORTC = 0b01010000 ;

    Delay_ms( 1000 ) ;

    PORTC = 0b01100000 ;

    Delay_ms( 1000 ) ;

    PORTC = 0b00100000 ;

    Delay_ms( 1000 ) ;
    }
    }

    The problem is , when I program the MCU , in the reality; on the board , only first 2 patterns will be shown on LEDs and suddenly everything starts from first. I mean :

    LED( PORTC.F4 ) is On , LED( PORTC.F5 ) is Off , LED( PORTC.F6 ) is Off
    Sleeps for 1 second ...
    LED( PORTC.F4 ) is On , LED( PORTC.F5 ) is On , LED( PORTC.F6 ) is On
    Sleeps for 1 second ...
    LED( PORTC.F4 ) is On , LED( PORTC.F5 ) is Off , LED( PORTC.F6 ) is On
    No wait ... everything is reset and it seems the main function is called again ...
    LED( PORTC.F4 ) is On , LED( PORTC.F5 ) is Off , LED( PORTC.F6 ) is Off -------> Same as the start of main function
    Sleeps for 1 second ...
    LED( PORTC.F4 ) is On , LED( PORTC.F5 ) is On , LED( PORTC.F6 ) is On
    Sleeps for 1 second ...
    LED( PORTC.F4 ) is On , LED( PORTC.F5 ) is Off , LED( PORTC.F6 ) is On
    No wait ... everything is reset and it seems the main function is called again ...
    And this will be continued ...

    Is there anyway to produce such patterns ?! Is it a hardware limitation that we can not write such programs ?! why the MCU goes from first ?! I've written the program using MPLAB but the same occurs with a little change that only the first command in the loop is occured and next everything is reset ...

    Please help ??!
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016

    Your explanation of what is wrong is very hard to read. Just write it like this:

    00010000

    Loop
    01110000
    01010000
    01100000
    00100000
    Gotto Loop



    This is what I am seeing on my demo board. I had to change it to PORTD but that is where my LEDs are.


    Are you sure that you can light all the lights and connections are good? What happens if you send a 255 to the PORT?

    Try using LAT instead of PORT.
     
  3. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    Hi,
    Try with this code:
    Code ( (Unknown Language)):
    1. void main(){
    2.  
    3.    TRISC = 0;
    4.    LATC = 0b00010000;
    5.    Delay_ms(1000);
    6.  
    7.    while(1){
    8.          LATC = 0b01110000;
    9.          Delay_ms(1000);
    10.          LATC = 0b01010000;
    11.          Delay_ms(1000);
    12.          LATC = 0b01100000;
    13.          Delay_ms(1000);
    14.          LATC = 0b00100000;
    15.          Delay_ms(1000);
    16.    }
    17. }
    Go to Project > Edit Project. If you have a crystal of less than 8MHz, check that your config is set like this:
    [​IMG]

    If the clock is greater than 8MHz, just change XT to HS.

    Check if the bytes in the Configuration Registers textbox are the same.

    Hope this helps.
    Tahmid.
     
  4. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    Not really. This problem shouldn't happen. Try with the code I provided above.
     
Loading...