CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problem with char table defined in header file

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jyuser



Joined: 19 Apr 2012
Posts: 2

View user's profile Send private message

Problem with char table defined in header file
PostPosted: Thu Apr 19, 2012 2:02 am     Reply with quote

Wondering if someone could help me with this one.

I'm using PCWH compiler version 3.224 and programming at the moment PIC18F4680. I'm facing annoying problems when I define a couple of char tables in header file which I use in c file RDA interrupt function. Defining tables in header they end up blank or other way corrupted but defining them in c file just before the interrupt function they are ok.

Here is some c file code (pay attention to char tables and their test printing):

char set_voltage_command[11] = {'s', 'e', 't', ' ', 'v', 'o', 'l', ' ', 'c', 'h'};
char set_polarity_command[11] = {'s', 'e', 't', ' ', 'p', 'o', 'l', ' ', 'c', 'h'};


#int_RDA
RDA_isr()
{
int8 command_string_length = 0;
int8 new_polarity = 0;
char channel_char = '0';
int8 channel_numb = 0;
int8 go = 1;
int8 i = 0, j = 0;
int8 new_voltage = 0;
int8 k = 0; // Needed for testing at the end

command_string_length = my_get_string(rs232_input_buffer, BUFFER_SIZE);
//printf("Command string length: %u\n\n", command_string_length); // For testing
printf("\nHelp: %s\n", help_command); // For testing
for(k = 0; k < 4; k++) {
printf("%c", help_command[k]);
}
printf("\nGet all: %s\n", get_command); // For testing
for(k = 0; k < 10; k++) {
printf("%c", get_command[k]);
}
printf("\nSet pol: %s\n", set_polarity_command); // For testing
for(k = 0; k < 10; k++) {
printf("%c", set_polarity_command[k]);
}
printf("\nSet vol: %s\n", set_voltage_command); // For testing
for(k = 0; k < 10; k++) {
printf("%c", set_voltage_command[k]);
}

And some h file code:

char help_command[] = {'h', 'e', 'l', 'p', '0'};
char get_command[] = {'g', 'e', 't', ' ', 'a', 'l', 'l', ' ', 'c', 'h', '0'};

//char help_command[] = {'h', 'e', 'l', 'p'};
//char get_command[] = {'g', 'e', 't', ' ', 'a', 'l', 'l', ' ', 'c', 'h'};

//char set_voltage_command[] = {'s', 'e', 't', ' ', 'v', 'o', 'l', ' ', 'c', 'h'};
//char set_polarity_command[] = {'s', 'e', 't', ' ', 'p', 'o', 'l', ' ', 'c', 'h'};


So in one line: c file definitions work, h file definitions don't.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Apr 19, 2012 6:21 am     Reply with quote

I think I know what you are telling us.

To avoid possible crossed lines, it would be easier if you post complete code which is stripped to the MINIMUM, so we can compile as is.

Mike
andyfraser



Joined: 04 May 2004
Posts: 47
Location: UK

View user's profile Send private message

Problem with char table defined in header file
PostPosted: Thu Apr 19, 2012 6:30 am     Reply with quote

This is also a shocking example of how to use the RDA interrupt.
It needs to be short and quick you should not be getting a complete string nor should you be calling printf from within this interrupt.
Look at the examples provided by CCS on receiving strings using a circular buffer under interrupt.

Andy
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Apr 19, 2012 6:42 am     Reply with quote

Several comments:
- You didn't post the test print results.
- When posting code please use the 'code' tags to preserve the formatting for easier reading.

Quote:
Defining tables in header they end up blank or other way corrupted but defining them in c file just before the interrupt function they are ok.
My Coding Practice Rules say you shouldn't define variables in header files. Perhaps you ran into this error because it is a less tested code method.

Quote:
'm using PCWH compiler version 3.224
This is one of the earlier v3.2xx compilers. We also had some strange memory related problems in a PIC18F8722 with v3.226 that were solved when upgrading to v3.249.

Printf inside an RDA-isr is very tricky. You are sending more data than the single receive character you are receiving. This will likely result into UART input buffer overflow (2 - 3 characters) and means loss of data.

Code:
char help_command[] = {'h', 'e', 'l', 'p', '0'};
char get_command[] = {'g', 'e', 't', ' ', 'a', 'l', 'l', ' ', 'c', 'h', '0'};
equals:
Code:
char help_command[] = {"help"};
char get_command[] = {"get all ch"};
The latter is easier to read.
Note that this always results in a zero terminated string, for your other lines where this is not needed you can stick to the existing notation and save 1 byte per line.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Apr 19, 2012 8:40 am     Reply with quote

...And '0' is the character zero, not NULL or the "zero terminator", which is '\0'.

RF Developer.
jyuser



Joined: 19 Apr 2012
Posts: 2

View user's profile Send private message

PostPosted: Sun Apr 22, 2012 11:51 pm     Reply with quote

Thanks for all the helpful comments! I really could correct some of my bad programming habits. Circular buffer seems to be the way to do it...

One more questions came up: when I handle received string by polling in main method, it appears that there is some delay between the first sent char and the rest of the string in terms of arrival (to the buffer).

Here is a simple example:
sending from PC via RS-232 a command 'get voltage' will appear in buffer first only as 'g' if constantly polled in program. Second time checking the buffer there will be the rest of the command 'et voltage'. Solution is adding some delay to polling, but do you know is there really some HW delay after first handled char?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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