C2C "For"

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
TerryV
Posts: 33
Joined: Tue Feb 21, 2006 10:06 am
Contact:

C2C "For"

Post by TerryV »

Hi,
I am working through the "C4PICs" version 3 and example 1.4 uses the "For" loop with v3 development board.

Whilst the program compiles and the assembler runs and the chip is programmed, the "For" loop does not appear to operate.

The program just appears to stop.

I have isolated the various parts of the program and it appears that the program gets as far as the loop and stops.

Is there any known problem or any suggestions as to what might be happening.

The board setting are all as they should be.

Thanks.
Last edited by TerryV on Sat Feb 20, 2010 7:40 am, edited 1 time in total.

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: C2C "If"

Post by Steve »

Is the watchdog timer on? This can appear to cause problems like this. I know all of these programs work.

Please post the program here and someone may be able to help.

TerryV
Posts: 33
Joined: Tue Feb 21, 2006 10:06 am
Contact:

Re: C2C "For"

Post by TerryV »

Hi,
Thanks for the response.

It is actuallly a for loop. i was jumping ahead of myself but the problem is the same.

The Watch dog timer is off.

As I said - it compiles and assemles and downloads - but only a (0) lights up and does not flash???

The instructions are:

The program we have written will make the LED flash, but we need to run the PICmicro at low speed to make it work. If you run the program at the speed of the crystal clock (remember to reload the program into the PICmicro with the new clock settings) you will not see a flashing LED, you will see a glowing one.

What is required is a way of "pausing" the processor once the LED has been switched on. Unfortunately we can't do this, but we can keep it busy with a loop. To do this you will need to make a for loop which does nothing. The PICmicro will run this, and it will provide a pause in the execution of the program.

I have used a for loop to give a nice big delay. I has also used an integer as the counter. This slows things down in two ways, we can count to a larger limit if we use integers, and the actual mathematics takes longer with integer variables.

If you run this program on the PICmicro (it would take around a month to execute on the Virtual CPIC!) you will find that the LED flashes at around 1 Hz.

Please note to run this program you will need to set the PICmicro FAST/SLOW switch to FAST. You will also need to configure the PPP program so that the oscillator is set to XT crystal device.

The flasher seems to spend the same amount of time on as off now. This is because the time taken to perform the while loop is insignificant as compared to the time taken in the two delay loops.


The code is as follows.

/* Flash one LED */
/* (c) Rob Miles July 2003 */

#include <system.h>

void main (void)
{
unsigned int i ;

/* set bit 0 of PORTA for output */
set_tris_a (0x1e);
while (1) {
/* now set the LED bit */
output_high_port_a (0);
for ( i=0 ; i<32000 ; i= i + 1 ) ;
output_low_port_a (0);
for ( i=0 ; i < 32000 ; i= i + 1 ) ;
output_high_port_a (1);
}
}

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: C2C "For"

Post by Steve »

It could be your configuration information is not appropriate for your hardware and the clock speed is actually much slower than the program expects. What hardware are you using - including details about clocking and chip type?

You could also try reducing the delay value to 3200 or 320 to see if this has an effect.

TerryV
Posts: 33
Joined: Tue Feb 21, 2006 10:06 am
Contact:

Re: C2C "For"

Post by TerryV »

Hi,
I have tried changing the value of "i", in fact I created the following to determine whether the program was getting past the for loop, and it appears it is not.

#include <system.h>
#include "Project_1A.h"

void main (void)
{
unsigned int i ;

/* set bit 0 of PORTA for output */
set_tris_a (0x1e);

for (i=0 ; i<10 ; i= i + 1 ){
output_high_port_a (0);
}
output_low_port_a (0);
}

a(o) goes high and stays there.

PPP is configured for RC and the board is set to RC fast.

Any ideas?

Thanks

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: C2C "For"

Post by Steve »

What chip are you using and what configuration word is being set in the chip?

Also, your test program is a little wrong. The intention of the "for" loop is to provide a delay. So instead of this:

Code: Select all

set_tris_a (0x1e);

for (i=0 ; i<10 ; i= i + 1 ){
output_high_port_a (0);
}
output_low_port_a (0);
You should try this:

Code: Select all

set_tris_a (0x1e);

for (i=0 ; i<10 ; i=i+1);
output_high_port_a (0);
for (i=0 ; i<10 ; i=i+1);
output_low_port_a (0);
You could also try accessing the port directly, instead of using the inbuilt functions. E.g.

Code: Select all

trisa=0x1e;

while (1)
{
    for (i=0 ; i<10 ; i=i+1);
    porta = 0x01;
    for (i=0 ; i<10 ; i=i+1);
    porta = 0x00;
}
(NOTE: it's a long time since I used C"C. I think you may need to capitalise the registers - i.e. TRISA and PORTA)

Post Reply