1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

serial communication with pic 18f4550

Discussion in 'Microcontrollers' started by neelam29, Feb 28, 2008.

  1. neelam29

    neelam29 New Member

    Joined:
    Nov 15, 2005
    Messages:
    105
    Likes:
    0
    dear all,
    m trying to make a project on interfacing rs232 with pic18f4550. m new to rs232 .cananybody please help me with some examples based on rs232 communication with pic18f.
     
  2. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Here's some code I wrote when I was learning to do RS232 on the 18F1320. It's just simple polled RS232. I have some 4550 code, but I don't know if it works. This 1320 code definitely does.
    Code (text):
        list    p=18F1320
        include <p18f1320.inc>
        CONFIG  OSC=INTIO2,WDT=OFF,MCLRE=ON,LVP=OFF,DEBUG=ON

        cblock  0x00
            d1,d2,d3,dec1,dec2,count,num
        endc

        org 0x0000
    init    bsf OSCCON,IRCF2    ;set to 8MHz clock
        bsf OSCCON,IRCF1
        bsf OSCCON,IRCF0
        movlw   0x7f        ;set PortA to all digital
        movwf   ADCON1
        clrf    TRISA       ;set PortA pins to output
        clrf    LATA        ;turn off LEDs
        clrf    count
        movlw   b'00010010' ;set PortB - RB4=RX=input
        movwf   TRISB
        call    rs_init
        bsf TXSTA,TXEN  ;transmit enable

    main    clrf    TBLPTRH
        movlw   text
        movwf   TBLPTRL
    next_ch tblrd   *+
        movf    TABLAT,W
        bz  goagain
        call    rs_send
        goto    next_ch
    goagain incf    num
        movf    num,W
        call    fix
        movf    dec1,W
        call    rs_send
        movf    dec2,W
        call    rs_send
        movlw   0x0d
        call    rs_send
        movlw   0x0a
        call    rs_send
        call    delay
        call    delay
        call    delay
        call    delay
        goto    main

    ;Sends byte in W to RS232
    rs_send movwf   TXREG
    sdloop  btfss   TXSTA,TRMT
        goto    sdloop
        return

    text    db  "RS232 on the 8 MHz clocked PIC 18F1320!",0x0d,0x0a,"It is the  
    greatest!",0x0d,0x0a,0x0a

    rs_init movlw   0x0c        ;set for 9600 baud @ 8MHz clock
        movwf   SPBRG
        bcf TXSTA,BRGH  ;clear Baud Rate Select Bit (low speed)
        bcf BAUDCTL,BRG16   ;select 8-bit baud rate generator
        bcf TXSTA,SYNC  ;asynchronous mode
        bsf RCSTA,SPEN  ;enable serial port
        return

    ;*********************************************************
    ;*fix - subroutine takes byte passed in W - splits it into
    ;*two ascii bytes in dec1 and dec2, representing decimal digits.     
    fix movwf   dec2        ;put number in dec2
        clrf    count       ;count = 0
        movlw   10      ;W = 10
    tens    subwf   dec2,F      ;subtract 10 from number
        btfsc   STATUS,C    ;check if result <10
        goto    again       ;no, go again
        movf    count,W     ;yes, put count in W
        addlw   0x30        ;add $30 to make it an ASCII number
        movwf   dec1        ;and store it in dec1
        movf    dec2,W      ;get remainder (2nd digit)
        addlw   10      ;put the last subtract back
        addlw   0x30        ;add $30 to make it ASCII
        movwf   dec2        ;store it back in dec2
        return          ;and return
    again   incf    count,F     ;increment count
        goto    tens        ;go again

    delay   movlw   0x01
        movwf   d1
        movlw   0x7f
        movwf   d2
        movlw   0x01
        movwf   d3
    delay_0 decfsz  d1,f
        goto    $+6
        decfsz  d2,f
        goto    $+6
        decfsz  d3,f
        goto    delay_0
        return

        end
     
     
  3. fpal

    fpal New Member

    Joined:
    Feb 13, 2008
    Messages:
    1
    Likes:
    0
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. neelam29

    neelam29 New Member

    Joined:
    Nov 15, 2005
    Messages:
    105
    Likes:
    0

    Dear frnds

    this code i tried out for usart with pic18f4550 but m geeting the error which i have decribed below ..please help out.m using mplabc18 compiler



    #include <p18f4550.h>

    #include <usart.h>

    void rx_handler (void);
    #define BUF_SIZE 25

    #pragma idata bigdata
    char data[11][BUF_SIZE+1] = {
    { "String #0\n\r" },
    { "String #1\n\r" },
    { "String #2\n\r" },
    { "String #3\n\r" },
    { "String #4\n\r" },
    { "String #5\n\r" },
    { "String #6\n\r" },
    { "String #7\n\r" },
    { "String #8\n\r" },
    { "String #9\n\r" },
    { "Invalid key (0-9 only)\n\r" }
    };
    #pragma idata
    #pragma code rx_interrupt = 0x8
    void rx_int (void)
    {
    _asm goto rx_handler _endasm
    }
    #pragma code
    #pragma interrupt rx_handler
    void rx_handler (void)
    {
    unsigned char c;
    /* Get the character received from the USART */
    c = ReadUSART();
    if (c >= '0' && c <= '9')
    {
    c -= '0'; /* Display value received on LEDs */
    PORTB = c;

    putsUSART (data[c]);
    }
    else
    {

    putsUSART (data[10]); /* Display value received on LEDs */
    PORTB = c;
    }
    /* Clear the interrupt flag */
    PIR1bits.RCIF = 0;
    }
    void main (void)
    {
    /* Configure all PORTB pins for output */
    TRISB = 0;

    OpenUSART (USART_TX_INT_OFF &
    USART_RX_INT_ON &
    USART_ASYNCH_MODE &
    USART_EIGHT_BIT &
    USART_CONT_RX &
    USART_BRGH_HIGH, 103);

    putrsUSART (
    (const far rom char *)"\n\rEnter a digit 0-9!\n\r");

    RCONbits.IPEN = 1;

    IPR1bits.RCIP = 1;

    INTCONbits.GIEH = 1;

    while (1)
    ;
    }

    the linker file added is


    // File: 18f4550.lkr
    // Sample linker script for the PIC18F4550 processor

    LIBPATH .

    FILES c018i.o
    FILES clib.lib
    FILES p18f4550.lib

    CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
    CODEPAGE NAME=page START=0x2A END=0x7FFF
    CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
    CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
    CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
    CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED

    ACCESSBANK NAME=accessram START=0x0 END=0x5F
    DATABANK NAME=gpr0 START=0x60 END=0xFF
    DATABANK NAME=gpr1 START=0x100 END=0x1FF
    DATABANK NAME=gpr2 START=0x200 END=0x2FF
    DATABANK NAME=gpr3 START=0x300 END=0x3FF
    DATABANK NAME=usb4 START=0x400 END=0x4FF PROTECTED
    DATABANK NAME=usb5 START=0x500 END=0x5FF PROTECTED
    DATABANK NAME=usb6 START=0x600 END=0x6FF PROTECTED
    DATABANK NAME=usb7 START=0x700 END=0x7FF PROTECTED
    ACCESSBANK NAME=accesssfr START=0xF60 END=0xFFF PROTECTED

    SECTION NAME=CONFIG ROM=config

    STACK SIZE=0x100 RAM=gpr3




    error which m getting while complilation is

    Error - section 'bigdata' can not fit the section. Section 'bigdata' length=0x0000011e
    Errors : 1
     
  6. picbits

    picbits Well-Known Member

    Joined:
    Jan 29, 2007
    Messages:
    2,003
    Likes:
    94
    Location:
    Northants, United Kingdom
    Getting back to basics, if you need to hook the 4550 up to a PC make sure you are using a level shifter such as a MAX232 or similar.
     
  7. amber967

    amber967 New Member

    Joined:
    Dec 18, 2009
    Messages:
    1
    Likes:
    0
    serial interfacing of computer with PIC 18F452

    i m doing my project... n facing the problem
    that how to send data from serial port of computer to PIC 18F452 microcontroller with the help of MAX232, and rs232 connector... plz help me and send its schematic diagram and assembly code for interfacing computer and pic micro controller. thnx
     
  8. MrNobody

    MrNobody New Member

    Joined:
    Aug 29, 2006
    Messages:
    251
    Likes:
    3
    The reason is because ur variable is too large to fit in the allocated memory..
    You can solve it by breaking down 'bigdata' into smaller chunks eg 'bigdata1', 'bigdata2' etc and store them in different banks by using

    Code (text):

    #pragma udata bigdata1

    #pragma udata bigdata2
     
    For more info, you can go to the link below..
    Microchip Technology User Forums

    HTH
     

Share This Page