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

    Question Counters and polling

    Thank you both for the info. Sorry I forgot to mention it earlier, I am using a 18F2525 which has a USART. Now I must admit that my knowledge is extremely limited at this time because I know next to nil about assembly except that it's not a lot of fun and quite complicated when I did try learning it a couple years ago.

    I haven't played with interrupts yet, but I'll be starting with those next week if not today. When you say poll the interrupt bit, bare with me, you mean set portb.2, for example, as an interrupt, read in the character when one is detected into a byte of my array (in this case RX[#]) and continue doing so until I get the last character? Also, I presume I can use another interrupt and hopefully set one as a higher priority than the other?

    Is there an example somewhere on using the counter on the PIC? Haven't had a lot of luck searching on the Net. Or is that used in conjunction with the COUNT command? I want to make sure that any time I receive a pulse from the encoder that it gets counted, this will be a priority over everything else.

    Again, thank you both for your advice.

  2. #2
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    695


    Did you find this post helpful? Yes | No

    Default

    Hi!

    =============================================
    The PicBasic Pro SERIN2 command has an optional
    flow control pin. The SERIN2 command does this
    handshaking flow control on a byte by byte basis.
    Some computers may send several bytes before
    they even check the status of their handshaking lines.
    ==============================================


    The idea below does not use the flow control pin.

    Define first the data frames.

    START character
    The start character is present only one time in
    the data frame. (The first byte). If you use
    Notepad, the START character has to be printable.

    END character
    The end character is present only one time in
    the data frame. (The last byte). If you use
    Notepad, the END character has to be printable.

    DUMMY character(s)
    The DUMMY character is present one or more times
    in the data frame. The frame can have
    from 0 to 42 DUMMY characters.(See example below).
    If you use Notepad, the DUMMY characters need to
    be printable.

    DATA character(s)
    Any value except the values used for the
    START, END and DUMMY characters.
    The frame can have from 1 to 40 DATA
    characters. If you use Notepad, the DATA characters
    need to be printable.

    When you choose the character for START, END
    and DUMMY, choose characters that you will never
    use in the DATA characters. Maybe one day you
    will send DATA to an LCD display, so don't
    waste useful ASCII characters.

    * * *

    Example data frames in a Notepad file:

    S = Start character
    X = DATA character(s)
    E = END character
    D = DUMMY character(s)

    <START><DATA><DUMMY><END>

    The DUMMY character(s) is (are) used only if
    DATA is less than 40 bytes or if you need
    to send DUMMY data frames to the PIC.
    While the PC sends out the dummy data frames
    the PIC has time to do something else.
    (Remember we don't use any flow control).


    The following 5 lines are the Notepad file:

    SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXE
    SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXDE
    DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
    SXXXXXXXXXXDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDE
    SXDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDE

    Where:

    The first line is a data frame with 40 bytes of data.
    The second line is a data frame with 39 bytes of data.
    The third line is a dummy frame.
    The fourth line is a data frame with 10 bytes of data.
    The fifth line is a data frame with 1 byte of data.

    * * * *

    At 9600 baud the PC sends 960 bytes per second.
    (Terminal 8N1, raw ASCII file send).

    The data frames are always 42 bytes long plus the
    two bytes that Notepad will put at the end of
    each line. (CR and LF).
    To send out 44 bytes the PC will need 46 ms
    at 9600 baud.

    * * *

    If the above idea is compatible with your design, I will
    help you with the PicBasic Pro code.


    Best regards,


    Luciano
    Last edited by Luciano; - 28th April 2005 at 19:45.

  3. #3
    Join Date
    Feb 2003
    Location
    Delaware
    Posts
    30


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by elec_mech

    I haven't played with interrupts yet, but I'll be starting with those next week if not today. When you say poll the interrupt bit, bare with me, you mean set portb.2, for example, as an interrupt, read in the character when one is detected into a byte of my array (in this case RX[#]) and continue doing so until I get the last character? Also, I presume I can use another interrupt and hopefully set one as a higher priority than the other?
    Thats not quite what I mean. A pic has a few different interrupts. Things like timer0, received character, and portb.0 are all things that can interrupt the processor. Portb.0 is an external interrupt.

    Now on the 16F devices when an interrupt occurs the program jumps to a set mem location (this is in assembly...not PBP) if you have enabled the interrupt (ie it is not masked). It is up to the programmer to test which interrupt caused the program to jump to the interrupt vector. The programmer also needs to save a bunch of stuff (context saving) upon entering the ISR and then needs to restore it all when leaving the ISR. I am only a little familiar with the 18F parts but I know that they have even more registers associated with interrupts. Now this sounds like alot of work. But there is a trick.

    You see interrupt bits toggle regardless of if you have set up any interrupt registers! This means that if you start a timer and it overflows the timer overflow (TMRIF) bit gets set. So if you where to find out what register and bit will toggle when your event occurs then you poll that bit by doing a compare inside of your loop. Something like :

    Code:
    loop:
    'do some stuff here
    'and some more here
    
    if TMRIF = 1 then                    'now check to see if the timer over flowed
         TMRIF = 0                         'it did so reset the bit
         My_counter = My_counter + 1   'increment the counter
    endif
    
    goto loop
    For this to work you would create an alias to the timers interrupt flag and call it TMRIF. You would also need to start the harware timer by setting the appropriate bit in the appropriate register.

    Now if you where to use the UART you would get a bit set (RCIF, received character interrupt flag) whenever a character came in! And all you would need to do would be to load the character into a variable to reset the bit. This would look something like this:

    Code:
    loop:
    'do some stuff here
    'and some more here
    
    if RCIF = 1 then char_in = RCV_IN
    
    goto loop
    For this example to work you would need to create an alias for the received character interrupt flag and name it RCIF and an alias for the buffer that holds the received character and name it RCV_IN. Also you would need to set up the comms parameters.

    Hope this helps.

    Joe.

    Edit....You REALLY need the data sheet for your PIC!

  4. #4
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    695


    Did you find this post helpful? Yes | No

    Default

    Microchip Application Notes:

    AN774 Asynchronous Communications with the PICmicro® USART
    http://ww1.microchip.com/downloads/e...tes/00774a.pdf

    AN774 Source Code (PIC16, PIC17, PIC18).
    http://ww1.microchip.com/downloads/e...otes/00774.zip


    From Source code ZIP file, Readme18.txt

    There are five code examples for the PIC18F452.
    They can be applied to any PIC18 part, with minor changes.
    Each file has a brief functional description in the header
    at the top of the file. They all receive data and transmit
    the data back, but do so in different ways to demonstrate
    typical applications of the USART.

    Here is a summary of the features of each code example:

    P18_TIRI.ASM Use interrupts for transmit and receive, Circular buffers, Eight bit data
    P18_TPRP.ASM Poll for transmit and receive, Simple buffers, Eight bit data
    P18_TWRP.ASM Poll to receive, Wait to transmit, No buffers, Eight bit data
    P18_2STP.ASM Poll to receive, Wait to transmit, No buffers, Eight bit data, Two stop bits
    P18_PRTY.ASM Poll to receive, Wait to transmit, No buffers, Eight bit data, Even parity bit

    Best regards,

    Luciano

  5. #5
    Join Date
    Feb 2003
    Location
    Delaware
    Posts
    30


    Did you find this post helpful? Yes | No

    Default Good info!

    Thanks for the links Luciano. Very good information.

    Regards,

    Joe.

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

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

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

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 : 0

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