[Beginer] usart question

Re: [Beginer] usart question

Postby ric » Thu Oct 01, 2015 11:15 pm

You seem to have forgotten/ignored what I said a month ago.
ric wrote:Replace all these:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <p18f2550.h>
#include <xc.h>
#include <usart.h>

with just:
Code: Select all
#include <xc.h>

That automatically brings in the correct processor file, and any required plib includes.
You rarely require stdio.h, or stdlib.h in an embedded project.



Entropy wrote:example getsUSART(message, 10); what is the procedure when the length of the string is not known? In this example it waits until I send him 10 characters/numbers.

Don't use getsUSART if you don't know the length.
You have a few choices. You could define some termination condition, e.g. a special character, or time for which no characters are received, and implement that yourself as you receive the characters one by one.
Or, you could use interrupts to receive each character, and place them into a circular FIFO buffer. Your non-interrupt code could send each character as soon as it is received.

Also note, this:
char message[] = "";

in combination with this
getsUSART(message, 10);

is a bug, as you have not allocated any space for the buffer. The declaration should have been
char message[10] = "";

and then later, ytou cannot do this
if (message!="") {

you can only directly compare single characters with an if(), you can't do a whole string. You have to use a function like strcmp(), and test the result of that.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: [Beginer] usart question

Postby Entropy » Fri Oct 02, 2015 5:34 am

ric wrote:You seem to have forgotten/ignored what I said a month ago.
Replace all these:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <p18f2550.h>
#include <xc.h>
#include <usart.h>

with just:
Code: Select all
#include <xc.h>

That automatically brings in the correct processor file, and any required plib includes.
You rarely require stdio.h, or stdlib.h in an embedded project.


I did not forgot, but sometimes when I write a specific register or command my compiler wont understand or recognise that. Because I'm still a beginner allways have a problem knowing if I write it wrong / if is the compiler fault / something else... and I try to reduce the possibilites as much as possibile. Mabye I forge this compiler too much not knowing what I'm doing and now is failing. For example just test it, if I remove #include <usart.h> everything about usart become error
is a bug, as you have not allocated any space for the buffer. The declaration should have been
char message[10] = "";

this is because I read a tutorial in some site that said, if I write char message[10], the compiler alocate memory space for 10 chars no matter if I use it or not, but if I leave it with just [] memory will be fill as need it (or at least I understand it like this). Atm I cant find the link.

and then later, ytou cannot do this
if (message!="")
you can only directly compare single characters with an if(), you can't do a whole string. You have to use a function like strcmp(), and test the result of that.

I will try this, if not... I will change if (stringLength>0){..} or something simple like that
Entropy
 
Posts: 24
Joined: Fri Sep 04, 2015 7:04 am
Location: Timisoara, Romania

Re: [Beginer] usart question

Postby ric » Fri Oct 02, 2015 5:43 am

Entropy wrote:...
For example just test it, if I remove #include <usart.h> everything about usart become error

Do you mean you get a compile error, or just that MPLAX starts putting a red wiggly line under some words?


this is because I read a tutorial in some site that said, if I write char message[10], the compiler alocate memory space for 10 chars no matter if I use it or not, but if I leave it with just [] memory will be fill as need it (or at least I understand it like this). Atm I cant find the link.

If you don't put a number between the braces, it is made big enough to just fit the string that you initialise it with. You initialised it with "", which is an empty string, so it will only be made just big enough to hold an empty string.
Once the array is created, it will NOT get bigger if yo utry to put a bigger string in there, it will just trash whatever follows it in memory.
You MUST create it big enough to hold the biggest string you might put into it.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: [Beginer] usart question

Postby Entropy » Fri Oct 02, 2015 6:11 am

ric wrote:or just that MPLAX starts putting a red wiggly line under some words?

this.
If you don't put a number between the braces, it is made big enough to just fit the string that you initialise it with. You initialised it with "", which is an empty string, so it will only be made just big enough to hold an empty string.
Once the array is created, it will NOT get bigger if yo utry to put a bigger string in there, it will just trash whatever follows it in memory.
You MUST create it big enough to hold the biggest string you might put into it.

I see...
Entropy
 
Posts: 24
Joined: Fri Sep 04, 2015 7:04 am
Location: Timisoara, Romania

Re: [Beginer] usart question

Postby ric » Fri Oct 02, 2015 6:55 am

Entropy wrote:
ric wrote:or just that MPLAX starts putting a red wiggly line under some words?

this.

That's just MPLABX playing up.
It will often go away of you shut it right down and start it up again.
The only errors that really matter are those produced when you compile your code.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Previous

Return to SCI/USART/EUSART

Who is online

Users browsing this forum: No registered users and 6 guests

cron