CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Initialisation array problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ibg



Joined: 19 Nov 2003
Posts: 22

View user's profile Send private message

Initialisation array problem
PostPosted: Mon May 17, 2004 11:41 am     Reply with quote

Hi,

I have a problem when I try to initiliase the 'time' field (it's a string) of a struct that belongs to an array.

The problem I got is that when I print 'time' just garbage appear.

The code is the following:
Code:

#include <18F452.h>      // The device (PIC 18F452) include file
#device *=16   ADC=10

#include <string.h>

#define MAX_FISH_DISPLAYED 3     // The Maximum number of fish entries displayed

#use delay(clock=19660800)   // Sets the speed for the PIC (it�s meausured in cycles per second)

#use fast_io(A)            // Fast method of doing Input/Output
#use fast_io(B)            // The direction register will be set later with the "set_tris_X()" instruction
#use fast_io(C)
#use fast_io(D)

// I/O ports are mapped in memory (Have a look to the Special Function Register (SFR) of PIC18F452)
#byte PORTA  = 0xF80
#byte PORTB  = 0xF81
#byte PORTC  = 0xF82
#byte PORTD  = 0xF83
#byte PORTE  = 0xF84

// Peripheral Interrupt Request (for TIMER1)
#byte PIR1 = 0xF9E

#use rs232(baud=38400, xmit=PIN_C0, stream=DEBUG)

struct data_fish{
   char time[10];
   unsigned int32 frequency;
   unsigned int pulses;
   unsigned long signal_strength;
   unsigned int32 time_unit;   
};

char table[3][5]= {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O'};
struct data_fish data_fish_array[MAX_FISH_DISPLAYED];

//-----------------------------------------------------------------------------------
// Functions used by the main program
void init();
void init_data_fish_array(struct data_fish a_data_fish_array[]);

//----------------------------------------------------------------------------------
// Main program
void main(void)
{
   int i, j, k= 0;
   int ascii_A= 65;
   int ascii_B= 66;
   int ascii_C= 67;
   int ascii_D= 68;
   int ascii_E= 69;
   int ascii_to_add= 0;

   init();
   init_data_fish_array(data_fish_array);
   fprintf(DEBUG, "Starting ...\n\r");

   for (i= 0; i < MAX_FISH_DISPLAYED; i++)
   {
       fprintf(DEBUG, "%s\n", data_fish_array[i].time);     
      fprintf(DEBUG, "%lu\n", data_fish_array[i].frequency);     
      fprintf(DEBUG, "%d\n", data_fish_array[i].pulses);     
      fprintf(DEBUG, "%lu\n", data_fish_array[i].signal_strength);     
      fprintf(DEBUG, "%lu\n", data_fish_array[i].time_unit);     
   }
}

//----------------------------------------------------------------------------------
// Initialisation routine
void init()
{
   // SET_TRIS is necessary when using fast_io (in order to establish the pin direction)
   // Remember:
   //   1 -> in
   //   0 -> out

   set_tris_a(0x00);       // 0000 0000
   set_tris_b(0x00);       // 0000 0000   
   set_tris_c(0x00);         // 0000 0000
   set_tris_d(0x00);       // 0000 0000
}

// Initialise the array that contains fish data
void init_data_fish_array(struct data_fish a_data_fish_array[])
{
   char the_time[10]= "--:--:--";
   int i;
   for (i= 0; i < MAX_FISH_DISPLAYED; i++)
   {
     strcpy(a_data_fish_array[i].time, the_time);     
      a_data_fish_array[i].frequency= 0;     
      a_data_fish_array[i].pulses= 0;     
      a_data_fish_array[i].signal_strength= 0;     
      a_data_fish_array[i].time_unit= 0;     
   }
}
//----------------------------------------------------------------------------------

Thanks for your help,

   ibg
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 17, 2004 12:17 pm     Reply with quote

Quote:
The problem I got is that when I print 'time' just garbage appear.
strcpy(a_data_fish_array[i].time, the_time);


Change the function name to "strcopy", and it should work.
strcopy(a_data_fish_array[i].time, the_time);

CCS uses strcopy() to do ram-to-ram copying.

They use strcpy() to copy from ROM-to-ram.

Since your source and destination strings are both in ram,
you must use strcopy.
ibg



Joined: 19 Nov 2003
Posts: 22

View user's profile Send private message

'strcopy' doesn't work either
PostPosted: Mon May 17, 2004 12:48 pm     Reply with quote

Sorry, but the function 'strcopy' doesn't work either. Any other suggestion?

Thanks,

ibg
Hans Wedemeyer



Joined: 15 Sep 2003
Posts: 226

View user's profile Send private message

try this
PostPosted: Mon May 17, 2004 3:06 pm     Reply with quote

#include <18F452.h> // The device (PIC 18F452) include file
#device *=16 ADC=10

#include <string.h>

#define MAX_FISH_DISPLAYED 3 // The Maximum number of fish entries displayed

#use delay(clock=19660800) // Sets the speed for the PIC (it�s meausured in cycles per second)

#use fast_io(A) // Fast method of doing Input/Output
#use fast_io(B) // The direction register will be set later with the "set_tris_X()" instruction
#use fast_io(C)
#use fast_io(D)

// I/O ports are mapped in memory (Have a look to the Special Function Register (SFR) of PIC18F452)
#byte PORTA = 0xF80
#byte PORTB = 0xF81
#byte PORTC = 0xF82
#byte PORTD = 0xF83
#byte PORTE = 0xF84

// Peripheral Interrupt Request (for TIMER1)
#byte PIR1 = 0xF9E

#use rs232(baud=38400, xmit=PIN_C0, stream=DEBUG)

typedef struct {
char time[10];
unsigned int32 frequency;
unsigned int pulses;
unsigned long signal_strength;
unsigned int32 time_unit;
}data_fish;

char table[3][5]= {'A', 'B', 'C','D', 'E', 'F','G', 'H', 'I','J', 'K', 'L','M', 'N', 'O'};

data_fish data_fish_array[MAX_FISH_DISPLAYED];
//-----------------------------------------------------------------------------------
// Functions used by the main program
void init();

// Initialise the array that contains fish data
void init_data_fish_array(data_fish a[])
{
char the_time[]={"--:--:--"};
int i;
for (i= 0; i < MAX_FISH_DISPLAYED; i++)
{
strcpy(a[i].time, the_time);
a[i].frequency= 0;
a[i].pulses= 0;
a[i].signal_strength= 0;
a[i].time_unit= 0;
}
}

//----------------------------------------------------------------------------------
// Main program
void main(void)
{
int i, j, k= 0;
int ascii_A= 65;
int ascii_B= 66;
int ascii_C= 67;
int ascii_D= 68;
int ascii_E= 69;
int ascii_to_add= 0;

init();
init_data_fish_array(data_fish_array);
fprintf(DEBUG, "Starting ...\n\r");


for (i= 0; i < MAX_FISH_DISPLAYED; i++)
{
fprintf(DEBUG, "%s\n", data_fish_array[i].time);
fprintf(DEBUG, "%lu\n", data_fish_array[i].frequency);
fprintf(DEBUG, "%d\n", data_fish_array[i].pulses);
fprintf(DEBUG, "%lu\n", data_fish_array[i].signal_strength);
fprintf(DEBUG, "%lu\n", data_fish_array[i].time_unit);
}
}

//----------------------------------------------------------------------------------
// Initialisation routine
void init()
{
// SET_TRIS is necessary when using fast_io (in order to establish the pin direction)
// Remember:
// 1 -> in
// 0 -> out

set_tris_a(0x00); // 0000 0000
set_tris_b(0x00); // 0000 0000
set_tris_c(0x00); // 0000 0000
set_tris_d(0x00); // 0000 0000
}
//----------------------------------------------------------------------------------
Guest








I still get garbage :(
PostPosted: Tue May 18, 2004 3:38 pm     Reply with quote

Hi,

I have compiled and run the above code and I still get garbage when the time field is printed. Any suggestion about how to fix it?

Thanks,

ibg
Hans Wedemeyer



Joined: 15 Sep 2003
Posts: 226

View user's profile Send private message

Re: I still get garbage :(
PostPosted: Tue May 18, 2004 6:04 pm     Reply with quote

Anonymous wrote:
Hi,

I have compiled and run the above code and I still get garbage when the time field is printed. Any suggestion about how to fix it?

Thanks,

ibg


try printing the array as it is initialized, and dump that strcpy:-

void init_data_fish_array(data_fish a[])
{
char the_time[]={"--:--:--"};
int i,j;
for (i= 0; i < MAX_FISH_DISPLAYED; i++)
{
for (j=0; j<strlen(the_time) ; j++)
{
a[i].time[j] = the_time[j];
puts(a[i].time[j]);
}
a[i].frequency= 0;
a[i].pulses= 0;
a[i].signal_strength= 0;
a[i].time_unit= 0;
}
}

If it prints OK then look for a problem elsewhere.
Guest








It's working
PostPosted: Wed May 19, 2004 11:33 am     Reply with quote

Hej again,

I don't know yet but the 'strcpy' has a weird behaviour and it doesn't work.

Anyway, I copy the string as suggested (going through the different elements of the string) and is working well.

Thanks,

ibg
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group