Jump to content

Boostc Pic18 String Array Issues


Recommended Posts

I am writing the program in BoostC for PIC18F24K22. It seems like the pointer is not working well.

 

My UART_putStr is as below:

void putStr (char *str) {

while(*str)
putCh(*str++);
}

putCh() is working pretty fine.

 

 

But this program:

UART_putStr("Hello");

is not working.

 

This way is also not working.

 

char str[5];

str[0]='H';

str[1]='e';

str[2]='l';

str[3]='l';

str[4]='o';

UART_putStr(str);

 

I must access my string array in this way:

 

char str[5];

char i=0;

str[i+0]='H';

str[i+1]='e';

str[i+2]='l';

str[i+3]='l';

str[i+4]='o';

UART_putStr(str);

 

 

 

I cannot quite understand the behavior of this program. Any help me to point out what is the issue behind?

 

Thank you very much.

 

 

Link to post
Share on other sites

Hi acroty,

 

Remember strings require a terminating null at the end so make room for it.

char str[6];
str[0]='H';
str[1]='e';
str[2]='l';
str[3]='l';
str[4]='o';
str[5] = '\0';
UART_putStr(str); 

Enclose while loop statements inside curly brackets even though you only have a single statement.

It looks better and is good practice. You may want to add more to your while loop later.

 

Cheers

 

Reynard

Link to post
Share on other sites

Dear Reynard:

 

Thank you very much for pointing out the string ending point.

 

But my problem is even though the starting bytes, like "hello" cannot be displayed properly.

char str[5];

char i=0;

str[i+0]='H';

str[i+1]='e';

str[i+2]='l';

str[i+3]='l';

str[i+4]='o';

UART_putStr(str);

 

String index seems like abnormal. I port over the program on PIC16F88, PIC12F1822, PIC16F1823 and all the string display is working quite well.

 

This situation only happened to PIC18. Is there any further setting that I need to do?

 

Thank you very much.

Link to post
Share on other sites

Hi acroty,

 

I am using the PIC18F26K22 with a GSM modem to send and recieve SMS messages.

I am manipulating text strings big time without any problems.

 

Your text array is still not NULL terminated as shown in my previous reply.

 

Cheers

 

Reynard

Link to post
Share on other sites

Hi

 

Without the string terminator, the putStr() function will dump all bytes in memory until it finds a nul ( 0 ).

Are you sure that your initial "Hello" is not being overriden by the this memory dump efect.

 

 

Just my 2 cents....

 

 

Best regards

Jorge

Link to post
Share on other sites

Hello, Reynard, Jorge:

 

Thank you very much for your kind reply. I tried the termination but it behaved as the same.

 

I found my string pointer is quite strange. If I want to output a string like putStr("Good\0"), I need to shift my pointer by 175 (0xAF). that means my putStr() will be like this:

void putStr1 (unsigned char *str)

{
str=str+195;
while(*str)
putCh1(*str++);
}

 

My Init and settings are:

#pragma DATA _CONFIG1H, _FOSC_INTIO67_1H & _PLLCFG_OFF_1H & _FCMEN_OFF_1H
#pragma DATA _CONFIG2L, _PWRTEN_OFF_2L &_BOREN_OFF_2L
#pragma DATA _CONFIG2H, _WDTEN_OFF_2H
#pragma DATA _CONFIG3H, _MCLRE_INTMCLR_3H &_PBADEN_OFF_3H
#pragma DATA _CONFIG4L, _LVP_OFF_4L & _DEBUG_OFF_4L
#pragma DATA _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
#pragma DATA _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H

 

 

void uart1Init(void)

 

{
txsta1.BRGH=1; // High Speed mode
baudcon1.BRG16=0; //8 bit baud rate generator
spbrg1=51; //Set baud rate as 9600bps
spbrgh1=0;
// For Tx
txsta1.TXEN=1; // Enable Transmitter
// For Rx
rcsta1.CREN=1; // Enable receiver
rcsta1.RX9=0; // 8 bit reception
// General Setting
txsta1.SYNC=0; // Asynchronous mode
rcsta1.SPEN=1; // Will auto configure TX as output
trisc.6 = 0; // TX as output
trisc.7 = 1; // RX as input
anselc.6 = 0;
anselc.7 = 0;
pie1.RC1IE=1;
intcon.GIE=1;
intcon.PEIE=1;
}

 

 

I am wondering if some of my setting is incorrect...

 

Thanks.

 

Acroty

Link to post
Share on other sites

Hi acroty ..

 

I'd expect that your UART settings are OK if your single-character Tx works OK.

While it's true that a string should be null terminated, try this anyway.... it works for me on the 18F24K22

 

(ps: txreg and txsta are both duplicate-mapped to the txreg1 and txsta1 addresses)

 

 

#include <system.h>

#include <stdio.h> //printf includes here

 

void SendChar(char s)

{

txreg = s; //Load char into UART Tx register

while(txsta.TRMT == 0); //Loop til TRMT==1 (Tx complete)

}

 

void SendStr(const char *outmsg)

{

while(*outmsg)

SendChar(*outmsg++);

}

 

void SendInt(unsigned int k)

{

char pbuff[6]; //Assign buffer length

sprintf(pbuff, "%u", k); //Format digits to buffer

SendStr(pbuff); //Send as string

}

 

void SendEOL(void) //End of Line

{

SendChar(0x0D);

SendChar(0x0A);

}

 

void main(void)

{

while(1) {

Sendstr("Hello");

SendEOL();

SendInt(99);

SendEOL();

delay_s(1);

}

 

}

 

 

 

Best of luck

Peter L

Link to post
Share on other sites

Hi acroty,

 

Your config bits need a little bit of work done to them.

 

SourceBoost doesn't respect the erased default values of the config words.

You MUST define all the bits whether they are of interest to you or not.

You have the extended instruction bit set even though you did not enable it.

SourceBoost enabled it for you even though it is not supported because you didn't explictly so not to (_XINST_OFF_4L

)

 

For PIC18, use the type 2 config method it is much clearer to what is going on.

Just cut and paste from the .h file into your own config.h file.

 

Here is a little taste of one of mine:

#pragma config FOSC = HSMP // HS oscillator (medium power 4-16 MHz)
#pragma config PLLCFG = ON // Oscillator multiplied by 4
#pragma config PRICLKEN = ON // Primary clock is always enabled
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor disabled
#pragma config IESO = OFF // Oscillator Switchover mode disabled
#pragma config PWRTEN = ON // Power up timer enabled
#pragma config BOREN = OFF // Brown-out Reset disabled in hardware and software
#pragma config BORV = 285 // VBOR set to 2.85 V nominal

Using the internal oscillator is not always a good idea for serial comms due to its accuracy and drift even if you do calibrate it.

Use a crystal such as 11.0592MHz and use x4 PLL to get the most out of your chip.

 

Cheers

 

Reynard

Link to post
Share on other sites

Hello, Peter:

 

Thank you for your reply.

 

Hello, Reynard:

 

Thank you very much for your point. I have solved the problem by disable the indexed addressing mode. I am quite new to PIC18. Thank you very much for all your help. But just to clarify one point. The '\0' is not necessary to be in my program. I think the sourceboost is taking care of it.

 

Thanks all~.

 

Regards,

Acroty

Link to post
Share on other sites

Hi acroty,

 

If the text string is between double quotes then the compiler will insert the terminating null for you.

 

If you are building a text string into an array character by character, it is you that has to insert the null at the end.

You character array must be one bigger than your largest text string.

 

Good you are making some progress.

 

Cheers

 

Reynard

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...
×
×
  • Create New...