Serial Communication Problem


Closed Thread
Results 1 to 24 of 24

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    Here's something to play with.
    Code:
        '   Program to echo incoming serial data 
        '   Baudrate : 2400 Bauds
        '   Method :   Polling USART interrupts flags
    
        '   Pic Definition
        '   ==============
            ' Using PIC 18F2320 @ 4MHZ and bootloader
            '
            DEFINE LOADER_USED 1
            DEFINE OSC 4
            
        '   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
            '
            RCSTA = $90 ' enable serial port, 
                        ' enable continuous receive
                        '
            TXSTA = $24 ' enable transmit, 
                        ' BRGH=1
                        '
            SPBRG = 103 ' set baudrate to 2400                   
            
        '   Alias definition
        '   ================
            '
            '
            RCIF VAR PIR1.5     ' Receive  interrupt flag (1=full , 0=empty)
            TXIF VAR PIR1.4     ' Transmit interrupt flag (1=empty, 0=full)
            OERR var RCSTA.1    ' Overrun error
            CREN var RCSTA.4    ' Continuous receive
                    
        '   Hardware initialisation
        '   =======================
            '
            '
            pause 10 ' safe start-up delay
        
    Main:
        if oerr then  ' Overrun error?
           cren=0     ' clear it
           cren=1
           endif
    
        if RCIF then    ' incomming data?
           TXREG=RCREG  ' take it, send it
           while TXIF=0 ' wait untill transmit buffer is empty
           wend
           endif
        goto main
    the following use HSERIN/HSEROUT statement. Since your PIC have internal USART... why not using it
    Code:
        '   Program to echo incoming serial data 
        '   Baudrate : 2400 Bauds
        '   Method :   Polling USART interrupts flags
    
        '   Pic Definition
        '   ==============
            ' Using PIC 18F2320 @ 4MHZ and bootloader
            '
            DEFINE LOADER_USED 1
            DEFINE OSC 4
            
        '   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 24h ' enable transmit, 
                                  ' BRGH=1
                                  '
            define HSER_SPBRG 103 ' set baudrate to 2400                   
            DEFINE HSER_CLOERR  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
    hope this help
    Last edited by mister_e; - 11th May 2005 at 02:41.
    Steve

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

  2. #2
    Join Date
    Apr 2005
    Location
    Virginia
    Posts
    65


    Did you find this post helpful? Yes | No

    Question HSer Woes

    Mister_E,

    Thank you for the lovely code! It works nicely and I've been examining it while referencing the datasheet and I have learned a good deal. I then tried to manipulate it to work with my application and I've hit a wall. Initially I'm trying to look at a string of characters sent via the hyperterminal, particularly the first and last bit.

    Now, perhaps I'm going into overkill with caution, but I only want to store the string into a variable array if the string starts with a ASCII "!". I then want to continue taking the rest of the string until an ASCII "L" is seen.

    The idea being if for some reason or another, I receive a string of data and I start storing the data string at the middle instead of the beginning and miss some data. Here's what I have:

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

    ' Pic Definition
    ' ==============
    ' Using PIC 18F2320 @ 4MHZ and bootloader
    '
    DEFINE LOADER_USED 1
    DEFINE OSC 4

    ' 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 24h ' enable transmit,
    ' BRGH=1
    '
    define HSER_SPBRG 103 ' set baudrate to 2400
    DEFINE HSER_CLOERR 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)
    a var byte
    i var byte
    ' Variable definition
    ' ===================
    '
    '
    SerialData var byte[40]

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

    Main:
    ' Clear - tried using this to clear, but then I get nothing back

    if RCIF then ' incomming data?
    hserin [SerialData[0]] 'Store the first character received in SerialData,
    'at byte 0
    endif

    if SerialData[0] = "!" then RCV 'if the first byte is an ASCII ! then
    'go on to receive rest of data string


    goto Main


    RCV:

    for i = 1 to 40

    hserin [SerialData[i]] ' take it
    if SerialData[i] = "L" then TRS 'if an ASCII L is
    'present stop
    'and output
    'string
    next

    goto Main 'If ASCII L is not detected return to main loop


    TRS:
    for a = 1 to i 'Okay, if I set a = 0 to i, I get a ! in
    'front of everything when it outputs
    'i.e. if I send !#00CL I get !!#00CL
    'back. Why, I don't know.
    hserout [SerialData[a]] ' send it
    next 'endif
    hserout [10,13] 'line feed and carriage return
    goto Main





    Alright, if I send !#X00CL I get back !#X00CL, which is great! However, and here's the where the confusion starts, if I send 123456ABCDEF#, I get nothing back, again, good news right? But, if I follow that string with !#X00CL, I get back 123456ABCDEF#!#X00CL. I am hitting a wall here.

    It seems as though the program stores up to 41 (0 to 40) bytes from a serial string (this because in my code I set SerialData to store up to 41 bytes, from 0 to 40). Once it sees 41 bytes, it resets itself and starts anew until it sees an ASCII L. For example, if I send 123456ABCDEF# three times then follow with !#X00CL I get back #1X00CL.

    Is there a way to tell the program to write and erase to only SerialData[0] until a specific character is seen? ASCII ! in this case. Or am I missing something else altogether? Thanks!

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


    Did you find this post helpful? Yes | No

    Default

    this should give you a hand!
    Code:
        '   Program to echo incoming serial data 
        '   Baudrate : 2400 Bauds
        '   Method :   Polling USART interrupts flags
    
        '   Pic Definition
        '   ==============
            ' Using PIC 18F2320 @ 4MHZ and bootloader
            '
            DEFINE LOADER_USED 1
            DEFINE OSC 4
            
        '   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
            '
            RCSTA=$90 ' enable serial port, 
                      ' enable continuous receive
                      '
            TXSTA=$24 ' enable transmit, 
                      ' BRGH=1
                      '
            SPBRG=103 ' set baudrate to 2400                   
            DEFINE HSER_CLOERR   1 
            
        '   Alias definition
        '   ================
            '
            '
            RCIF VAR PIR1.5     ' Receive  interrupt flag (1=full , 0=empty)
            TXIF VAR PIR1.4     ' Transmit interrupt flag (1=empty, 0=full)
            CREN var RCSTA.4    ' Receiver enable bit
            
        '   Variable definition
        '   ===================
            '
            '
            SerialString var byte[40]
            Counter      var byte
            Success      var bit
            i            var byte
            
        '   Hardware initialisation
        '   =======================
            '
            '
            pause 100 ' safe start-up delay
    
    Main:
       clear
       if RCIF then               ' incomming data?
          I=RCREG                 ' take it
          if i !="!" then discard ' is it the header character?
          Redo:
              hserin 500,discard,[i]  ' get next character, if it's too long
                                      ' get out of here and wait the next header
                                      ' character
                                      '
              if i ="L" then discard  ' is it the end?
              serialstring[counter]=i ' Store into the array
              counter=counter+1       '
              goto Redo      
          Discard:
                  if i="L" then success=1 ' Yeah i got a valid string
          endif
      
       if success then
          cren=0  ' Disable Receiver
          hserout ["String received : ",str serialstring\counter,13,10,_
                   "Character count : ", dec counter,13,10]
          cren=1  ' Enable receiver
          endif
       goto main
    Steve

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

  4. #4
    Join Date
    Apr 2005
    Location
    Virginia
    Posts
    65


    Did you find this post helpful? Yes | No

    Question Replacing the clear command?

    Steve,

    Thank you for the code, it worked beautifully. I did notice that moving the clear command down to the very bottom right before the 'goto main' worked better. I think the data came in so fast that if the timing were off I missed some data streams.

    I have a question regarding the clear command. I understand it completely clears the ram which prevents a constant repeating loop of a single data input stream. Is there another way to specifically clear or erase whatever register or ram location the USART uses? I ask because I am running several if then statements that require checking the status of certain variables and if I use the clear command, I lose the original status/data in those variables.

    I've tried using RCIF = 0 and RCREG = 0 to no avail. I've also tried the ERASECODE $FAEh to clear the RCREG register, but this command doesn't work in PicBasic Pro with the 18F2525 chip I'm using. Any suggestions?

    Again, thanks again for the code, I've been playing with it all day, making mistakes, gaining victories and generally learning a lot in the process.

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


    Did you find this post helpful? Yes | No

    Default

    You're welcome.

    You'll clear then RCREG by reading it 2 times

    TempVar=RCREG
    TempVar=RCREG
    Steve

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

  6. #6
    Join Date
    Apr 2005
    Location
    Virginia
    Posts
    65


    Did you find this post helpful? Yes | No

    Talking Solution!

    Upon having another set of eyes look at my code and using the debugger I'm learning to use slowly with more interest, I forgot to clear the counter and i variables. Effectively it was continuing to loop because those variables still had values assigned. Thank you for your time and help Steve, I do appreciate it.

    To anyone else who may be interested in receiving a serial string, reading it and then doing something based upon a character(s) received, let me know and I'll post some really nice code to play with.

    Thanks again to Joe, Luciano, and Steve. I learned a great deal by all the info and code posted.

  7. #7
    Dalomir's Avatar
    Dalomir Guest


    Did you find this post helpful? Yes | No

    Post Same programming

    I put the schematic in the attachement of this post.

    Its an usart with 2 PIC18F452 linked together by PIN 25 and PIN 26

    One receive a paralelle 4 bits number from a numerical keyboard, light LED at PIN 2, 3, 4, 5, 6, 8, 9 and 10 to show that number and send that number directly to the other PIC RX PIN.

    Thats other PIC then receive that serial number and lights LED in the same way as the other PIC, showing that the number was successfully sent.

    If anyone has a code that would make it work, I would really appreciate.
    Attached Images Attached Images  

  8. #8
    leemin's Avatar
    leemin Guest


    Did you find this post helpful? Yes | No

    Default help

    To anyone else who may be interested in receiving a serial string, reading it and then doing something based upon a character(s) received, let me know and I'll post some really nice code to play with.

    yupe. i need a program to receive a serial string, reading it and then doing something. my string is something like this X-Addr-Mode-Data(30bytes)-Y
    where X stand for SOT and Y stand for EOT. Addr is use to indicate the dedicated address value i want to send data.

    my code is something like below:
    include "modedefs.bas"
    DEFINE SHIFT_PAUSEUS 20
    define OSC 20
    DEFINE HSER_CLOERR 1 ' automatic clear overrun error

    TRISA=%11110000
    TRISB= %11111011
    TRISC.6 = 1
    TRISC.7 = 0

    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 = $90 ' Enable serial port and continuous receive
    TXSTA = $24 ' Enable transmit and asynchronous mode
    RCIF VAR PIR1.5 ' Receive interrupt flag (1=full , 0=empty)
    TXIF VAR PIR1.4 ' Transmit interrupt flag (1=empty, 0=full)
    OERR var RCSTA.1 ' Overrun error
    CREN var RCSTA.4 ' Continuous receive
    ADCON1=6

    display: if RCIF then
    i=RCREG
    if i != "X" then discard

    redo: hserin 500,discard,[i]
    if i="Y" then discard
    serialstring[counter]=i
    counter=counter+1
    goto redo

    discard: if i="Y" then success=1
    endif
    if success then
    cren = 0
    if (serialstring[0] <> address_Value) then reset
    if serialstring[1] = $30 then turn_off_led
    serialstring[2]=a29
    serialstring[3]=a28
    serialstring[4]=a27
    serialstring[5]=a26
    serialstring[6]=a25
    serialstring[7]=a24
    serialstring[8]=a23
    serialstring[9]=a22
    serialstring[10]=a21
    serialstring[11]=a20
    serialstring[12]=a19
    serialstring[13]=a18
    serialstring[14]=a17
    serialstring[15]=a16
    serialstring[16]=a15
    serialstring[17]=a14
    serialstring[18]=a13
    serialstring[19]=a12
    serialstring[20]=a11
    serialstring[21]=a10
    serialstring[22]=a9
    serialstring[23]=a8
    serialstring[24]=a7
    serialstring[25]=a6
    serialstring[26]=a5
    serialstring[27]=a4
    serialstring[28]=a3
    serialstring[29]=a2
    serialstring[30]=a1
    serialstring[31]=a0
    endif
    reset: cren=1 ' Enable receiver
    goto display

Similar Threads

  1. Replies: 18
    Last Post: - 4th July 2017, 14:26
  2. serial communication problem
    By kindaichi in forum Serial
    Replies: 13
    Last Post: - 11th March 2010, 16:37
  3. Replies: 5
    Last Post: - 20th March 2006, 01:34
  4. Problem in Serial Communication
    By uuq1 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th June 2005, 07:17
  5. Replies: 8
    Last Post: - 11th November 2004, 20:08

Members who have read this thread : 2

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