|
|
View previous topic :: View next topic |
Author |
Message |
shson
Joined: 12 Feb 2016 Posts: 11
|
pass-by-pointer not changing values |
Posted: Fri Nov 11, 2016 1:56 am |
|
|
I was writing a code for circular buffer to be used for handling incoming data packets on hardware UART on PIC18F25K80. The actual buffer seems to work perfectly, except buffer overflows randomly. After debugging, I found that the FIFO_Init() function does not initialise the variables to zero, but rather arbitary values like 253 etc.
Edit* I'm using CCS v5.064
Code: |
#define UINT8 unsigned int8
#define FIFO_SIZE 128
typedef struct
{
UINT8 Start;
UINT8 End;
volatile UINT8 NbBytes; /* number of bytes stored in the FIFO, this count is increased by INT_RDA interrupt */
UINT8 Buffer[FIFO_SIZE];
} TFIFO;
TFIFO RxFIFO;
void FIFO_Init(TFIFO *FIFO)
{
FIFO->Start = 0;
FIFO->End = 0;
FIFO->NbBytes = 0;
/* If I printf("%u", FIFO->NbBytes), it outputs '0', which is correct but */
}
void main()
{
FIFO_Init(&RxFIFO);
printf("%u", RxFIFO->NbBytes);
/* if I print here, the value of NbBytes is non-zero value */
}
|
I would've thought passing a pointer to a function using '&' operator and using '*' to update the variable would work. But apparently, the variables aren't getting initialised.
Any ideas?
Thank you
Eric |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Fri Nov 11, 2016 2:10 am |
|
|
In your main, you need to be printing RxFIFO.NbBytes. At this point RxFIFO is not a pointer..... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 11, 2016 2:24 am |
|
|
Here is a test program to explore this.
Code: | #include <18F25K80.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
#define UINT8 unsigned int8
#define FIFO_SIZE 128
typedef struct
{
UINT8 Start;
UINT8 End;
volatile UINT8 NbBytes;
UINT8 Buffer[FIFO_SIZE];
} TFIFO;
TFIFO RxFIFO;
void FIFO_Init(TFIFO *FIFO)
{
FIFO->Start = 0x01;
FIFO->End = 0x02;
FIFO->NbBytes = 0x03;
}
//======================================
void main()
{
int8 temp;
FIFO_Init(&RxFIFO);
temp = RxFIFO->NbBytes; // Your method
printf("RxFIFO->NbBytes = %x \r", temp);
// Two better methods:
temp = &RxFIFO->NbBytes;
printf("&RxFIFO->NbBytes = %x \r", temp);
temp = RxFIFO.NbBytes;
printf("RxFIFO.NbBytes = %x \r", temp);
while(TRUE);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Fri Nov 11, 2016 3:48 am |
|
|
and (of course) going on from here, the actual problem in your original code is almost certainly that at some point you are getting this selection wrong, so something then talks to the wrong variable.... |
|
|
|
|
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
|