Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Pointer not carrying the value

Status
Not open for further replies.

FrustratedEngineer

Newbie level 6
Joined
Nov 30, 2015
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
122
Guys,

I have developed an application for PIC18F45K40.

The char array is passed to the function as Pointer. When I see in the definition of the function, the pointer doesn't carry the values of the Array element. I have tried passing directly string as an argument but even that doesn't work. My first guess is that it is Near/Far pointer problem. I tried using that but code never compiled. Please let me know how to resolve this problem.

Code:
CopyBuf(Pu8Data, &gstUart.stTx.u8Buf[gstUart.stTx.u8BufIndex], u8Len);
CopyBuf((U8*)"AT+IPR=9600\r\n", &gstUart.stTx.u8Buf[gstUart.stTx.u8BufIndex], 13);

void CopyBuf(const U8* Pu8Src, U8* Pu8Dest, U16 u16Len)
{
	if((NULL != Pu8Src)&&(NULL != Pu8Dest)&&(u16Len > NULL))
	{
		while(u16Len)
		{
			*Pu8Dest = *Pu8Src;
			Pu8Dest++;
			Pu8Src++;
			u16Len--;
		}
	}
}
 

Code will work.
Code:
void CopyBuf(const U8* Pu8Src, U8* Pu8Dest, U16 u16Len)
{
	if((NULL != Pu8Src)&&(NULL != Pu8Dest)&&(u16Len > NULL))
	{
		while(u16Len)
		{
			*Pu8Dest = *Pu8Src;
			Pu8Dest++;
			Pu8Src++;
			u16Len--;
		}
	}
}
Can be written in shorter way:
Code:
void CopyBuf(const U8* Pu8Src, U8* Pu8Dest, U16 u16Len)
{	
		while(u16Len--)		
			*Pu8Dest++ = *Pu8Src++;		
}
Or simpy use memcpy or strcpy function of stdio.h library.
But this have to be written in different way:
Code:
CopyBuf(Pu8Data, gstUart.stTx.u8Buf + gstUart.stTx.u8BufIndex, u8Len);
Code:
#include <stdio.h>
#include <string.h>
strcpy((char*)gstUart.stTx.u8Buf + gstUart.stTx.u8BufIndex, (char*)Pu8Data);

- - - Updated - - -

Sorry, I forgot one stupid PIC limitation. It has 8 bit address data bus wide. So, any pointer operations are limited to 256 lenght. You can't keep in memory array longer than 256 bytes. So, it is only your choice to suffer with this :)
 

Sorry, I forgot one stupid PIC limitation. It has 8 bit address data bus wide. So, any pointer operations are limited to 256 lenght. You can't keep in memory array longer than 256 bytes. So, it is only your choice to suffer with this

Standard PIC18 supports up to 4kB data memory and respectively 12 address bits. Access is implemented via FSR register indirect addressing. PIC compilers like XC8 support 8 and 16 bit data pointer type, the latter can directly access the available data memory range.
 

Code will work.
Code:
void CopyBuf(const U8* Pu8Src, U8* Pu8Dest, U16 u16Len)
{
	if((NULL != Pu8Src)&&(NULL != Pu8Dest)&&(u16Len > NULL))
	{
		while(u16Len)
		{
			*Pu8Dest = *Pu8Src;
			Pu8Dest++;
			Pu8Src++;
			u16Len--;
		}
	}
}
Can be written in shorter way:
Code:
void CopyBuf(const U8* Pu8Src, U8* Pu8Dest, U16 u16Len)
{	
		while(u16Len--)		
			*Pu8Dest++ = *Pu8Src++;		
}
Or simpy use memcpy or strcpy function of stdio.h library.
But this have to be written in different way:
Code:
CopyBuf(Pu8Data, gstUart.stTx.u8Buf + gstUart.stTx.u8BufIndex, u8Len);
Code:
#include <stdio.h>
#include <string.h>
strcpy((char*)gstUart.stTx.u8Buf + gstUart.stTx.u8BufIndex, (char*)Pu8Data);

- - - Updated - - -

Sorry, I forgot one stupid PIC limitation. It has 8 bit address data bus wide. So, any pointer operations are limited to 256 lenght. You can't keep in memory array longer than 256 bytes. So, it is only your choice to suffer with this :)


Sir, I have tried many ways to make it work but above code is not working.
I have tried using memcpy function as well but even that didn't work.

Surprisingly, in debug mode the *Pu8Src points to the data but when
Code:
*Pu8Dest = *Pu8Src;

is executed in stepwise execution mode, the *Pu8Dest doesn't receive the data. This is ridiculous.
 

If you are able to step the code in debugger, you can see where exactly the function fails.

To perform a complete analysis myself, I would need the full code including all type and variable definitions, also need to know which compiler is used.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top