<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>lpc1114 - OCFreaks!</title>
	<atom:link href="https://www.ocfreaks.com/tag/lpc1114/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.ocfreaks.com/tag/lpc1114/</link>
	<description>Overclocking , Gaming , Technology , Robotics &#38; DIY!</description>
	<lastBuildDate>Mon, 06 Nov 2017 14:11:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.4</generator>
<site xmlns="com-wordpress:feed-additions:1">42777727</site>	<item>
		<title>Function to calculate Prescaler value for LPC1114 &#038; LPC1343</title>
		<link>https://www.ocfreaks.com/function-calculate-prescaler-value-lpc1114-lpc1343/</link>
					<comments>https://www.ocfreaks.com/function-calculate-prescaler-value-lpc1114-lpc1343/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[Umang Gajera]]></dc:creator>
		<pubDate>Mon, 06 Nov 2017 14:11:24 +0000</pubDate>
				<category><![CDATA[Embedded]]></category>
		<category><![CDATA[lpc1114]]></category>
		<category><![CDATA[lpc1343]]></category>
		<guid isPermaLink="false">http://www.ocfreaks.com/?p=2975</guid>

					<description><![CDATA[<p>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 prescale value for a given resolution in micro-seconds or milli-seconds. In this tutorial we will define a function to calculate prescaler value for LPC1114 &#038; LPC1343</p>
<p>The post <a href="https://www.ocfreaks.com/function-calculate-prescaler-value-lpc1114-lpc1343/">Function to calculate Prescaler value for LPC1114 &#038; LPC1343</a> appeared first on <a href="https://www.ocfreaks.com">OCFreaks!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In previous tutorials I had explained prescale calulations for <a href="https://www.ocfreaks.com/lpc1343-timer-programming-tutorial/#Prescaler_Calc">LPC1343</a> and <a href="https://www.ocfreaks.com/lpc1114-timer-programming-tutorial/#Prescaler_Calc">LPC1114</a>. 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 &#8211; one that takes argument as Desired Timer Resolution in Milli-seconds viz. <span class="code_var">calcPrescaleMS(..)</span>, and one that takes the argument in Mirco-Seconds viz. <span class="code_var">calcPrescaleUS(..)</span>. 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. <span class="code_var">SystemCoreUpdate()</span> is a &#8216;system function&#8217;, defined in startup code, which updates <span class="code_var">SystemCoreClock</span> variable to current System AHB Clock(CCLK) frequency in Hz.</p>
<p>C++ Source Code for function definition:</p>
<pre><code class="language-cpp">
/*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);
}
</code></pre>
<h4>Example Usage</h4>
<pre><code class="language-cpp">
//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);
</code></pre>
<p>The post <a href="https://www.ocfreaks.com/function-calculate-prescaler-value-lpc1114-lpc1343/">Function to calculate Prescaler value for LPC1114 &#038; LPC1343</a> appeared first on <a href="https://www.ocfreaks.com">OCFreaks!</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ocfreaks.com/function-calculate-prescaler-value-lpc1114-lpc1343/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2975</post-id>	</item>
		<item>
		<title>LPC1114 PWM Programming Tutorial</title>
		<link>https://www.ocfreaks.com/lpc1114-pwm-programming-tutorial/</link>
					<comments>https://www.ocfreaks.com/lpc1114-pwm-programming-tutorial/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[Umang Gajera]]></dc:creator>
		<pubDate>Mon, 02 Oct 2017 16:07:02 +0000</pubDate>
				<category><![CDATA[Embedded]]></category>
		<category><![CDATA[lpc1114]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">http://www.ocfreaks.com/?p=2911</guid>

					<description><![CDATA[<p>This time I will discuss ARM Cortex-M0 LPC1114 Pulse Width Modulation (PWM) Tutorial. This programming guide is also applicable for other devices of same family like LPC1115. I will also cover 2 simple LPC1114 PWM examples. I assume you are comfortable with Timers in ARM Cortex-M0 based LPC11xx micro-controllers since PWM block is basically a [&#8230;]</p>
<p>The post <a href="https://www.ocfreaks.com/lpc1114-pwm-programming-tutorial/">LPC1114 PWM Programming Tutorial</a> appeared first on <a href="https://www.ocfreaks.com">OCFreaks!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p class="tocpostimage"> This time I will discuss <strong>ARM Cortex-M0 LPC1114 </strong><strong>Pulse Width Modulation</strong> (PWM) Tutorial. This programming guide is also applicable for other devices of same family like LPC1115. I will also cover 2 simple LPC1114 PWM examples. I assume you are comfortable with Timers in ARM Cortex-M0 based LPC11xx micro-controllers since PWM block is basically a Timer with PWM mode enabled. Please go through the <a href="https://www.ocfreaks.com/lpc1114-timer-programming-tutorial/">previous tutorial</a> since we will be using the same registers except for PWM Control Register which is covered in this tutorial.</p>
<div class="toc_container">
<span class="toc_title">Table of Contents</span></p>
<div class="toc">
<ol class="toc_list">
<li class="toc_text"><a href="#PWM_Module"><span>ARM Cortex-M0 LPC1114 PWM Module</span></a></li>
<ol class="toc_list">
<li class="toc_text"><a href="#Leading_Edge_PWM"><span>Leading Edge PWM Waveform</span></a></li>
<li class="toc_text"><a href="#PWM_timing_diagram"><span>PWM Timing Diagram Example</span></a></li>
</ol>
<li class="toc_text"><a href="#PWM_Registers"><span>PWM Registers</span></a></li>
<li class="toc_text"><a href="#PWM_Configuration"><span>Configuring LPC111x PWM Block</span></a></li>
<ol class="toc_list">
<li class="toc_text"><a href="#PWM_Prescaler_formula"><span>LPC1114 PWM Prescaler Calculations</span></a></li>
</ol>
<li class="toc_text"><a href="#LPC1114_PWM_Examples"><span>LPC111x PWM Examples</span></a></li>
<ol class="toc_list">
<li class="toc_text"><a href="#Example1"><span>LPC1114 PWM Example 1</span></a></li>
<li class="toc_text"><a href="#Example2"><span>LPC1114 PWM Example 2</span></a></li>
</ol>
</ol>
</div>
</div>
<h4 style="clear:both;"> Pulse Width Modulation (PWM) Basics: </h4>
<p>I&#8217;ve have posted a <a href="https://www.ocfreaks.com/pulse-width-modulation-pwm-tutorial/">Beginner PWM tutorial</a>. If you are new to PWM please have a look there. A Diagram that sums it up is given below. It shows Single Edge Non-Inverting or Left-Aligned PWM with T<sub>ON</sub>, T<sub>OFF</sub> &#038; Period. Duty Cycle is simply T<sub>ON</sub> Divided by Period.</p>
<p><img decoding="async" class="aligncenter" src="https://www.ocfreaks.com/imgs/embedded/common/pwm_basics.png" alt="PWM timing showing T-on, T-off and Period" width="425px" height="180px" /></p>
<h2 class="shead" id="PWM_Module">ARM Cortex-M0 LPC1114 PWM Module</h2>
<p>In LPC11xx Microcontrollers a dedicated PWM block is absent. Instead, PWM support is integrated in the Timers themselves. We just need to use them in PWM mode to generate PWM. LPC111x micro-controllers only support Single Edge PWM outputs. More over, the way PWM support is implemented in these microcontrollers, the PWM pulse is Leading-Edge or Right-Aligned compared to Trailing-Edge or Left-Aligned  PWM which is more common. But thankfully, this does not change the way we control external PWM controlled devices since Leading-Edge/Right-Aligned PWM is just a Phase-Shifted Left-Aligned pulse <strong>&#8211; with the only condition that Duty Cycle (hence T<sub>ON</sub>) remains the same</strong>. Given this condition is satisfied: consider it having a &#8220;offset&#8221; at the beginning, afterwhich its basically Left-Aligned PWM if you disregard the &#8220;initial-offset&#8221; or phase-shift. Here is what it looks like:</p>
<p><img decoding="async" class="aligncenter" id="Leading_Edge_PWM" src="https://www.ocfreaks.com/imgs/embedded/common/leading_edge_inverted_pwm.png" alt="Single edge Inverted, Leading Edge, Right-Aligned PWM waveform" width="385px" height="235px" /></p>
<p>Since we are using Timer in PWM mode, calculations for Prescale and configuration is exactly the same. Essentially we have two 16-bit PWM blocks and two 32-bit PWM blocks. Each capable of generating 3 PWM outputs using first 3 match registers (MR0 to MR2). The last match register (<strong>MR3</strong>) is used to set the PWM Period. The Match outputs pins are used get the PWM output when PWM mode is enabled for the corresponding Match output. Please refer my previous timer tutorial for MAT pins.</p>
<div class="highlight">
<strong>PWM Rules &#8211;</strong> The User Manual mentions Rules for using Single Edge PWM on page 374. </p>
<ol style="margin-bottom:0px">
<li>All PWM outputs will go LOW at the beginning of a PWM cycle unless their match value is 0.</li>
<li>Each PWM output will go HIGH when its match value is reached. <strong>Note:</strong> If no match occurs i.e Match value is greater than Period then the output will remain LOW!</li>
<li>If the Match value is greater than PWM Period or the Cycle Time, and PWM signal is already HIGH, then the signal will be made LOW at the start of next cycle.</li>
<li>If Match value is same as the Period, then PWM output will be reset to LOW at the next clock tick after TC reaches its reset value. Hence, we end up getting one clock pulse at the beginning. This implies we can never get 0% duty-cycle. Its almost 0% but not 0%!</li>
<li>If Match value is zero then PWM output will be HIGH continuously.</li>
</ul>
</div>
<p id="PWM_timing_diagram">Consider that our PWM Period duration is 5 milliseconds and TC increments every 1 millisecond using appropriate prescale value. We set MR3 to 5 i.e. <strong>5 ticks of PWM TC</strong>. We have MR1 = 2 and MR2 = 4. We using T<sub>OFF</sub> as Match values. Whenever the value in TC matches the value in any of the match register, its corresponding output it set to HIGH until the start of next PWM cycle as shown in PWM Timing Diagram below:</p>
<p><img decoding="async" class="aligncenter" src="https://www.ocfreaks.com/imgs/embedded/common/lpc_leading_edge_pwm_waveform.png" alt="Leading Edge or Right-Aligned PWM timing diagram in some LPC MCUs" width="515px" height="315px" /></p>
<h2 class="shead" id="PWM_Registers">Registers used in LPC1114 PWM Programming</h2>
<p>I have already explained TCR, PR, MRx &#038; MCR registers in <a href="https://www.ocfreaks.com/lpc1114-timer-programming-tutorial/">previous LPC1114 Tutorial</a>, so I won&#8217;t explaining them in detail except for PWMC.</p>
<div class="highlight">
<strong>1) <span class="doc_ref">TCR</span> &#8211; Timer Control Register:</strong> This register is used to control the Timer Counter. Only Bits: 0, 1  &#038; 3 are used rest are reserverd.</p>
<p><strong>2) <span class="doc_ref">PR</span> &#8211; (PWM) Prescale Register:</strong> Used to control the resolution of the PWM outputs. The Timer Counter(TC) will increment every PR+1 Peripheral Clock Cycles (PCLK).</p>
<p><strong>3) <span class="doc_ref">MR0 &#8211; MR3</span> (Match Registers):</strong> These four Match registers contain <strong>Pulse T<sub>OFF</sub> (or LOW Time) Values i.e the Number of TC Ticks required to change the output from LOW to HIGH.</strong>.</p>
<p><strong>4) <span class="doc_ref">MCR</span> &#8211; (PWM) Match Control Registers:</strong> It is used to specify what operations can be done when the value in a particular Match register equals the value in TC &#8211; Either generate an Interrupt, or Reset the TC, or Stop, which stops the counters.</p>
<p><strong>5) <span class="doc_ref">PWMC (TM32BxPWMC) </span> &#8211; PWM Control Register:</strong> It is used to configure Match outputs as PWM outputs. As mentioned earlier only <strong>three PWM outputs</strong> can be used at a time since one additional Match Register is used to generate the PWM Period. As recommended by the User Manual MR3 must be used to generate the Period. Only the first 4 bits are used, one for each Match Output. <strong>BITx</strong> is used to enable <strong>PWM Mode</strong> for <strong>MATx</strong>. Hence BIT0 for MAT0, BIT1 for MAT1 and so on. When a bit is 0 the corresponding Match Output is defined by EMx Bit in EMR register. When a bit is 1, PWM Output is enabled for the corresponding Match Output Pin. We are only interested in setting these bits to 1.
</div>
<h2 class="shead" id="PWM_Configuration">Configuring and Initializing PWM in LPC111x</h2>
<p>Now lets see how to configure PWM. First we need to do some basic Calculations for defining the <strong>PWM Period</strong> time and the <strong>PWM Resolution</strong> using a prescale value. For this first we need to define the resolution of our PWM signal. PWM resolution is the minimum time delay that can used to increase or decrease the pulse width. More smaller the increment more fine will be the resolution. PWM Resolution is set using an appropriate Prescale Value.</p>
<div class="special sp_blue noteinfo" id="PWM_Prescaler_formula">
<h4>LPC1114 PWM Prescale Calculations:</h4>
<p>The general formula for <strong>PWM Resolution</strong> at X Mhz PCLK(CCLK) and a given value for prescale (PR) is as given below:</p>
<div class="equation">PWM<sub>RES</sub> =</p>
<div class="fraction"><span class="fup">PR+1</span><span class="bar">/</span><span class="fdn">PCLK<sub>Hz</sub></sup></span></div>
<p> =</p>
<div class="fraction"><span class="fup">PR+1</span><span class="bar">/</span><span class="fdn">X * 10<sup>6</sup></span></div>
<p>Seconds</p></div>
<p>Hence, we get the <strong>Formula for Prescaler (PR) for required PWM resolution (PWM<sub>RES</sub> in Secs)</strong> at given <strong>PCLK(in Hz)</strong> frequency as:</p>
<div class="equation">PR = (PCLK<sub>Hz</sub> * PWM<sub>RES</sub>) &#8211; 1</div>
<div class="equation">PR = ((X * 10<sup>6</sup>) * PWM<sub>RES</sub>) &#8211; 1</div>
<p>Note that here, the PWM Resolution is also the time delay required to increment TC by 1.</p>
<p>Hence, Prescaler value for 1 micro-second resolution i.e. 1us time delay at 48 Mhz PCLK(CCLK) is,</p>
<div class="equation">PR<sub>1us</sub> = (48Mhz * 1uS) -1 = (48*10<sup>6</sup> * 10<sup>-6</sup>) -1 = 47</div>
<p><strong>Note:</strong> with &#8220;PCLK&#8221; I basically mean CCLK since for LPC11xx Microcontrollers: <strong>PCLK=CCLK</strong>.
</div>
<div class="highlight">
After we&#8217;ve worked out the resolution we want, we can now Configure &#038; Initialize PWM Mode as per the following steps:</p>
<ol style="margin-bottom:0px">
<li>Select the MATx function for the PIN on which you need the PWM output using applicable <span class="code_var">LPC_IOCON</span> register.</li>
<li>Assign the Calculated value to <span class="code_var">PR</span>.</li>
<li>Set the Value for PWM Period in <span class="code_var">MR3</span>.</li>
<li>Set the Values for other Match Registers i.e the Pulse Widths.</li>
<li>Set appropriate bit values in <span class="code_var">MCR</span> For e.g. Reset on MR3 Match for using MR3 to define PWM Period</li>
<li>Then Enable PWM outputs using <span class="code_var">PWMC</span>.</li>
<li>Now Reset PWM Timer using <span class="code_var">TCR</span>.</li>
<li>Finally, Enable TC using <span class="code_var">TCR</span>.</li>
</ol>
</div>
<p><strong>Example :</strong></p>
<pre><code class="language-cpp">
//Select MATn.x Function Using appropriate IOCON register for PWM output
LPC_IOCON->..    = ... ; //Select MATx Function for pins being used
LPC_TMRnBx->PR   = ... ; //assign calculated PR value 
LPC_TMRnBx->MR3  = ... ; //Assign PWM Period Duration
LPC_TMRnBx->MRx  = ... ; //Assign Pulse off duration i.e. T<sub>OFF</sub> for other Match Regs
LPC_TMRnBx->MCR  = (1<<10); //Reset TC on MR3 match
LPC_TMRnBx->PWMC = ... ; //enable PWM mode for MATx pins as required
LPC_TMRnBx->TCR  = 0x2; //Reset PWM TC & PR

//Now , the final moment - enable TC
LPC_TMRnBx->TCR  = 0x1; //enable counters for PWM
//Done!
</code></pre>
<h2 class="shead" id="LPC1114_PWM_Examples">ARM Cortex-M0 LPC1114 PWM Examples with code</h2>
<h4 id="Example1">LPC1114 PWM Example 1 &#8211; LED Dimming</h4>
<p>In this PWM Example we will use a Period of 1ms and vary the Duty-cycle from &#8220;almost&#8221; 0%(LED almost OFF) to 100%(Brightest). As mentioned earlier we cannot generate a duty-cycle of 0%(i.e. Signal Continuosly LOW), but it will close to 0%. Connect the LED anode to P1.18 and cathode to GND using a suitable resistor (like >330 Ohms). In the C++ code given below, I have also used 16Bit-Timer0 which is used by <span class="code_var">delayMS(..)</span> function to generate delay before we update MR0 register with new value. Also note that we are using match values corresponding to <strong>T<sub>OFF</sub></strong> time. So we subtract T<sub>ON</sub> from Period to get the match values which essentially makes the output a Phase-shifted PWM.</p>
<p><strong>C++ Source code for Example 1:</strong></p>
<pre><code class="language-cpp">
/*(C) Umang Gajera - www.ocfreaks.com
More Embedded tutorials @ https://www.ocfreaks.com/cat/embedded/
LPC1114 PWM Tutorial Example 1 - LED Dimming using PWM.
License: GPL.*/
#include &lt;lpc11xx.h&gt;

#define PRESCALE_US (48-1) //Used by PWM
#define PRESCALE_MS (48000-1) //Used by 16Bit-TMR0 for generating delay
#define PWM_PERIOD 1000 //in micro-seconds

void init32BTMR0_PWM(void);
inline void updatePulseWidth(unsigned int pulseWidth);
void delayMS(unsigned int milliseconds);
void init16BTimer0(void);

int main (void)
{
	//SystemInit(); //called by Startup Code before main(), hence no need to call again.
	LPC_IOCON->PIO1_6 |= 0x2; //Select CT32B0_MAT0 (MAT0.0) function for PWM O/P, Marked as RXD on LPCXpresso Board

	int pulseWidths[] = {
			PWM_PERIOD-0,PWM_PERIOD-250,
			PWM_PERIOD-500,PWM_PERIOD-750,
			PWM_PERIOD-1000}; //Inverted Pulse Widths for varying LED Brightness
	//Note: PWM_PERIOD-0 = LED almost OFF, PWM_PERIOD-1000 = LED Brightest!
	//PWM Pulses in LPC1114 are Right-Aligned or "inverted"!
	const int numPulseWidths = 5;
	int count=1;
	int dir=0; //direction, 0 = Increasing, 1 = Decreasing

	init32BTMR0_PWM(); //Initialize 32Bit-TMR0 as PWM
	init16BTimer0(); //Used by delayMS()

	while(1)
	{
		updatePulseWidth(pulseWidths[count]); //Update LED Pulse Width
		delayMS(500);

		if(count == (numPulseWidths-1) || count == 0)
		{
			dir = !dir; //Toggle direction if we have reached count limit
		}

		if(dir) count--;
		else count++;
	}
  //return 0; //normally this won't execute ever
}

void init32BTMR0_PWM(void)
{
	/*Assuming CCLK (System Clock) = 48Mhz (set by startup code)*/
	LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9); //Enable 32Bit-TMR0 Clock
	LPC_TMR32B0->CTCR = 0x0;
	LPC_TMR32B0->PR = PRESCALE_US; //Increment TC at every 47999+1 clock cycles
	//48000 clock cycles @48Mhz = 1 mS
	LPC_TMR32B0->MR3 = PWM_PERIOD; //1ms Period duration
	LPC_TMR32B0->MR0 = PWM_PERIOD-250; //Default TOFF time in mS
	LPC_TMR32B0->MCR = (1<<10); //Reset on MR3 Match

	LPC_TMR32B0->PWMC = 0x1; //Enable PWM Mode for MAT0.0

	LPC_TMR32B0->TCR = 0x2; //Reset Timer
	LPC_TMR32B0->TCR = 0x1; //Enable timer
}

inline void updatePulseWidth(unsigned int pulseWidth)
{
	LPC_TMR32B0->MR0 = pulseWidth; //Update MR1 with new value
}

void delayMS(unsigned int milliseconds) //Using 16Bit-TMR0
{
	LPC_TMR16B0->TCR = 0x2; //Reset Timer
	LPC_TMR16B0->TCR = 0x1; //Enable timer

	while(LPC_TMR16B0->TC < milliseconds); //wait until timer counter reaches the desired delay

	LPC_TMR16B0->TCR = 0x0; //Disable timer
}

void init16BTimer0(void)
{
	/*Assuming CCLK (System Clock) = 48Mhz (set by startup code)*/
	LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7); //Enable 16Bit_Timer0 Clock
	LPC_TMR16B0->CTCR = 0x0;
	LPC_TMR16B0->PR = PRESCALE_MS; //Increment TC at every 47999+1 clock cycles
	//48000 clock cycles @48Mhz = 1 mS
	LPC_TMR16B0->TCR = 0x2; //Reset Timer
}
</code></pre>
<div class="highlight"><strong>KEIL ARM uV5 Project for Example #1 on GitHub @</strong> <a href="https://github.com/OCFreaks/LPC1114-Tutorial-Examples/tree/master/MCUXpresso/PWM/Example_1" target="_blank">PWM Example 1 Source</a> [Successfully tested on Keil uV5.23], <strong>Download Project Zip</strong> @ <a href="https://minhaskamal.github.io/DownGit/#/home?url=https://github.com/OCFreaks/LPC1114-Tutorial-Examples/tree/master/MCUXpresso/PWM/Example_1" target="_blank">Example_1.Zip</a></div>
<h4 id="Example2">LPC1114 PWM Example 2 &#8211; Basic RC Servo Control</h4>
<p>In this Example we will sweep a servo from 1000us to 2000us pulse widths, back and forth in steps. The period is set to 20ms(20000us) which is common for servos. PWM1.1 output is selected and will be available on Pin P1.18. <strong>Warning:</strong> Double check all connections when connecting the motor or it will damage your board.</p>
<p><strong>C++ Source Code for Example 2:</strong></p>
<pre><code class="language-cpp">
/*(C) Umang Gajera - www.ocfreaks.com
More Embedded tutorials @ https://www.ocfreaks.com/cat/embedded/
LPC1114 PWM Tutorial Example 2 - Simple Servo Control.
License: GPL.*/
#include &lt;lpc11xx.h&gt;

#define PRESCALE_US (48-1) //Used by PWM
#define PRESCALE_MS (48000-1) //Used by 16Bit-TMR0 for generating delay
#define PWM_PERIOD 20000 //in micro-seconds

void init32BTMR0_PWM(void);
inline void updatePulseWidth(unsigned int pulseWidth);
void delayMS(unsigned int milliseconds);
void init16BTMR0(void);

int main (void)
{
	//SystemInit(); //called by Startup Code before main(), hence no need to call again.
	LPC_IOCON->PIO1_6 |= 0x2; //Select CT32B0_MAT0 (MAT0.0) function for PWM O/P, Marked as RXD on LPCXpresso Board

	int pulseWidths[] = {
			PWM_PERIOD-1000,PWM_PERIOD-1250,
			PWM_PERIOD-1500,PWM_PERIOD-1750,
			PWM_PERIOD-2000}; //Inverted Pulse Widths
	//Note: PWM_PERIOD-1000 will generate a Right aligned Pulse of 1000us and so on..
	//PWM Pulses in LPC1114 are Right-Aligned or "inverted"!
	const int numPulseWidths = 5;
	int count=1;
	int dir=0; //direction, 0 = Increasing, 1 = Decreasing

	init32BTMR0_PWM(); //Initialize 32Bit-TMR0 as PWM
	init16BTMR0(); //Used by delayMS()

	while(1)
	{
		updatePulseWidth(pulseWidths[count]); //Update LED Pulse Width
		delayMS(1000);

		if(count == (numPulseWidths-1) || count == 0)
		{
			dir = !dir; //Toggle direction if we have reached count limit
		}

		if(dir) count--;
		else count++;
	}
  //return 0; //normally this won't execute ever
}

void init32BTMR0_PWM(void)
{
	/*Assuming CCLK (System Clock) = 48Mhz (set by startup code)*/
	LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9); //Enable 32Bit-TMR0 Clock
	LPC_TMR32B0->CTCR = 0x0;
	LPC_TMR32B0->PR = PRESCALE_US; //Increment TC at every 47999+1 clock cycles
	//48000 clock cycles @48Mhz = 1 mS
	LPC_TMR32B0->MR3 = PWM_PERIOD; //20ms Period duration
	LPC_TMR32B0->MR0 = PWM_PERIOD-1250; //Default TOFF time in mS
	LPC_TMR32B0->MCR = (1<<10); //Reset on MR3 Match

	LPC_TMR32B0->PWMC = 0x1; //Enable PWM Mode for MAT0.0

	LPC_TMR32B0->TCR = 0x2; //Reset Timer
	LPC_TMR32B0->TCR = 0x1; //Enable timer
}

inline void updatePulseWidth(unsigned int pulseWidth)
{
	LPC_TMR32B0->MR0 = pulseWidth; //Update MR1 with new value
}

void delayMS(unsigned int milliseconds) //Using 16B-TMR0
{
	LPC_TMR16B0->TCR = 0x2; //Reset Timer
	LPC_TMR16B0->TCR = 0x1; //Enable timer

	while(LPC_TMR16B0->TC < milliseconds); //wait until timer counter reaches the desired delay

	LPC_TMR16B0->TCR = 0x0; //Disable timer
}

void init16BTMR0(void)
{
	/*Assuming CCLK (System Clock) = 48Mhz (set by startup code)*/
	LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7); //Enable 16Bit-TMR0 Clock
	LPC_TMR16B0->CTCR = 0x0;
	LPC_TMR16B0->PR = PRESCALE_MS; //Increment TC at every 47999+1 clock cycles
	//48000 clock cycles @48Mhz = 1 mS
	LPC_TMR16B0->TCR = 0x2; //Reset Timer
}
</code></pre>
<div class="highlight"><strong>KEIL ARM uV5 Project for Example #2 on GitHub @</strong> <a href="https://github.com/OCFreaks/LPC1114-Tutorial-Examples/tree/master/MCUXpresso/PWM/Example_2" target="_blank">PWM Example 2 Source</a> [Successfully tested on Keil uV5.23], <strong>Download Project Zip</strong> @ <a href="https://minhaskamal.github.io/DownGit/#/home?url=https://github.com/OCFreaks/LPC1114-Tutorial-Examples/tree/master/MCUXpresso/PWM/Example_2" target="_blank">Example_2.Zip</a></div>
<p>The post <a href="https://www.ocfreaks.com/lpc1114-pwm-programming-tutorial/">LPC1114 PWM Programming Tutorial</a> appeared first on <a href="https://www.ocfreaks.com">OCFreaks!</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ocfreaks.com/lpc1114-pwm-programming-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2911</post-id>	</item>
		<item>
		<title>LPC1114 Timer Programming Tutorial</title>
		<link>https://www.ocfreaks.com/lpc1114-timer-programming-tutorial/</link>
					<comments>https://www.ocfreaks.com/lpc1114-timer-programming-tutorial/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[Umang Gajera]]></dc:creator>
		<pubDate>Mon, 25 Sep 2017 19:07:09 +0000</pubDate>
				<category><![CDATA[Embedded]]></category>
		<category><![CDATA[lpc1114]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">http://www.ocfreaks.com/?p=2900</guid>

					<description><![CDATA[<p>Table of Contents Introduction Registers used in LPC111x Timer Programming Configuring Timers in LPC1114 LPC1114 Timer Prescaler Calculation LPC1114 Timer Examples Blinky Example Match Output Example Timer Interrupt Example This time we will go through ARM Cortex-M0 LPC1114 Timer Tutorial. In a previous LPC1114 tutorial we did a blinky example using GPIO and harcoded delays, [&#8230;]</p>
<p>The post <a href="https://www.ocfreaks.com/lpc1114-timer-programming-tutorial/">LPC1114 Timer Programming Tutorial</a> appeared first on <a href="https://www.ocfreaks.com">OCFreaks!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p class="tocpostimage"><img decoding="async" style="margin-top:20px;"class="aligncenter" src="https://www.ocfreaks.com/imgs/lpc1114-tutorial/tmr_title.png" width="335px" height="140px" /></p>
<div class="toc_container">
<span class="toc_title">Table of Contents</span></p>
<div class="toc">
<ol class="toc_list">
<li class="toc_text"><a href="#Intro"><span>Introduction</span></a></li>
<li class="toc_text"><a href="#Timer_Register"><span>Registers used in LPC111x Timer Programming</span></a></li>
<li class="toc_text"><a href="#Configure_Timer"><span>Configuring Timers in LPC1114</span></a></li>
<ol class="toc_list">
<li class="toc_text"><a href="#Prescaler_Calc"><span>LPC1114 Timer Prescaler Calculation</span></a></li>
</ol>
<li class="toc_text"><a href="#Examples"><span>LPC1114 Timer Examples</span></a></li>
<ol class="toc_list">
<li class="toc_text"><a href="#Example_1"><span>Blinky Example</span></a></li>
<li class="toc_text"><a href="#Example_2"><span>Match Output Example</span></a></li>
<li class="toc_text"><a href="#Example_3"><span>Timer Interrupt Example</span></a></li>
</ol>
</ol>
</div>
</div>
<p>This time we will go through ARM Cortex-M0 LPC1114 Timer Tutorial. In a <a href="https://www.ocfreaks.com/lpc1114-gpio-programming-tutorial/" target="_blank">previous LPC1114 tutorial</a> we did a blinky example using GPIO and harcoded delays, now its time to improvise and use precise delay using timers. LPC111x MCUs have two 16-bit Timers and two 32-bit Timers. Both of them are similar in operation except for the bit size. We will be using 32-bit timers in this tutorial. Each Timer block can be used as a &#8216;Timer&#8217; (e.g. triggering an interrupt every &#8216;t&#8217; microseconds) or as a &#8216;Counter&#8217; and can be also used to demodulate PWM signals given as input, find frequency of external signal, etc.</p>
<h2 class="shead" id="Intro">Introduction</h2>
<p>Each Timer module has its own <strong>Timer Counter(TC)</strong> and <strong>Prescale Register(PR)</strong> associated with it. When a Timer is Reset and Enabled, the TC is set to 0 and incremented by 1 every <strong>&#8216;PR+1&#8217;</strong> clock cycles &#8211; where PR is the value stored in Prescale Register. When it reaches its maximum value it gets reset to 0 and hence restarts counting. Prescale Register is used to define the <strong>resolution</strong> of the timer. If <strong>PR is 0</strong> then TC is incremented every <strong>1 clock cycle of the peripheral clock</strong>. If <strong>PR=1</strong> then TC is incremented every <strong>2 clock cycles of peripheral clock and so on</strong>. By setting an appropriate value in PR we can make timer increment or count: every peripheral clock cycle or 1 microsecond or 1 millisecond or 1 second and so on.</p>
<p>LPC1114/302 (LPC1100L Series) which is found on LPCXpresso Board has <strong>four Match Outputs</strong> and <strong>one Capture Input</strong>.</p>
<h4>Match Register</h4>
<p>A Match Register is a Register which contains a specific value set by the user. When the Timer starts &#8211; every time after TC is incremented the value in TC is compared with match register. If it matches then it can Reset the Timer or can generate an interrupt as defined by the user. We are only concerned with match registers in this tutorial.</p>
<p style="margin-bottom:0px;"><strong>Match Registers can be used to:</strong></p>
<ul style="margin-bottom:10px;">
<li>Stop Timer on Match(i.e when the value in count register is same as than in Match register) and trigger an optional interrupt.</li>
<li>Reset Timer on Match and trigger an optional interrupt.</li>
<li>
To count continuously and trigger an interrupt on match.</li>
</ul>
<h4>External Match Output</h4>
<p>When a corresponding Match register(MRx) equals the Timer Counter(TC) the match output can be controlled using External Match Register(EMR) to : either toggle, go HIGH, go LOW or do nothing.</p>
<h4>Capture Register</h4>
<p>As the name suggests it is used to Capture Input signal. When a transition event occurs on a Capture pin , it can be used to copy the value of TC into any of the 4 Capture Register or to genreate an Interrupt. Hence these can be also used to demodulated PWM signals. We are not going to use them in this tutorial since we are only concerned with using Timer block as a &#8216;Timer&#8217;. We&#8217;ll see them in a later tutorial.</p>
<div class="special sp_blue noteinfo"><strong>Naming Conventions used in this Tutorial</strong>: <strong>CT32Bx</strong> and <strong>TMR32Bx</strong> both are same and refer to 32-Bit Timer &#8216;<strong>x</strong>&#8216;. E.g. CT32B0 and TMR32B0 refer to 32-bit Timer 0. <strong>CT32Bx_MATn</strong> refers to Match output &#8216;<strong>n</strong>&#8216; for 32bit Timer &#8216;<strong>x</strong>&#8216;. These are the naming conventions used in the Datasheet and User Manual of LPC11xx.</div>
<h2 class="shead" id="Timer_Register">Registers used in LPC111x Timer Programming</h2>
<p>We will be using CMSIS based <span class="code_var">lpc11xx.h</span> header file for programming. In CMSIS, all the Registers used to program and use 32-bit timers are defined as members of structure(pointer) <span class="code_var">LPC_TMR32Bx</span> where x is the 32-bit Timer module 0 or 1. So for Timer0 we will use <span class="code_var">LPC_TMR32B0</span> and so on. Registers can be accessed by de-referecing the pointer using &#8220;->&#8221; operator. For, example we can access TCR of Timer0 block as <span class="code_var">LPC_TMR32B0->TCR</span>. Similary for 16-bit Timer block, the structure is defined as  <span class="code_var">LPC_TMR16Bx</span> where x is the 16-bit Timer module 0 or 1. Now lets see some of the main 32-bit timer registers.</p>
<div class="highlight">
<p style="margin-bottom:15px;"><strong>1) <span class="doc_ref">PR (TMR32BxPR)</span>: Prescale Register</strong> &#8211; Stores the maximum value of Prescale counter after which it is reset.</p>
<p><strong>2) <span class="doc_ref">PC (TMR32BxPC)</span>: Prescale Counter Register</strong> &#8211; This register increments on every PCLK(Peripheral clock). This register controls the resolution of the timer. When PC reaches the value in PR , PC is reset back to 0 and Timer Counter is incremented by 1. Hence if PR=0 then Timer Counter Increments on every 1 PCLK. If PR=9 then Timer Counter Increments on every 10th cycle of PCLK. Hence by selecting an appropriate prescale value we can control the resolution of the timer. </p>
<p><strong>3) <span class="doc_ref">TC (TMR32BxTC)</span>: Timer Counter Register</strong> &#8211; This is the main counting register. Timer Counter increments when PC reaches its maximum value as specified by PR. If timer is not reset explicitly(directly) or by using an interrupt then it will act as a free running counter which resets back to zero when it reaches its maximum value which is 0xFFFFFFFF.</p>
<p><strong>4) <span class="doc_ref">TCR (TMR32BxTCR)</span>: Timer Control Register</strong> &#8211; This register is used to enable , disable and reset TC. When bit0 is 1 timer is enabled and when 0 it is disabled. When bit1 is set to 1 TC and PC are set to zero together in sync on the next positive edge of PCLK. Rest of the bits of TCR are reserved.</p>
<p><strong>5) <span class="doc_ref">CTCR  (TMR32BxCTCR)</span>: Count Control register</strong> &#8211; Used to select Timer/Counter Mode. When the value of the CTCR is set to 0x0 Timer Mode is selected.</p>
<p><strong>6) <span class="doc_ref">MCR (TMR32BxMCR)</span>: Match Control register</strong> &#8211; This register is used to control which all operations can be done when the value in MR matches the value in TC. Bits 0,1,2 are for MR0 , Bits 3,4,5 for MR1 and so on.. Here&#8217;s a quick table which shows the usage:</p>
<p><strong>For MR0:</strong></p>
<ul style="margin-bottom:0px;">
<li>Bit 0 : Interrupt on MR0 i.e. trigger an interrupt when MR0 matches TC. Interrupts are enabled when set to 1 and disabled when set to 0.</li>
<li>Bit 1 : Reset on MR0. When set to 1 , TC will be reset when it matched MR0. Disabled when set to 0.</li>
<li>Bit 2 : Stop on MR0. When set to 1 , TC &#038; PC will stop when MR0 matches TC.</li>
</ul>
<p><strong>Similarly bits 3-5 , 6-8 , 9-11 are for MR1 , MR2 , MR3 respectively.</strong></p>
<p><strong>7) <span class="doc_ref">IR (TMR32BxIR)</span>: Interrupt Register</strong> &#8211; It contains the interrupt flags for 4 match and 2 capture interrupts. Bit0 to bit3 are for MR0 to MR3 interrupts respectively. And similarly the next 2 for CR0 &#038; CR1 interrupts. when an interrupt is raised the corresponding bit in IR will be set to 1 and 0 otherwise. Writing a 1 to the corresponding bit location will reset the interrupt &#8211; which is used to acknowledge the completion of the corresponding ISR execution.</p>
<p><strong>8) <span class="doc_ref">EMR  (TMR32BxEMR)</span>: External Match Register</strong> &#8211; It provides both status and control of External Match Output Pins. First four bits are for EM0 to EM3. Next 8 bits are for EMC0 to EMC3 in pairs of 2.</p>
<ul style="margin-bottom:0px;">
<li>Bit 0 &#8211; EM0: External Match 0. When a match occurs between TC and MR0, depending on bits[5:4] i.e. EMC0 of this register, this bit can either toggle, go LOW, go HIGH, or do nothing. This bit is driven to MATx.0 where x=Timer number. </li>
<li>Similarly for Bits 1 (EM1), 2 (EM2) &#038; 3 (EM3).</li>
<li>Bits[5:4] &#8211; EMC0: External Match 0. The values in these bits select the functionality of EM0 as follows:
<ul style="margin-bottom:0px;margin-top:0px;">
<li>0x0 &#8211; Do nothing</li>
<li>0x1 &#8211; Clear the corresponding External Match output to 0 (MATx.m pin is LOW).</li>
<li>0x2 &#8211; Set the corresponding External Match output to 1 (MATx.m pin is HIGH).</li>
<li>0x3 &#8211; Toggle the corresponding External Match output.</li>
</ul>
</li>
<li>Similarly for Bits[7:6] &#8211; EMC1, Bits[9,8] &#8211; EMC2, Bits[11:10] &#8211; EMC3.</li>
</ul>
</div>
<h2 class="shead" id="Configure_Timer">Configuring Timers in LPC1114</h2>
<p>To use timers we need to first configure &#038; initialize them. We need to set appropriate values in <span class="doc_ref">CTCR</span>, <span class="doc_ref">IR</span>, <span class="doc_ref">PR</span> registers and reset <span class="doc_ref">PC</span>, <span class="doc_ref">TC</span> registers. Finally we assign <span class="doc_ref">TCR = 0x01</span> which enables the timer. </p>
<div class="highlight">I would recommend to use the following sequence for Setting up 32-bit Timers:</p>
<ol style="margin-bottom:0px">
<li>Enable Clock Input for the Timer Block using <span class="code_var">LPC_SYSCON->SYSAHBCLKCTRL</span> register</li>
<li>Set appropriate value in <span class="code_var">LPC_TMR32Bx->CTCR</span></li>
<li>Define the Prescale value in <span class="code_var">LPC_TMR32Bx->PR</span></li>
<li>Set Value(s) in Match Register(s) if required</li>
<li>Set appropriate value in <span class="code_var">LPC_TMR32Bx->MCR</span> if using Match registers / Interrupts</li>
<li>Reset Timer using <span class="code_var">LPC_TMR32Bx->TCR</span> &#8211; Which resets PR and TC</li>
<li>Set <span class="code_var">LPC_TMR32Bx->TCR</span> to <span class="code_var">0x01</span> to Enable the Timer when required</li>
<li>Reset <span class="code_var">LPC_TMR32Bx->TCR</span> to <span class="code_var">0x00</span> to Disable the Timer when required</li>
</ol>
</div>
<p><strong>Selecting Match Outputs:</strong> Match outputs for Timer0(CT32B0) and Timer1(CT32B1) is available as an alternate function on pins given below. Match output function must be selected using <a href="https://www.ocfreaks.com/lpc1114-lpc1343-lpc_iocon-register-tutorial/" target="_blank">IOCON registers</a> for the following pins:</p>
<ul style="margin-bottom:10px;">
<li>Timer0 MAT 0 (CT32B0_MAT0) &#8211; PIO1_6</li>
<li>Timer0 MAT 1 (CT32B0_MAT1) &#8211; PIO1_7</li>
<li>Timer0 MAT 2 (CT32B0_MAT2) &#8211; PIO0_1</li>
<li>Timer0 MAT 3 (CT32B0_MAT3) &#8211; PIO2_8</li>
<li>Timer1 MAT 0 (CT32B1_MAT0) &#8211; PIO1_1</li>
<li>Timer1 MAT 1 (CT32B1_MAT1) &#8211; PIO1_2</li>
<li>Timer1 MAT 2 (CT32B1_MAT2) &#8211; PIO1_3</li>
<li>Timer1 MAT 3 (CT32B1_MAT3) &#8211; PIO1_4</li>
</ul>
<p>You can go through my previous <a href="https://www.ocfreaks.com/lpc1114-lpc1343-lpc_iocon-register-tutorial/" target="_blank">tutorial on IOCON register</a> regarding its usage.</p>
<p><strong>Enabling System AHB Clock for Timer:</strong> The input clock for timers is from System AHB clock or simply the System Clock. The System Clock can be derived from the Main Clock using System AHB Clock Divider Register <span class="doc_ref">SYSAHBLKDIV</span>. System Clock for Peripherals like Timers needs to be enabled, before using them, by setting appropriate bits (1=enable, 0=disable) in System AHB Clock Control Register <span class="doc_ref">SYSAHBCLKCTRL</span> as given below:</p>
<ul>
<li>Bit 7 For 16-Bit Timer0 (CT16B0)</li>
<li>Bit 8 For 16-Bit Timer1 (CT16B1)</li>
<li>Bit 9 For 32-Bit Timer0 (CT32B0)</li>
<li>Bit 10 For 32-Bit Timer1 (CT32B1)</li>
</ul>
<div class="special sp_red noteinfo">Note: In LPC1114, the Timer block doesn&#8217;t have a dedicated input Clock Divider. The System Clock (CCLK) is directly given to Timer block. Hence for Timer, <strong>PCLK=CCLK</strong>. Other Peripherals like UART and SPI have their own dividers for clock inputs which is derived directly from Main Clock (MCLK). The default System Clock configured by Startup Code is 48Mhz.</div>
<div class="special sp_blue notestar">
<h4 id="Prescaler_Calc">LPC1114 Timer Prescaler Calculations:</h4>
<p>The delay or time required for 1 clock cycle when PCLK/CCLK(System Clock) = &#8216;X&#8217; Mhz is given by:</p>
<div class="equation">T<sub>PCLK</sub> =</p>
<div class="fraction"><span class="fup">1</span><span class="bar">/</span><span class="fdn">PCLK<sub>Hz</sub></span></div>
<p>=</p>
<div class="fraction"><span class="fup">1</span><span class="bar">/</span><span class="fdn">X * 10<sup>6</sup></span></div>
<p>Seconds</p></div>
<p>It is also the maximum resolution Timer block can provide at a given PCLK(CCLK) frequency of X Mhz. The general formula for Timer resolution at X Mhz PCLK(CCLK) and a given value for prescale (PR) is as given below:</p>
<div class="equation">T<sub>RES</sub> =</p>
<div class="fraction"><span class="fup">PR+1</span><span class="bar">/</span><span class="fdn">PCLK<sub>Hz</sub></sup></span></div>
<p> =</p>
<div class="fraction"><span class="fup">PR+1</span><span class="bar">/</span><span class="fdn">X * 10<sup>6</sup></span></div>
<p>Seconds</p></div>
<p>Hence, we get the <strong>Formula for Prescaler (PR) for required Timer resolution (T<sub>RES</sub> in Secs)</strong> at given <strong>PCLK(in Hz)</strong> frequency as:</p>
<div class="equation">PR = (PCLK<sub>Hz</sub> * T<sub>RES</sub>) &#8211; 1</div>
<div class="equation">PR = ((X * 10<sup>6</sup>) * T<sub>RES</sub>) &#8211; 1</div>
<p>Note that here, the resolution is also the time delay required to increment TC by 1.</p>
<p>Hence, Prescaler value for 1 micro-second resolution/ 1us time delay at 48 Mhz PCLK(CCLK) is,</p>
<div class="equation">PR<sub>1us</sub> = (48Mhz * 1uS) -1 = (48*10<sup>6</sup> * 10<sup>-6</sup>) -1 = 47</div>
<p>Prescale for 1 mS (milli-second) resolution at 48Mhz PCLK(CCLK) is,</p>
<div class="equation">PR<sub>1ms</sub> = (48Mhz * 1ms) -1 = (48*10<sup>6</sup> * 10<sup>-3</sup>) -1 = 47999</div>
</div>
<p>Now lets implement to basic function required for Timer Operation:<br />
<strong>1.</strong> <span class="code_var">void initTimer0(void);</span><br />
<strong>2.</strong> <span class="code_var">void delayMS(unsigned int milliseconds);</span> </p>
<h4>#1) initTimer0(void); [Used in Example #1] </h4>
<div class="special sp_red notestar">
<strong>Attention Plz! :</strong> This function is used to setup and initialize the Timer block. Timer blocks use System AHB Clock (CCLK) as their Input. In our case it is assumed that LPC1114 CPU Clock (CCLK) is set at 48Mhz. Note that this clock speed is default which is configured by startup/boot code.</p>
</div>
<pre><code class="language-cpp">
#define PRESCALE (48000-1)

void initTimer0(void)
{
	/*Assuming CCLK (System Clock) = 48Mhz (set by startup code)*/

	LPC_TMR32B0->CTCR = 0x0;
	LPC_TMR32B0->PR = PRESCALE; //Increment TC at every 47999+1 clock cycles
	//48000 clock cycles @48Mhz = 1 mS

	LPC_TMR32B0->TCR = 0x02; //Reset Timer
}
</code></pre>
<h4> #2) delayMS(unsigned int milliseconds); &#8211; LPC1114 Timer Delay Function </h4>
<pre><code class="language-cpp">
void delayMS(unsigned int milliseconds) //Using Timer0
{
	LPC_TMR32B0->TCR = 0x02; //Reset Timer

	LPC_TMR32B0->TCR = 0x01; //Enable timer

	while(LPC_TMR32B0->TC < milliseconds); //wait until timer counter reaches the desired delay

	LPC_TMR32B0->TCR = 0x00; //Disable timer
}
</code></pre>
<div class="spaced">
</div>
<h2 class="shead" id="Examples">Real World LPC1114 Timer Examples with sample code</h2>
<h4 id="Example_1"> Example 1) &#8211; LPC111x Blinky Example using Timer &#038; GPIO</h4>
<p>Now lets write a C/C++ blinky program which flashes an LED every half a second. Since 0.5 second = 500 millisecond we will invoke timer delay function &#8216;delayMS&#8217; as <span class="code_var">delayMS(500)</span>. The LED is connected to Pin PIO0_7.</p>
<pre><code class="language-cpp">
/*(C) Umang Gajera - www.ocfreaks.com 2011-17.
More Embedded tutorials @ www.ocfreaks.com/cat/embedded/
LPC1114 Basic Timer example. License: GPL.*/

#include &lt;lpc11xx.h&gt;

#define PRESCALE (48000-1) //48000 PCLK clock cycles to increment TC by 1 

void delayMS(unsigned int milliseconds);
void initTimer0(void);

int main(void)
{
	//SystemInit(); //called by Startup Code before main(), hence no need to call again.
	initTimer0(); //Initialize Timer0
	LPC_GPIO0->DIR = (1<<7); //Configure PIO0_7 as output

	while(1)
	{
		LPC_GPIO0->DATA = (1<<7); //Turn LED ON
		delayMS(500); //0.5 Second(s) Delay
		LPC_GPIO0->DATA = ~(1<<7); //Turn OFF LED
		delayMS(500);
	}
	//return 0; //normally this wont execute ever
}

void initTimer0(void)
{
	/*Assuming CCLK (System Clock) = 48Mhz (set by startup code)*/
	LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9); //Enable 32Bit Timer0 Clock
	LPC_TMR32B0->CTCR = 0x0;
	LPC_TMR32B0->PR = PRESCALE; //Increment LPC_TMR32B0->TC at every 47999+1 clock cycles
	//48000 clock cycles @48Mhz = 1 mS
	LPC_TMR32B0->TCR = 0x02; //Reset Timer
}

void delayMS(unsigned int milliseconds) //Using Timer0
{
	LPC_TMR32B0->TCR = 0x02; //Reset Timer
	LPC_TMR32B0->TCR = 0x01; //Enable timer

	while(LPC_TMR32B0->TC < milliseconds); //wait until timer counter reaches the desired delay

	LPC_TMR32B0->TCR = 0x00; //Disable timer
}

</code></pre>
<div class="highlight"><strong>MCUXpresso Project for Example #1 on GitHub @</strong> <a href="https://github.com/OCFreaks/LPC1114-Tutorial-Examples/tree/master/MCUXpresso/Timer/LPC1114_Timer_Ex_1/" target="_blank">Example 1</a>, <strong>Download Project Workspace as Zip</strong> @ <a href="https://minhaskamal.github.io/DownGit/#/home?url=https://github.com/OCFreaks/LPC1114-Tutorial-Examples/tree/master/MCUXpresso/Timer" target="_blank">Timer_Ex_1_2_3.Zip</a>. <strong>Note:</strong> Source code for all examples will also work for KEIL ARM as well.</div>
<h4 id="Example_2"> Example 2) &#8211; LPC111x example code using Timer &#038; Match Outputs </h4>
<p>In this C/C++ Example we will use Match outputs 0 and 1 of 32-bit Timer 0 i.e. CT32B0_MAT0 and CT32B0_MAT1. CT32B0_MAT0 is pinned to PIO1_6(P1.6) and CT32B0_MAT1 pinned to PIO1_7(P1.7). You can connect LED to any of the Match ouput pin (PIO1_6 or PIO1_7) and see the code in action. Note that, on LPCXpresso 1114 Board PIO1_6 is marked as RXD and PIO1_7 is marked as TXD since these pin have UART alternate functions.</p>
<pre><code class="language-cpp">
/*(C) Umang Gajera - https://www.ocfreaks.com 2011-17.
More Embedded tutorials @ https://www.ocfreaks.com/cat/embedded/
LPC1114 Timer example 2. License: GPL.*/

#include &lt;lpc11xx.h&gt;

#define PRESCALE (48000-1) //48000 PCLK clock cycles to increment TC by 1 

void initTimer0(void);

int main (void)
{
	//SystemInit(); //called by Startup Code before main(), hence no need to call again.

	LPC_IOCON->PIO1_6 |= 0x2; //Select CT32B0_MAT0 function - Marked as TXD on LPCXpresso Board
	LPC_IOCON->PIO1_7 |= 0x2; //Select CT32B0_MAT1 function - Marked as RXD on LPCXpresso Board

	initTimer0(); //Initialize Timer0

	while(1)
	{
		//Idle loop
	}
	//return 0; //normally this won't execute
}

void initTimer0(void)
{
	/*Assuming CCLK (System Clock) = 48Mhz (set by startup code)*/
	LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9); //Enable 32Bit Timer0 Clock
	LPC_TMR32B0->CTCR = 0x0;
	LPC_TMR32B0->PR = PRESCALE; //Increment LPC_TMR32B0->TC at every 47999+1 clock cycles
	//48000 clock cycles @48Mhz = 1 mS

	LPC_TMR32B0->MR0 = 500; //toggle time in mS
	LPC_TMR32B0->MCR = (1<<1); //Reset on MR0 Match
	LPC_TMR32B0->EMR |= (1<<7) | (1<<6) | (1<<5) | (1<<4); //Toggle Match output for MAT0.0(P2.5), MAT0.1(P2.6)

	LPC_TMR32B0->TCR = 0x02; //Reset Timer
	LPC_TMR32B0->TCR = 0x01; //Enable timer
}
</code></pre>
<div class="highlight"><strong>MCUXpresso Project for Example #2 on GitHub @</strong> <a href="https://github.com/OCFreaks/LPC1114-Tutorial-Examples/tree/master/MCUXpresso/Timer/LPC1114_Timer_Ex_2/" target="_blank">Example 2</a>, <strong>Download Project Workspace as Zip</strong> @ <a href="https://minhaskamal.github.io/DownGit/#/home?url=https://github.com/OCFreaks/LPC1114-Tutorial-Examples/tree/master/MCUXpresso/Timer" target="_blank">Timer_Ex_1_2_3.Zip</a>.</div>
<h4 id="Example_3"> Example 3) &#8211; LPC1114 Timer Interrupt Example Code </h4>
<p>Here we will use a timer interrupt function which will be called periodically to blink an LED. The Timer Interrupt Service Routine (ISR) will toggle PIO0_7(P0.7) every time it is called. If you are using C++ file then you will need to add <span class="code_var">extern &#8220;C&#8221;</span> before the ISR definition or C++ linker won&#8217;t be able to link it properly to generate finally binary/hex file. For C code this is not required. </p>
<div class="special sp_red noteinfo">
<strong>Note:</strong> Please take your time to go through &#8220;initTimer0()&#8221; function. Here I&#8217;ve setup LPC111x Timer Interrupt handler which gets triggered when value in TC equals the value in MR0. I will discuss Interrupts in an upcoming tutorial.</a> </div>
<pre><code class="language-cpp">
/*(C) Umang Gajera - https://www.ocfreaks.com
More Embedded tutorials @ https://www.ocfreaks.com/cat/embedded/
LPC1114 Timer example 3. License: GPL.*/

#include &lt;lpc11xx.h&gt;

#define PRESCALE (48000-1) //48000 PCLK clock cycles to increment TC by 1 

void initTimer0();

int main(void)
{
	//SystemInit(); //called by Startup Code before main(), hence no need to call again.
	LPC_GPIO0->DIR |= (1<<7); //set PIO0_7 as output
	initTimer0();

	while(1)
	{
		//Idle loop
	}
	//return 0; //normally this won't execute
}

void initTimer0(void)
{
	/*Assuming CCLK (System Clock) = 48Mhz (set by startup code)*/
	LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9); //Enable 32Bit Timer0 Clock

	LPC_TMR32B0->CTCR = 0x0;
	LPC_TMR32B0->PR = PRESCALE; //Increment LPC_TMR32B0->TC at every 47999+1 clock cycles
	//48000 clock cycles @48Mhz = 1 mS

	LPC_TMR32B0->MR0 = 500; //Toggle Time in mS
	LPC_TMR32B0->MCR |= (1<<0) | (1<<1); // Interrupt &#038; Reset on MR0 match
	LPC_TMR32B0->TCR |= (1<<1); //Reset Timer0

	NVIC_EnableIRQ(TIMER_32_0_IRQn); //Enable timer interrupt

	LPC_TMR32B0->TCR = 0x01; //Enable timer
}

extern "C" void TIMER32_0_IRQHandler(void) //Use extern "C" so C++ can link it properly, for C it is not required
{
	LPC_TMR32B0->IR |= (1<<0); //Clear MR0 Interrupt flag
	LPC_GPIO0->DATA ^= (1<<7); //Toggle LED
}

</code></pre>
<div class="highlight"><strong>MCUXpresso Project for Example #1 on GitHub @</strong> <a href="https://github.com/OCFreaks/LPC1114-Tutorial-Examples/tree/master/MCUXpresso/Timer/LPC1114_Timer_Ex_3/" target="_blank">Example 3</a>, <strong>Download Project Workspace as Zip</strong> @ <a href="https://minhaskamal.github.io/DownGit/#/home?url=https://github.com/OCFreaks/LPC1114-Tutorial-Examples/tree/master/MCUXpresso/Timer" target="_blank">Timer_Ex_1_2_3.Zip</a>.</div>
<p>The post <a href="https://www.ocfreaks.com/lpc1114-timer-programming-tutorial/">LPC1114 Timer Programming Tutorial</a> appeared first on <a href="https://www.ocfreaks.com">OCFreaks!</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ocfreaks.com/lpc1114-timer-programming-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2900</post-id>	</item>
		<item>
		<title>LPC1114 GPIO Programming Tutorial</title>
		<link>https://www.ocfreaks.com/lpc1114-gpio-programming-tutorial/</link>
					<comments>https://www.ocfreaks.com/lpc1114-gpio-programming-tutorial/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[Umang Gajera]]></dc:creator>
		<pubDate>Wed, 06 Sep 2017 16:21:47 +0000</pubDate>
				<category><![CDATA[Embedded]]></category>
		<category><![CDATA[lpc1114]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">http://www.ocfreaks.com/?p=2844</guid>

					<description><![CDATA[<p>In this tutorial we will learn LPC1114/LPC1115 GPIO Programming. This tutorial is also applicable for LPC11U14, LPC11C14 as well. LPC1114 is a ARM Cortex-M0 based MCU by NXP. The Name of Registers, Data structures that I have used in this guide are defined in LPC11xx.h header file. Most of the function pins on LPC1114, LPC1115, [&#8230;]</p>
<p>The post <a href="https://www.ocfreaks.com/lpc1114-gpio-programming-tutorial/">LPC1114 GPIO Programming Tutorial</a> appeared first on <a href="https://www.ocfreaks.com">OCFreaks!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In this tutorial we will learn LPC1114/LPC1115 GPIO Programming. This tutorial is also applicable for LPC11U14, LPC11C14 as well. LPC1114 is a <strong>ARM Cortex-M0</strong> based MCU by NXP. The Name of Registers, Data structures that I have used in this guide are defined in <span class="code_var">LPC11xx.h</span> header file. </p>
<p>Most of the function pins on LPC1114, LPC1115, LPC11C14 Micro-controllers are grouped into <strong>Ports</strong>. LPC1114 has maximum of <strong>4 ports</strong> viz. Port <strong>0 to 3</strong>. For some LPC1100 devices ports 2 &#038; 3 may be disabled; and even if enabled some pins may be disabled/reserved. The GPIO block in LPC11xx devices is connected to <strong>Peripheral AHB-Lite</strong> bus (<a href="https://en.wikipedia.org/wiki/Advanced_Microcontroller_Bus_Architecture#Advanced_High-performance_Bus_.28AHB.29" target="_blank">Advanced High performance Bus &#8211; Lite</a>) for fast read/write timing. <strong>By default, all pins are configured as inputs after reset.</strong></p>
<p>The registers for each port are grouped into a structure with the following naming convention: <span class="code_var">LPC_GPIOx</span> , where <strong>x</strong> is the port number. The GPIO ports are <strong>12-bit wide</strong> i.e. a <strong>maximum 12 pins</strong> can be mapped, but each port may some pins which cannot be used i.e. they are &#8216;reserved&#8217;. Since the ports are only 12 bits wide, half-word(16 bit) values can be used to assign GPIO registers. For this tutorial I will be using <strong>LPC1114F/302</strong> in LQFP48 package as reference. For other packages like TSSOP28, QFN33, etc. please refer table no. 172 on page 191 of the manual (Rev 12.4) for which pins are available.</p>
<p><strong>For LPC1114 (LQFP48):</strong></p>
<ul>
<li>All 12 pins on Ports 0, 1 and 2 are available.</li>
<li>On Port 3 only first 6 pins (i.e. PIO3_0 to PIO3_5) are available. Rest are reserved.</li>
</ul>
<div class="special sp_blue noteinfo">The naming convention(as used in the Datasheet) for Pins on MCU is ‘<strong>PIOx_y</strong>’ where ‘<strong>x</strong>’ is the port number (0, 1, 2 or 3) and ‘<strong>y</strong>’ is simply the pin number in port ‘<strong>x</strong>’. For example : <strong>PIO0_2</strong> refers to Pin number <strong>2</strong> of Port <strong>0</strong> , <strong>PIO1.10</strong> refers to Pin number <strong>10</strong> in Port <strong>1</strong> on some development boards like LCPXpresso it will be marked <strong>Px.y</strong> instead of PIOx_y. Both mean the same.</div>
<h2 class="shead">GPIO Registers in LPC111x</h2>
<p>The GPIO block has many registers. We will only go through some of these registers that are within the scope of this tutorial.</p>
<div class="highlight">
<p><strong>1) <span class="doc_ref">DATA</span> :</strong> This register can be used to Read or Write values directly to the pins. Regardless of the direction set or any function begin selected for the particular pins, it gives the current state of the GPIO pin when read. When the a pin is configured as GPIO input, then writing to this register won&#8217;t have any affect on the pin. Similarly when a pin is configured for any other digital function, a write will have no effect. Used as <span class="code_var">LPC_GPIOn->DATA</span> in programming when using CMSIS.</p>
<p><strong>2) <span class="doc_ref">DIR</span> :</strong> This is the GPIO direction control register. Setting a bit to 0 in this register will configure the corresponding Pin[0 to 11] to be used as an Input while setting it to 1 will configure it as Output. Used as <span class="code_var">LPC_GPIOn->DIR</span> in programming when using CMSIS.</p>
</div>
<p><strong>IOCON (I/O Configuration) Registers</strong> &#8211; Each pin has a dedicated IOCON register with which we can control: the function of PIN, enable pull-up or pull-down, Hysteresis to filter out spurious changes in input, and other modes like I2C, ADC and Open-Drain. For using digital GPIO we are only concerned with : Changing Pin function , enabling internal pull-up/down resistors and hysteresis. In CMSIS, all the IOCON registers are grouped under <span class="code_var">LPC_IOCON</span> structure. While programming these can be accessed as <span class="code_var">LPC_IOCON->[register-name]</span>. Where <span class="code_var">[register-name]</span> is the name of the IOCON register for a specific PIN as given in datasheet/manual.</p>
<p>The general bit description for IOCON register is as shown in the diagram below:</p>
<p><img decoding="async" class="aligncenter" src="https://www.ocfreaks.com/imgs/embedded/common/lpc1114_lpc1343_iocon.png" height="215px" width="423px" /></p>
<ul>
<li>First 2 bits [2:0] are used for selecting PIN function: 0x0 &#8211; 1st function, 0x1 &#8211; 2nd function, 0x2 &#8211; 3rd function, 0x3 &#8211; 4th function. Refer datasheet for which functions are available for the given PIN.</li>
<li>
Next 2 bits [3:4] are used to select on-chip pull resistors: 0x0 &#8211; Both pull-up/down resistors disabled, 0x1 &#8211; Pull-down resistor enabled, 0x2 &#8211; Pull-up resistor enabled, 0x3 &#8211; Repeater mode.</li>
<li>5th Bit [5] is for Hysteresis. Setting it to 0 will disable Hysteresis and 1 will enable it.</li>
</ul>
<p>I have discussed IOCON register in detail for LPC1114 in my previous tutorial : <a href="https://www.ocfreaks.com/lpc1114-lpc1343-lpc_iocon-register-tutorial/" target="_blank">LPC11xx and LPC13xx LPC_IOCON Register Tutorial</a></p>
<p>Most of the PINS of LPC11xx MCU are <strong>Multiplexed</strong> i.e. these pins can be configured to provide up to 4 alternate <strong>functions</strong>. Not all pins on LPC111x are configured as GPIO by default after reset! For these pins you will need to explicitly re-assign the function to GPIO using IOCON register for the respective pins. Other pins can be directly used as GPIO, since their default function is GPIO (configured as inputs with pull-ups enabled) after reset.</p>
<h2 class="shead">LPC111x GPIO Programming &#038; Example Code in C/C++</h2>
<p><span class="code_var">LPC11xx.h</span> header is based on <span class="doc_ref">CMSIS</span>(Cortex Microcontroller System Interface Standard) developed by ARM. System startup, core CPU access and peripheral definitions are given by <strong>CMSIS-CORE</strong> component of CMSIS. We will be using the definitions given by CMSIS-CORE. The <strong>register definitions</strong> for Cortex-M0 LPC111x MCUs are organized into groups depending on their functionality using &#8220;<strong>C Structure</strong>&#8221; definitions. From C/C++ programming point of view, this makes interfacing peripherals simple. For example all registers for Port 0 are grouped into structure defined as <span class="code_var">LPC_GPIO0</span>. This is similar when programming for any LPC ARM Cortex-M0 MCU using CMSIS.</p>
<p>As per the CMSIS convention, the registers that we saw are grouped into structures. <span class="code_var">LPC_GPIOx</span> is defined as a pointer to this structure in <span class="code_var">LPC11xx.h</span> header. These registers are defined as members of this structure. Hence to use any register, for e.g. DATA, we must use the arrow &#8220;->&#8221; operator to de-reference members of structure (since the structure itself is a pointer) to access the register as follows : <span class="code_var">LPC_GPIO0->DATA</span> = value. For creating LPC1114 projects you can either use KEIL uV5, LPCXpresso or MCUXpresso, but make sure you include CMSIS library. If you are using LPCXpresso LPC1114 board you can check out <a href="https://www.ocfreaks.com/tutorial-using-mcuxpresso-create-cortex-m-project-cmsis/" target="_blank">this tutorial</a>.</p>
<p><strong>Prerequisite :</strong> Before we start programming gpio you need to have basic understanding of Binary and Hexadecimal system and Bitwise operations in C/C++, here are two tutorials which can go through (or if you are already acquainted with these you can skip these and continue below) : </p>
<ul>
<li><a href="https://www.ocfreaks.com/hexadecimal-and-binary-number-system-basics-for-embedded-programming/">Hexadecimal and Binary Number System basics for Embedded Programming</a></li>
<li><a href="https://www.ocfreaks.com/tutorial-embedded-programming-basics-in-c-bitwise-operations/">Tutorial : Embedded programming basics in C – bitwise operations</a></li>
</ul>
<p>Now lets see how we can assign values to registers. We can use Hexadecimal notation &#038; decimal notation for assigning values. If your compiler supports other notations like binary notation use can use that too. Lets say, we want to set <strong>PIN 5</strong> of <strong>Port 0 as output</strong>. It can be done in following ways:</p>
<pre><code class="language-cpp">
CASE 1. LPC_GPIO0->DIR = (1<<5); //(binary using left shift - direct assign: other pins set to 0)

CASE 2. LPC_GPIO0->DIR |= 0x0000020; //or 0x20; (hexadecimal - OR and assign: other pins not affected)

CASE 3. LPC_GPIO0->DIR |= (1<<5); //(binary using left shift - OR and assign: other pins not affected)
</code></pre>
<ul>
<li>In many scenarios, Case 1 must be avoided since we are directly assigning a value to the register. So while we are making PIO0_5 ‘1’ others are forced to be assigned a ‘0’ which can be avoided by <strong>ORing</strong> and then assigning Value.</li>
<li>Case 2 can be used when bits need to be changed in bulk and</li>
<li>Case 3 when some or single bit needs to be changed.</li>
</ul>
<p>First thing to note here is that preceding Zeros in Hexadecimal Notation can be ignored because they have no meaning since we are working with unsigned values here (positive only) which are assigned to Registers. For eg. <span class="code_var">0x1C</span> and <span class="code_var">0x01C</span> and <span class="code_var">0x001C</span> all mean the same.</p>
<div class="special sp_red noteinfo">Note that bit <strong>31</strong> is <strong>MSB on extreme left</strong> and bit <strong>0</strong> is the <strong>LSB on extreme right</strong> i.e. we are using Big Endian Format. Hence bit 0 is the 1st bit from right , bit 1 is the 2nd bit from right and so on. BIT and PIN Numbers are <strong>Zero</strong>(0) indexed which is quite evident since Bit ‘<strong>x</strong>’ refers to <strong>(x-1)<sup>th<sup></strong> location in the corresponding register.</div>
<p>Now, Lets go through some example codes:</p>
<p><strong>Ex. 1)</strong></p>
<p>Consider that we want to configure Pin 1 of Port 0 i.e PIO0_1(P0.1) as Output and want to drive it HIGH. This can be done as :</p>
<pre><code class="language-cpp">
LPC_GPIO0->DIR |= 0x2; //same as (1<<1), Config PIO0_1 as Ouput
LPC_GPIO0->DATA |= 0x2; //Drive Output High for PIO0_1
</code></pre>
<p><strong>Ex. 2)</strong></p>
<p>Making output configured Pin 7 High of Port 0 i.e PIO0_7(P0.7) and then Low can be does as follows:</p>
<pre><code class="language-cpp">
LPC_GPIO0->DIR |= (1<<7); //PIO0_7 is Output pin
LPC_GPIO0->DATA |= (1<<7); //Output for PIO0_7 becomes High
LPC_GPIO0->DATA &= ~(1<<7); //Output for PIO0_7 becomes Low
</code></pre>
<p><strong>Ex. 3)</strong></p>
<p>Configuring PIO0_9(P0.9) and PIO0_3(P0.3) as Output and Setting them High:</p>
<pre><code class="language-cpp">
LPC_GPIO0->DIR |= (1<<9) | (1<<3); //Config PIO0_9 and PIO0_3 as Output
LPC_GPIO0->DATA |= (1<<9) | (1<<3); //Drive Output High for PIO0_9 and PIO0_3
</code></pre>
<p><strong>Ex. 4)</strong></p>
<p>Configuring Pins 4 to 11 of Port 1 (PIO1_4 to PIO1_11) as Output and Setting them High:</p>
<pre><code class="language-cpp">
LPC_GPIO1->DIR |= 0xFF0; //Config PIO1_4 to PIO1_11 as Output
LPC_GPIO1->DATA |= 0xFF0; //Make output High for PIO1_4 to PIO1_11
</code></pre>
<p><strong>Ex. 5)</strong></p>
<p>In this example code, we will configure Pin 5 of Port 1 as Input with Pull-Down & Hysteresis enabled :</p>
<pre><code class="language-cpp">
LPC_GPIO1->DIR &= ~(1<<5); //Config PIO1_5 as input (It will be anyways input after reset)
LPC_IOCON->PIO1_5 = (1<<3) | (1<<5); 
//Enable on-chip Pull-down resistor [4,3]=01, Enable HYS [5]=1
</code></pre>
<p>When using switches as inputs, you can use an RC filter with Hysteresis enabled to debounce the input. Bouncing is the spurious changes in input until the contacts of the switch have stabilized. This can be filtered out using debouncing techniques, either in Software or Hardware. </p>
<p>Now lets play with some real world examples.</p>
<div class="special sp_red notewarning">The below examples are given, assuming <strong>48Mhz CCLK</strong> which is configured & initialized by system startup code generated by Keil UV5/UV4, MCUXpresso, LPCXpresso, CoIDE, etc.</div>
<p><strong>Ex. 6)</strong></p>
<p>LPC11xx Blinky Example Code - Here we drive pin 7 of port 0 (PIO0_7) repeatedly high to low. PIO0_7(P0.7) of LPC11xx devices has 20mA current capability so you can directly drive an LED with it. Connect LED between PIO0_7 and GND. Here we will introduce some "hard-coded" delay between making all pins High and Low (and vice-versa) so it can be noticed. </p>
<pre><code class="language-cpp">
#include &lt;lpc11xx.h&gt;

void delay(void);

int main(void)
{
	LPC_GPIO0->DIR = (1<<7); //Configure PIO0_7 as Output
	
	while(1)
	{
		LPC_GPIO0->DATA = (1<<7); //Drive output high to turn on LED
		// Better way would be LPC_GPIO0->DATA |= (1<<7);
		
		delay();
		LPC_GPIO0->DATA = 0x0; //Drive output low to turn off LED
		// Better way would be LPC_GPIO0->DATA &= ~(1<<7);
		
		delay();
	}
	return 0; //normally this wont execute
}	

void delay(void) //Hard-coded delay function
{
	int count,i=0;
	for(count=0; count < 3000000; count++) //You can edit this as per your needs
	{
		i++; //something needs to be here else compiler will remove the for loop!
	}
}
</code></pre>
<p><strong>Ex. 7)</strong></p>
<p>In this example we will configure PIO1_5(P1.5) as Input and monitor it for a logic LOW on the pin. Here we will use a tactile switch whose one end is connected to PIO1_5 and other to GND (+3.3V). PIO0_7(P0.7) is configured as output and connected to an LED. Initially LED will be off but when the switch is pressed, LED will be turned ON. Once the LED is turned ON it will stay ON until the MCU is reset externally. The setup is shown in the figure below:</p>
<p><img decoding="async" class="aligncenter" src="https://www.ocfreaks.com/imgs/embedded/common/lpc_gpio_eg.png" height="175px" width="492px" /></p>
<pre><code class="language-cpp">
#include &lt;lpc11xx.h&gt;

int main(void)
{
	LPC_GPIO1->DIR &= ~((1<<5)) ; //explicitly making PIO1_5 as Input - even though by default its already Input
	LPC_GPIO0->DIR |= (1<<7); //Configuring PIO0_7 as Output

	LPC_GPIO0->DATA &= ~(1<<7); //drive output low initially

	while(1)
	{
		if( LPC_GPIO1->DATA & (1<<5) ) //Evaluates to True for a 'HIGH' on PIO1_5
		{
			LPC_GPIO0->DATA |= (1<<7); //drive PIO0_7 High
			//Now PIO0_7 will be held high unless the MCU is reset
		}
	}
	return 0; //this wont execute normally
}

</code></pre>
<p><strong>Ex. 8)</strong></p>
<p>Now lets extend example 7 so that when the button is pressed, the LED will glow and when released or not pressed the LED won't glow. Note that in both Example 7 and 8: Since internal pulls are enabled by default when switch is not pressed, internal pull-up resistor will force the input to be HIGH.</p>
<pre><code class="language-cpp">
#include &lt;lpc11xx.h&gt;

void tinyDelay(void);

int main(void)
{
	LPC_GPIO1->DIR &= ~((1<<5)); 
	LPC_GPIO0->DIR |= (1<<7);
	
	LPC_GPIO0->DATA &= ~(1<<7); //Turn off LED initially
	
	while(1)
	{
		if( !(LPC_GPIO1->DATA & (1<<5)) )
		{
			LPC_GPIO0->DATA |= (1<<7); //Input low, so turn led ON
		}
		else
		{
			LPC_GPIO0->DATA &= ~(1<<7); //Input high, so turn led OFF
		}
	}
	
	return 0; //normally this won't execute
}
</code></pre>
<div class="special sp_red notewarning" style="margin-top:20px;">
<strong>Imp. Note:</strong> As mentioned in Datasheet, many of the Pins on LPC111x are <strong>5V tolerant</strong> in digital mode but when used as inputs for ADC block the input voltage must <strong>not exceed V<sub>dd</sub></strong>. However, I would recommend that wherever possible, use a buffer/level-shifter for level translation between <strong>5V and 3.3V</strong> for digital I/O. If you need any help regarding level translation just let me know in the comment section below.</div>
<p>The post <a href="https://www.ocfreaks.com/lpc1114-gpio-programming-tutorial/">LPC1114 GPIO Programming Tutorial</a> appeared first on <a href="https://www.ocfreaks.com">OCFreaks!</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ocfreaks.com/lpc1114-gpio-programming-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2844</post-id>	</item>
		<item>
		<title>LPC1114 and LPC1343 LPC_IOCON Register Tutorial</title>
		<link>https://www.ocfreaks.com/lpc1114-lpc1343-lpc_iocon-register-tutorial/</link>
					<comments>https://www.ocfreaks.com/lpc1114-lpc1343-lpc_iocon-register-tutorial/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[Umang Gajera]]></dc:creator>
		<pubDate>Mon, 04 Sep 2017 17:14:26 +0000</pubDate>
				<category><![CDATA[Embedded]]></category>
		<category><![CDATA[lpc1114]]></category>
		<category><![CDATA[lpc1343]]></category>
		<guid isPermaLink="false">http://www.ocfreaks.com/?p=2835</guid>

					<description><![CDATA[<p>Many ARM Cortex-M series of MCUs from NXP from the LPC1000 family viz. LPC11xx, LPC13xx, etc. have I/O configuration registers for Port Pins called IOCON. In this tutorial we will discuss about the structure of these register and how to use them when programming in C/C++. The structure of IOCON might vary depending on the [&#8230;]</p>
<p>The post <a href="https://www.ocfreaks.com/lpc1114-lpc1343-lpc_iocon-register-tutorial/">LPC1114 and LPC1343 LPC_IOCON Register Tutorial</a> appeared first on <a href="https://www.ocfreaks.com">OCFreaks!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Many ARM Cortex-M series of MCUs from NXP from the LPC1000 family viz. LPC11xx, LPC13xx, etc. have I/O configuration registers for Port Pins called <span class="doc_ref">IOCON</span>. In this tutorial we will discuss about the structure of these register and how to use them when programming in C/C++. The structure of IOCON might vary depending on the specific micro-controller. </p>
<p>Each pin on MCUs like LPC1114, LPC1115, LPC1343, LPC1347, etc.. have a dedicated IOCON register with which we can control: </p>
<ul>
<li>the Function (for e.g. GPIO or any other Function like TXD,I2C,etc..)</li>
<li>the Input Mode i.e. pull-up, pull-down, repeater mode or none </li>
<li>Hysteresis which gives schmitt trigger action to filter out spurious changes on inputs</li>
<li>I2C bus modes for I2C bus pins</li>
<li>selection of analog input mode for ADC pins</li>
<li>selection of open-drain mode for output, if available</li>
</ul>
<p>You can check the User-Manual or Datasheet of your LPC Cortex-M Micro-controller to check which of the above are available. </p>
<div class="special sp_blue notestar">The naming convention of these IOCON registers is <strong>&lt;default_function&gt;_PIOx_y or PIOx_y</strong>. Where x is port number, y is pin number and &#8220;default_function&#8221; is the function that is selected by default after reset, for example: RESET_PIO0_0 , SWCLK_PIO0_10 , PIO0_7 , etc. Those which don&#8217;t have &lt;default_function&gt; prefix, will be GPIOs by default after reset.</div>
<p>General Bit Description of IOCON registers for LPC1114 and LPC1343 is as given below:</p>
<p><img decoding="async" class="aligncenter" src="https://www.ocfreaks.com/imgs/embedded/common/lpc1114_lpc1343_iocon.png" height="215px" width="423px" alt="ioconfig for lpc1114 and lpc1343" /></p>
<p>For pins hosting ADC function the IOCON structure is as follows:</p>
<p><img decoding="async" class="aligncenter" src="https://www.ocfreaks.com/imgs/embedded/lpc1114_lpc1343_iocon/lpc_iocon_adc.png" height="189px" width="441px" alt="lpc_iocon for ADC pins" /></p>
<p>For pins hosting I2C function IOCON bit Mapping is as given below:</p>
<p><img decoding="async" class="aligncenter" src="https://www.ocfreaks.com/imgs/embedded/lpc1114_lpc1343_iocon/lpc_iocon_i2c.png" height="166px" width="423px" alt="lpc_iocon for I2C pins" /></p>
<table class="ocf-table">
<thead>
<td>Bits</td>
<td>Symbol</td>
<td>Significance of values</td>
</thead>
<tr>
<td>[2:0]</td>
<td><strong>FUNC</strong></td>
<td>
Used to select pin Function.<br />
0x0 &#8211; First function, 0x1 &#8211; Second function<br />
0x2 &#8211; Third function, 0x3 &#8211; fourth function
</td>
</tr>
<tr>
<td>[4:3]</td>
<td><strong>MODE</strong></td>
<td>
For selecting pin Mode. Not applicable for pins having I2C functionality.<br />
0x0 &#8211; Both pull-up/pull-down resistors disabled<br />
0x1 &#8211; Pull-down resistor enabled<br />
0x2 &#8211; Pull-up resistor enabled<br />
0x3 &#8211; Repeater mode
</td>
</tr>
<tr>
<td>[5]</td>
<td><strong>HYS</strong></td>
<td>
0 &#8211; Disable Hysteresis, 1 &#8211; Enable Hysteresis
</td>
</tr>
<tr>
<td>[9:6],<br />[9:8],<br />[7]</td>
<td>N/A,<br /><strong>I2CMODE</strong>,<br /><strong>ADMODE</strong></td>
<td>
For most registers bits 6 to 9 are reserved.<br />
For I2C pins bits [9:8] respresents I2C mode and bits [7:3] are reserved.<br />
For ADC pins bit 7 represents ADMODE while bits 6,8,9 are reserved.<br />
Refer Datasheet for info about how to use them.
</td>
</tr>
<tr>
<td>[10]</td>
<td><strong>OD</strong></td>
<td>
For selecting pseudo open-drain mode.<br />
0 &#8211; Standard GPIO Mode, 1 &#8211; Open-drain Mode
</td>
</tr>
<tr>
<td>[31:11]</td>
<td>N/A</td>
<td>
Reserved
</td>
</tr>
</table>
<p><strong>Note:</strong> pseudo open-drain mode is Not available for LPC1343/42/13/12 devices.</p>
<h4>IOCON register for other LPC13x7 devices</h4>
<p>For devices like LPC1347 ICOCON register given additional control like:</p>
<ul>
<li>Logic Inversion of input pins, if available</li>
<li>Input Glitch filter, on pins which support it, if available</li>
</ul>
<p>The general register map for similar devices is as given below:</p>
<p><img decoding="async" class="aligncenter" src="https://www.ocfreaks.com/imgs/embedded/common/lpc1347_iocon.png" alt="lpc_iocon for lpc1347" height="206px" width="416px" /></p>
<p>The different in structure is that:</p>
<ul>
<li>Bit 6 now represents <strong>INV</strong> which is used to invert the Input(HIGH is read LOW and vice-versa) by setting this bit to 1. If the bit is 0 inputs are not inverted.</li>
<li>For a few pins Bit 8 represents <strong>FILTR</strong> which is used to enabled/disable glitch filter. 0 means glitch filter is enabled and 1 means its disabled.</li>
</ul>
<h2 class="shead">Using IOCON registers in programming</h2>
<p>In the device header files based on CMSIS viz. <span class="code_var">lpc11xx.h</span> and <span class="code_var">lpc13xx.h</span> all the IOCON registers are grouped under <span class="code_var">LPC_IOCON</span> structure. While programming in C/C++ , these can be accessed as: <span class="code_var">LPC_IOCON->[register-name]</span>. Where <span class="code_var">[register-name]</span> is the name of the IOCON register for a specific PIN as given in datasheet/manual.</p>
<p>For example, to access IOCON for PIO0_1(P0.1) in your code you must use <span class="code_var">LPC_IOCON->PIO0_1</span> = value.</p>
<p>Eg.:</p>
<pre><code class="language-cpp">
/*LPC11xx*/
LPC_IOCON->RESET_PIO0_0
LPC_IOCON->PIO0_8
LPC_IOCON->R_PIO0_11
...

/*LPC13xx*/
LPC_IOCON->PIO0_1
LPC_IOCON->SWDIOCLK_PIO0_10
LPC_IOCON->R_PIO1_10
...
</code></pre>
<p>The post <a href="https://www.ocfreaks.com/lpc1114-lpc1343-lpc_iocon-register-tutorial/">LPC1114 and LPC1343 LPC_IOCON Register Tutorial</a> appeared first on <a href="https://www.ocfreaks.com">OCFreaks!</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ocfreaks.com/lpc1114-lpc1343-lpc_iocon-register-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2835</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/

Page Caching using Disk: Enhanced 

Served from: www.ocfreaks.com @ 2026-03-05 23:35:22 by W3 Total Cache
-->