USART Question

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Mon Aug 10, 2009 10:07 am

Ooops, duplicate post

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Mon Aug 10, 2009 10:16 am

Francis wrote:Eh?
Methinks you should have a read of the USART routne and a PIC Data Sheet.

Work on a general routine like this.

Create a byte array and then something along these lines:

Code: Select all

    Dim ByteNo As Byte
    ByteNo=0
    Repeat
        MyArray(ByteNo) = USART.ReadByte()
        Inc(ByteNo)
    Until Not USART.DataAvailableTimeout(10)
If you need to Clear then clear after the above.

If you use the timeout then yu can send variable length strings. If you wish just 3 chars then modifiy the above and count the bytes.

You will need True for PICAXE serial out.
Note that PICAXE does NOT set the pin high before the first srial out, so you will have to do that or potentially lose the first byte.

PS. when I tried a similar thingin proton years ago (i.e. PICAXE to PIC USART) I occasionally had issues with inaccuracies in baud. You may have to twiddle BRG. I only mention this in the unlikely event that you have some funnies happening.
Would a "while" not be better in this case?
I don't need it to keep looking for bytes when none are coming.

Thankyou for the help.
I will try it out tommorow.

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Mon Aug 10, 2009 10:24 am

Whichever you prefer.
Have a play. I'm sure you can change it to suit your application, depending whether While/Wend is a better structure.

There are usually easier/better ways to do things; this was merely to get you started.

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Tue Aug 11, 2009 8:37 pm

Francis wrote:Eh?
Methinks you should have a read of the USART routne and a PIC Data Sheet.

Work on a general routine like this.

Create a byte array and then something along these lines:

Code: Select all

    Dim ByteNo As Byte
    ByteNo=0
    Repeat
        MyArray(ByteNo) = USART.ReadByte()
        Inc(ByteNo)
    Until Not USART.DataAvailableTimeout(10)
If you need to Clear then clear after the above.

If you use the timeout then yu can send variable length strings. If you wish just 3 chars then modifiy the above and count the bytes.

You will need True for PICAXE serial out.
Note that PICAXE does NOT set the pin high before the first srial out, so you will have to do that or potentially lose the first byte.

PS. when I tried a similar thingin proton years ago (i.e. PICAXE to PIC USART) I occasionally had issues with inaccuracies in baud. You may have to twiddle BRG. I only mention this in the unlikely event that you have some funnies happening.
The array has to be 256 bytes long? Or do I just put a check to make sure it doesn't get over a specific value (e.g. 64) (which is beyond the size of the array)

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Tue Aug 11, 2009 9:26 pm

Mind This post... Skip to the bottom post. The code here was useless.
Last edited by xnederlandx on Wed Aug 12, 2009 12:28 am, edited 1 time in total.

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Tue Aug 11, 2009 11:21 pm

Again....

Mind This post... Skip to the bottom post. The code here was useless.
Last edited by xnederlandx on Wed Aug 12, 2009 12:28 am, edited 1 time in total.

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Wed Aug 12, 2009 12:18 am

I loaded the echo sample program - It works perfectly. :?

Edit:

Code: Select all

Device = 18F2525                            '
Clock = 32                                  ' PIC 18F4431 Config with external 8 Mhz Resonator
Config OSC = HSPLL                          '

// import usart module...
Include "usart.bas"

SetBaudrate(br9600)
// read in characters and echo to screen...
Repeat
While Usart.Dataavailable = True
    WriteByte(ReadByte)
Wend
DelayMS(500)  'Simulate Program...
Until 1=0
Some adjustments made to the echo program. Does the internal USART Hardware only have a two byte buffer? When I send Data via my terminal program, I can only send a maximum of 2 bytes each time before it jams up.

Does that mean that when the data is sent, I need to send 1 byte, which when read makes it go into a continuous reading loop untill all data is transferred?

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Wed Aug 12, 2009 12:51 am

I've read through the ISRRX library. Looks like it has an inbuilt buffer... I might try and use that instead.

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Wed Aug 12, 2009 3:27 am

Success :D!

Using the ISRRX library (with the handy buffer) It works flawlessly. I just got my original code, changed a few things... and whala! It works.

Thankyou Francis, for all your help!

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Wed Aug 12, 2009 4:47 am

Success is short lived.

I had it working on the EasyPic Development Board, communicating with the serial program on my PC, but when I moved it onto the PCB I made to communicate with the picaxe.... Data = 0.

I'm not using a MAX232 between the PIC18F2525 and Picaxe 18X - just a 100 ohm resistor. Are you sure I need to send it True?
I've set the pin high at the beggining of the program (on the picaxe side), but when I make a debugging program, reading out the data, it always seems that it is 0.

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Wed Aug 12, 2009 8:36 am

The example I gave reads bytes into a buffer array.

Your code reads and then writes, so any more than the PIC buffer can hold will be lost.
Becuase of the time involved writing you are probabaly losing your data coming in.
That maybe explains your 2 byte limit you found.

There are no magic buffers. You can either read manually or by interrupt - whichever floats your boat and isappropriate for your app.

You shouldn't use a MAX232 between PICAXE and PIC. If both running @5V then you are simply using 5V logic levels.

Yes, polarity is true Serout <pin>, Txxxx from PICAXE.

Some time ago I used almost exactly my code example to send data from PICAXE to PIC. No problems whatsoever.
Thousands of writes without error or dropped data.

And right now I am playing with sending soft serial to USART PIC-PIC - again no problems.

Check your hardware connections, check you are using correct pin, check your baudrate.

Coinicidentally, I did initially have one or two funnies from a PICAXE 18X to 18F2520.
After a lot of messing about I found it was a baud rate issue.
The cure was either, to POKE PICAXE OSCTUNE or adjust PIC BRG.

As I can't see your hardware setup I can't suggest much more other than having a good play around.
Needless to say, you will have thoroughly checked you PCB?
Time for the multimeter, 'scope and magnifying glass.

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Wed Aug 12, 2009 10:25 am

Doing some measurements with a counter, the 10Mhz resonator (PCB) is doing about 9.96 Mhz ~.

However, the Easypic 5 development board crystal (8 Mhz) is spot on.

I think the speed difference is causing problems. When I change clock = 40 to = 39, no more problems seem to occur. (Note x4 HSPLL)

I will try getting a crystal for the PCB.

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Wed Aug 12, 2009 12:21 pm

I've had occasional problems when using PLL for comms previously.

Get a few crystals while you're at it.
Anyway, sounds like you're tracking down the problem.

Keep experimenting and trying different things - it's usually quicker than waiting for a reply. Search for issues relating to PLL.

Post Reply