Usart problem with 18F4550


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2005
    Posts
    14

    Angry Usart problem with 18F4550

    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 ???????????

  2. #2
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154

    Default

    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.

    Code:
        '   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

  3. #3
    Join Date
    Oct 2005
    Posts
    14

    Thumbs up

    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

  4. #4

    Default

    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

    Code:
        '   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?

  5. #5

    Default

    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........

  6. #6
    Join Date
    May 2008
    Location
    Italy
    Posts
    825

    Default

    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.
    All progress began with an idea

  7. #7

    Default

    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....

  8. #8
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818

    Default

    Quote Originally Posted by f_lez View Post
    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
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Similar Threads

  1. 18f4550 usb problem.. so im getting crazy..
    By MeSaKu in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 11th May 2009, 22:03
  2. byte Tx w/ USART 16f876a problem
    By GargamelDigi in forum mel PIC BASIC
    Replies: 2
    Last Post: - 15th July 2008, 05:50
  3. 18F4550 XTAL Problem
    By Pesticida in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd January 2008, 20:12
  4. USART Problem , but don't know where, in pc? or in PIC?
    By precision in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th July 2007, 08:12
  5. problem with USART
    By leemin in forum Serial
    Replies: 4
    Last Post: - 11th December 2006, 17:56

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts