problem with USART


Closed Thread
Results 1 to 5 of 5
  1. #1
    leemin's Avatar
    leemin Guest

    Exclamation problem with USART

    i encounter some problem when using the hardware USART for communication. pls help. my code as below:

    include "modedefs.bas"
    DEFINE SHIFT_PAUSEUS 20
    define OSC 20
    DEFINE SER2_ODD 1

    TRISA=%11110000
    TRISB= %11111011

    symbol Addr_Data_In=porta.4
    symbol en=porta.3
    symbol stb=porta.2
    symbol clk=porta.1
    symbol data_out=porta.0

    SPBRG = 32 ' Set baud rate to 9600
    RCSTA = %10010000 ' Enable serial port and continuous receive
    TXSTA = %00100000 ' Enable transmit and asynchronous mode
    RCIF VAR PIR1.5 ' Alias RCIF (USART Receive Interrupt Flag)
    OERR VAR RCSTA.1 ' Alias OERR (USART Overrun Error Flag)
    CREN VAR RCSTA.4 ' Alias CREN (USART Continuous Receive Enable)
    LED VAR PORTD.0 ' Alias LED to PORTD.0

    ADCON1=6

    protocol_2 var byte
    protocol var byte
    addr var word


    mode var byte
    Address_Value var word
    digit var byte

    i var byte

    buffer_size CON 35 ' Sets the size of the ring buffer
    buffer VAR BYTE[buffer_size] ' Array variable for holding received characters
    index_in VAR BYTE ' Pointer - next empty location in buffer
    index_out VAR BYTE ' Pointer - location of oldest character in buffer
    bufchar VAR BYTE ' Stores the character retrieved from the buffer
    errflag VAR BYTE ' Holds error flags

    index_in = 0
    index_out = 0
    i = 0

    INTCON = %11000000 ' Enable interrupts
    ON INTERRUPT GoTo serialin ' Declare interrupt handler routine
    PIE1.5 = 1 ' Enable interrupt on USART

    '================================================= ==================================================

    main: call display
    CALL ending

    display: IF errflag Then error ' Handle error if needed
    IF index_in = index_out Then display ' loop if nothing in buffer

    GoSub getbuf ' Get a character from buffer

    protocol=buffer[1]
    if (protocol<>"X") then display

    addr=buffer[2]
    If (addr<>Address_Value) then display

    mode=buffer[3]
    if (mode=$30) Then Turn_Off_Led

    a29=buffer[4]
    a28=buffer[5]
    a27=buffer[6]
    a26=buffer[7]
    a25=buffer[8]
    a24=buffer[9]
    a23=buffer[10]
    a22=buffer[11]
    a21=buffer[12]
    a20=buffer[13]
    a19=buffer[14]
    a18=buffer[15]
    a17=buffer[16]
    a16=buffer[17]
    a15=buffer[18]
    a14=buffer[19]
    a13=buffer[20]
    a12=buffer[21]
    a11=buffer[22]
    a10=buffer[23]
    a9=buffer[24]
    a8=buffer[25]
    a7=buffer[26]
    a6=buffer[27]
    a5=buffer[28]
    a4=buffer[29]
    a3=buffer[30]
    a2=buffer[31]
    a1=buffer[32]
    a0=buffer[33]

    protocol_2=buffer[34]
    if (protocol_2<>"Y") then display

    lookup a0,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg1=digit
    lookup a1,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg2=digit
    lookup a2,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg3=digit
    lookup a3,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg4=digit
    lookup a4,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg5=digit
    lookup a5,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg6=digit
    lookup a6,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg7=digit
    lookup a7,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg8=digit
    lookup a8,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg9=digit
    lookup a9,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg10=digit
    lookup a10,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg11=digit
    lookup a11,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg12=digit
    lookup a12,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg13=digit
    lookup a13,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg14=digit
    lookup a14,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg15=digit
    lookup a15,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg16=digit
    lookup a16,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg17=digit
    lookup a17,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg18=digit
    lookup a18,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg19=digit
    lookup a19,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg20=digit
    lookup a20,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg21=digit
    lookup a21,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg22=digit
    lookup a22,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg23=digit
    lookup a23,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg24=digit
    lookup a24,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg25=digit
    lookup a25,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg26=digit
    lookup a26,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg27=digit
    lookup a27,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg28=digit
    lookup a28,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg29=digit
    lookup a29,[$FD,$85,$DB,$9F,$A7,$BE,$FE,$95,$FF,$BF,$80],digit
    seg30=digit

    shiftout data_out,clk,MSBFIRST,[seg30,seg29,seg28,seg27,seg26,seg25,_
    seg24,seg23,seg22,seg21,seg20,seg19,seg18,seg17,_
    seg16,seg15,_
    seg14,seg13,seg12,seg11,seg10,seg9,_
    seg8,seg7,seg6,seg5,seg4,seg3,seg2,seg1]
    high stb
    pause 5
    low stb

    goto display


    ' Subroutines

    Disable ' Don't check for interrupts in this section

    getbuf: ' move the next character in buffer to bufchar
    index_out = (index_out + 1) ' Increment index_out pointer (0 to 63)
    IF index_out > (buffer_size-1) Then index_out = 0 ' Reset pointer if outside of buffer
    bufchar = buffer[index_out] ' Read buffer location
    Return


    error: ' Display error message if buffer has overrun
    errflag = 0 ' Reset the error flag
    CREN = 0 ' Disable continuous receive to clear overrun flag
    CREN = 1 ' Enable continuous receive
    GoTo display ' Carry on


    serialin: ' Buffer the character received
    IF OERR Then usart_error ' Check for USART errors
    index_in = (index_in + 1) ' Increment index_in pointer (0 to 63)
    IF index_in > (buffer_size-1) Then index_in = 0 'Reset pointer if outside of buffer
    IF index_in = index_out Then buffer_error ' Check for buffer overrun
    HSerin [buffer[index_in]] ' Read USART and store character to next empty location
    IF RCIF Then serialin ' Check for another character while we're here


    Resume ' Return to program

    buffer_error:
    errflag.1 = 1 ' Set the error flag for software
    ' Move pointer back to avoid corrupting the buffer. MIN insures that it ends up within the buffer.
    index_in = (index_in - 1) MIN (buffer_size - 1)
    HSerin [buffer[index_in]] ' Overwrite the last character stored (resets the interrupt flag)
    usart_error:
    errflag.0 = 1 ' Set the error flag for hardware

    Resume ' Return to program


    ending: END
    Last edited by leemin; - 24th August 2005 at 23:51.

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default

    This thread should give you some tips

    Care about the Enable and Disable placement
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    leemin's Avatar
    leemin Guest

    Default

    yupe. i got it d. no problem d. thanks ya.

  4. #4
    Join Date
    Oct 2006
    Posts
    11

    Unhappy PIC to PIC comm

    hi
    i saw ur coding.i m also doing the same thing.i program is to transmit a string from one pic and receiving the same string in the another pic(USART comm) and displaying the received string in the lcd. my transmit program is working well(got the string in hyper terminal).i have the problem in receiving the string,it s not working.i m using 16f877a and micro c complier.seperately my lcd prgm is also working well
    my code is

    void receive()
    {
    int i=0;
    char s[10];
    STATUS.f5=0;
    STATUS.f6=0;
    for(i=0;i<5;i++)
    {
    while(PIR1.f5==0)
    {}
    s[i]=RCREG;
    PIR1.f5=0;
    }
    init_lcd();
    display();
    }
    void main()
    {
    STATUS.f6=0;
    STATUS.f5=1;
    TRISC=0;
    PORTC=0;
    STATUS.f5=0;
    RCSTA=0x90;
    SPBRG=0x20;
    receive();
    while(1)
    {}
    }

    if u find any wrong in my code plz help me to correct it

    -Sakthi

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default

    OK you're using C language but here we use Melabs PICBasic but anyways, let's try to help you on that.

    your problem is with this line
    Code:
    TRISC=0;
    Sure if the RX pin is set as an output, it won't work. Just change it and i suspect it will work. but i didn't look the whole thing so i could have miss something.

    Good luck!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Usart problem with 18F4550
    By maus in forum Serial
    Replies: 7
    Last Post: - 3rd November 2009, 03:54
  2. byte Tx w/ USART 16f876a problem
    By GargamelDigi in forum mel PIC BASIC
    Replies: 2
    Last Post: - 15th July 2008, 05:50
  3. 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
  4. USART and interrupt latency - not a problem
    By barkerben in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 18th January 2005, 22:57
  5. 16F628 Hardware USART problem
    By atomski in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 26th May 2004, 07:10

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