Problem with programming PIC P18F452 , Help plz !

Thread Starter

k.a.docpp

Joined Jan 2, 2011
1
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 ??!
 

spinnaker

Joined Oct 29, 2009
7,830
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 ??!

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.
 

Tahmid

Joined Jul 2, 2008
343
Hi,
Try with this code:
Rich (BB code):
void main(){

   TRISC = 0;
   LATC = 0b00010000;
   Delay_ms(1000);

   while(1){
         LATC = 0b01110000;
         Delay_ms(1000);
         LATC = 0b01010000;
         Delay_ms(1000);
         LATC = 0b01100000;
         Delay_ms(1000);
         LATC = 0b00100000;
         Delay_ms(1000);
   }
}
Go to Project > Edit Project. If you have a crystal of less than 8MHz, check that your config is set like this:


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.
 
Top