|
|
View previous topic :: View next topic |
Author |
Message |
colin
Joined: 08 Sep 2003 Posts: 3
|
NEED HELP with STRUCT!!!!! |
Posted: Mon Sep 08, 2003 1:01 pm |
|
|
Hi all,
If you've experienced creating a struct, pls help and give suggestion!
I created a struct to hold about 30 bytes of data, and declared a pointer pointing to the struct. Pointer would pass around the program thru function to read and write value to the data in the specific object.
My code have a weird problem:
In a while(true) loop, I write and read 30 bytes of data to the object. However, the read, write operation didn't execute and the program counter keep pointing back to the very first function in main() which I have init fuction.
The weird problem is, if I just write says 10 bytes of data to the object, the result was fine(datas were able to print and accurate).
Once I write says 11 bytes of data, the 11 th byte data mess up(11th data( count=0x10, sb->k=count)), as I could see in the register file window, the data was overwritten by some data after the write operation.
If I run the program with write and read 30 bytes of data, the program counter keep pointing back to the very first function in main which is out side the while(true) loop.
If you'v experienced creating a struct and declare a pointer pointing to the object, could you pls provide the code which I could test and see if my code contains problem or the compile contains problem.
You help would be greatly appreciated!!
Pic16f877, mplab 6.3, pcw 3.176, icd2, picdem 2 plus
what i'm tring to do is:
int coutn=0;
while(1)
{
data->a=count;
data->b=count;
.......
printf("%x", data->a);
printf("%x", data->b);
.........
count++;
} |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Mon Sep 08, 2003 1:16 pm |
|
|
Try to do this with arrays. The code created from structures is less predictable for me. Maybe I just don't understand structures well enough.
int8 DataA[30];
int8 DataB[30];
while(1)
{ x++;
DataA[x]=x;
DataB[x]=x-30;
if(x>29) x=0;
printf("%x", DataA[x]);
printf("%x", DataB[x]);
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Sep 08, 2003 1:42 pm |
|
|
The following program works OK. The structure has 14 elements,
and it was compiled with PCM vs. 3.176. It does, however, use
the "." method of accessing structure elements.
#include "c:\Program Files\Picc\Devices\16F877.H"
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 8000000)
#use rs232(baud = 9600, xmit=PIN_C6, rcv = PIN_C7, ERRORS)
struct
{
char a;
char b;
char c;
char d;
char e;
char f;
char g;
char h;
char i;
char j;
char k;
char l;
char m;
char n;
}my_struct;
//=========================
void main()
{
my_struct.a = 'a';
my_struct.b = 'b';
my_struct.c = 'c';
my_struct.d = 'd';
my_struct.e = 'e';
my_struct.f = 'f';
my_struct.g = 'g';
my_struct.h = 'h';
my_struct.i = 'i';
my_struct.j = 'j';
my_struct.k = 'k';
my_struct.l = 'l';
my_struct.m = 'm';
my_struct.n = 'n';
printf("%c\n\r", my_struct.a);
printf("%c\n\r", my_struct.b);
printf("%c\n\r", my_struct.c);
printf("%c\n\r", my_struct.d);
printf("%c\n\r", my_struct.e);
printf("%c\n\r", my_struct.f);
printf("%c\n\r", my_struct.g);
printf("%c\n\r", my_struct.h);
printf("%c\n\r", my_struct.i);
printf("%c\n\r", my_struct.j);
printf("%c\n\r", my_struct.k);
printf("%c\n\r", my_struct.l);
printf("%c\n\r", my_struct.m);
printf("%c\n\r", my_struct.n);
while(1);
} |
|
|
john cutler
Joined: 06 Sep 2003 Posts: 82 Location: Hot Tub, California
|
|
Posted: Mon Sep 08, 2003 2:57 pm |
|
|
This worked for me....
typedef struct {
byte addr_to; //destination address
byte addr_from; //origination address
byte serv_req; //service request flag
byte enc_data[8]; //encoder data array
int check; // crc
} packet, *packetPtr;
packet mypack, *mp;
..........
...........
main () {
mp = &mypack;
mp->addr_to = 0x12;
mp->enc_data{0} = 0x88;
printf( "addr_to = %u\n",mp->addr_to);
printf( "enc_data[0] = %u\n", mp->enc_data[0]);
etc etc |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Sep 08, 2003 4:26 pm |
|
|
I suggest that you post the actual program and the struct. Your example does not contain a data++. Without this, the pointer will never increment. Maybe you have it in your actual code but it is hard to help without a true representation of the code. |
|
|
|
|
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
|