lpcware

LPC1759 PWM Capture Control

Discussion created by lpcware Employee on Jun 15, 2016
Content originally posted in LPCWare by stevenvdschoot on Wed May 08 09:13:24 MST 2013

I have a LPC1759 microcontroller. Now I want to use p1[28] and p1[29] as PWM input. I tried to make the code for this using the datasheet, but I seem to do something wrong. Is there some example code available for PWM capture control?


Here is the code I tried:


<hr />
<div>uint32_t PWM1_COUNTER;</div>
<div>void SetupPWM(void)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>SetupPWMOut();</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM_ResetCounter(LPC_PWM1);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM_CounterCmd(LPC_PWM1, ENABLE);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM_Cmd(LPC_PWM1, ENABLE);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM1_COUNTER = 0;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>NVIC_EnableIRQ(PWM1_IRQn);</div>
<div>}</div>
<div>void SetupPWMOut(void)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PINSEL_CFG_Type PinCfg;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM_TIMERCFG_Type PWMCfgDat;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM_MATCHCFG_Type PWMMatchCfgDat;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PinCfg.Funcnum <span class="Apple-tab-span" style="white-space: pre;"> </span>= PINSEL_FUNC_1;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PinCfg.OpenDrain <span class="Apple-tab-span" style="white-space: pre;"> </span>= PINSEL_PINMODE_NORMAL;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PinCfg.Pinmode <span class="Apple-tab-span" style="white-space: pre;"> </span>= PINSEL_PINMODE_PULLUP;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PinCfg.Portnum <span class="Apple-tab-span" style="white-space: pre;"> </span>= 2;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PinCfg.Pinnum <span class="Apple-tab-span" style="white-space: pre;"> </span>= 0;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PINSEL_ConfigPin(&amp;PinCfg);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWMCfgDat.PrescaleValue = 8;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void *) &amp;PWMCfgDat);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM_MatchUpdate(LPC_PWM1, 0, 1024, PWM_MATCH_UPDATE_NOW);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWMMatchCfgDat.IntOnMatch = DISABLE;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWMMatchCfgDat.MatchChannel = 0;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWMMatchCfgDat.ResetOnMatch = ENABLE;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWMMatchCfgDat.StopOnMatch = DISABLE;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM_ConfigMatch(LPC_PWM1, &amp;PWMMatchCfgDat);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM_MatchUpdate(LPC_PWM1, 1, 512, PWM_MATCH_UPDATE_NOW);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWMMatchCfgDat.IntOnMatch = DISABLE;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWMMatchCfgDat.MatchChannel = 1;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWMMatchCfgDat.ResetOnMatch = DISABLE;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWMMatchCfgDat.StopOnMatch = DISABLE;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM_ConfigMatch(LPC_PWM1, &amp;PWMMatchCfgDat);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM_ChannelCmd(LPC_PWM1, 1, ENABLE);</div>
<div>}</div>
<div>void setupPWMIN(uint8_t channel, FunctionalState rising_edge, FunctionalState falling_edge)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PINSEL_CFG_Type PinCfg;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PinCfg.Funcnum <span class="Apple-tab-span" style="white-space: pre;"> </span>= PINSEL_FUNC_2;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PinCfg.OpenDrain <span class="Apple-tab-span" style="white-space: pre;"> </span>= PINSEL_PINMODE_NORMAL;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PinCfg.Pinmode <span class="Apple-tab-span" style="white-space: pre;"> </span>= PINSEL_PINMODE_TRISTATE;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PinCfg.Portnum <span class="Apple-tab-span" style="white-space: pre;"> </span>= 1;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PinCfg.Pinnum <span class="Apple-tab-span" style="white-space: pre;"> </span>= (channel==0)?28:29;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PINSEL_ConfigPin(&amp;PinCfg);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM_CAPTURECFG_Type PWMCaptureCfgDat;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWMCaptureCfgDat.CaptureChannel = (channel==1);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWMCaptureCfgDat.RisingEdge = ENABLE;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWMCaptureCfgDat.FallingEdge = ENABLE;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWMCaptureCfgDat.IntOnCaption = ENABLE;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM_ConfigCapture(LPC_PWM1, &amp;PWMCaptureCfgDat);</div>
<div>}</div>
<div>uint32_t readPWM(uint8_t channel)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>return PWM_GetCaptureValue(LPC_PWM1, (channel==1));</div>
<div>}</div>
<div>uint32_t lastPWMData;</div>
<div>void PWM1_IRQHandler(void)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>PWM1_COUNTER++;</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>lastPWMData = readPWM(0);</div>
<div><span class="Apple-tab-span" style="white-space: pre;"> </span>LPC_PWM1-&gt;IR = 0xFF &amp; PWM_IR_BITMASK;</div>
<div>}</div>
<hr />

Outcomes