• AVR Freaks

Helpful ReplyHot!Array Declaration problem

Page: 12 > Showing page 1 of 2
Author
preston51
Starting Member
  • Total Posts : 38
  • Reward points : 0
  • Joined: 2014/12/05 19:46:29
  • Location: 0
  • Status: offline
2020/02/19 07:58:53 (permalink)
0

Array Declaration problem

I am using Mplab X, v5.1, compiler XC32(v1.34)
 
I cannot get my project to work unless I pre-load the array with data. If I do not put data in the array like this, the array does not work:
int CorrectnFactr[9] = {0,1,2,3,4,5,6,0,0};
 
This does not work:
int CorrectnFactr[9] ;
 
What the program does, as seen when debugging and watching the variables, it is as if I had only declared this:
int CorrectnFactr[1] ;
 
As part of my program I also need to declare an array of more than 100 elements. I should not have to pre-load all 100 elements with data. What am I doing wrong? The program was working before without this problem, but something has changed. Any advice?
#1
LdB_ECM
Super Member
  • Total Posts : 297
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Array Declaration problem 2020/02/19 08:21:24 (permalink)
0
Arrays start at ZERO for an array of 9 the last array element is 8
So CorrectnFactr[9]  is illegal and the compiler knows it because it knows the size of the array.
#2
1and0
Access is Denied
  • Total Posts : 10546
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Array Declaration problem 2020/02/19 08:27:31 (permalink)
+2 (2)
preston51
I am using Mplab X, v5.1, compiler XC32(v1.34)
 
This does not work:
int CorrectnFactr[9] ;

How do you know this definition does not work?
 
#3
crosland
Super Member
  • Total Posts : 1865
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Warks, UK
  • Status: offline
Re: Array Declaration problem 2020/02/19 08:30:28 (permalink)
+2 (2)
Create a minimal example that demonstrates the problem and show ALL your code.
#4
pcbbc
Super Member
  • Total Posts : 1631
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: online
Re: Array Declaration problem 2020/02/19 08:54:19 (permalink)
+1 (1)
My guess is the OP is viewing CorrectnFactr in the debugger.
"Doesn't work" == "Debugger is handling array as a pointer, and only showing first element".
 
As already pointed out, accessing the nth element of an n element array is illegal.  The last array element is n-1.
#5
LdB_ECM
Super Member
  • Total Posts : 297
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Array Declaration problem 2020/02/19 09:10:06 (permalink)
0
Yep that is the default behaviour for the watch window for an illegal index. It can't access it or it would throw an access violation on lots of systems so they default it to showing element 0.
#6
preston51
Starting Member
  • Total Posts : 38
  • Reward points : 0
  • Joined: 2014/12/05 19:46:29
  • Location: 0
  • Status: offline
Re: Array Declaration problem 2020/02/19 09:24:57 (permalink)
0
Here is my code. The SerialNumber output is displayed correctly on my LCD. The CorrectnFactr[] shows all zeros in the variable window, or shows the pre-loaded numbers, depending on which line is the comment. The CorrectnFactr[] does not show the numbers that get merged to make the SerialNumber.
 
// Include these libraries:
#include <xc.h>
// *********** Global Variables ***************
unsigned char DataFromSensor;
int SerialNumber;
//int CorrectnFactr[9];
int CorrectnFactr[9] = {0,1,2,3,4,5,6,0,0};
// List Function Declarations
void InitializeSensor();
void GetSensorSerialNumber();
void SendToSensor( int DataToSensor);

void InitializeSensor(void)// Sensor A ports and SPI settings
{
TRISGbits.TRISG6 =0; //SPI SCLK output
TRISGbits.TRISG7 =1; //MISO input
TRISGbits.TRISG8 =0; //MOSI output
TRISCbits.TRISC4 =0; //Sensor ADC SS
TRISCbits.TRISC3 =0; //EEPROM CS output
SPI2BRG=100;// Baud Rate Generator
SPI2CON=0x8120;//SPI ON,8bits,SMP=0,Master mode
LATCbits.LATC3 = 1;// pull EE_CS high

LATCbits.LATC4 = 0;//ADC CS low to send Reset
// Send four bytes of 0xFF to reset the sensor ADC
SendToSensor(0xFF); SendToSensor(0xFF); SendToSensor(0xFF); SendToSensor(0xFF);
LATCbits.LATC4 = 1;//ADC CS high to end Reset
Delayms(10);//delay for > 500us before continuing

GetSensorSerialNumber();
}
void GetSensorSerialNumber(void)
{
unsigned int DataToSensor;
unsigned int DataToProm;
LATCbits.LATC3 = 0;// pull EE_CS low to start the command
DataToProm = 3; // read EEPROM command
SendToSensor(DataToProm); // send Read command to EEPROM

// Get serial number of Sensor from EEPROM bytes 8-11
DataToSensor = 0; // assign MSB of memory address to read
SendToSensor(DataToSensor); // send MSB of memory address to read
DataToSensor = 8; // assign LSB of memory address to read
SendToSensor(DataToSensor); // send LSB of memory address to read
DataToSensor = 0; // assign blank data to clock out address memory
SendToSensor(DataToSensor); // send blank data and clock out address memory
CorrectnFactr[0] = DataFromSensor; // store memory data received
SerialNumber = DataFromSensor; // store memory data received

SendToSensor(DataToSensor); // send blank data and clock out address memory
CorrectnFactr[1] = DataFromSensor; // store memory data received
SerialNumber = SerialNumber + (DataFromSensor<<8); // store memory data received

SendToSensor(DataToSensor); // send blank data and clock out address memory
CorrectnFactr[2] = DataFromSensor; // store memory data received
SerialNumber = SerialNumber + (DataFromSensor<<16); // store memory data received

SendToSensor(DataToSensor); // send blank data and clock out address memory
CorrectnFactr[3] = DataFromSensor; // store memory data received
SerialNumber = SerialNumber + (DataFromSensor<<24); // store memory data received

LATCbits.LATC3 = 1;// pull EE_CS high to stop the command
}
void SendToSensor(int DataToSensor)
{ // Send Data To Sensor A
SPI2BUF = DataToSensor; // put the byte in the SPI hardware buffer and send
while ( !SPI2STATbits.SPIRBF); // wait for transfer complete
DataFromSensor = SPI2BUF; // read data from sensor
}
 
#7
NKurzman
A Guy on the Net
  • Total Posts : 18388
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: Array Declaration problem 2020/02/19 09:31:00 (permalink)
+1 (1)
"does not show the numbers"  how are YOU Looking at the Numbers?  What do you expect? What do you see?
#8
1and0
Access is Denied
  • Total Posts : 10546
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Array Declaration problem 2020/02/19 09:37:19 (permalink)
0
In addition, is CorrectnFactr[] being used anywhere else?
 
#9
preston51
Starting Member
  • Total Posts : 38
  • Reward points : 0
  • Joined: 2014/12/05 19:46:29
  • Location: 0
  • Status: offline
Re: Array Declaration problem 2020/02/19 09:40:59 (permalink)
0
For example,
If I pre-load the values, CorrectnFactr[2] shows 0x00000002.
If I do not pre-load the values, CorrectnFactr[2] shows 0x00000000.
 
It does not show that DataFromSensor value has been copied to CorrectnFactr[2] , but DataFromSensor is being copied to SerialNumber.
 
The same goes for the other array variables - all zeros or only the pre-loaded number. Nothing changes in the running of the program.
 
#10
ibbro
Starting Member
  • Total Posts : 87
  • Reward points : 0
  • Joined: 2015/10/22 07:34:56
  • Location: 0
  • Status: offline
Re: Array Declaration problem 2020/02/19 09:43:03 (permalink)
0
I think I would check the SPISTAT register to see if the TX buffer is empty before sending. The read buffer may already have data in it and pass right through the SPIRBF check allowing another transmit too soon. That of course has nothing to do with the array declaration, but depending on what values you are looking at, and when, it might be involved. Also if you get a receive buffer overflow and don't clear it, the spi peripheral goes and stays wonky.
 
post edited by ibbro - 2020/02/19 09:45:16
#11
preston51
Starting Member
  • Total Posts : 38
  • Reward points : 0
  • Joined: 2014/12/05 19:46:29
  • Location: 0
  • Status: offline
Re: Array Declaration problem 2020/02/19 09:59:08 (permalink) ☄ Helpfulby Jim Nickerson 2020/02/19 10:01:52
+1 (1)
Ok. Something else is going on. The variable SerialNumber shows on my LCD, but gives all zeros in my variable debug window. Maybe that is a clue.
#12
pcbbc
Super Member
  • Total Posts : 1631
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: online
Re: Array Declaration problem 2020/02/19 10:11:21 (permalink)
+3 (3)
preston51Maybe that is a clue.

Yes, never trust the debugger/simulator over real hardware...
 
And please use [​code]//code tags[​/code] when posting.
//code tags

 
Also...
DataToSensor = 0; // assign blank data to clock out address memory
SendToSensor(DataToSensor); // send blank data and clock out address memory

...you realise you can use a litteral in a function call, you do not have to place it in a variable first...
SendToSensor(0); // send blank data and clock out address memory

 
And...
void SendToSensor(int DataToSensor) 
{ // Send Data To Sensor A
   SPI2BUF = DataToSensor; // put the byte in the SPI hardware buffer and send
  while ( !SPI2STATbits.SPIRBF); // wait for transfer complete
  DataFromSensor = SPI2BUF; // read data from sensor
}

...could return the value directly (not place in a global).  Also using an int as a parameter, seeing as it can only send a single byte via SPI, is more than a little wasteful...
#include <stdint.h>
uint8_t SendToSensor(uint8_t DataToSensor)
{ // Send Data To Sensor A
  SPI2BUF = DataToSensor; // put the byte in the SPI hardware buffer and send
  while (!SPI2STATbits.SPIRBF); // wait for transfer complete
  return SPI2BUF; // read data from sensor
}

post edited by pcbbc - 2020/02/19 10:18:46
#13
preston51
Starting Member
  • Total Posts : 38
  • Reward points : 0
  • Joined: 2014/12/05 19:46:29
  • Location: 0
  • Status: offline
Re: Array Declaration problem 2020/02/19 10:18:08 (permalink)
0
Ok. I can see the correct value for CorrectnFactr[x] on my LCD screen, but not in the debugger window. I have no idea why. But at least I know that the hardware is right.
#14
NKurzman
A Guy on the Net
  • Total Posts : 18388
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: Array Declaration problem 2020/02/19 10:19:47 (permalink)
+2 (2)
That is why you were asked repeatedly how you were looking.  MPLabX can "show" debug variables incorrectly.  when it does it will often show 0.
 
Try making the variable Static and volatile.   this may help.  be sure to remove it after you are done debugging, or your code will be bigger and slower.
 
Or Try the latest version of MPLabX
#15
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11673
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Array Declaration problem 2020/02/19 11:25:27 (permalink)
+4 (4)
So CorrectnFactr[9]  is illegal

 
accessing the nth element of an n element array is illegal.

 
But the OP isn't referencing CorrectnFactr[9] anywhere.
#16
GeorgePauley
Moderator
  • Total Posts : 1220
  • Reward points : 0
  • Joined: 2009/12/01 13:59:30
  • Location: Chandler AZ
  • Status: offline
Re: Array Declaration problem 2020/02/19 11:56:08 (permalink)
+3 (3)
pcbbc
Yes, never trust the debugger/simulator over real hardware...

 
Pretty sure OP is not using the simulator.  Why do we always assume it's the simulator's fault?  #sigh
 
#17
NKurzman
A Guy on the Net
  • Total Posts : 18388
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: Array Declaration problem 2020/02/19 12:03:13 (permalink)
+1 (1)
GeorgePauley
pcbbc
Yes, never trust the debugger/simulator over real hardware...

 
Pretty sure OP is not using the simulator.  Why do we always assume it's the simulator's fault?  #sigh
 


The OP Blamed the Compiler.  The Compiler usually gets first blame.
#18
GeorgePauley
Moderator
  • Total Posts : 1220
  • Reward points : 0
  • Joined: 2009/12/01 13:59:30
  • Location: Chandler AZ
  • Status: offline
Re: Array Declaration problem 2020/02/19 12:10:03 (permalink)
0
I just tried this using a PIC32MX795F512L and XC32 2.30.  Worked fine.
 

Attachment(s)

Attachments are not available: Download requirements not met
#19
LdB_ECM
Super Member
  • Total Posts : 297
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Array Declaration problem 2020/02/19 19:59:05 (permalink)
0
The OP is on the debugger and watch window he stated it in the first post, it has nothing to do with simulator. After much confusion because of lack of detail with the OP, the issue is clearly the watch isn't being triggered to update as the actual hardware is showing the change. Initially I just thought he used the wrong index but it appears it isn't updating on change.
 
What he should try is run to debug point delete the watch and put it back again and see if it reflects the new value. That at least would also give a work around if somewhat annoying.
 
Then probably try a re-install of the software in case something has got corrupted or messed up and if still not resolved open a ticket under the title "debugger watch" because that is the problem. A lot of people wasted a lot of time due to the strange way to describe the problem and the issue has nothing to do with compiler or simulator it is the debugger.
post edited by LdB_ECM - 2020/02/19 20:22:00
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5