Serial Communication Problem


Closed Thread
Results 1 to 24 of 24

Hybrid View

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


    Did you find this post helpful? Yes | No

    Exclamation Still confused

    Joe and Luciano,

    I appreciate the info up to this point, I do feel I've gotten a better grasp of what I'm trying to do and how the PIC works, yet I am as confused as ever.

    Here's the latest code (not including the tons of modifications I've tried) that appears to do nothing. Joe, I also used the code you presented in the
    "Can an asm interrupt contain gosub to a picbasic routine?" forum question which looked related to this subject.

    Code:

    include "modedefs.bas"

    Trisb = %00000000
    Trisc = %01000000
    Portb = %00000000
    Portc = %00000000
    PIE1 = %00100001 'Allow Timer1 & serial in to cause a periheral interrupt.
    INTCON = %11110000 ' Enable interrupt for Timer0 AND peripherals

    RX var byte[40]
    TX var byte[40]
    i var byte

    RCIF VAR PIR1.5
    ' Alias RCIF (USART Receive Interrupt Flag)



    Main:


    if RCIF = 1 Then
    serin portc.7, T2400, PDA

    'I've also tried: "RCREG = PDA" in place of "serin portc.7, T2400, PDA" to
    'no avail

    else
    goto main
    endif

    goto DataOut


    DataOut:

    serout portc.6, T2400, [10, 13]

    goto Main

    I'm still using the Hyper terminal to check this out.


    I have looked at the App Notes, the datasheet and the forums and I still cannot get my head around this. I believe I am running into a wall looking at the App Notes and datasheets because they explain EXACTLY how the PIC works with serial comms all the while using assembly, while PicBasic Pro takes care of many of these things yet not all. Do I need to set up interrupts, define registers and the like? Again, I appreciate the help, the info thus far has been great, I'm just not applying it right and I can't figure out why.

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


    Did you find this post helpful? Yes | No

    Default Oops

    Whoops, I forgot to include this line immediately under DataOut:

    serout portc.6, T2400, [PDA]

    Sorry.

  3. #3
    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 03:41.
    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 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!

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

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

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

Similar Threads

  1. Replies: 18
    Last Post: - 4th July 2017, 15:26
  2. serial communication problem
    By kindaichi in forum Serial
    Replies: 13
    Last Post: - 11th March 2010, 17:37
  3. Replies: 5
    Last Post: - 20th March 2006, 02:34
  4. Problem in Serial Communication
    By uuq1 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th June 2005, 08:17
  5. Replies: 8
    Last Post: - 11th November 2004, 21: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