Peripheral Bus and UART

General discussion on mikroC PRO for PIC32.
Post Reply
Author
Message
Bill Legge
Posts: 235
Joined: 28 Oct 2007 03:16
Location: West Australia

Peripheral Bus and UART

#1 Post by Bill Legge » 08 May 2023 10:38

I need to set the peripheral bus to DIV8 and thus need to use UART2_Init_Advanced to get the correct BAUD rate. It does not work.
My rig is:
1. PIC32MX795F512L with 8MHz Xtal on the carrier.
2. Config bits set so that Fosc is DIV2 TIMES 20 = 80MHz.
3. Edit project set so that PB is Fosc/8 = 10MHz = 10,000kHz.
4. Board is EasyPIC Fusion v7
5. Compiler is the latest REv 4.0.0
Code below does work OK but the frequency should be 10000 - and that fails:

Code: Select all

////////////////////////////////////////////////////////////////////////////////
// Project:         WVL_Fusion_Morse_PBTest                                   //
// File:            Main.c                                                    //
// Function:        Check RS232 and PB                                        //
// MCU:             P32MX795F512L with 8MHz on-board Xtal                     //
// Board:           EasyPIC_Fusion_v7_for_PIC32                               //
// Power            7-12V input. 3.3V                                         //
// Compiler:        mikroC PRO for PIC32 V4.0                                 //
// Oscillator:      8Mhz Xtal, Div2. PLLX20 = 80MHz                           //
// Programmer:      On-board Mikro using PGEC2 and PGED2 on RB6 and RB7       //
// Author:          WVL                                                       //
// Date:            Apr 2023                                                  //
////////////////////////////////////////////////////////////////////////////////

// Edit Project sets peripheral clock division to div8 so PB = 80/8 = 10MHz

////////////////////////////////////////////////////////////////////////////////
// Timer1 with PB set to div8 = 80/8 = 10MHz. T = 0.1uS                       //
// Prescaler 1:1; PR1 Preload = 10000; Actual Interrupt Time = 1 ms           //
void InitTimer1(){                                                            //
    T1CON	  = 0x8000;                                                       //
    TCS_bit   = 0;  // use PB = 80/10 = 10MHz                                 //
    T1IP0_bit = 1;                                                            //
    T1IP1_bit = 1;                                                            //
    T1IP2_bit = 1;                                                            //
    T1IF_bit  = 0;                                                            //
    T1IE_bit  = 1;                                                            //
    PR1		  = 10000;                                                        //
    TMR1	  = 0;                                                            //
}                                                                             //
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// TIMES1 interrupt every 1[mS]                                               //
void Timer1Interrupt() iv IVT_TIMER_1 ilevel 7 ics ICS_SRS {                  //
  T1IF_bit	 = 0;                                                             //
  LATA0_bit = ~LATA0_bit;                                                     //
}                                                                             //
////////////////////////////////////////////////////////////////////////////////

void main(){
    // Init MCU
    AD1PCFG = 0xFFFF;                   // configure all pins digital
    JTAGEN_bit = 0;                     // disable JTAG
    //UART2_Init(57600);                // initialize UART module on RF4 and RF5, USB UART A
    // Use advanced init for UART2 - frequency[kHz] should be 10,000 but that does not work???
    UART2_Init_Advanced(57600,80000,_UART_HIGH_SPEED,_UART_8BIT_NOPARITY,_UART_ONE_STOPBIT);
    Delay_ms(100);                      // wait for UART module to stabilize
    UART2_Write_Text("\r\nWVL_Fusion_Morse_Rx\r\n");
    // Init pins
    TRISA0_bit = 0;                     // LED blink by TIMER 1
    TRISA1_bit = 0;                     // LED blink by dalay_mS
    // Init TIMER 1
    InitTimer1();          // call function to set up timer1
    EnableInterrupts();
    
    while(1){
        delay_ms(20);
        LATA1_bit = ~LATA1_bit;
        UART2_Write_Text("WVL_Fusion_Morse_Rx\r\n");
    }
}
I've checked TIMER1 (driven by the PB at 10MHz) runs OK and the delay_ms(20) is correct.
What am I doing wrong?

Regards Bill Legge in Australia

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Peripheral Bus and UART

#2 Post by filip » 31 May 2023 07:38

Hi,

I apologize for the late reply, have you been able to sort out this issue ?

Regards,
Filip.

Bill Legge
Posts: 235
Joined: 28 Oct 2007 03:16
Location: West Australia

Re: Peripheral Bus and UART

#3 Post by Bill Legge » 31 May 2023 09:12

No solution yet.
The help notes about UARTxInit_Advanced says to use the frequency of the Peripheral Bus.
However, to get it to work I have to use the MCU oscillator frequency. For example:
Fosc = 80MHz, PB is set to divide by 8 so PB frequency is = 10,000kHz so the code should be:

UART2_Init_Advanced(57600,10000,_UART_HIGH_SPEED,_UART_8BIT_NOPARITY,_UART_ONE_STOPBIT);

But this does not work. This does:

UART2_Init_Advanced(57600,80000,_UART_HIGH_SPEED,_UART_8BIT_NOPARITY,_UART_ONE_STOPBIT);

Is this just an error in the help file or am I getting something wrong?

Regards Bill Legge in Australia

Post Reply

Return to “mikroC PRO for PIC32 General”