PIC32 Perfect delay using Core Timer Code + Simulation
This post will provide a tutorial of how to use Core Timer for generating precise delay in PIC32. PIC32MX architecture has a core timer having fixed clock, system frequency divided by 2. It has a separate clock (System clock/2) from the peripheral clock. It is also used to generate interrupt and has a highest priority of interrupt in PIC32. It has great importance when using it with RTOS (Real Time Operating System) for scheduling code.
This post will also make you familiar with the usage of built-in Macros in XC32 compiler. In this post I going to toggle 2 pins RB0 and RB4 two times per second. In this post I supposed that you must know following if not then please read them before proceeding further:
You may download code and schematic from the download section at the bottom of this page. I am using MPLAB X IDE, XC32 Compiler. This code is written in C Language and will work on all PIC32 (32bit microcontroller by Microchip) by slightly changing the code according to their features.
I have an external high speed crystal of 8MHz and my system clock is adjusted to 80MHz using PLL multipliers by setting appropriate configuration bits.
As this controller is not available for simulation in Proteus, therefore I go for practical simulation. So this is my custom designed PIC32 I/O board with necessary components on board and has a very small footprint as compare to my pervious DsPIC33 I/O board. I am using PIC32MX460F256L in this particular example. In this PIC32 I/O board I have five LEDs connected with the PortB0 ~ PortB4. A simple animation is shown in the following figure 1. In which I toggle RB0 and RB4 two times in a second.
|Figure 1. LED toggling simulation.|
In the following figure 2. We have an oscilloscope image representing toggling of RB0 pin. In which we have 500ms time division.
|Figure 2. Scope capture.|
As mentioned earlier in this post the code is written in C language using MPLAB X IDE and XC compiler and can be downloaded from the downloads section at the bottom of this page. In the following figure 3. We have a main C file in which I first configure the system to its maximum performance without overclocking my PIC32. The function named “SYSTEMConfig()” is used to do this. This function also changes the wait states of flash, RAM and also enable the Prefetch Cache to optimize performance. But this function also has a warning that do not change peripheral Clock frequency (FPBDIV) it is already set in configuration bits. Then I opened the core timer for rate defined outside the main function (2 times in a second) the function named as “OpenCoreTimer()”. This function has an include header file “Time.h”. This function accepts 32bit value time period as a parameter. Now in the next line I made RB0 and RB4 pins as digital outputs. I used built-in macros. You may use an ordinary method like “TRISBbits.TRISB0 = TRISBbits.TRISB4 = 0” actually both are same. Then I write 1 to RB0 and RB4 by doing this LEDs are turned ON. Now in a while loop I have a wait for core timer to expire. By calling function “mCTGetIntFlag()”. It returns a value of Core timer flag which is set when timer expires. Likewise in other interrupts we have to clear the flags so core timer flag is cleared by calling macro named “mCTClearIntFlag()”. After clearing the flag we must update the core timer again. This is done by passing Rate value as parameter to function named “UpdateCoreTimer()”. Now in the next step I have to toggle the LEDs which is done by calling function named “mPORTBToggleBits”.
|Figure 3. Main C function.|
The schematic with code is also available for download, in the download section. In this schematic some of the basic connections recommended for running this code.
|Figure 4. Schematic PIC32.|
You can download Code (MPLAB X and XC32 compiler Schematic is also given). Click here
For all topics Click here:
That’s all for this post hope you will learn please comment if you have any questions for upcoming posts please subscribe or follow.
All the text and graphics contained on this blog belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission.
© Copyright 2013 IbrahimLabs. All rights reserved.