• AVR Freaks

Hot!PIC16F887 and USART Timing

Page: 12 > Showing page 1 of 2
Author
cabletie
New Member
  • Total Posts : 23
  • Reward points : 0
  • Joined: 2011/07/02 06:21:46
  • Location: 0
  • Status: offline
2019/04/11 10:18:35 (permalink)
0

PIC16F887 and USART Timing

Hi,
  I am using the PIC16F887 at 8Mhz (MPLABX v5.0, XC8 V2) to communicate with
another vendors' processor (running at 10Mhz) via the USART pins.   The question I have
stems from an issue with my code stopping and waiting for an empty 887 receiver
buffer - like forever man!   I wrote up a couple of test loops and observed the tx/rx pins
on an O'scope.   The two devices are communicating (YEA!!) and what i have witnessed
is my code waiting for an empty rx buffer because of a timing difference between
the two UARTs.  My code establishes a 104us pulse width (9600 baud +/-) while the
other processor establishes a 100us pulse width (10000 baud).  I cannot change
the other vendors' code; however, I am thinking I can manipulate my USART
registers to generate bit timing more inline with the other processor so things
can 'get along' -> (here it is) if I do this to generate a 10000 baud signal will
this cause problems inside the PIC16F887 with the USART module?  i.e. deviating
from a standard baud rate.    Thanks in advance for the assist, I appreciate it.
Regards.
#1

24 Replies Related Threads

    jack@kksound
    code tags!
    • Total Posts : 3183
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/11 10:30:09 (permalink)
    +2 (2)
    Using a baud rate that is "not a standard" should not cause any issues INSIDE the pic, that is one reason it has registers to set the timers that determine the rate, as long as the selected rate uses frequencies that are within the specified limits of the pic (see the datasheet). Your slight change from 9600 to 10000 should not result in any issues of out-of-spec frequencies or times, it is only a small change of about 4% increase. This will however NOT allow a good communications with any device that is actually running at 9600 since 4% is out of tolerance for most USARTs.
    #2
    pcbbc
    Super Member
    • Total Posts : 926
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/11 10:31:45 (permalink)
    +1 (1)
    You can autodetect the baud rate, or program it.
     
    Assuming you program it explicitly using BRG/BRG16, you can program any rate you like using the formual in the datasheet page 160 EXAMPLE 12-1.
    http://ww1.microchip.com/...eviceDoc/40001291H.pdf
     
    The PIC doesn't give two hoots if it is a "standard" rate or not.
    #3
    pcbbc
    Super Member
    • Total Posts : 926
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/11 10:40:58 (permalink)
    +2 (2)
    My calculations say that, with BRG16=1 and BRGH=1, the values you need to program to BRG16 are:
     
    10000 baud
    8000000 / 10000 = 800 / 4 = 200 - 1 = 199
     
    9600 baud
    8000000 / 9600 = 833 / 4 = 208 - 1 = 207
     
    #4
    cabletie
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2011/07/02 06:21:46
    • Location: 0
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/21 19:43:27 (permalink)
    0
    Adjusting the bit timing did help; however, other timing issues remain, mostly with
    packet transfer chronology.   To keep it short, I tried using an interrupt but MPLABX
    and XC8 wont allow an ISR to be declared.  I get various compile time errors
    directly pointing at the interrupt function declaration.  I referenced the XC8 User Guide
    and copied the example code snippet but still get the error:
    Invalid or unrecognized type interrupt
    unexpected (   and unexpected ; .......  I had a pic of the errors but seem to
    have misplaced it; I can get another if it will help.
    I'm guessing the response to this is going to be:
    1. use an earlier MPLAB version    or
    2. use a newer target
      I thought I saw the PIC16F887 is supported with the tool chain I am using (see 1st post)
    but maybe something is askew ... ?  the include file?
    I know the whole suite works because I have another project using the PIC18F2580 which
    I used successfully to program a prototype and utilize CanBus comm's....
    any ideas?
    Thank you for the assist.
     
    #5
    1and0
    Access is Denied
    • Total Posts : 9040
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/21 20:06:46 (permalink)
    +1 (1)
    A post of your ISR code would help.  Are you using C90 or C99 mode of the XC8 compiler?
     
     
    #6
    qhb
    Superb Member
    • Total Posts : 9990
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: PIC16F887 and USART Timing 2019/04/21 23:17:24 (permalink)
    +1 (1)
    cabletie
    ..
    To keep it short, I tried using an interrupt but MPLABX
    and XC8 wont allow an ISR to be declared.  I get various compile time errors
    directly pointing at the interrupt function declaration. 

    You're using the wrong syntax.
     

    I referenced the XC8 User Guide
    and copied the example code snippet but still get the error:

    WHICH "XC8 User Guide"?
    There are two.
    MPLAB_XC8_C_Compiler_Legacy_User_Guide.pdf is for C90 mode.
    MPLAB_XC8_C_Compiler_User_Guide_for_PIC.pdf is for C99 mode.
     

    I'm guessing the response to this is going to be:
    1. use an earlier MPLAB version    or
    2. use a newer target
      I thought I saw the PIC16F887 is supported with the tool chain I am using (see 1st post)
    but maybe something is askew ... ?  the include file?

    None of your guesses are correct. Both chips are fully supported, it is the new version of the Compiler (XC8) not the IDE (MPABX) that is causing you confusion.
     

    Only 10 to go...
    #7
    cabletie
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2011/07/02 06:21:46
    • Location: 0
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/22 07:48:41 (permalink)
    0
    @1and0
        The various permutations of the declaration I tried are:

     
          void interrupt I_Usart_Rx(void);
     
          interrupt void I_Usart_Rx(void);
     
          void interrupt void I_Usart_Rx(void);
     

     
       and the corresponding ISR permutations are:

     
         void interrupt I_Usart_Rx(void) { /* isr code here */ };
     
         interrupt void I_Usart_Rx(void) { /* isr code here */ };
     
         void interrupt void I_Usart_Rx(void) { /* isr code here */  };
     

     
    qhb
    You're using the wrong syntax.

    (*snort*)  ...really?  The industry is moving to 64-bit operating systems and
    syntax is still an issue?  I understand the implementation of the interrupt process
    is device specific; however, how many ways to do spell 'interrupt'?
    Can't we agree on anything????
     

    WHICH "XC8 User Guide"?
    There are two.
    MPLAB_XC8_C_Compiler_Legacy_User_Guide.pdf is for C90 mode.
    MPLAB_XC8_C_Compiler_User_Guide_for_PIC.pdf is for C99 mode.
     

      Not trying to be difficult; but, evidently I have a rogue version because
    I get a 'string mismatch' error for the two listed titles.
    The manual I have is: "MPLAB XC8 C Compiler User's Guide.pdf"
    Copyright 2012-2013   Document number:DS50002053C.
    The only reference I see to the C*something* standard is on page 137,
    Section 5.2 ANSI C Standard Issues.  It states, "This compiler conforms
    to..." what "... is commonly called the C90 standard.".  Am I using the wrong manual?
      Where the C language is explicit I find the English language to be confusing:
    is that ANSI 'C Standard' Issues  or ANSI C 'Standard Issues'.... ? (the implied
    difference here being scope).
     

    None of your guesses are correct. Both chips are fully supported, it is the new version of the Compiler (XC8) not the IDE (MPABX) that is causing you confusion.

     For each new project I find myself using a module or some functionality which I have never used.  This,
    for the most part, constantly leaves me feeling like a rookie....  maybe I should change my avatar
    label to: ThePerpetualRookie....??
    Thank you for your support, I do appreciate your assistance.
     
     
    edit: install code tags.
     
     
    post edited by cabletie - 2019/04/22 10:15:35
    #8
    jack@kksound
    code tags!
    • Total Posts : 3183
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/22 09:11:36 (permalink)
    +1 (1)
    You could try posting the version of XC8 that shows in the PRoject Properties window that YOU have selected to use with this project, MPLABX requires it be selected (it may have dome that automatically for you). It is not a matter of spelling "interrupt" correctly, it is a matter of the syntax (this is NOT the 64 bit world here) of the entire declaration statement. the two versions of c, c90 and c99, that the latest version (v2.x) of XC8 supports use slight differences in the syntax of the interrupt statement, get even a small part of it wrong and you get an error.
    Tell us which version you are using and if it is v2.x then which mode (c90 or c99), both can be seen in the Project Properties.
    #9
    cabletie
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2011/07/02 06:21:46
    • Location: 0
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/22 09:30:34 (permalink)
    0
    XC8 v2.00.
    C standard: C 99
    Output file format: ELF/DWARF.
     
    I have also tried __interrupt() I_Usart_Rx(void);
    but this gives an IDE error of "unexpected token: ("
     
     
    post edited by cabletie - 2019/04/22 09:38:57
    #10
    Mysil
    Super Member
    • Total Posts : 3322
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/22 09:41:12 (permalink)
    +2 (2)
    Hi,
    This should work for PIC16 devices  in XC8 version v2.xx,
    both in C90 and C99 mode, and also in some previous versions:
    void  __interrupt()  I_Usart_Rx(void) { /* isr code here */ }; 

    The double underscore indicate the namespace reserved for the compiler,
    both for some features specified in the standards, and for implementation dependent features and extensions.
     
        Mysil
    #11
    jack@kksound
    code tags!
    • Total Posts : 3183
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/22 09:46:23 (permalink)
    +1 (1)
    From the User Guide for XC8: section 4.9.1 Writing an Interrupt Service Routine
    (this is for 16F or other non 18F parts OR 18F in legacy (non priority) mode)....
    void __interrupt() tcInt(void) // <---- use your ISR function name instead of tcInt(void)
    {
        if (TMR0IE && TMR0IF)
            { // any timer 0 interrupts?
                 TMR0IF=0;             // this is example code only - put your code here
                 ++tick_count;
            }
    // process other interrupt sources here, if required return;
    }

    #12
    cabletie
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2011/07/02 06:21:46
    • Location: 0
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/22 09:46:50 (permalink)
    0
    Mysil
    Hi,
    This should work for PIC16 devices  in XC8 version v2.xx,
    both in C90 and C99 mode, and also in some previous versions:
    void  __interrupt()  I_Usart_Rx(void) { /* isr code here */ }; 

    The double underscore indicate the namespace reserved for the compiler,
    both for some features specified in the standards, and for implementation dependent features and extensions.
     
        Mysil


    for this I get an "unknown type name" error.
    #13
    cabletie
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2011/07/02 06:21:46
    • Location: 0
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/22 10:26:04 (permalink)
    0
    this gives an "unknown type name" error.
    #14
    jack@kksound
    code tags!
    • Total Posts : 3183
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/22 10:38:22 (permalink)
    +1 (1)
    cabletie
    this gives an "unknown type name" error.


    post EXACTLY what you tried, you have something wrong.
    #15
    1and0
    Access is Denied
    • Total Posts : 9040
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/22 11:09:30 (permalink)
    +1 (1)
    cabletie
    (*snort*)  ...really?  The industry is moving to 64-bit operating systems and
    syntax is still an issue?  I understand the implementation of the interrupt process
    is device specific; however, how many ways to do spell 'interrupt'?
    Can't we agree on anything????
     

    WHICH "XC8 User Guide"?
    There are two.
    MPLAB_XC8_C_Compiler_Legacy_User_Guide.pdf is for C90 mode.
    MPLAB_XC8_C_Compiler_User_Guide_for_PIC.pdf is for C99 mode.
     

      Not trying to be difficult; but, evidently I have a rogue version because
    I get a 'string mismatch' error for the two listed titles.
    The manual I have is: "MPLAB XC8 C Compiler User's Guide.pdf"
    Copyright 2012-2013   Document number:DS50002053C.
    The only reference I see to the C*something* standard is on page 137,
    Section 5.2 ANSI C Standard Issues.  It states, "This compiler conforms
    to..." what "... is commonly called the C90 standard.".  Am I using the wrong manual?
      Where the C language is explicit I find the English language to be confusing:
    is that ANSI 'C Standard' Issues  or ANSI C 'Standard Issues'.... ? (the implied
    difference here being scope).

    Read Post #7 in this topic:  https://www.microchip.com/forums/FindPost/1095409
    #16
    1and0
    Access is Denied
    • Total Posts : 9040
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/22 11:21:12 (permalink)
    +2 (2)
    cabletie
    this gives an "unknown type name" error.

    cabletie
    XC8 v2.00.
    C standard: C 99
    Output file format: ELF/DWARF.
     
    I have also tried __interrupt() I_Usart_Rx(void);

    Remove this prototype line.


    #17
    cabletie
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2011/07/02 06:21:46
    • Location: 0
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/22 12:36:34 (permalink)
    0

    post EXACTLY what you tried, you have something wrong.

    my therapist has been telling me this for years (it's nice to have a second opinion) and yet
    she wont invite me onto her yacht.
     
    ...here is exactly what I have tried and the respective errors:
    the target is powered by 3.2vdc

    configuration bits set as follows:

    #pragma config FOSC = HS
    #pragma config WDTE = OFF
    #pragma config PWRTE = OFF
    #pragma config MCLRE = ON
    #pragma config CP = OFF
    #pragma config CPD = OFF
    #pragma config BOREN = OFF
    #pragma config IESO = OFF
    #pragma config FCMEN = OFF
    #pragma config LVP = OFF
    #pragma config BOR4V = BOR21V
    #pragma config WRT = OFF

    #include <xc.h>
    #include <math.h>
    #include <pic16f887.h>



    the various permutations of the interrupt declarations I have tried and their respective errors:


    __interrupt() I_Usart(void);

    and

    __interrput(void) I_Usart(void);

    generated IDE error (red ball in line number column):
     unexpected token: (

    next:

    void __interrupt() I_Usart(void);

    run build for debugging.
    XC8: 2 errors generated.
    error: unknown type name 'interrupt'
    error: expected identifier or '('

    next:

    void __interrupt(void) I_Usart_Rx(void);

    build for debugging.
    XC8: 3 errors generated.
    error: __interrupt attribute parameter 'void' is invalid
    note: expanded from macro '__interrupt'
           #define __interrupt(...) __attribute__((picinterrupt(#__VA_ARGS__)))  
    --->with a carrot under the p in picinterrupt
    error: unknown type name 'interrupt'
    error: expected identifier or '('

    next:

    void interrput(void) I_Usart_Rx(void);

    and

    interrupt(void) I_Usart_Rx(void);

    generated IDE error (red ball in line number column):
            unexpected token: (
    and  unexpected token: I_Usart_Rx


    next:

    void interrupt() I_USart_Rx(void);

    and

    interrupt() I_Usart_Rx(void);

    generated IDE error (red ball in line number column):
           unexpected token: (
    and unexpected token: )
    and  unexpected token: I_Usart_Rx
     
    edit: to correct typos.
     
    post edited by cabletie - 2019/04/22 12:39:12
    #18
    cabletie
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2011/07/02 06:21:46
    • Location: 0
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/22 12:46:54 (permalink)
    0
    1and0
    cabletie
    this gives an "unknown type name" error.

    cabletie
    XC8 v2.00.
    C standard: C 99
    Output file format: ELF/DWARF.
     
    I have also tried __interrupt() I_Usart_Rx(void);

    Remove this prototype line.




    didn't help
    XC8 2 errors generated:
    error: unknown type name 'interrupt'
    error: expected indentifier or '('
     
    but then I edited the ISR function header to read

    __interrupt() void I_Usart_Rx(void) {  /* isr code here */ };

    and it compiled.....
     
         WHHHhheeewww!!!
     
    ...holy smokes....  the IDE/Compiler are smart enough to recognize an error but not the solution.... ?
    It would seem in order to see the error they would have to see the solution ... so that they can report the error....
    and if they report the error why not the solution???   ....   anyway....  
    A heartfelt thank you to all whom responded. 
    I sincerely appreciate the talent willing to offer assistance.
    ok  ... well ... back at it....
    Best Regards,
    Cabletie
     
     
    post edited by cabletie - 2019/04/22 13:02:21
    #19
    jack@kksound
    code tags!
    • Total Posts : 3183
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: PIC16F887 and USART Timing 2019/04/22 13:56:00 (permalink)
    0
    What you posted:

    but then I edited the ISR function header to read
    __interrupt() void I_Usart_Rx(void) {  /* isr code here */ };

    and it compiled.....

    does appear to produce an actual ISR function in the resultant code however the syntax I posted (post #12) does produce the proper ISR function in both c90 and c99 modes. XC8 v2.05. Your syntax is definitely NOT as recommended in the XC8 User Guide. 
    This does not occur for me:
    next:

    void __interrupt() I_Usart(void);
    run build for debugging.
    XC8: 2 errors generated.
    error: unknown type name 'interrupt'
    error: expected identifier or '('

    Maybe you forgot it is 2 underscores in your actual code? Or some other typo maybe?
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5