View previous topic :: View next topic |
Author |
Message |
ljmnunes
Joined: 02 Sep 2004 Posts: 13 Location: Brazil
|
Struct or array problem? |
Posted: Thu Jan 15, 2015 8:01 am |
|
|
Hi!
I have a problem to transfer one value from an array of bits to a member of an array of struct. I don't understand why the compiler don't transfer the value from the matrix to the struct directly.
The compiler is Version 5.036. Any ideas?
Code: |
#include <18F46K22.h>
#include globals.c
#include tscc.c
main()
{
...
while(1){
if(x==2 || x== 4)
TransferToTable();
...
}
}
|
inside globals.c:
Code: |
struct VirtualOut{
unsigned int8 Fcod;
unsigned int16 VAL;
}VirtualOut[NUM_VOUT];
int1 VarDig[512];
|
inside tscc.c:
Code: |
void TransferToTable(void)
{
unsigned INT16 Add;
unsigned INT8 Ind;
Add = i2c_readword();
Ind = i2c_readbyte();
VirtualOut[Ind].Val = Vardig[Ind]; // This line don't work!
}
|
After the changes below VirtualOut[ Ind].Val has the expected values​:
Code: |
int1 x = Vardig[Add];
VirtualOut[Ind].Val = x;
|
Where is my fail?
Thanks a lot! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Thu Jan 15, 2015 8:51 am |
|
|
You NEED to post a complete,small program NOT 'bits and pieces' It's too hard to follow.
I'd get rid of the includes....
Also you have an I2C...... lines implying you're reading from an I2C device ? We don't know if your drivers are correct, hardware works properly, or what you expect to happen...
More details are needed especially a small,complete 'copy/paste' program so others can try it.
hth
jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Thu Jan 15, 2015 9:23 am |
|
|
However as a comment, try one thing. Cast the int1, to an int1, when you read it.
I remember there being problems in the past, with int1's. The default type for all operations is int8. However without a maths operation involved, the cast doesn't take place, giving the wrong result. So try:
Code: |
VirtualOut[Ind].Val = (int1)Vardig[Ind];
|
I've said before that anything that involves 'using' an int1, in an unexpected place, should explicitly cast it before use!. |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Thu Jan 15, 2015 10:01 am |
|
|
CCS C does not support arrays of bits. This is from the manual:
Quote: | Arrays of bits are not permitted
Arrays may not be of SHORT INT. Arrays of Records are permitted but the record size is always rounded up to the next byte boundary.
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Thu Jan 15, 2015 2:47 pm |
|
|
Sorry RF_Developer, you are out of date.
From the current manual:
"Arrays of bits (INT1 or SHORT ) in RAM are now supported."
However they do have some oddities. Hence my suggestion about the cast. Problem is that they don't automatically convert when passed to other types. |
|
|
ljmnunes
Joined: 02 Sep 2004 Posts: 13 Location: Brazil
|
|
Posted: Fri Jan 16, 2015 7:24 am |
|
|
Ttelmah wrote: | However as a comment, try one thing. Cast the int1, to an int1, when you read it.
I remember there being problems in the past, with int1's. The default type for all operations is int8. However without a maths operation involved, the cast doesn't take place, giving the wrong result. So try:
Code: |
VirtualOut[Ind].Val = (int1)Vardig[Ind];
|
|
I tryed do casting with int1 and it don't work.
temtronic wrote: | I'd get rid of the includes.... |
I can't do that. Here is my main #include list:
Code: | #include "vars/globals.c"
#include "func/prototypes.c"
#include "drivers/DS1307.C"
#include "drivers/AT24C512.C"
#include "memory/mem.c"
#include "serial/serial.c"
#include "rtc/agenda.c"
#include "esidio/common.c"
#include "crc/crc.c"
#include "modbus/ModbusSlave.c"
#include "modbus/Tscc.c"
#include "codi/codi.c"
#include "io/perifericos.c"
#include "io/io.c"
#include "memory/tabelas.c"
#include "io/interrupts.c" |
There are tens of thousands of code lines to manage. As you can see, my project is too big to put the code here. I think that the snippets that I posted are sufficient and has all information about the problem. I have many others functions getting data of i2c line and are working fine. Sorry by wasting your time. I'll take care next time.
I also posted the solution that I have found. This shows that rest of the program is working. Whenever I post doubts I take care to eliminate all factor that don't care about the problem.
In this case the problem is only about transfer value form an array of bits and a struct. My solution is not elegant, I know. This is because I posted my doubt.
Any ideas will be welcome.
Anyway thanks Everyone for your help. We learn a lot here!
If I found other solution I'll update the post. |
|
|
|