Re: USART readbyte

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
liak
Registered User
Registered User
Posts: 195
Joined: Fri Oct 05, 2007 12:26 am

Re: USART readbyte

Post by liak » Tue Jul 29, 2008 7:53 am

Dear all,
I am having problem in synchronizing the communication between PIC and my computer via RS232.
I have tried to establish synchronization by sending synch command between the PIC and PC. After receiving a synch command from PC, the PIC will reply and prepare for receiving. But there seems to be a mismatch between the timing of the two despite this.
I need to ask that is there any time out limit for USART.ReadByte? If I code as below, will the PIC wait at this line until the complete commands are given by the PC?

Code: Select all

     Data1 = USART.ReadByte
     Data2 = USART.ReadWord

...
...
I am assuming the PIC will stay in the ReadByte state until a full 3 bytes are read from the PC reply. Am I getting it wrong? I have tried looking into the USART module, it seems that the readbyte command will wait for the PIR status flag. But I am not sure if there is a time out for this.

Please clarify. Thanks.
Regards,
Liak. :oops:

Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

Post by Doj » Tue Jul 29, 2008 2:09 pm

Your assumption is true, it will wait for 3 bytes of data, but the hardware serial buffer is only 2 bytes and if you do not read data out of the buffer to leave at least 1 spare byte the serial port will not store any more data.

if you clear the USART module in the PIC before you start:-

Code: Select all

    RCSTA1.4=0                                      'disable serial port 1 receive
    RCSTA1.4=1                                      'enable serial port 1 receive
You will know the buffer is clear and you can receive 2 bytes maximum.

Also remember that noise will be interpreted as data, to the PIC it is the same.

liak
Registered User
Registered User
Posts: 195
Joined: Fri Oct 05, 2007 12:26 am

Post by liak » Wed Jul 30, 2008 9:36 am

Dear all,
Thank you, Doj, for the clarification.
I tried running your addition, but the compiler does not recognize RCSTA1. I instead switch to what I think is the equivalent of what you taught me:

Code: Select all

     SPEN = 0
     SPEN = 1
Is this right? :?:

The other problem I would like to ask is regarding PIC reset on interruption by external electrical power switching. I suspect the problem is from the MCLR pin. Do you think disabling the fuse:

Code: Select all

 Config MCLRE = off
will be helpful? Or are there anything else you can recommend?

Regards,
Liak

Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

Post by Doj » Wed Jul 30, 2008 12:15 pm

Hello liak,

Yes SPEN is the correct bit to use!

MCLR is safe to switch off if you do not require this in your application, most of my projects do not use MCLR so it is switched off.

This may not help the reset in your project, if the external disturbance is in the power supply to your PIC it will still reset.
You must have a very stable power supply with capacitors as close to the PIC power pins as you can.
Also the layout of the PCB is very important to stop interference getting into the PIC.
It is a complex subject but there are many posts on several PIC forums that have detailed help in their archive.

Post Reply