Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to work with two UARTS of PIC18F67K40 in mikroC Pro for PIC compiler simultaneously

kumarr123

Junior Member level 1
Joined
Oct 18, 2023
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
155
Hi,
I am not able to work with two UARTS simultaneously in PIC18F67K40 in mikroC Pro for PIC compiler
If anyone has done something similar it would be nice to see a code snippet.

Kindly looking for a reply,
Thank you.
 
Hi,

First you should describe "why not".

I mean there are five (?) independently working UARTs inside. So, what's the problem?

As always:
* give a link to the datasheet, directly at the manufacturer site
* show your code
* show how you tested it
* tell us what you expect
* show us what is not like expected

Klaus
 
Hello,
1) We have a PIC18F67K40 controller. We are trying to work with its UART1 and UART2 simultaneously. But were are not able to do so.
2) Initializing and accessing (read or write) each uart individually will work.
3) But initializing them together and accessing them together don't work.
4) When talked with Microchip support, they said it was working for them simultaneously. And they send a code snippet. But they were
using MPLAB IDE. Which wont help me.

Kindly looking for a reply,
Thank you.
 

Attachments

  • MultipleUartTest.zip
    944 bytes · Views: 40
Part of the attached code seems to be for a completely different processor. Did you send the wrong file?

If you can talk to the UART from within the program but not communicate externally, have you configured the PPS registers to connect the UART TX and RX to the appropriate pins?

Brian.
 
Hi
I am a beginner I m trying to use 2 UART with a PIC18F67K40 controller and UART Library from mikroC pro for dspic

here is my code and it doesn't work
can anyone help me?
Code:
     int i,j;
    char uart_rd;
    char PitTag[17];
    unsigned int i=0;
    unsigned int j=0;
    unsigned char LF=0x0A;
    AD1PCFG = 0xFFFF;               // Configure all pins as digital I/O
    TRISA.B4=0;
    LATA.B4=1;
    Delay_ms(1500);
    LATA.B4=0;


    Unlock_IOLOCK();
    PPS_Mapping(15, _INPUT,_U1RX);    // Map RP15 as Rx1
    PPS_Mapping(14, _OUTPUT, _U1TX);  // Map RP14 as Tx1
    PPS_Mapping(6, _INPUT,_U2RX);     // Map RP6 as Rx2
    PPS_Mapping(7, _OUTPUT, _U2TX);   // Map RP7 as Tx2
    Lock_IOLOCK();

    UART1_Init(9600);
    Delay_ms(200);                   // Wait for UART module to stabilize
    UART2_Init(9600);
    Delay_ms(200);                   // Wait for UART module to stabilize

    UART_Set_Active (& UART1_Read, & UART1_Write, & UART1_Data_Ready, & UART1_Tx_Idle); // active UART1
    UART1_Write_Text("Hello");
    while(1)
    {
        UART_Set_Active (& UART1_Read, & UART1_Write, & UART1_Data_Ready, & UART1_Tx_Idle); // active UART1
        if(UART1_Data_Ready()) // If data is received
        {
            PitTag = UART1_Read(); // read the received data
            //UART1_Write(PitTag);
            i++;
        }
        if(i==16)
        {
            LATA.B4=1;
            Delay_ms(50);
            LATA.B4=0;
            Delay_ms(50);
            LATA.B4=1;
            Delay_ms(50);
            LATA.B4=0;
            Delay_ms(50);
            UART_Set_Active (& UART2_Read, & UART2_Write, & UART2_Data_Ready, & UART2_Tx_Idle); // active UART2
            do
            {
                if (UART2_Tx_Idle())
                {
                    UART2_Write(PitTag[j]);
                    j++;
                }
            } while (j!=17);
            UART2_Write(LF);
            i=0;
            j=0;
        }
    }

Regards,
Himanshu Kumar
 
I think your problem is because you poll the UART to see if it has received anything and again to see if it is safe to transmit. I don't use MikroC so I can't try your code here, I don't think it runs under Linux.

While polling, the program will miss incoming characters and may be delayed sending them out. Also adding delays will block the UARTs being serviced. A far better strategy is to use interrupts so the UARTs get immediate attention when they need servicing. I can't help you with code for that compiler but I suggest you read it's help to see how to implement interrupts and then write an ISR. It isn't complicated and will make your program far more efficient.

Brian.
 
Quasi simultaneous reception with two or more UARTs can be best performed in interrupt routine.
--- Updated ---

Searching for "mikro-c uart interrupt" gives many hits.
 
Last edited:
PIC18F67K40 controller and UART Library from mikroC pro for dspic
I don't use MikroC and you seem to be relying on their library functions, but please make sure that you are using the right library - looking at your comment above, please be aware that the dsPIC families are 16-bit devices whereas the PIC18 MCUs are 8 bit. They also have very different register names and layouts.
Also please tell us EXACTLY what "doesn't work" - as a problem statement that is useless. What should it do that it doesn't? What does it do that it shouldn't?
Your code does not seem to try to use the 2 UARTs simultaneously. Rather you (try to) read 16 values form UART1 and then write them to UART2.
HOWEVER you are missing the indexing of the 'PitTag' array in your reading code.
(BTW this type of coding is very error prone - if you miss a single character being read from UART1 then whatever you write to UART2 will always be 1 character out. There is no error checking in this code. If the code stream has any sort of structure - e.g. ending wth a CR/LF - then use that to ensure you re-synchronise if something goes wrong.)
I agree with @FvM - for what you seem to be trying ot do, interrupts are the way to go but I suspect that you are not ready for them yet as they do take a bit of understanding of how these PIC MCUs work.
Also you define 'i' and 'j' twice with different types - one is signed and the other unsigned. Not that it matters in the code you have shows as you never get into the number range where this woudl cause a problem.)
Susan
 

LaTeX Commands Quick-Menu:

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top