• AVR Freaks

Hot!PIC18F47Q10 Curiosity and UART

Author
sdow123
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2017/12/17 17:38:24
  • Location: 0
  • Status: offline
2020/05/18 17:04:12 (permalink)
0

PIC18F47Q10 Curiosity and UART

Hello 
 
I want to initialize my UART with MCC. I want to have RX as interruption  and TX as polling .  Can you help me to configure this correctly .
 
Thanks 
#1

11 Replies Related Threads

    katela
    Super Member
    • Total Posts : 1753
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: PIC18F47Q10 Curiosity and UART 2020/05/18 17:10:35 (permalink)

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #2
    sdow123
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2017/12/17 17:38:24
    • Location: 0
    • Status: offline
    Re: PIC18F47Q10 Curiosity and UART 2020/05/18 17:29:20 (permalink)
    0
    i not no how use the interrupt as RX
    #3
    sdow123
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2017/12/17 17:38:24
    • Location: 0
    • Status: offline
    Re: PIC18F47Q10 Curiosity and UART 2020/05/19 05:54:35 (permalink)
    2 (1)
    No body can help me ?
    #4
    RISC
    Super Member
    • Total Posts : 5607
    • Reward points : 0
    • Status: offline
    Re: PIC18F47Q10 Curiosity and UART 2020/05/19 06:11:31 (permalink)
    0
    Hi,
    Go to the "Interrupt module" tab in MCC.
    There you can decide which peripherals are interrupt or not (polling)
    regards
    post edited by RISC - 2020/05/19 12:26:58
    #5
    sdow123
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2017/12/17 17:38:24
    • Location: 0
    • Status: offline
    Re: PIC18F47Q10 Curiosity and UART 2020/05/19 06:22:46 (permalink)
    0
    yes that is ok but witch function do I need to add in my main ...to have operating interrupt RX
    #6
    davekw7x
    Entropy++
    • Total Posts : 1870
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Second star on the right, straight on till morning
    • Status: offline
    Re: PIC18F47Q10 Curiosity and UART 2020/05/19 10:08:07 (permalink)
    0
    sdow123
    yes that is ok but witch function do I need to add in my main ...to have operating interrupt RX


    Jeez, MCC does a lot of stuff, but you have to do a little work yourself.

    MCC has generated low-level application code, and has even indicated how some of its functions are used.
     
    For example, comments in the description of is_rx_ready() in eusart1.h:
     
            while(1)
            {
                // Logic to echo received data
                if(EUSART1_is_rx_ready())
                {
                    rxData = UART1_Read();
                    if(EUSART1_is_tx_ready())
                    {
                        EUSART1_Write(rxData);
                    }
                }
            }

    Now, to use interrupts, you enable them in the MCC Interrupt Module and use the appropriate Interrupt_Enable functions in your main().  The functions themselves have the same names, but MCC generates different code depending on whether you tell MCC to set them up to use interrupts.
     
    For test purposes, to make sure that the interrupt stuff is working. I put in a delay so that you can enter a  bunch of chars while the CPU is doing something else, then let it pick them up later.
     
    My simple test program looks like the following.  Note that I checked the "Redirect STDIO to USART" in the EUSART setup window so that I can use printf.  I also use several other interrupts (not tested in this example) so I enabled all of them.  In general, you only enable the interrupts that your application needs.
     

    // From davekw7x: Suppress those "function never called" warnings for MCC generated stuff
    #pragma warning disable 520
    #include "mcc_generated_files/mcc.h"

    // Note that a "real" application should check for errors.  For test
    // purposes, I'll not do that.
    //
    void main(void)
    {
        // Initialize the device
        SYSTEM_Initialize();
        INTERRUPT_GlobalInterruptHighEnable();
        INTERRUPT_GlobalInterruptLowEnable();
        INTERRUPT_PeripheralInterruptEnable();

        printf("\r\nCompiled on %s at %s by XC8 version %u",
                __DATE__, __TIME__, __XC8_VERSION);
        
    #if __XC8_VERSION >= 2000
    #  ifdef __CLANG__
        printf(" (C99)");
    #  else
        printf(" (C90)");
    #  endif
    #endif
        printf("\r\n");
                
        uint8_t rxdata;
        
        while (1) { // Main loop
            LED1_Toggle();
            while (EUSART1_is_rx_ready()) {
                rxdata = EUSART1_Read();
                while (!EUSART1_is_tx_ready()) // Wait until it can accept a char
                    ;
                EUSART1_Write(rxdata);
            }
            // Type a bunch of chars while it is delaying.  It will pick them up
            // and print them out the next time through the loop
            __delay_ms(2000);
        } // End of Main loop
    } // End of main()

     
    Output when I typed some stuff
     
    Compiled on May 19 2020 at 09:44:27 by XC8 version 2200 (C99)
    this is a test


    Note that if your receive buffer is specified at eight bytes and you type more than eight chars during the delay, some input will be lost.   In a "real" application it is your duty to see that the receiver is serviced more often than the time that would be taken to fill up the buffer.
     
    Tested on a PIC18F27K10 plugged into my Curiosity HPC board
     
    MPLABX version 5.40, XC8 version 2.20, C99 mode Optimization level 2
     
    CPU operated at 64 MHz with internal oscillator. UART1 at 115200 Baud connected through USB-Serial TTL converter to my Windows 10 workstation running TeraTerm.
     
    Regards,

    Dave


     
    post edited by davekw7x - 2020/05/19 10:52:19

    Sometimes I just can't help myself...
    #7
    sdow123
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2017/12/17 17:38:24
    • Location: 0
    • Status: offline
    Re: PIC18F47Q10 Curiosity and UART 2020/05/20 07:18:38 (permalink)
    0
    This is the code generated where is the function interrupt in usart1?
    /**
    Section: Included Files
    */
    #include "eusart1.h"
    /**
    Section: Macro Declarations
    */
    #define EUSART1_TX_ BUFFER_SIZE 8
    #define EUSART1_RX_BUFFER_SIZE 8
    /**
    Section: Global Variables
    */
    int iii =0;
    volatile uint8_t eusart1RxHead = 0;
    volatile uint8_t eusart1RxTail = 0;
    volatile uint8_t eusart1RxBuffer[EUSART1_RX_BUFFER_SIZE];
    volatile eusart1_status_t eusart1RxStatusBuffer[EUSART1_RX_BUFFER_SIZE];
    volatile uint8_t eusart1RxCount;
    volatile eusart1_status_t eusart1RxLastError;
    /**
    Section: EUSART1 APIs
    */
    void (*EUSART1_RxDefaultInterruptHandler)(void);
    void (*EUSART1_FramingErrorHandler)(void);
    void (*EUSART1_OverrunErrorHandler)(void);
    void (*EUSART1_ErrorHandler)(void);
    void EUSART1_DefaultFramingErrorHandler(void);
    void EUSART1_DefaultOverrunErrorHandler(void);
    void EUSART1_DefaultErrorHandler(void);
    void EUSART1_Initialize(void)
    {
    // disable interrupts before changing states
    PIE3bits.RC1IE = 0;
    EUSART1_SetRxInterruptHandler(EUSART1_Receive_ISR);
    // Set the EUSART1 module to the options selected in the user interface.
    // ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled;
    BAUD1CON = 0x08;
    // SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled;
    RC1STA = 0x90;
    // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
    TX1STA = 0x24;
    // SP1BRGL 103;
    SP1BRGL = 0x67;
    // SP1BRGH 0;
    SP1BRGH = 0x00;

    EUSART1_SetFramingErrorHandler(EUSART1_DefaultFramingErrorHandler);
    EUSART1_SetOverrunErrorHandler(EUSART1_DefaultOverrunErrorHandler);
    EUSART1_SetErrorHandler(EUSART1_DefaultErrorHandler);
    eusart1RxLastError.status = 0;

    eusart1RxHead = 0;
    eusart1RxTail = 0;
    eusart1RxCount = 0;
    // enable receive interrupt
    PIE3bits.RC1IE = 1;
    }
    bool EUSART1_is_tx_ready(void)
    {
    return (bool)(PIR3bits.TX1IF && TX1STAbits.TXEN);
    }
    bool EUSART1_is_rx_ready(void)
    {
    return (eusart1RxCount ? true : false);
    }
    bool EUSART1_is_tx_done(void)
    {
    return TX1STAbits.TRMT;
    }
    eusart1_status_t EUSART1_get_last_status(void){
    return eusart1RxLastError;
    }
    uint8_t EUSART1_Read(void)
    {
    uint8_t readValue = 0;

    while(0 == eusart1RxCount)
    {
    }
    eusart1RxLastError = eusart1RxStatusBuffer[eusart1RxTail];
    readValue = eusart1RxBuffer[eusart1RxTail++];
    if(sizeof(eusart1RxBuffer) <= eusart1RxTail)
    {
    eusart1RxTail = 0;
    }
    PIE3bits.RC1IE = 0;
    eusart1RxCount--;
    PIE3bits.RC1IE = 1;
    return readValue;
    }
    void EUSART1_Write(uint8_t txData)
    {
    while(0 == PIR3bits.TX1IF)
    {
    }
    TX1REG = txData; // Write the data byte to the USART.
    }
     
    void EUSART1_Receive_ISR(void)
    {

    eusart1RxStatusBuffer[eusart1RxHead].status = 0;
    if(RC1STAbits.FERR){
    eusart1RxStatusBuffer[eusart1RxHead].ferr = 1;
    EUSART1_FramingErrorHandler();
    }
    if(RC1STAbits.OERR){
    eusart1RxStatusBuffer[eusart1RxHead].oerr = 1;
    EUSART1_OverrunErrorHandler();
    }

    if(eusart1RxStatusBuffer[eusart1RxHead].status){
    EUSART1_ErrorHandler();
    } else {
    EUSART1_RxDataHandler();
    }

    // or set custom function using EUSART1_SetRxInterruptHandler()
    }
    void EUSART1_RxDataHandler(void){
    // use this default receive interrupt handler code
    eusart1RxBuffer[eusart1RxHead++] = RC1REG;
    if(sizeof(eusart1RxBuffer) <= eusart1RxHead)
    {
    eusart1RxHead = 0;
    }
    eusart1RxCount++;
    }
    void EUSART1_DefaultFramingErrorHandler(void){}
    void EUSART1_DefaultOverrunErrorHandler(void){
    // EUSART1 error - restart
    RC1STAbits.CREN = 0;
    RC1STAbits.CREN = 1;
    }
    void EUSART1_DefaultErrorHandler(void){
    EUSART1_RxDataHandler();
    }
    void EUSART1_SetFramingErrorHandler(void (* interruptHandler)(void)){
    EUSART1_FramingErrorHandler = interruptHandler;
    }
    void EUSART1_SetOverrunErrorHandler(void (* interruptHandler)(void)){
    EUSART1_OverrunErrorHandler = interruptHandler;
    }
    void EUSART1_SetErrorHandler(void (* interruptHandler)(void)){
    EUSART1_ErrorHandler = interruptHandler;
    }

    void EUSART1_SetRxInterruptHandler(void (* interruptHandler)(void)){
    EUSART1_RxDefaultInterruptHandler = interruptHandler;





    }
    #8
    davekw7x
    Entropy++
    • Total Posts : 1870
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Second star on the right, straight on till morning
    • Status: offline
    Re: PIC18F47Q10 Curiosity and UART 2020/05/20 07:51:56 (permalink)
    5 (2)
    sdow123
    ...where is...

    OK.  It is time for you to take charge of your own investigation.  There are a total of five sets of .c and .h files the mcc-generated files directory of my simple project.  You can look at them in MPLABX, but I'll show them as they appear on my computer.
     
     Directory of <path to my mplabx projects directory>\27Q10_MCC_Forum.X\mcc_generated_files

    05/19/2020  09:18 AM    <DIR>          .
    05/19/2020  09:18 AM    <DIR>          ..
    05/19/2020  09:18 AM             6,099 device_config.c
    05/19/2020  09:18 AM             2,088 device_config.h
    05/19/2020  09:18 AM             7,794 eusart1.c
    05/19/2020  09:18 AM            12,289 eusart1.h
    05/19/2020  09:18 AM             2,871 interrupt_manager.c
    05/19/2020  09:18 AM             3,936 interrupt_manager.h
    05/19/2020  09:18 AM             3,533 mcc.c
    05/19/2020  09:18 AM             2,951 mcc.h
    05/19/2020  09:18 AM             2,977 pin_manager.c
    05/19/2020  09:18 AM             6,488 pin_manager.h
     
    Look in each of them to see what the heck MCC has done for you.  Several of the header files explain the use of some of the functions in the source files.
     
    First suggestion: If I wanted to see what's happening with interrupts, I would start by looking in interrupt_manager.c
     
    Then, if my main interest were UART interrupt functionality, I would look at the specific functions in eusart.c that are called by the ISR (in the interrupt manager).  You will have to understand the UART initialization function to see how the actual interrupt-related functions are put into play.
     
    Now, I know that even some fairly experienced C programmers are sometimes confused by the chain of events that respond to, say, a UART receive interrupt, but just follow the yellow brick road...
     
    Then, I would go back to the example test program posted in response to your initial question to see if I understood use of the MCC functions to receive and transmit a character.
     
    If there were things I didn't understand, I would refer to the data sheet and, possibly the XC8 User's Manual for edification.  If there were still things I didn't understand, I would ask specific questions on the forum.
     
    Bottom line: I feel that an hour or so spent investigating the actual behavior (from the bottom up) will be worth it.
     
    Regards,

    Dave
    post edited by davekw7x - 2020/05/20 07:56:23

    Sometimes I just can't help myself...
    #9
    sdow123
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2017/12/17 17:38:24
    • Location: 0
    • Status: offline
    Re: PIC18F47Q10 Curiosity and UART 2020/05/23 05:31:34 (permalink)
    0
    Hello
    Yes i have these files in MPLAB X . But how i can validate that interrupt work good ?
     
    #10
    davekw7x
    Entropy++
    • Total Posts : 1870
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Second star on the right, straight on till morning
    • Status: offline
    Re: PIC18F47Q10 Curiosity and UART 2020/05/23 10:54:25 (permalink)
    5 (2)
    sdow123
    Hello
    Yes i have these files in MPLAB X . But how i can validate that interrupt work good ?

    Before I write a single line of code in a new project or when I am implementing new functionality, I figure out how I am going to test it.  Personally I simply never make a test plan that requires a debugger and break points and inspecting variables with the IDE.  Some people feel differently. (Chacun à son goût!)
     
    Anyhow...
    I told you how I tested it in my first post:
    davekw7x
    For test purposes, to make sure that the interrupt stuff is working. I put in a delay so that you can enter a  bunch of chars while the CPU is doing something else, then let it pick them up later.

     
    And the comment at the end of the Main Loop in the test program:
            // Type a bunch of chars while it is delaying.  It will pick them up
            // and print them out the next time through the loop
            __delay_ms(2000);
        } // End of Main loop
     

     
    If you type more than two chars during the time it is delaying and interrupts aren't working, some input will be lost.
     
     
    Regards,

    Dave
    post edited by davekw7x - 2020/05/23 11:30:58

    Sometimes I just can't help myself...
    #11
    dan1138
    Super Member
    • Total Posts : 3496
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: PIC18F47Q10 Curiosity and UART 2020/05/23 16:48:57 (permalink)
    0
    @sdow123,

    I used MCC to generate a simple UART project as you described but I do not have any PIC18F47K10 controllers around.

    This is the implementation I came up with:
    /*
     * Date: 2020-05-23
     * Project: 18F47Q10_UART
     *
     * Description:
     *
     *  MPLAB Code Configurator (MCC) project that implements
     *  an Interrupt Service Routine (ISR) for UART receive
     *  interrupts and a polled transmit data output function
     *  at 9600 baud.
     *
     *  This may only work in real hardware because the MPLABX
     *  simulation tool is really kind of lame.
     *
     */
    #define VERSION_STRING "18F47Q10 UART Test v1.0\r\n"

    #include "mcc_generated_files/mcc.h"

    /*
     * Send buffer out the UART
     */
    void DumpBuffer(void *Buffer, uint8_t Length)
    {
        uint8_t *Buf = (uint8_t *)Buffer;
        
        if(Buf && Length) do
        {
            EUSART1_Write(*Buf++);
        } while(--Length);
        while(!EUSART1_is_tx_done());
    }

    /*
                             Main application
     */
    void main(void)
    {
        uint8_t UART_InputBuffer[80];
        uint8_t Index;
        uint8_t Count;
        
        SYSTEM_Initialize();

        INTERRUPT_GlobalInterruptEnable();

        INTERRUPT_PeripheralInterruptEnable();

        DumpBuffer(VERSION_STRING, sizeof(VERSION_STRING)-1);
        while (1)
        {
            Index = 0;
            Count = 0;
            while(EUSART1_is_rx_ready())
            {
                UART_InputBuffer[Index++] = EUSART1_Read();
                Count++;
                if(Index >= sizeof(UART_InputBuffer))
                {
                    break;
                }
            }
            if(Count)
            {
                if(EUSART1_is_tx_ready())
                {
                    DumpBuffer(UART_InputBuffer,Count);
                }
            }
        }
        /*
         * These function calls suppress annoying compiler warnings
         * that are a result of MCC generated code that is not used.
         */
        EUSART1_get_last_status();
        PIN_MANAGER_IOC();
    }


    I could not test that this code actually works as the MPLABX simulator seems broken and does not show the UART1 output for a simulation.

    I did create the same project with MPLABX v4.05 and that simulation did output what was expected.

    So give this code a spin on your hardware and see if it does what you want.
    post edited by dan1138 - 2020/05/24 20:05:11
    #12
    Jump to:
    © 2020 APG vNext Commercial Version 4.5