In previous tutorials I had explained prescale calulations for LPC1343 and LPC1114. Since ARM Cortex based LPC111x and LPC134x MCUs use CCLK as PCLK for Timer blocks its relatively easy to write a function which can compute required prescaler value for a given resolution in micro-seconds or milli-seconds. Given below, are two functions to find the prescale value – one that takes argument as Desired Timer Resolution in Milli-seconds viz. calcPrescaleMS(..), and one that takes the argument in Mirco-Seconds viz. calcPrescaleUS(..). The argument cannot be less than 1 since it is the minimum resolution required to find prescale, and we will limit it to a max value of 1000. Both functions will return 0 if argument is invalid. SystemCoreUpdate() is a ‘system function’, defined in startup code, which updates SystemCoreClock variable to current System AHB Clock(CCLK) frequency in Hz.
C++ Source Code for function definition:
/*Find a prescale given Timer Resolution in Milli-Secs (ms) at CCLK*/
unsigned int calcPrescaleMS(unsigned short resMS) //Argument in ms
{
if( resMS<1 || resMS>1000 ) return 0; //Accept between 1 and 1000ms only
SystemCoreClockUpdate();
float prescale = SystemCoreClock * ((float)resMS/1000);
return ((unsigned int)prescale - 1);
}
/*Find a prescale given Timer Resolution in Micro-Secs (us) at CCLK*/
unsigned int calcPrescaleUS(unsigned short resUS) //Argument in us
{
if( resUS<1 || resUS>1000 ) return 0; //Accept between 1 and 1000us only
SystemCoreClockUpdate();
float prescale = SystemCoreClock * ((float)resUS/1000000);
return ((unsigned int)prescale - 1);
}
Example Usage
//Get prescaler value for 1ms Resolution/Delay per TC increment
LPC_TMR32B0->PR = calcPrescaleMS(1);
//Find prescaler value for 10ms Resolution/Delay per TC increment
LPC_TMR32B0->PR = calcPrescaleMS(10);
//Use prescaler value for 1us Resolution/Delay per TC increment
LPC_TMR32B0->PR = calcPrescaleUS(1);
//Get prescaler value for 100us Resolution/Delay per TC increment
LPC_TMR32B0->PR = calcPrescaleUS(100);