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

Multidimensional array RAM location

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



Joined: 23 Dec 2015
Posts: 37

View user's profile Send private message

Multidimensional array RAM location
PostPosted: Wed Jul 06, 2016 7:33 am     Reply with quote

Hi,

I'm using the PIC 18f2685 and my compiler version is 4.114.
I'm programming a system that monitors temperatures. The information is organized in a 3 dimensional array:

T[location][Part1/Part2][sensor]
Being location the place where it's read. Part1/Part2 each of the 2 bytes that conform the temperature and finally sensor the number of sensor of that location:

25,12ÂșC in the first location by the first sensor would be:

T[0][0][0]=12;
T[0][0][0]=25;

All this information is received from 7 Slave PCBs, finally all the information is organized as mentioned before in T[a][b][c].

My doubt comes when using the can_putd function for retransmitting this information to an external data acquisition system. I don't know how does the PIC internally organizes the information and so if my code is fine.


Code:
#include <Master.h>
#define LED PIN_B7
int8 temp[7][2][6];
int8 adr[7] = {0xA1,0xAB,0xB5,0xBF,0xC9,0xD3,0xDD};
int8 n,i,Dir,cont,subcontador,stack=0,s,;
int32 cid=0x320;//cobid base de temperatura

void lectura(int8 Dir);
void escritura();
void canrx0_int();

void main(){
   if(1==1){//CAN ini
   can_init();
   disable_interrupts(GLOBAL);
   can_set_mode(CAN_OP_CONFIG);
   BRGCON1.brp=1;
   BRGCON1.sjw=1;
   BRGCON2.prseg=2;
   BRGCON2.seg1ph=7;
   BRGCON2.sam=FALSE;
   BRGCON2.seg2phts=FALSE; 
   BRGCON3.seg2ph=6;
   BRGCON3.wakfil=FALSE;
   can_set_mode(CAN_OP_NORMAL);
   enable_interrupts(int_canrx0);
   enable_interrupts(int_canrx1);
   enable_interrupts(int_cantx0);
   enable_interrupts(int_cantx1);
   enable_interrupts(int_cantx2);
   enable_interrupts(GLOBAL);}
 
   while(1){
      for (n=0;n<7;n++){
         lectura(adr[n]);}
      if (cont=1){
         cont=0;
         escritura();}
      delay_ms(5);//tiempo por ciclo, optimizar.
   }
}

void lectura(int8 Dir){//I2C Enlace

   i2c_start();
   i2c_write(Dir);// direccion de slave & slave->Master(1)

   for (i=0;i<6;i++){
      temp[n][1][i]=i2c_read(1);
      temp[n][2][i]=i2c_read(1);}
   
   temp[n][1][6]=i2c_read(1);
   temp[n][2][6]=i2c_read(0);
   i2c_stop();
   }
   
void escritura(){//envio de mensajes por CAN
   if (stack==7){
      stack=0;}
   
   [b]can_putd(cid+stack*10,&temp[stack][0][0],8,FALSE,FALSE);
   can_putd(cid+stack*10+1,&temp[stack][1][2],4,FALSE,FALSE);[/b]
   stack++;
   }

#int_canrx0
void canrx0_int ( ) {//Deteccion mensaje sincronismo
   cobid=((unsigned int16)RXB0SIDH << 3)|((RXB0SIDL & 0xE0)>>5);
   
   lengthCAN = (unsigned int8)RXB0DLC & 0xF;
   
   if (cobid == 0x80){
      subcontador++
      if (subcontador==20){
         cont=1;
         subcontador=0;
      }
   }
}


Is the can_putd system correct? The information will be transmitted correctly?
I'm afraid the written code will send the information haphazardly. I want it to send
Thank you very much,

Murguizur.


Last edited by murgui on Wed Jul 06, 2016 8:42 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Wed Jul 06, 2016 8:38 am     Reply with quote

This is actually a C question not CCS!.... Very Happy

C stores things in 'row major' order in memory.

So if you have an array [x][y][z]

Then in memory, you get [X0, Y0, Z0] , [X0, Y0, Z1], ...[X0, Y0, Zmax],
[X0, Y1, Z0] etc.

As you advance through memory, the right hand variable varies first, then the second etc..
murgui



Joined: 23 Dec 2015
Posts: 37

View user's profile Send private message

PostPosted: Wed Jul 06, 2016 8:48 am     Reply with quote

Thank you very much, you were really helpful. I didn't know how to look for this.
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