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 support@ccsinfo.com

structures and pointers again

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



Joined: 30 Dec 2003
Posts: 9

View user's profile Send private message

structures and pointers again
PostPosted: Tue May 16, 2006 3:17 pm     Reply with quote

After looking at all the forum questions and answers i'm still a little unclear what goes where. The code below has an array of structures ( data_array) and a single structure ( temp_data ). The send_serial routine needs to be able receive either a structure of the array or the temp_data structure. What do i need to place in the function prototype and in the send_serial routine?




Code:
#include "C:\CCS Projects\18f6620.h"
#opt 11
#zero_ram

#define Serial_RX_PIN       PIN_G2
#define Serial_TX_PIN       PIN_G1
#use rs232(baud=57600, xmit=Serial_TX_PIN, rcv=Serial_RX_PIN, errors, stream=Serial_Input,RESTART_WDT)


=====================What do i put in this function prototype?

void Send_Serial(            ,int8 i);          // send data out the serial port

struct data_packet
{
   int   error_code :5;             // zero if no errors
                                    // 1 if addr sent not equal to address rec
                                    // 2 if current pulse greater then >30 ma( dup addr )
                                    // 4 data not same as previous
                                    // 8 incorrect parity
   int   leds :3;
   int   extra_flags :3;
   int   drift :1;
   int   sensitivity :4;
   int   interrupt :1;
   int   extended_protocol :1;
   int   alarm_flag :1;
   int   parity   :1;
   int   input_command :4;
   int8 address_trans;
   int8  analog2;
   int8 analog_value;
   int8 device_type;
   int8 address_rec;
   int8  interrupt_alarm_address;
};




   struct data_packet data_array[127] ;
   struct data_packet temp_data;





int1 Service_serial(void){
      struct data_packet *ptr;
      int8  i;
      int16 eeprom2_address,data_ptr;
      int8  temp;
      switch (buffered_data[1]){ // PACKET TYPE
         case 16: // Normal type
            switch (buffered_data[5]){   //COMMAND
               case 01: // write data to eeprom
                  break;
               case 02: // read data from scan
                  if (buffered_data[2]==255){ // is it a broadcast
                     for(i=1;i<127;i++){
                        if (data_array[i].error_code==0){
                           Send_Serial(&data_array[i],i);
                        }
                     }
                  }
                  else{
                    Send_Serial(&data_array[i],i);
                  }
                  break;
               case 03:  // write data to eeprom from scan
                  active_node_counter=0;
                  eeprom2_address=0;
                  for(i=1;i<127;i++){
                     if ((int8)data_array[i].error_code==0)
                        active_node_counter++;
                  }
                  init_ext_eeprom();
                  write_ext_eeprom(0,active_node_counter);   //save the number of active nodes
                  for(i=1;i<127;i++){
                     write_data_array_to_eeprom(&data_array[i],i);
                  }
                   /*
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].interrupt;
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].extended_protocol;
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].alarm_flag;
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].parity;
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].input_command;
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].error_code;
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].leds;
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].address_trans;
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].analog_value;
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].analog2;
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].drift;
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].sensitivity;
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].device_type;
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].address_rec;
                     write_ext_eeprom(eeprom2_address,data_array[nodeid].interrupt_alarm_address;
                   */
                  break;
               case 04: // read data from eeprom
                  if (buffered_data[2]==255){
                     for(i=1;i<127;i++){
                        read_eeprom_to_temp_data(i);
                        Send_Serial(&temp_data,i);
                     }
                  }
                  else{
                     read_eeprom_to_temp_data(buffered_data[2]);
                     Send_Serial(&temp_data,buffered_data[2]);
                  }
                  break;
                  case 255:
                     reset_cpu();
               default:
                  break;
            }
         default :
            break;
      }
}

void read_eeprom_to_temp_data(int8 i){
      int16 eeprom_address2;
      int8 a[9];
      int8 j;
      eeprom_address2=1 + (i-1)*sizeof(temp_data);
      for(j=0;j<sizeof(temp_data);j++){
         a[j]=read_ext_eeprom(eeprom_address2);
         eeprom_address2++;
      }
     memcpy(&temp_data,a,sizeof(temp_data));
}

void write_data_array_to_eeprom(int8 *tp,int8 i){
      int16 eeprom_address2;
      int8   *ptr;
      int8   q,j;
      ptr=tp;
      eeprom_address2=1 + (i-1)*sizeof(temp_data);
      for(j=0;j<sizeof(temp_data);j++){
          q=*ptr++;
         write_ext_eeprom(eeprom_address2,q);
         eeprom_address2++;
      }
}

================ What do i put in here?

void Send_Serial(          ,i){
      int8  temp,temp2;
      fprintf(serial_input,"NODE# %u\r",i);
      temp=0;
      if (ptr->interrupt)           // can't force a type conversion on int1 within a struct.
         temp=1;                                   // this sucks but it works
      fprintf(serial_input,"Interrupt %u\r",temp);
      temp=0;
      if (ptr->extended_protocol)
         temp=1;
      fprintf(serial_input,"Extended protocol %u\r",temp);
      temp=0;
      if (ptr->drift)
         temp=1;
      fprintf(serial_input,"Drift %u\r",temp);
      fprintf(serial_input,"Sensitivity %u\r",(int)ptr->sensitivity);
      temp=0;
      if (ptr->alarm_flag)
         temp=1;
      fprintf(serial_input,"Alarm flag %u\r",temp);
      fprintf(serial_input,"Parity %u\r",(int)ptr->parity);
      fprintf(serial_input,"Input command %u\r",(int)ptr->input_command);
      fprintf(serial_input,"Error code %u\r",(int)ptr->error_code);
      fprintf(serial_input,"Led state of last scan %u\r",(int)ptr->leds);
      fprintf(serial_input,"Address trans %u\r",ptr->address_trans);
      fprintf(serial_input,"Analog2 %u\r",ptr->analog2);
      fprintf(serial_input,"Analog1 %u\r",ptr->analog_value);
      fprintf(serial_input,"Device type %u\r",ptr->device_type);
      fprintf(serial_input,"Address_rec %u\r",ptr->address_rec);
      fprintf(serial_input,"Interrupt_alarm %u\n\r",ptr->interrupt_alarm_address);
}

Mark



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

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

PostPosted: Tue May 16, 2006 6:03 pm     Reply with quote

struct data_packet *ptr
Ted Ems



Joined: 30 Dec 2003
Posts: 9

View user's profile Send private message

pointers and structures
PostPosted: Wed May 17, 2006 5:38 am     Reply with quote

Mark
The compiler returns with an error message saying "Function definition different from previous". Got any suggestions
ckielstra



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

View user's profile Send private message

Re: pointers and structures
PostPosted: Wed May 17, 2006 6:20 am     Reply with quote

Ted Ems wrote:
The compiler returns with an error message saying "Function definition different from previous". Got any suggestions
This error means your function declaration (at the start of the program) is different from the implementation (your actual function).
Mark



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

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

PostPosted: Wed May 17, 2006 6:31 am     Reply with quote

You also need to declare the struct before your function prototype.
Ttelmah
Guest







PostPosted: Wed May 17, 2006 6:45 am     Reply with quote

Remember the structure type, must be declared, before the prototype function.
As an 'aside', there is no point in using an int1 return fom a function. The compiler always has to return at least a byte. The effect varies with compiler versions, but on some, using int1, actually results in slightly bulkier code, as the compiler evaluates the logic, converts it to a byte, then tests this again on the return. Like you, I felt it seemed logical to return a single bit for a flag, but I found on one particular program the result got larger. Inspecting the .LST, showed what was happening.

Best Wishes
Ted Ems



Joined: 30 Dec 2003
Posts: 9

View user's profile Send private message

PostPosted: Wed May 17, 2006 8:24 am     Reply with quote

I've looked at this code off and on for a couple days. Did not even occur to me that the declaration was in the wrong place.


Thanks for all the help
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