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

[SOLVED] Structure with array of int16 issue

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



Joined: 30 Jan 2012
Posts: 216

View user's profile Send private message

[SOLVED] Structure with array of int16 issue
PostPosted: Mon Jun 15, 2020 3:50 am     Reply with quote

hello,

I'm working on a project and want to use a structure which contain an array of int16, but I have wird result :(

exemple of code :
Code:
#include <18F66J94.h>
#device ADC=12

#FUSES NOWDT                    //No Watch Dog Timer

#use delay(clock=64MHz,crystal=16MHz,USB_FULL)

#include <usb_cdc.h>

typedef struct {
   int   puissances [10];
   int16 profondeurs[10];
} exerciceAl;

void affichage(exerciceAl exer_loc){
   int i;
   if(usb_cdc_carrier.dte_present){
      printf(usb_cdc_putc,"puissances           :");
      for(i=0;i<10;i++)
         printf(usb_cdc_putc," %2u",exer_loc.puissances[i]);
      printf(usb_cdc_putc,"\r\nprofondeurs G         :");
      for(i=0;i<10;i++)
         printf(usb_cdc_putc," %2lu",exer_loc.profondeurs[i]);
      printf(usb_cdc_putc,"\r\n");
   }
                                 
}

void main()
{
   setup_lcd(LCD_DISABLED);
   setup_adc_ports(sAN0 | sAN1 | sAN7, VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL | ADC_TAD_MUL_0);
   
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64);      //262 ms overflow,  resolution 4us
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);        //32,7 ms overflow, resolution 0,5us
   
   disable_interrupts(INT_TIMER0);
   disable_interrupts(INT_TIMER1);
   disable_interrupts(INT_EXT);
   disable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   
   usb_init();
   usb_task();
   
   int connectedUSB = 0;
   
   exerciceAl exer_testAL =  {5,5,5,5,5,5,5,5,5,5,
   200,200,200,200,200,200,200,200,200,200};
   
   while(true){
      usb_task();
      if(usb_cdc_carrier.dte_present){
         if(connectedUSB == 0){
            connectedUSB = 1;
            printf(usb_cdc_putc,"Test\r\n");
            affichage(exer_testAL);
         }
      }
   }
}


I got :
Quote:
Test :
puissances : 5 5 5 5 5 5 5 5 5 5
profondeurs G : 0 0 0 0 0 0 0 0 0 0


but I expected :
Quote:
est :
puissances : 5 5 5 5 5 5 5 5 5 5
profondeurs G : 200 200 200 200 200 200 200 200 200 200


what is wrong ???

thanks for your help

regards


Last edited by spilz on Mon Jun 15, 2020 7:24 am; edited 2 times in total
spilz



Joined: 30 Jan 2012
Posts: 216

View user's profile Send private message

PostPosted: Mon Jun 15, 2020 3:55 am     Reply with quote

same result if I add
Code:
for(int i=0;i<10;i++)
   exer_testAL.profondeurs[i] = 100;

just before the "while" loop
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Jun 15, 2020 6:01 am     Reply with quote

others will KNOW but I'm guessing you really need for i=10 to 19 to get the int16 elements from the array ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Jun 15, 2020 6:41 am     Reply with quote

Brackets. You are sending all the elements to the first array (so the second
lot gets thrown away):
Code:

exerciceAl exer_testAL =  {{5,5,5,5,5,5,5,5,5,5},
  {200,200,200,200,200,200,200,200,200,200}};

Note the extra two brackets, and the change in the comma position.

A single structure, containing an array of ten elements, then a second
array of ten elements.
spilz



Joined: 30 Jan 2012
Posts: 216

View user's profile Send private message

PostPosted: Mon Jun 15, 2020 7:23 am     Reply with quote

I change it

I found the issue :
Code:
printf(usb_cdc_putc," %2lu",exer_loc.profondeurs[i]);

changing it by
Code:
printf(usb_cdc_putc," %lu",exer_loc.profondeurs[i]);

seems to solve the issue

thanks for your help

regards
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