Jump to content
Sign in to follow this  
Gilles

Access to char array

Recommended Posts

It looks like C2C does sometimes skip some assignments (or have I missed something????). Here is the sample of code where it does not work:

char i,j;
for (i = 0; i < size; i++)
{	
char value = buffer[i];
...
}

In this example, value is initialized with buffer[0] and then never updated.

However, if I do:

char i,j,value;
for (i = 0; i < size; i++)
{	
value = buffer[i];
...
}

then it works perfectly fine!

 

Does anyone have an idea? (Or is it really a compiler bug?).

Note: I am using C2C 4.6.3 (the one where I have to patch C2C.exe and pp.exe). Also I looked at the generated assembly and for me it really looks like the compiler is using value only once...

 

Thanks for your help,

 

Gilles.

 

Here follows the complete code (In case it could be related to something else in my code):

/*
* Module allowing to do radiotransmissions through 
* Manchester encoding.
*/

#include <system.h>

#pragma CLOCK_FREQ 4000000

/* 
* Max size of the buffer 
*/
#define	MAX_PACKET_SIZE  0x0008

/* 
* Size of the message preamble in terms the number of successive 1/0 sequence.
*/
#define PREAMBLE_SIZE  0x000F

/*
* Duration of half a period in microseconds.
*/
#define HALF_PERIOD 	 250

/*
* Defines which line of port B should be used for RF output.
*/
#define LINE_RF_OUTPUT  1
/* 
* Buffer where to receive or send the data from 
*/
char buffer[MAX_PACKET_SIZE];

/*
* Write on the line "LINE_RF_OUTPUT" of port B 
* a preamble followed by a synchronization sequence (11001001)
*/ 
void writePreamble()
{
char i;
// first write the preamble
for (i = 0; i < PREAMBLE_SIZE; i++)
{  
 set_bit(portb, LINE_RF_OUTPUT);
 delay_us(HALF_PERIOD);
 clear_bit(portb, LINE_RF_OUTPUT);
 delay_us(HALF_PERIOD);
}
// then  write the synchronization sequence
// which is 11001001
set_bit(portb, LINE_RF_OUTPUT);
delay_us(HALF_PERIOD);
delay_us(HALF_PERIOD);
clear_bit(portb, LINE_RF_OUTPUT);
delay_us(HALF_PERIOD);
delay_us(HALF_PERIOD);
set_bit(portb, LINE_RF_OUTPUT);
delay_us(HALF_PERIOD);
clear_bit(portb, LINE_RF_OUTPUT);
delay_us(HALF_PERIOD);
delay_us(HALF_PERIOD);
set_bit(portb, LINE_RF_OUTPUT);
delay_us(HALF_PERIOD);
}

/*
*	Method allowing to output the buffer content in manchester encoding
*	(includes preamble, sync and checksum) on the specified line
*	of the port B.
*	@param size The number of characters from the buffer to send
*  (starting from the offset 0).
*/
void writeRF(char size)
{
// first write the preamble and synchronization bits
//writePreamble();
char i,j;
for (i = 0; i < size; i++)
{
 // send the character at the index 'i'
 char value = buffer[i];
 // through sending each of its 8 bits...
 for (j = 0; j < 8; j++)
 {
	 if ((value & 1) == 0)
	 {
   clear_bit(portb , LINE_RF_OUTPUT);
	 }
	 else
	 {
   set_bit(portb , LINE_RF_OUTPUT);
	 }
	 value >>= 1;
	 //delay_ms(HALF_PERIOD);
 }
}
}


main()
{
disable_interrupt( GIE );
   trisb = 0;
   portb = 0;	

buffer[0] = 0xF0;
buffer[1] = 0xAA;
writeRF(2);
   while( 1 )
   {
 portb = 0;
 delay_us( 100 );
 portb = 255;
       delay_us( 100 );
   }
}

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...