for loop error when programming with dsPIC v3_20b programmer

Status
Not open for further replies.

Cantafford

Member
Hello. I'm playing with a dsPIC30F2010 and I wanted to make a led blink. I wrote this code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include "header.h"


void main()
{
    TRISB = 0;
    while(1)
    {
        LATBbits.LATB0 = 1;
        for(int delay=0; delay<2; delay++) __delay_ms(5);
    }
}

When I run this code I get the following error:
main.c:19: error: 'for' loop initial declaration used outside C99 mode

I tried initialising the 'delay' variable outside the loop. Still get that error.
When I program PIC's, not dsPIC's and I write such a loop to get a delay I don't get that error. I only get it when I'm using the dsPIC programmer. Any ideea why? Thanks for reading.
 
Hello, thanks for answering

Well thus far I have been using it like that and got no errors(I've seen it in a tutorial). I even tested with a program that I wrote for PIC18f2550 and it worked fine. I'm guessing it's included in stdio or stdlib.
In header.h I have: "#define __XTAL_FREQ 8000000" if that's what you mean by defining a frequency.
 
The dspPic range use the C30 compiler.... I don't think it uses that syntax...

The __delay_ms() was defined in the pic.h on the HTC compiler.. If you loose the for loop and just write the delay instruction... What does the compiler say!!
 
They want this:

C:
        int delay;
        for(delay=0; delay<2; delay++) __delay_ms(5);

For the delay to work, you need to define something of that sort:

C:
#define FOSC (7370000ULL)
#define FCY (FOSC/2) // if you have one of obsolete dsPIC30, it's FOSC/4 for most of them
 
The dspPic range use the C30 compiler.... I don't think it uses that syntax...

The __delay_ms() was defined in the pic.h on the HTC compiler.. If you loose the for loop and just write the delay instruction... What does the compiler say!!
It says:
undefined reference to `___delay_ms'

So I just include them in my header file and it should work? Shouldn't I include a library that has the __delay_ms() function? I don't understand
 
In your header write a delay function that will cover all

C:
void delayUs(int x)
   {
    x>>= 5; // change to  4, 3 or 2 to suit OSC
    while(x--);
    }

void delayMs(int x)
   {    
    while(x--)
       delayUs(1000);
    }

Then you can specify any delay!!
 
They want this:

C:
        int delay;
        for(delay=0; delay<2; delay++) __delay_ms(5);

NorthGuy has the correct answer. This error:

Code:
main.c:19: error: 'for' loop initial declaration used outside C99 mode

is basically saying that you can't declare an int within the for loop. You need to declare the int externally (before the for loop):

C:
        int delay;
        for(delay=0; delay<2; delay++) __delay_ms(5);
 
Had a bit of a tussle with this and PIC24EP For these delays with XC16 ,
Code:
#define FCY 8000000UL       // instruction cycle  16M osc
#include <libpic30.h>

// libpic30 delays
#define __delay_ms(d) \
  { __delay32( (unsigned long) (((unsigned long long) d)*(FCY)/1000ULL)); }
#define __delay_us(d) \
  { __delay32( (unsigned long) (((unsigned long long) d)*(FCY)/1000000ULL)); }

Then use as __delay_ms(250) ; etc.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…