# Pic32MX Clock too slow

Using pic32mx340F512H and XC32 compiler and ICD3 debugger on MPLAB.

So I'm working on displaying colors on a screen using VGA protocol and, thus, need to max the clock speed. The standard for a pixel clock is 25.175 Mhz, however 20Mhz should work just fine with some timing tweaks.

My issue is: I need an interrupt every 1/20,000,000 seconds (20Mhz), BUT, I've tried everything (or everything I can think of) but I can't get enough speed.

(Maximum about 3 Mhz measured on a pin with a multimeter and a basic while(1) {LATDbits.LATD1 ^= 1}

I first tried with almost every combination, but this "should" normally work (I only wrote the ones that matter):

#pragma config FPLLIDIV = DIV_2
#pragma config FPLLMUL = MUL_20
#pragma config FPLLODIV = DIV_1
#pragma config FNOSC = FRCPLL    // Tried FRCPLL PRIPLL and multiple other possibilities
#pragma config FSOSCEN = ON
#pragma config IESO = OFF
#pragma config POSCMOD = HS      // HighSpeed mode
#pragma config OSCIOFNC = ON
#pragma config FPBDIV = DIV_1    // Pb_Clock is Sys_Clock / 1


And my tests where with SYSTEMConfigPerformance(80000000), I even looked up the code and made myself the function since in my project I can't use plib.h. It behaves exactly like the plib function and even better since SYSTEMConfigPerformance only sets CHECON to a submaximal value.

Tried shadow registers too, but since even with one instruction in the main while loop, I can't get enough speed, i don't think the issue relies on the interrupt or modules but on the sysclock not being fast enough.

To sum up: I need an interrupt every 1/20,000,000 second and an adequate clock speed, but with many attempts and quite some research, I can't get even close to these values. If anyone has an idea of how i could get this working I would be very pleased and thankful to hear it. Thank you :).

• aside from the CPU core throughput and interrupt latency issues making this impractical, have you even considered whether the MCU's peripherals even allow for reliable operation of the output you plan to use at those bandwidths? Jun 5 '17 at 17:06
• Well, regarding if i'm sure whether this is even feasible or not, is the fact that my teacher gave me this task on this pic32MX340F512H. It's hard for me to put it properly, but do you get my point ? Jun 5 '17 at 18:58
• But you are right, I've read everybody's comments and i should definitely look into a different approach. Thank you Jun 5 '17 at 19:07

since the pic32mx340F512H runs at 80 MHz clock rate max., that means you only have four instruction cycles for the CPU to interrupt program flow, jump to the interrupt handler, execute it, and jump back into program flow. That simply won't work. Not even mentioning the fact that I wonder what you'd do with all the four cycles you have in each interrupt if there was no overhead and interrupt latency.

Your application should definitely not be interrupt driven, but simply be a loop that does something useful in 3 CPU cycles, and jumps back to the beginning in 1. I'm not sure if the PIC32 core has a single-cycle jump instruction at all – making this even more unlikely to work, even under perfect, zero-overhead cases

• If I remember correctly the pic32 core has a necessary delay after each jump, so the cycle count for a jump would be at least 2 cycles. Jun 5 '17 at 17:04
• that would mean that the loop would, at most, only have two cycles. Uh-oh. Jun 5 '17 at 17:05
• I need to have multiple if statements and to increment a variable, do you think i should still try to put all of this in the main loop or should i try something else ? If the answer is the latter, would you suggest anything in particular ? Jun 5 '17 at 19:19
• you can't have multiple branching instructions in four cycles. That's not even logically possible. Hence, you can't do this, in any case, on your microcontroller. You will probably have to let go of the idea of creating the complete VGA signal in software. Jun 5 '17 at 19:27
• @RonànChoquert the program you want is not possible to write on this microcontroller. Jun 25 '18 at 5:14

As the other answer suggests you are asking a lot for the mcu's clock speed. You can try a couple things though.

First, use the atomic instructions instead of the read-modify-write you are doing to change the pin:

LATDINV = (1<<1); // invert d1 in single instruction

You could also try setting one of the spi modules to framed mode and using that clock output, which should be continuous and may work for your application. Or try a pwm module.

• I'll try that to make sure i get somewhat close to 20Mhz and will put some code in the main loop as suggested by Marcus, Thank you :) Jun 5 '17 at 19:09