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

Char Stars not passing to functions

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



Joined: 27 Dec 2004
Posts: 21

View user's profile Send private message

Char Stars not passing to functions
PostPosted: Wed Apr 13, 2005 7:01 pm     Reply with quote

I cannot get a variable to pass to a function as a pointer.

for example, I call:
Code:
JeffSend(SomeString, 100);

in which the function is:
Code:
int16 JeffSend(char *data, int16 len)
{
//some code
}


but in the function, data is 0x0000, it is not the location of SomeString

Any idea why? SomeString is a gobally decleared array of char, i.e.: a string (and is not a constant).
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Apr 13, 2005 7:30 pm     Reply with quote

This has always worked, so likely the problem is in your code. There are several things that can be wrong therefor please post a small but complete working program showing your problem.

Also mention the compiler version you are using.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 14, 2005 1:06 pm     Reply with quote

Here is a test program that shows how to do it. This was tested with
PCM vs. 3.222. The output on the terminal window is:

Hello World
Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

void display_string(char *ptr)
{
printf("%s", ptr);
}

//===================
void main()
{
char array[20];

strcpy(array, "Hello World \n\r");
display_string(array);

while(1);
}
jmann



Joined: 27 Dec 2004
Posts: 21

View user's profile Send private message

PostPosted: Fri Apr 15, 2005 4:09 am     Reply with quote

when send-in calls writenetwork, data should have the value of 0x015F (the location of wr_ptr), however it gets the value 0x0020.

compiler Version 3.207 target chip 18F452

(also, I know that endian is reversed between the two devices I am trying to use, so void endian(int32) reverses endian after and before writing, but that is not important to this problem)


Code:
//-----------------------------------
//Preform a sequential write
//-----------------------------------
int1 WriteNetwork(int16 Addr, char* Data, int16 len)
{
   int16 i;
   char Err = NOERROR;

   I2CInterrupts(DisableI2C);// Disable Interrupt
   i2c_start();            // START Signal Of Address
   if(i2c_write(W3100Awrite)==NoError   &&
      i2c_write(make8(addr,1))==NoError && //address MSD
      i2c_write(make8(addr,0))==NoError )  //address LSD
   {
      for(i = 0 ; i < len ; i++)
         if(i2c_Write(*Data++)==ERROR)
         {
            err=ERROR;
            break;
         }
   }
   else //error occured
   {
      err=ERROR;
   }
   i2c_stop();                   // STOP Signal Of Address
   I2CInterrupts(EnableI2C);// Enable Interrupt
   return Err;
}

Code:
int16 send_in(char* data, int16 len)
{
   char k;
   signed int16 size;
   int32 wr_ptr, ack_ptr;
   int16 address;

S_START:
//   I2CInterrupts(DisableI2C);

   ReadNetwork(SHADOW_TXWR_PTR(0), wr_ptr, 3);   // Must read the shadow register for reading 4byte pointer registers
   delay_us(2);                    // wait for reading 4byte pointer registers safely
   ReadNetwork(TX_WR_PTR(0), wr_ptr, 4);
   endian(wr_ptr);

   ReadNetwork(SHADOW_TXACK_PTR(0), ack_ptr, 3);  // Must read the shadow register for reading 4byte pointer registers
   delay_us(2);                    // wait for reading 4byte pointer registers safely
   ReadNetwork(TX_ACK_PTR(0), ack_ptr, 4);
   endian(ack_ptr);

//   I2CInterrupts(EnableI2C);

   // Calculate send free buffer size
   if ((wr_ptr&SMASK) >= (ack_ptr&SMASK))
      size = SSIZE - ((wr_ptr&SMASK) - (ack_ptr&SMASK));
   else
      size = SSIZE - (0 - (ack_ptr&SMASK) + (wr_ptr&SMASK));

   if (size > SSIZE)                                                            // Recalulate after some delay because of error in pointer caluation
   {
      if (ReadNetworkRegister(SOCK_STATUS(0)) != SOCK_ESTABLISHED) return 0;              // Error

      delay_ms(1);

      goto S_START;
   }

   if (size == 0)                                                                  // Wait when previous sending has not finished yet and there's no free buffer
   {
      if (ReadNetworkRegister(SOCK_STATUS(0)) != SOCK_ESTABLISHED) return 0;              // Error
      delay_ms(1);
      goto S_START;
   }
   else if (size < len)   len = size;

   address = (int16)( SEND_DATA_BUF + ((int16)(wr_ptr)) & SMASK);    // Calculate pointer to data copy
   WriteNetwork(address, data, len);                                               // data copy

   while (WriteNetworkRegister(COMMAND(0), CSEND))                                       // Confirm send command
      if (ReadNetworkRegister(SOCK_STATUS(0)) != SOCK_ESTABLISHED) return 0;              // Error

   wr_ptr = wr_ptr + len;   
   endian( wr_ptr);                                            // tx_wr_ptr update
   WriteNetwork(TX_WR_PTR(0), wr_ptr, 4);

   WriteNetworkRegister(COMMAND(0),CSEND);                                               // SEND

    return   (len);
}
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Apr 15, 2005 6:12 am     Reply with quote

Take a look at these three statements. Data is defined as a pointer to a char. wr_ptr is defined as int32 and you are not even calling the function with the address of wr_ptr but using the value. I can see why you are having problems.
Code:

int1 WriteNetwork(int16 Addr, char* Data, int16 len)
int32 wr_ptr, ack_ptr;
WriteNetwork(TX_WR_PTR(0), wr_ptr, 4);
jmann



Joined: 27 Dec 2004
Posts: 21

View user's profile Send private message

PostPosted: Sat Apr 16, 2005 9:27 am     Reply with quote

but a pointer is a pointer, right?

then wouild this work?:

Code:
int16 send_in(char* data, int16 len)
{
   char k;
   signed int16 size;
   int32 wr_ptr, ack_ptr;
   int16 address;
   char* pointer;

   pointer = (&wr_ptr);                                   //make pointer point to the location of wr_ptr
   WriteNetwork(TX_WR_PTR(0), pointer, 4); //now call function with a pointer pointing of the same type



I know it is little endian, and would have to deal with that.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Sat Apr 16, 2005 12:35 pm     Reply with quote

All you need is
Code:

WriteNetwork(TX_WR_PTR(0), &wr_ptr, 4);


but
Code:

WriteNetwork(TX_WR_PTR(0), (char*)&wr_ptr, 4);
is more correct
Guest








PostPosted: Sat Apr 16, 2005 4:04 pm     Reply with quote

thank you, I am making those changes.
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