Help with USART

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
ameel
Posts: 3
Joined: Tue Nov 15, 2011 10:51 pm
Location: Australia

Help with USART

Post by ameel » Wed Nov 16, 2011 1:38 am

Relevant extract:

Code: Select all


Dim c as Word

{
...skipping part of the code...
}

Initialize
    While 1=1
        While DataAvailableTimeout(10)
		    c = USART.ReadWord
			USART.write("Received: ")
			USART.Write(c)
			USART.write(".   ", 13,10)

{ ...commented this out for now
            If c = "Off" Then
                mStatusLED_Off()
                USART.Write("LED turned off")
            ElseIf c = "On" Then
                mStatusLED_On()
                USART.Write("LED turned on")
            EndIf
}

        Wend
    Wend

So, I'm using mplab ide so i can test using simulator. my simulator sends hex message "74 65 73 74 69 6e 67" (testing) through USART. The application through "c = USART.ReadWord" only actually stores "te". When i opened the usart.bas and looked up USART.ReadWord, i see that it only waits for and readbyte 2 bytes.

Im a bit confused about certain things:
1) why does my message through simulator has to be in hex?
2) given above, why can the message sent through USART.write be a string?
2) is there anyway to get all bytes that come through? (would really appreciate link to a sample code/tutorial)

As you can see from code above, I wanted to test if I can use USART to turn on/off LED, but I'm stuck D:

cheers

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Wed Nov 16, 2011 11:38 am

According to the MPLAB SIM help file, uart IO will accept data in one of two forms:

HEX-style - spaced, separated hexadecimal numbers will be treated as one packet, e.g., 05 07 6A 6B 105 107. Since the UART is 9-bits wide, receive values larger than 0x1FF will be masked off.

RAW-style - text between a pair of quotation characters (") will be treated as one packet, e.g., "this is a packet"


Something that trips up a lot of people is that the read functions in USART.bas module are simple non-buffered blocking functions. They're good for simple single character transfers since the only buffering is the two character fifo in the chip itself. Much past that and you begin to get into issues syncing the timing of transfers on both ends and you usually end up with overruns and missing chars. There are some functions that deal with reading strings and/or arrays of bytes such as WaitForStr() and WaitForCount(). Perhaps you would have better luck with them, or for now just make sure you send two chars at a time in the uart IO sim.


Later on, check out the ISRRX.bas module described in the 'interrupts' examples. That module provides an interrupt-driven uart with buffered input. I find it to be much more reliable using that method since it removes the timing issues. However, that code is quite a bit more involved for someone just starting out.

Post Reply