How to make millisecond and microsecond delays
Ever wanted to make an LED toggle on and off every second? It's the dream of every hobbyist, especially when we're dealing with the PIC32MZ. A sign of success, that things are working and clock speeds are right. Unfortunately, the XC32 compiler doesn't give us a delay function to call. A quick google of "PIC32 millisecond timer" brings back countless results relating to other PIC devices, not PIC32, and people saying we should be using Harmony. There are also plenty of results relating to using the Timer peripherals and the calculations look complex and really aren't what we want for this simple task. Oh, and "read the data sheet" of course.
Instead, let's do it the easy way. There's a built-in timer in every PIC32 called the Core Timer. It doesn't need to be set up and it is always running. It runs at half the system frequency, so if you're running at 200Mhz, it'll update at 100Mhz. First, let's add a define for how fast we've set the fuses to.
#define SYS_FREQ 200000000 // Running at 200MHz
This value will be useful for many calculations we'll need to do later. OK, so now how do we use the Core Timer? Well, first we need to calculate how long we need to wait.
TL;DR
In our example, the core timer updates at 100Mhz. Let's say we want to wait for 500 microseconds. First, let's calculate how many clock ticks that is. One clock tick takes 1 / 100,000,000 seconds, which is 0.000001 seconds, which is 0.01 microseconds. So this means that for every 100 clock ticks 1 microsecond has passed. So, for 500 microseconds we need to wait 500 * 100 = 50,000 clock ticks.
Still with me? Good :) Let's put that knowledge to use and make a microsecond delay function:
void delay_us(unsigned int us)
{
// Convert microseconds us into how many clock ticks it will take
us *= SYS_FREQ / 1000000 / 2; // Core Timer updates every 2 ticks
_CP0_SET_COUNT(0); // Set Core Timer count to 0
while (us > _CP0_GET_COUNT()); // Wait until Core Timer count reaches the number we calculated earlier
}
First we calculate how many clock ticks we need to wait. Then we set the Core Timer counter to 0 by calling
CP0_SET_COUNT(0)
TL;DR
CP0_SET_COUNT(value) is a built-in way to set the Core Timer's value. For the fancy pantses among us, yes, you can use:
_mtc0(9, 0, 0); // Register 9 is the Count register, set it to 0
But it comes down to the same thing, so take your pick.
Then we just need to wait until the Core Timer counter reaches the value we set and the delay will be done. Simple! Let's use that to make a millisecond delay now.
void delay_ms(int ms)
{
delay_us(ms * 1000);
}
Is that cheating? Maybe? Off by a few clock cycles? Probably. As the audience for this blog is going to be tiny (or non-existent) anyway I don't think it matters :) Oh, one more thing. The Core Timer is a 32-bit number, which means it can only count to 4 billion before it'll wrap back around to 0. So don't use delay_ms() for hundreds of seconds. At least up to 20 seconds should be fine. Now let's make that LED dance!
while (1)
{
LATAINV = 1 << 0; // Toggle LAT A bit 0
delay_ms(1000); // Delay 1 second
}
LATAINV is an easy way to toggle bits on Port A and requires no work on our part. Hope the TL;DR tags help :)
21 comments
colle
hi, I'm learning to program the pic32MZ microcontroller series without harmony and your blog helped me to start. do you have plans to continue on this?
Aidan Author
Hi there, I didn't expect anyone to actually read this stuff, glad it's been useful.
I'll try to stop putting it off and get back to updating the blog today, thanks for dropping by.
Aidan Author
Trying to update the blog but having issues with the blog software. It should be fixed by Monday.
colle
yess!!! many thanks!! i'll keep my eyes here!
Malte
Hi Aidan,
there is a small calculation Error:
// Convert microseconds us into how many clock ticks it will take
us *= SYS_FREQ / 1000000 / 2; // Core Timer updates every 2 ticks
with some clinchs you get an correct answer:
us *= (SYS_FREQ / 1000000) / 2; // Core Timer updates every 2 ticks
And I got another question for this rows
_mtc0(9, 0, 0); // Register 9 is the Count register, set it to 0
here you are taking register 9. In your Code you are taking register 16. Which one is correct or where are the differences? Do you got any documentaion from Microchip about the Core Timer? Didn´t found something.
Greetings
Malte
Aidan Author
Hi Malte,
9 is the register for the core timer. I use _CP0_SET_COUNT(0) instead of _mtc0(9, 0, 0), but it's the same thing.
Register 16 has to do with the cache and it's in the set_performance_mode() function.
All this information comes from the PIC32 Family Reference Manual section 2.12: Coprocessor 0 (CP0) registers if you want to look it up.
Thanks for fixing my delay function :)
Nic
Hey Aidan,
I'm looking forward to the completion of your evaluation board. I do have the explorer1632 very expensive tool.
I'm starting to feel like a one legged man in a kicking contest.
I have two programs in MPLAB X IDE v5.10 Source Files. If I compile them one at a time, compiles with no error.
When I compile both of them I get errors. I know more then two files can be in the Project source file.
One legged man, guessing, maybe because of the Set up caching. The datasheet does tell me about the registers (3.7)
but no format or maybe it's in another section. If I'm right here's the two cach set ups;
Set up caching
cp0 = _mfc0(16, 0);
cp0 &= ~0x07;
cp0 |= 0b011; // K0 = Cacheable, non-coherent, write-back, write allocate
_mtc0(16, 0, cp0);
Lock Sequence
SYSKEY = 0x33333333;
asm volatile("ei"); // Enable all interrupts
The second set up caching
// ******************************************************************************
// CPU Performance Optimization:
// ******************************************************************************
register unsigned long tmp_cache; //KSEG0 cache enable
asm("mfc0 %0,$16,0" : "=r"(tmp_cache));
tmp_cache = (tmp_cache & ~7) | 3;
asm("mtc0 %0,$16,0" :: "r" (tmp_cache));
Aidan Author
Hi Nic,
Do you have a pickit or another programmer too?
Those two pieces of code you sent me seem to do exactly the same thing it's just one is in C and one is in assembler. They are for setting up the cache, yes.
What errors do you get?
Nic
Good Day Aidan,
For the pickit, I'm using Start Kit (PKOB) it's build on explorer1632. I have Pick Kit 3 and 4.
The compiler errors are coming from here I think. These pragma's are in the first file in the source tree;
//----------------------------------------------------------
// BF1SEQ0
//----------------------------------------------------------
#pragma config TSEQ = 0x0000
#pragma config CSEQ = 0xFFFF
Here's the error
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
c:\program files (x86)\microchip\xc32\v2.10\bin\bin\gcc\pic32mx\4.8.3\..\..\..\..\bin/pic32m-ld.exe: dist/default/production/32LED.X.production.elf section `.config_BFC0FFC0' will not fit in region `config_BFC0FFC0'
this error repeats with
config_BFC0FFC4'
config_BFC0FFC8'
config_BFC0FFCC'
config_BFC0FFDC'
c:\program files (x86)\microchip\xc32\v2.10\bin\bin\gcc\pic32mx\4.8.3\..\..\..\..\bin/pic32m-ld.exe: Link terminated due to previous error(s).
collect2.exe: error: ld returned 255 exit status
Aidan Author
Hi Nic,
Just off to bed here but at a guess I'd say you might have some duplicate lines with pragma config in them. Is that using Harmony? If it is, run the Harmony configurator and generate the code again, that sometimes fixes weirdness.
If not, maybe choose a different configuration, hit Apply and then go back to your original configuration and hit apply again.
That's all I can think of offhand, I'll look into it more tomorrow.
Nic
Good Morning Aidan,
I put all my pragma's in my HEADER files on my project tree. So maybe that is the problem. Consequently, I must not understand the project tree. I was thinking the header file would write the pragmas and save those pragma to the pic for each source files???
Aidan, I have this well DOCUMENTED skeleton program of an ADC . It was on the internet. I would like to share it with you. It's long so tell me what you think.
Aidan Author
Hey Nic, sure, share it with me and I'll see what I can do to help.
Nic
Hi, Aidan
// *********************************************************************************************
// PIC32MZ Family 12.5msps Interleaved ADC sample code example
//
// SUMMARY:
// TARGET CPU: PIC32MZxxEFxx >= 100 pin
// OSC INPUT TYPE = 24 MHz Clock oscillator (EC Mode)
// SYSCLK=200 MHz
// PBCLK = 100 MHz
// ADC TAD = SYSCLK / 4 = 50 MHz = 20ns
// ADC SAMC=3 TAD
// Interleaved ADC SAMPLE RATE = 12.5msps(max)
// INTERLEAVED ADCS
// ADC 0: 12bit mode, ADCTRG1<trgsrc0> Output Compare 1
// ADC 1: 12bit mode, ADCTRG1<trgsrc1> Output Compare 3
// ADC 2: 12bit mode, ADCTRG1<trgsrc2> Output Compare 5
// ADC 3: 12bit mode, ADCTRG1<trgsrc3> Timer3
//
// interleaved ADC0/1/2/3 with AN0/AN1/AN2/AN3 inputs respectively
// (12.5 msps combined throughput rate)
//
// USER NOTE:
// Although the number of interleaved ADC's the user wishes to use is selectable, it is required
// "FOR THIS CODE ONLY" that those ADCx modules be sequential starting from AN0 to the last sequential
// ANx to be used for interleaving. In different code than below, any combination of interleaved ADC are allowed
//
// NOTE: (FYI only)
// A user could configure the PIC32MZ to have multiple sets of
// interleaved ADC's to measure multiple different analog input streams like:
// ----------------------------- EXAMPLE 1 CONFIGURATION -----------------------------
// 1) AN0-AN1 pair with triggers OC5 and TMR3 @ 6.25msps(max)
// 2) AN2-AN3 pair with triggers OC1 and TMR5 @ 6.25msps(max)
// ----------------------------- EXAMPLE 2 CONFIGURATION -----------------------------
// 3) ADC0-ADC2 (3) ADCs with triggers OC1, OC3 and TMR5 @ 8.333333 msps(max) combined
// 4) ADC3-ADC4 (2) ADCs with triggers OC5 and TMR3 @ 6.25msps(max)
// ----------------------------- EXAMPLE 3 CONFIGURATION -----------------------------
// 5) ADC0-ADC3 (4) ADCs with triggers OC1, OC3, OC5 and TMR3 @ 12.5msps(max) combined
// *********************************************************************************************
//
// *********************************************************************************************
// USER NOTE:
// Although the number of interleaved ADC's the user wishes to use is selectable in this code
// example, it is required "FOR THIS CODE ONLY" that those ADCx modules be sequential starting
// from AN0 to the last sequential ANx to be used for interleaving.
// In a user’s own original code however they can define any combination
// of ADC's they prefer.
//
// ****************************************************************************
// F I L E I N C L U D E S
// ****************************************************************************
#include <xc.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys attribs.h="">
// ****************************************************************************
// C O N F I G U R A T I O N W O R D S
// ****************************************************************************
//
//----------------------------------------------------------
// DEVCFG0
//----------------------------------------------------------
#pragma config DEBUG = OFF
#pragma config JTAGEN = OFF
#pragma config ICESEL = ICS_PGx2
#pragma config TRCEN = OFF
#pragma config BOOTISA = MIPS32
#pragma config FECCCON = OFF_UNLOCKED
#pragma config FSLEEP = OFF
#pragma config DBGPER = PG_ALL
#pragma config SMCLR = MCLR_NORM
#pragma config SOSCGAIN = GAIN_2X
#pragma config SOSCBOOST = ON
#pragma config POSCGAIN = GAIN_2X
#pragma config POSCBOOST = ON
#pragma config EJTAGBEN = NORMAL
#pragma config CP = OFF
//----------------------------------------------------------
// DEVCFG1
//----------------------------------------------------------
#pragma config FNOSC = SPLL
#pragma config DMTINTV = WIN_127_128
#pragma config FSOSCEN = OFF
#pragma config IESO = OFF
#pragma config POSCMOD = EC //24 Mhz External clock osc
#pragma config OSCIOFNC = OFF
#pragma config FCKSM = CSECME
#pragma config WDTPS = PS1048576
#pragma config WDTSPGM = STOP
#pragma config FWDTEN = OFF
#pragma config WINDIS = NORMAL
#pragma config FWDTWINSZ = WINSZ_25
#pragma config DMTCNT = DMT31
#pragma config FDMTEN = OFF
//----------------------------------------------------------
// DEVCFG2
// EC Ext OSC in =24Mhz
// SYSCLK=200Mhz, PBxCLK=100Mhz
//----------------------------------------------------------
#pragma config FPLLIDIV = DIV_3
#pragma config FPLLRNG = RANGE_5_10_MHZ
#pragma config FPLLICLK = PLL_POSC
#pragma config FPLLMULT = MUL_50
#pragma config FPLLODIV = DIV_2
#pragma config UPLLFSEL = FREQ_24MHZ
//----------------------------------------------------------
// DEVCFG3
//----------------------------------------------------------
#pragma config USERID = 0x0ADC
#pragma config FMIIEN = OFF
#pragma config FETHIO = OFF
#pragma config PGL1WAY = OFF
#pragma config PMDL1WAY = OFF
#pragma config IOL1WAY = OFF
#pragma config FUSBIDIO = OFF
//----------------------------------------------------------
// BF1SEQ0
//----------------------------------------------------------
#pragma config TSEQ = 0x0000
#pragma config CSEQ = 0xFFFF
// *************************************************************************************************
// U S E R D E F I N E S
// Note: User must configure as required
// *************************************************************************************************
#define TAD_TRIGGER_SOURCE_SPACING 4 // TAD trigger spacing value from ADC Table 13 or 15 accordingly w/12bit & 4 interleaved ADCs
#define INTERLEAVED_ADC_COUNT 4 // Number of interleaved ADC (Cannot be <2 or >4). Changing this value will
// automatically scale the number of interleaved ADC and triggers utilized in this code.
#define ADC_DMA_BUFF_SIZE 2048 //ADC DMA buffer size
//
// *************************************************************************************************
// EQUATION #1: (Assumptions: INTERLEAVED_ADC_COUNT=4)
// (Fastest Triggering possible)
// TAD_TRIGGER_SOURCE_SPACING(min) =
// = ((SAMC(min) + (((#bits Resolution+1) + ADJ) * TAD)) / INTERLEAVED_ADC_COUNT)
// = (3+12+1) + ADJ) * TAD)) / 4 ADCs)
// = (16 + ADJ) * TAD)) / 4 ADCs
// = (16 + 0) * TAD)) / 4 ADCs
// = 4.0 TAD (minimum trigger spacing possible)
//
// NOTE: "ADJ" term = A user selected whole number adjustment factor between 0-4
// that must yield a Minimum TAD Trigger interval that equates to an exact
// multiple of a ½ TAD. This represents the minimum TAD_TRIGGER_SOURCE_SPACING
// and therefore the fastest throughput possible. Additional TAD_TRIGGER_SOURCE_SPACING
// and hence ADC Throughput Rates are possible in ½ TAD trigger spacing increments.
// (See examples 1-3 below)
//
// NOTE: Once the minimum, TAD trigger spacing possible is determined, the user can configure for any
// additional sampling frequency required if needed in 0.5 TAD trigger spacing increments.
//
// Example 1: (INTERLEAVED_ADC_COUNT=4)
// TAD_TRIGGER_SOURCE_SPACING(min) = 4
// interleaved ADC Throughput rate = TAD clock freq / TAD_TRIGGER_SOURCE_SPACING
// = 50Mhz / 4.0(min)
// = 12.5msps
//
// Example 2: (INTERLEAVED_ADC_COUNT=4)
// TAD_TRIGGER_SOURCE_SPACING = 4.5
// interleaved ADC Throughput rate = TAD clock freq / TAD_TRIGGER_SOURCE_SPACING
// = 50Mhz / 4.5
// = 11.111111msps
//
// Example 3: (INTERLEAVED_ADC_COUNT=4)
// TAD_TRIGGER_SOURCE_SPACING = 5.0
// interleaved ADC Throughput rate = TAD clock freq / TAD_TRIGGER_SOURCE_SPACING
// = 50Mhz / 5.0
// = 10msps
//
// *************************************************************************************************
//
// *************************************************************************************************
// P R O G R A M D E F I N E S
// (User Do Not Change)
// Note:
// TAD_TRIGGER_SOURCE_SPACING is multiplied by (2) because
// OCx and TMR3 trigger source time base is 2x faster than an ADC TAD clock.
// 2 TMR3 clocks = 1 ADC TAD Clock
// **************************************************************************************************
// ------------------------------------------------------------------------------------------------------------------
// NOTE: Remember that the peripheral trigger clock frequency is 2x TAD clock frequency.
// This is why the 2x. The -1 is because the Si peripheral triggers are always
// on (match+1), so to compensate for correct timing subtract 1.
//
#define ADC_TRIG (TAD_TRIGGER_SOURCE_SPACING * 2) //
#define OC1_ADC_TRIG (ADC_TRIG - 1) //
#define OC3_ADC_TRIG ((2 * ADC_TRIG) - 1) //
#define OC5_ADC_TRIG ((3 * ADC_TRIG) - 1) //
#define T3_ADC_TRIG ((INTERLEAVED_ADC_COUNT * ADC_TRIG) - 1) //
#define ADC0_IRQ 59 //PIC32MZxxEFxx ADC0 Data_0 IRQ number
#define ADC1_IRQ 60 //PIC32MZxxEFxx ADC1 Data_1 IRQ number
#define ADC2_IRQ 61 //PIC32MZxxEFxx ADC2 Data_2 IRQ number
#define ADC3_IRQ 62 //PIC32MZxxEFxx ADC3 Data_3 IRQ number
#define VirtAddr_TO_PhysAddr(v) ((unsigned long)(v) & 0x1FFFFFFF)
#if ((INTERLEAVED_ADC_COUNT < 2) | (INTERLEAVED_ADC_COUNT > 4))
#error INTERLEAVED_ADC_COUNT error, it is either less than 2 or greater than 4
#endif
// *************************************************************************************************
// F U N C T I O N P R O T O T Y P E S
// *************************************************************************************************
void init_ADC(void);
void init_DMA(void);
void init_TMR3(void);
Nic
Here's the other half. It would not all go in one step.
// *************************************************************************************************
// G L O B A L V A R I A B L E S
// *************************************************************************************************
uint32_t __attribute__((coherent)) adc_bufA[INTERLEAVED_ADC_COUNT * ADC_DMA_BUFF_SIZE];
uint32_t __attribute__((coherent)) adc_bufB[INTERLEAVED_ADC_COUNT * ADC_DMA_BUFF_SIZE];
uint32_t adc_buf_index = 0;
bool adc_dma_buf_full_flg = 0;
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// $ M A I N R O U T I N E $
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
int main(void)
{
// ******************************************************************************
// CPU Performance Optimization:
// ******************************************************************************
register unsigned long tmp_cache; //KSEG0 cache enable
asm("mfc0 %0,$16,0" : "=r"(tmp_cache));
tmp_cache = (tmp_cache & ~7) | 3;
asm("mtc0 %0,$16,0" :: "r" (tmp_cache));
PRECONbits.PFMWS=2; //Flash wait states = 2 CPU clock cycles @ 200Mhz
PRECONbits.PREFEN = 2; //Enable predictive prefetch for CPU instructions and CPU data
PRISSbits.PRI7SS = 7; //DMA Interrupt with priority level of 7 uses Shadow Set 7
PRISSbits.PRI6SS = 6; //Interrupt with priority level of 6 uses Shadow Set 6
PRISSbits.PRI5SS = 5; //Interrupt with priority level of 5 uses Shadow Set 5
PRISSbits.PRI4SS = 4; //Interrupt with priority level of 4 uses Shadow Set 4
PRISSbits.PRI3SS = 3; //Interrupt with priority level of 3 uses Shadow Set 3
PRISSbits.PRI2SS = 2; //Interrupt with priority level of 2 uses Shadow Set 2
INTCONbits.MVEC = 1; //Enable multi-vector interrupts
__builtin_mtc0(12,0,(__builtin_mfc0(12,0) | 0x0001)); // Global Interrupt Enable
// ******************************************************************************
// * Disable all ANx pins that are enabled by default on reset
// ******************************************************************************
ANSELACLR = 0xFFFF; //Disable all analog ANx input pins
ANSELBCLR = 0xFFFF; //Disable all analog ANx input pins
ANSELCCLR = 0xFFFF; //Disable all analog ANx input pins
ANSELDCLR = 0xFFFF; //Disable all analog ANx input pins
ANSELECLR = 0xFFFF; //Disable all analog ANx input pins
ANSELFCLR = 0xFFFF; //Disable all analog ANx input pins
ANSELGCLR = 0xFFFF; //Disable all analog ANx input pins
ANSELHCLR = 0xFFFF; //Disable all analog ANx input pins
ANSELJCLR = 0xFFFF; //Disable all analog ANx input pins
// ******************************************************************************
// Initialize and enable ADC(s) followed by ADC trigger sources 2nd
// ******************************************************************************
init_ADC(); //Initialize and enable interleaved ADCs first before PWM ADC triggers are enabled
while(1)
{
if(adc_dma_buf_full_flg) //If current DMA buffer is full
{
adc_dma_buf_full_flg = 0; //Clr DMA buff full flag
//***********************************************************************
// In main(), "adc_buf_index" represents the current active buffer being
// filled as the companion buffer is already full.
//***********************************************************************
if(adc_buf_index == 0) //if "adc_bufA" active then "adc_bufB" full
{
//-----------------------------------------------------------------------------------------------------------------------
// User must process here previously filled ADC DMA Buffer B ptr starting at "adc_bufB"
//-----------------------------------------------------------------------------------------------------------------------
}
else //if "adc_bufB" active then "adc_bufA" full
{
//-----------------------------------------------------------------------------------------------------------------------
// User must process here previously filled ADC DMA Buffer A ptr starting at "adc_bufA"
//-----------------------------------------------------------------------------------------------------------------------
}
}
//*******************************************************
//* USERS OTHER MAIN CODE GOES HERE
//*******************************************************
}
}
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// $ F U N C T I O N S U B - R O U T I N E S $
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// *********************************************************************************
// * init_ADC(void)
// * I N T E R L E A V E D A D C I N I T I L I Z A T I O N R O U T I N E
// * Used analog inputs if:
// * INTERLEAVED_ADC_COUNT = 2 (Analog inputs, AN0 and AN1 only)
// * INTERLEAVED_ADC_COUNT = 3 (Analog inputs, AN0, AN1 and AN2 only)
// * INTERLEAVED_ADC_COUNT = 4 (Analog inputs, AN0, AN1, AN2 and AN3 only)
// *********************************************************************************
// *********************************************************************************
// These steps are already done in this code example, this is just a reminder
// to the user for any new original code development.
//
// CAUTION:(Required)
// 1) The user SHOULD always set the minimum sampling time of each Class_1 ADC
// equal to 3TAD as defined by ADCxTIME<samc>.
// 2) The user should insure that the selected trigger source time base clock rate is
// exactly 2x faster than the ADC clock TAD rate. This will provide the highest
// timing resolution along with the most sampling rate options as defined in
// Table 13 or 15 accordingly in this app note.
// 3) The user selected trigger sources have to be able to be mapped to a single
// synchronized time base.
// For example:
// a) TMR3 & OCx (x=1,3,5 PIC32MZxxxx)
// b) TMR3 & OCx (x=1-4 PIC32MKxxGPxx)
// c) PTMRx & TRIGx ( x=1-12 PIC32MKxxMCxx PWM)
// d) PTMRx & STRIGx ( x=1-12 PIC32MKxxMCxx PWM)
// 4) In a same group of interleaved ADC’s a user “MUST NOT” use two separate timer trigger sources.
// Example: ADC0, ADC1 and ADC2 are being used in an interleaved group.
// Trigger sources can be any combination of valid ADC OCx edge triggered sources synchronized to a SINGLE TMRx.
// It would not be allowed to use in the same group of interleaved ADCs OCx, TMR3 & TMR5 for example.
// Separate TMRx in different interleaved ADC groups is OK.
// 5 If using the DMA for the ADC the user “MUST” use SYSCLK as the ADC
// source clock, (i.e. ADCCON3<adcsel>).
// 6) User must configure ADC for 512 TAD warm-up time
// (ADCANCONbits.WKUPCLKCNT = 0x9) "BEFORE" enabling ADC.
// 7) Users SW must wait for ADC BANGAP and ADC warm-up time "AFTER"
// enabling ADC and before activating the ADC trigger sources.
// 8) ADC must always be initialized 1st “BEFORE” activating the ADC trigger sources
//
// NOTE: Failure to follow these recommendations will result in inaccurate ADC
// data acquisition and poor performance.
//******************************************************************************
void init_ADC(void)
{
ADC0CFG = DEVADC0; //Load ADC0 Calibration values
ADC1CFG = DEVADC1; //Load ADC1 Calibration values
ADC2CFG = DEVADC2; //Load ADC2 Calibration values
ADC3CFG = DEVADC3; //Load ADC3 Calibration values
ADC4CFG = DEVADC4; //Load ADC4 Calibration values
ADC7CFG = DEVADC7; //Load ADC7 Calibration values
ADCANCONbits.WKUPCLKCNT = 0x9; // ADC Warm up delay = (512 * TADx)
ADCCON1bits.AICPMPEN = 0; //Disable ADC charge pump
CFGCONbits.IOANCPEN = 0; //Disable ADC I/O charge pump
// ******************************************************************************
// * If using the DMA the user MUST use SYSCLK as the ADC source clock
// ******************************************************************************
ADCCON1bits.FSSCLKEN = 1; //Fast synchronous SYSCLK to ADC control clock is enabled
ADCCON3bits.ADCSEL = 1; // Select ADC input clock source = SYSCLK 200Mhz
ADCCON3bits.CONCLKDIV = 1; // Analog-to-Digital Control Clock (TQ) Divider = SYSCLK/2
// ******************************************************************************
// * ADC0 Module setup:
// * TAD = SYSCLK/4, 12bit, SAMC=3TAD, OC1 Trigger
// ******************************************************************************
ANSELBSET = 0x1; //Enable analog AN0 input
ADC0TIMEbits.ADCDIV = 1; // ADCx Clock Divisor, Divide by 2, 50Mhz
ADC0TIMEbits.SAMC = 1; // 3 TAD (Hardware enforced sample time)
ADC0TIMEbits.SELRES = 3; // ADC0 12bit resolution mode
ADCTRG1bits.TRGSRC0 = 8; // Set AN0 to trigger from OC1
ADCANCONbits.ANEN0 = 1; // Enable ADC0 analog bias/logic
ADCCON3bits.DIGEN0 = 1; // Enable ADC0 digital logic
ADCGIRQEN1bits.AGIEN0 = 1; //Enb ADC0 AN0 interrupts for DMA
// ******************************************************************************
// * ADC1 Module setup:
// * TAD = SYSCLK/4, 12bit, SAMC=3TAD
// * if(INTERLEAVED_ADC_COUNT == 2) TMR3 trigger else OC3 Trigger
// ******************************************************************************
ANSELBSET = 0x2; //Enable analog AN1 input
ADC1TIMEbits.ADCDIV = 1; // ADCx Clock Divisor, Divide by 2, 50Mhz
ADC1TIMEbits.SAMC = 1; // 3 TAD
ADC1TIMEbits.SELRES = 3; // ADC1 12bit resolution mode
if(INTERLEAVED_ADC_COUNT == 2) ADCTRG1bits.TRGSRC1 = 6; // Set ADC1 to trigger from TMR3
else ADCTRG1bits.TRGSRC1 = 9; // Set ADC1 to trigger from OC3
ADCANCONbits.ANEN1 = 1; // Enable ADC1 analog bias/logic
ADCCON3bits.DIGEN1 = 1; // Enable ADC1 digital logic
ADCGIRQEN1bits.AGIEN1 = 1; //Enb ADC1 AN1 interrupts for DMA
// ******************************************************************************
// * ADC2 Module setup:
// * TAD = SYSCLK/4, 12bit, SAMC=3TAD, OC5 Trigger
// * if(INTERLEAVED_ADC_COUNT == 3) TMR3 trigger else OC5 Trigger
// ******************************************************************************
if(INTERLEAVED_ADC_COUNT > 2)
{
ANSELBSET = 0x4; //Enable analog AN2 input
ADC2TIMEbits.ADCDIV = 1; // ADCx Clock Divisor, Divide by 2, 50Mhz
ADC2TIMEbits.SAMC = 1; // 3 TAD
ADC2TIMEbits.SELRES = 3; // ADC2 12bit resolution mode
if(INTERLEAVED_ADC_COUNT == 3) ADCTRG1bits.TRGSRC2 = 6; // Set ADC2 to trigger from TMR3
else ADCTRG1bits.TRGSRC2 = 10; // Set ADC2 to trigger from OC5
ADCANCONbits.ANEN2 = 1; // Enable ADC2 analog bias/logic
ADCCON3bits.DIGEN2 = 1; // Enable ADC2 digital logic
ADCGIRQEN1bits.AGIEN2 = 1; //Enb ADC2 AN2 interrupts for DMA
}
// ******************************************************************************
// * ADC3 Module setup:
// * if(INTERLEAVED_ADC_COUNT == 4)
// * TAD = SYSCLK/4, 12bit, SAMC=3TAD, TMR3 Trigger
// ******************************************************************************
if(INTERLEAVED_ADC_COUNT > 3)
{
ANSELBSET = 0x8; //Enable analog AN3 input
ADC3TIMEbits.ADCDIV = 1; // ADCx Clock Divisor, Divide by 2, 50Mhz
ADC3TIMEbits.SAMC = 1; // 3 TAD
ADC3TIMEbits.SELRES = 3; // ADC3 12bit resolution mode
ADCTRG1bits.TRGSRC3 = 6; // Set ADC3 to trigger from TMR3
ADCANCONbits.ANEN3 = 1; // Enable ADC3 analog bias/logic
ADCCON3bits.DIGEN3 = 1; // Enable ADC3 digital logic
ADCGIRQEN1bits.AGIEN3 = 1; //Enb ADC3 AN3 interrupts for DMA
}
ADCCON1bits.ON = 1; // Turn the ADC on
// ******************************************************************************
// * Waiting for ADC warm-up time and ADC bandgap reference to stabilize
// ******************************************************************************
while(!ADCCON2bits.BGVRRDY); // Wait until the reference voltage is ready
while(!ADCANCONbits.WKRDY0); // Wait until ADC0 is ready
while(!ADCANCONbits.WKRDY1); // Wait until ADC1 is ready
if(INTERLEAVED_ADC_COUNT > 2) while(!ADCANCONbits.WKRDY2); // Wait until ADC2 is ready
if(INTERLEAVED_ADC_COUNT > 3) while(!ADCANCONbits.WKRDY3); // Wait until ADC3 is ready
ADCDATA0; // Read ADC data to make sure that data ready bits are clear.
ADCDATA1; //
ADCDATA2; //
ADCDATA3; //
init_DMA(); //Initialize DMA
init_TMR3(); //Initialize and enable ADC interleaved triggers
}
// ***************************************************************
// * D M A I N I T I A L I Z A T I O N R O U T I N E *
// ***************************************************************
void init_DMA(void)
{
// **********************************************************
// * Set the DMA trigger source. Each interrupt on the indicated
// * ADC_DATA_VECTOR begins a DMA transfer.
// **********************************************************
if(INTERLEAVED_ADC_COUNT == 2)DCH0ECONbits.CHSIRQ = ADC1_IRQ; //DMA_TRIGGER_ADC0_DATA0;
if(INTERLEAVED_ADC_COUNT > 2)DCH0ECONbits.CHSIRQ = ADC2_IRQ; //DMA_TRIGGER_ADC2_DATA2;
if(INTERLEAVED_ADC_COUNT > 3)DCH0ECONbits.CHSIRQ = ADC3_IRQ; //DMA_TRIGGER_ADC3_DATA3;
// **********************************************************
// * Enable DMA Channel Start IRQ.
// **********************************************************
DCH0ECONbits.SIRQEN = 1; //DMA Chan_0 Start IRQ Enable
// **********************************************************
// * Set DMA Source Starting Address
// **********************************************************
DCH0SSA = (uint32_t) VirtAddr_TO_PhysAddr((const void *)&ADCDATA0);
// **********************************************************
// * Set DMA Source Size in bytes
// **********************************************************
if(INTERLEAVED_ADC_COUNT == 2) DCH0SSIZ = 8; // ADC0 + ADC1, 4+4bytes = 8bytes
if(INTERLEAVED_ADC_COUNT > 2) DCH0SSIZ = 12; //ADC0 + ADC1 + ADC2 = 12bytes
if(INTERLEAVED_ADC_COUNT > 3) DCH0SSIZ = 16; //ADC0 + ADC1 + ADC2 + ADC3 = 16bytes
// **********************************************************
// * Set DMA Destination Starting Address
// **********************************************************
DCH0DSA = (uint32_t) VirtAddr_TO_PhysAddr((const void *)&adc_bufA[0]);
// **********************************************************
// * Set DMA Destination Size
// **********************************************************
DCH0DSIZ = (uint16_t) INTERLEAVED_ADC_COUNT*(ADC_DMA_BUFF_SIZE)*sizeof(adc_bufA[0]);
// **********************************************************
// * Set Cell Size
// **********************************************************
DCH0CSIZ = (uint16_t) sizeof(adc_bufA[0]);
DCH0INTbits.CHDDIF = 0; //Clr Chan Dest Done Int Flg
DCH0INTbits.CHDDIE = 1; //Enable DMA Destination Interrupt
// **********************************************************
// * Enable DMA channel 0
// **********************************************************
DCH0CONbits.CHEN = 1; //DMA Chan 0 enable
// **********************************************************
// * Enable DMA and DMA interrupts
// **********************************************************
IFS4bits.DMA0IF = 0; //Clr DMA Chan0 int flg
IPC33bits.DMA0IP = 7; //Set DMA Chan0 int priority 7
IPC33bits.DMA0IS = 3; //Set DMA Chan0 sub priority 3
IEC4bits.DMA0IE = 1; //Enable DMA Chan0 interrupt
DMACONbits.ON = 1; //Enb DMA
}
// *********************************************************************************
// * I N T E R L E A V E D A D C T R I G G E R I N I T I A L I Z A T I O N
// *********************************************************************************
void init_TMR3(void)
{
// *****************************************************************************
// * AN0-ANx Interleaved Triggers: (x=1-3)
// * Same analog input connected to all interleaved ANx inputs w/staggered triggers:
// *****************************************************************************
PR3 = T3_ADC_TRIG; //ADC3 TRM3 Trigger throughput rate
// ******************************************************************************
// * ADC0 Trigger setup:
// * TMR3 = OC1 time base, continuous pulse
// ******************************************************************************
CFGCONbits.OCACLK = 0; //
OC1CONbits.OCTSEL = 1; //Timer"y" is clk source for OC1 module
OC1CONbits.OCM = 5; //Init OC1 low; gen continuous output pulses
OC1R = OC1_ADC_TRIG; //ADC0 OC1 Trigger throughput rate
OC1RS = OC1_ADC_TRIG+2;
OC1CONSET = 0x8000; // Enable OC1
// ******************************************************************************
// * ADCx Trigger setup:
// * TMR3 = OC3 time base, continuous pulse
// ******************************************************************************
if(INTERLEAVED_ADC_COUNT > 2)
{
OC3CONbits.OCTSEL = 1; //Timer"y" is clk source for OC3 module
OC3CONbits.OCM = 5; //Init OC3 low; gen continuous output pulses
OC3R = OC3_ADC_TRIG; //ADC1 OC3 Trigger throughput rate
OC3RS = OC3_ADC_TRIG+2;
OC3CONSET = 0x8000; // Enable OC3
}
// ******************************************************************************
// * ADCy Trigger setup:
// * TMR3 = OC5 time base, continuous pulse
// ******************************************************************************
if(INTERLEAVED_ADC_COUNT > 3)
{
OC5CONbits.OCTSEL = 1; //Timer"y" is clk source for OC5 module
OC5CONbits.OCM = 5; //Init OC5 low; gen continuous output pulses
OC5R = OC5_ADC_TRIG; //ADC2 OC5 Trigger throughput rate
OC5RS = OC5_ADC_TRIG+2;
OC5CONSET = 0x8000; // Enable OC5
}
T3CONSET = 0x8000; //Start TMR3
} //End TMR3 Subroutine
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// $ I N T E R R U P T S E R V I C E R O U T I N E S $
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// *************************************************************************************************
// D M A I S R (Interrupt Service Routine)
// *************************************************************************************************
void __attribute__((interrupt(ipl7auto), vector(_DMA0_VECTOR), aligned(16), nomips16)) isr ()
{
IFS4bits.DMA0IF = 0; //Clr DMA Chan 0 int flg
DCH0INTbits.CHDDIF = 0; //Clr DMA dest done flg
adc_dma_buf_full_flg = 1; //Current DMA buffer is full
if(adc_buf_index == 0) //if "adc_bufA" full, change DMA dest to "adc_bufB"
{
DCH0DSA = (uint32_t) VirtAddr_TO_PhysAddr((const void *)&adc_bufB[0]);
adc_buf_index = 1;
}
else //if "adc_bufB" full, change DMA dest to "adc_bufA"
{
DCH0DSA = (uint32_t) VirtAddr_TO_PhysAddr((const void *)&adc_bufA[0]);
adc_buf_index = 0;
}
DCH0CONbits.CHEN = 1; //DMA Chan 0 enable
}
Nic
So it would nice if the program would run with other source files on the project tree. Biggest thing to figure out how to display inputs INTERLEAVED_ADC_COUNT = 3 (Analog inputs, AN0, AN1 and AN2 only) on a screen or in my case at this time An LED at a give voltage.
Aidan Author
Hi Nic, sorry for the late reply it was a busy day. I didn't see the source code, did you post it somewhere?
viral vaghela
This code is running for PIC32MK1024MCF100 which toggles port pin every 1 ms.
External Crystal used of 16MHz with PLL so SYS FREQ is 64MHz.
// PIC32MK1024MCF100 Configuration Bit Settings
// 'C' source line config statements
// DEVCFG3
// USERID = No Setting
#pragma config PWMLOCK = OFF // PWM IOxCON lock (PWM IOxCON register writes accesses are not locked or protected)
#pragma config FUSBIDIO2 = OFF // USB2 USBID Selection (USBID pin is controlled by the port function)
#pragma config FVBUSIO2 = OFF // USB2 VBUSON Selection bit (VBUSON pin is controlled by the port function)
#pragma config PGL1WAY = ON // Permission Group Lock One Way Configuration bit (Allow only one reconfiguration)
#pragma config PMDL1WAY = ON // Peripheral Module Disable Configuration (Allow only one reconfiguration)
#pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow only one reconfiguration)
#pragma config FUSBIDIO1 = OFF // USB1 USBID Selection (USBID pin is controlled by the port function)
#pragma config FVBUSIO1 = OFF // USB2 VBUSON Selection bit (VBUSON pin is controlled by the port function)
// DEVCFG2
#pragma config FPLLIDIV = DIV_1 // System PLL Input Divider (2x Divider)
#pragma config FPLLRNG = RANGE_5_10_MHZ // System PLL Input Range (5-10 MHz Input)
#pragma config FPLLICLK = PLL_POSC // System PLL Input Clock Selection (FRC is input to the System PLL)
#pragma config FPLLMULT = MUL_16 // System PLL Multiplier (PLL Multiply by 2)
#pragma config FPLLODIV = DIV_2 // System PLL Output Clock Divider (2x Divider)
#pragma config VBATBOREN = ON // VBAT BOR Enable (Enable ZPBOR during VBAT Mode)
#pragma config DSBOREN = ON // Deep Sleep BOR Enable (Enable ZPBOR during Deep Sleep Mode)
#pragma config DSWDTPS = DSPS32 // Deep Sleep Watchdog Timer Postscaler (1:2^36)
#pragma config DSWDTOSC = LPRC // Deep Sleep WDT Reference Clock Selection (Select LPRC as DSWDT Reference clock)
#pragma config DSWDTEN = ON // Deep Sleep Watchdog Timer Enable (Disable DSWDT during Deep Sleep Mode)
#pragma config FDSEN = OFF // Deep Sleep Enable (Disable DSEN bit in DSCON)
#pragma config BORSEL = HIGH // Brown-out trip voltage (BOR trip voltage 2.1v (Non-OPAMP deviced operation))
#pragma config UPLLEN = OFF // USB PLL Enable (USB PLL Disabled)
// DEVCFG1
#pragma config FNOSC = SPLL // Oscillator Selection Bits (Internal Fast RC (FRC))
#pragma config DMTINTV = WIN_127_128 // DMT Count Window Interval (Window/Interval value is 127/128 counter value)
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Disable Secondary Oscillator)
#pragma config IESO = OFF // Internal/External Switch Over (Disabled)
#pragma config POSCMOD = HS // Primary Oscillator Configuration (Primary osc disabled)
#pragma config OSCIOFNC = ON // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FCKSM = CSECME // Clock Switching and Monitor Selection (Clock Switch Enabled, FSCM Enabled)
#pragma config WDTPS = PS128 // Watchdog Timer Postscaler (1:1048576)
#pragma config WDTSPGM = STOP // Watchdog Timer Stop During Flash Programming (WDT stops during Flash programming)
#pragma config WINDIS = NORMAL // Watchdog Timer Window Mode (Watchdog Timer is in non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled)
#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window size is 25%)
#pragma config DMTCNT = DMT31 // Deadman Timer Count Selection (2^31 (2147483648))
#pragma config FDMTEN = OFF // Deadman Timer Enable (Deadman Timer is enabled)
// DEVCFG0
#pragma config DEBUG = OFF // Background Debugger Enable (Debugger is disabled)
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)
#pragma config ICESEL = ICS_PGx3 // ICE/ICD Comm Channel Select (Communicate on PGEC3/PGED3)
#pragma config TRCEN = ON // Trace Enable (Trace features in the CPU are enabled)
#pragma config BOOTISA = MIPS32 // Boot ISA Selection (Boot code and Exception code is MIPS32)
#pragma config FSLEEP = OFF // Flash Sleep Mode (Flash is powered down when the device is in Sleep mode)
#pragma config DBGPER = PG_ALL // Debug Mode CPU Access Permission (Allow CPU access to all permission regions)
#pragma config SMCLR = MCLR_NORM // Soft Master Clear Enable (MCLR pin generates a normal system Reset)
#pragma config SOSCGAIN = GAIN_1X // Secondary Oscillator Gain Control bits (2x gain setting)
#pragma config SOSCBOOST = ON // Secondary Oscillator Boost Kick Start Enable bit (Boost the kick start of the oscillator)
#pragma config POSCGAIN = GAIN_LEVEL_0 // Primary Oscillator Gain Control bits (Gain Level 3 (highest))
#pragma config POSCBOOST = ON // Primary Oscillator Boost Kick Start Enable bit (Boost the kick start of the oscillator)
#pragma config EJTAGBEN = NORMAL // EJTAG Boot Enable (Normal EJTAG functionality)
// DEVCP
#pragma config CP = OFF // Code Protect (Protection Disabled)
// SEQ
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define SYS_FREQ 64000000
void delay_us(unsigned int us)
{
// Convert microseconds us into how many clock ticks it will take
us *= SYS_FREQ / 1000000; // Core Timer updates every 2 ticks
_CP0_SET_COUNT(0); // Set Core Timer count to 0
while (us > _CP0_GET_COUNT()); // Wait until Core Timer count reaches the number we calculated earlier
}
void delay_ms(int ms)
{
delay_us(ms * 1000);
}
void main(void) {
TRISAbits.TRISA7 = 0;
LATAbits.LATA7 = 0;
while(1){
LATAbits.LATA7 = !LATAbits.LATA7;
delay_us(1000);
}
Ciaran Watters
Hi Aidan, It has being a long time since you helped me out.
I was recently experimenting with the Flip & Click mikroBootlooder and this lead me to trying to install the Bootlooder Component in MPLAB Harmony Configurator but it "<configuration database="">[Error]: Failed to activate component: bootloader"
Did you ever experiment with the Bootloader and PIC32's?
Kind Regards Ciaran
Aidan Author
Hi Ciaran,
Nice to see you back here again. Sadly no, I've never worked with bootloaders. I've been off PIC32 for a few months now trying to get some ESP32 , Arduino and now Tizen stuff working, I hope to be back to PIC32 soon enough.
Cirilo Bernardo
I use the cycle counter as a general purpose high-resolution timer and one of the uses is as a busy wait timer as you have here. I'd just like to suggest that you never set the value of the counter - just let it keep running and overflowing to 0. To implement a timeout you want logic like: if ((current_value - start_value) < microsec_delay_equivalent) { ; /* waste precious time */ }
So long as all the variables are uint32_t you have a MAX timeout period equal to 2^32 - 1 cycles which is a little over 40s. If you let the counter run continuously then you can also use it to time code execution, interrupt service latency, and what-not. To avoid spamming CP0 I have a short wait loop that executes if the timeout condition isn't met; my wait loop blows out the timer granularity to roughly half a microsecond but I have no need of timing precision better than about 2us.
I use a similar trick for my system timer which is a 1ms interrupt (about a 12 day wrap-around period).
Aidan Author
Hi Cirilo,
Interesting idea, thanks for that :) You're right that most of the delay stuff doesn't need to be super accurate so your method would work great too. I think I'd still set CP0 once though in case it starts on a huge number like 2 billion. Anyway, thanks for all your useful comments.
Comments are hosted by Disqus, which loads only when you ask for it.