Page 1 of 1

Understanding Programming

Posted: Tue Dec 29, 2009 10:40 am
by TerryV
As a beginner I have written a small C program which simply turns on the LEDs on the V3 development board.

The program compiles and down loads successfully but functions differently to what I expected:

My understanding was that an o/p will stay hi unless it is turned off, this does not appear to be the case, also having reached the end, the program appears to randomly restart with out my resetting.

Can some one explain?

Many thanks

Re: Understanding Programming

Posted: Wed Dec 30, 2009 12:04 pm
by Benj
Hello
My understanding was that an o/p will stay hi unless it is turned off
PICs work in 8-bit therefore if you modify one bit on a port then you are actually modifying an entire port. To get around this you could use something like this.

portb = portb | 0x01; //sets bit 1 of port b
portb = portb & 0xFE; //clears bit 1 of port b
having reached the end, the program appears to randomly restart with out my resetting.
The program counter on the device that fetches instructions will keep going even if your program does not. Therefore at the end of your program you must have a infinite loop to stop the program memory wrapping around back to the start or worse.

while (1) ;