PDA

View Full Version : Usart problem with 18F4550



maus
- 25th February 2006, 22:43
When i use a 18F4550 to communicate with the pc it will not work right.
The interface between pic and PC is oke (RS232), because when i use a 16F876 with the same code it works fine.

I've gone over the datasheet over and over and i think that is setup everything right.
but just a simple character transfer does not even work.

who can help me or has faced the same problem??

p.s. USB etc works great, but now i also need Usart!!

Here is the simple code that will not work.

send char "A" to the PC

at 2400 baud, no data received, at lower or higher speed "crap" is received.


' simple usart test pic18f4550

define osc 20

trisc.7=1 'portc.7 input RX
trisc.6=0 'portc.6 output TX

baudcon.3=0'BRG16 :0=8 bit baud generator
txsta.2=0 'BRGH 0=low speed
SPBRG=129 '2400 baud see datasheet

txsta.4=0 'SYNC 0=asynchoon
rcsta.7=1 'SPEN serial port enable
txsta.6=0 'TX-9 :8 bit data
txsta.5=1 'TXEN transmit enable

ADCON1=15 'all ports digital i/o


start:
Hserout [$41] 'send char A at 2400 baud
pause 2000

goto start

what i'm i doing wrong ???????????

Tissy
- 26th February 2006, 04:17
I too have had problems using USART on the 18F2550.

Try this piece of code below. It works fine on my PIC now. It will basically echo back what ever you put on the PIC. Obvioulsy you can change the bottom lines to just send data if you wish.

Good luck and i hope it helps.



' Program to echo incoming serial data
' Baudrate : 9600 Bauds
' Method : Polling USART interrupts flags

' Pic Definition
' ==============
' Using PIC 18F2550 @ 20MHZ and bootloader
'
Clear
DEFINE OSC 20
DEFINE LOADER_USED 1
ADCON1 = %00001111

' Hardware configuration
' ======================
TRISC = %10000000 ' PORTC.7 is the RX input
' PORTC.6 is the TX output

' Serial communication definition
' ===============================
' Using internal USART and MAX232 to interface to PC
'
DEFINE HSER_RCSTA 90h ' enable serial port,
' enable continuous receive
'
DEFINE HSER_TXSTA 20h ' enable transmit,
' BRGH=1
'
DEFINE HSER_BAUD 9600
DEFINE HSER_CLROERR 1 ' automatic clear overrun error

' Alias definition
' ================
RCIF VAR PIR1.5 ' Receive interrupt flag (1=full , 0=empty)
TXIF VAR PIR1.4 ' Transmit interrupt flag (1=empty, 0=full)

' Variable definition
' ===================
SerialData var byte

' Hardware initialisation
' =======================
pause 10 ' safe start-up delay

Main:
if RCIF then ' incomming data?
hserin [Serialdata] ' take it
hserout [serialdata] ' send it
endif
goto main

Steve

maus
- 5th March 2006, 21:42
thanks steve for helping me on the way again.
i figured it out with your sample code and i'm back on track.


thx again

f_lez
- 2nd November 2009, 01:21
Oh I clapped my hands when I found this, thought i had got a working example of hserout...

didnt work for me.........


I changed it a little as I like to run at OSC 48 etc, and dont use a boot loader, and didnt need hserin, so just did a hserout and a 1 second pause to see if i could receive at the PC end, but I get the wrong chars, usually =_Y=_Y etc repeated



' Baudrate : 9600 Bauds
' Method : Polling USART interrupts flags

' Pic Definition
' ==============
' Using PIC 18F2550 @ 48 MHZ '

DEFINE OSC 48
clear
ADCON1 = %00001111

' Hardware configuration
' ======================
TRISC = %10000000 ' PORTC.7 is the RX input
' PORTC.6 is the TX output

' Serial communication definition
' ===============================
' Using internal USART and MAX232 to interface to PC
'
DEFINE HSER_RCSTA 90h ' enable serial port,
' enable continuous receive
'
DEFINE HSER_TXSTA 20h ' enable transmit,
' BRGH=1
'


DEFINE HSER_BAUD 9600

DEFINE HSER_CLROERR 1 ' automatic clear overrun error

' Alias definition
' ================
RCIF VAR PIR1.5 ' Receive interrupt flag (1=full , 0=empty)
TXIF VAR PIR1.4 ' Transmit interrupt flag (1=empty, 0=full)

' Variable definition
' ===================
SerialData var byte

' Hardware initialisation
' =======================
pause 10 ' safe start-up delay

Main:
pause 1000
toggle portb.0

hserout [65,10,13] ' send it

goto main



Also portC.1 goes high ?

led flashes 1 second on and 1 second off so i'd say cpudiv etc is right.

Have i done anything obviously wrong?

f_lez
- 2nd November 2009, 14:16
ok double checked the hardware on the pc side, its a pl2303/usb cable, as my laptop nhas no com: ports.

if i short pins 2/3 then i see what i type, so the hardware to that point is working.

i then plug this into my dev board, which has a max232 on it, this is connected to the PIC via a dip switch on rc6/rc7. if i disconnect it from the pic with the switches and then short these, i see what i type again, so the max 232 is working both ways...

I flip the switches and reconnect to the pic, and i get garbage on screen, about once a second, and the chr counter advances by 3, so its almost right, but not quite........:confused:

aratti
- 2nd November 2009, 15:08
Below the EUSART setting for 48 Mhz as per Mr E pic calculator.



DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
DEFINE HSER_SPBRG 225 ' 9600 Baud @ 48MHz, 0,0%
SPBRGH = 4
BAUDCON.3 = 1 ' Enable 16 bit baudrate generator


Al.

f_lez
- 2nd November 2009, 18:06
I did have a look at the Mister_e tool, but it said it needed a dll i didnt have.

Also still same, but then i found baudcon.4=1.......bingo! (Ok a friend pointed to it!)

Seems i needed my bits the other way up...

Other issue still remains, portc.1=high, but i see nothing in the above to cause it?

No doubt its a quirk of using the Hw instead of the software routine.

I could always consider it a power light....

Archangel
- 3rd November 2009, 04:54
I did have a look at the Mister_e tool, but it said it needed a dll i didnt have.


Hi f_lez,
check out post 10 in this thread. I had that problem too, this fixed it.
http://www.picbasic.co.uk/forum/showthread.php?t=4994