Gehad Mohamed
Gehad Mohamed

Reputation: 11

MPLAB infinite loop

I have 2 questions.

The first: I have a problem in the behavior of this code; when I run it in Proteus the program make flasher "repeat the code in the main function" what should I do?

This is the code:

#include <p18f452.h>
#include <delays.h>
#include <io.h>

void main ()
{
    TRISC=0x00;
    PORTC=0xff;
    Delay1KTCYx(900);
    PORTC=0x00;
    Delay1KTCYx(900);
    while(1)
    {

    }
}

The second question: what is the proper delay function I can use? and how can I measure the delay time?

Upvotes: 0

Views: 646

Answers (2)

fjardon
fjardon

Reputation: 7996

Is the watchdog disabled in simulation ? If it is enabled it will cause the repetition of the program.

Try adding this line after the includes.

#pragma config WDT = OFF

Upvotes: 2

unwind
unwind

Reputation: 399979

You only have code to generate one flash. Move the flash and delays into the loop:

for(;;)
{
  PORTC = 0xff;
  Delay1KTCYx(900);
  PORTC = 0x00;
  Delay1KTCYx(900);
}

Measuring roughly can be made manually by timing N flashes with a stopwatch. It's of course easier to use a measurement intrument (an oscilloscope is nice for this) if you have it.

Also, since your duty cycle is 50%, you can simplify the code:

PORTC = 0;
for (;;)
{
  PORTC = ~PORTC;
  Delay1KTCYx(900);
}

This uses bitwise not (~) to invert the bits of PORTC, which will make them toggle from one to zero and vice versa. Setting the entire port to 0 before the loop makes sure all pins are at a known state.

Upvotes: 1

Related Questions