Lpc1347 pwm

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Lpc1347 pwm

2,977 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ECamino on Tue Oct 16 06:52:33 MST 2012
Hi,

We have a board wired with PIO0_16 needing PWM as CT32B1_MAT3. We just need a 30% duty on 38kHz. I have the following code to set it up. Using MR2 for period and MR3 for the duty. But I see no output. Is there something else I need to do to get a signal?

[SIZE=2]GPIOSetDir(0,16,1); [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// set port 1, pin 31 to output[/COLOR][/SIZE][/COLOR][/SIZE]

[LEFT][SIZE=2]LPC_IOCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]PIO0_16[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] |= (0x01<<2); [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// set up for CT32B1_MAT3.[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]

[SIZE=2]LPC_SYSCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]SYSAHBCLKCTRL[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] |= (0x1<<10); [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// enable peripheral clock to drive timer CT32B1[/COLOR][/SIZE][/COLOR][/SIZE]

[SIZE=2]LPC_CT32B1->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]MR2[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 0x13b; [/SIZE]

[SIZE=2]LPC_CT32B1->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]MR3[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]= 0x69;[/SIZE]


[LEFT][SIZE=2]LPC_CT32B1->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]EMR[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] |= (0x01<<10); [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// may not be necessary for PWM?[/COLOR][/SIZE][/COLOR][/SIZE]
[LEFT][SIZE=2]LPC_CT32B1->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]MCR[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] |= (0x01<<7); [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Set MR2R. TC reset on MRR match[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
[/LEFT]



[SIZE=2]LPC_CT32B1->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]PWMC[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] |= (0x01<<3); [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// PWM enabled for channel 3.[/COLOR][/SIZE][/COLOR][/SIZE]

[SIZE=2]LPC_CT32B1->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TCR[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 1; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// turn on the timer[/COLOR][/SIZE][/COLOR][/SIZE]
0 Kudos
Reply
15 Replies

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mathseng on Thu Oct 22 20:50:01 MST 2015
I have just found a bug in the calculation of the PR Prescale Register value, in my code   PWM.C

The buggy code:
LPC_CT16B0->PR = (periphClock + freq115*gran115) / (freq115*gran115) - 1;

Aim: The aim was to have the PR prescale register count up to the size of a granule, and the MR Match Register count the number of granules.

Error: The PR calculation is in error when the multiplication of frequency and granule overflows ('C' promotion rules on multiplication apply).

Correction: Cast the one or both of the two multiplication variables to uint64_t, thus giving a uint64_t result, in both multiplications.

Note: Example code ignores corrections for overflow for 16 bit timers, in calculating PR and MR. My production code does handle this scenario.


0 Kudos
Reply

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by nidalpres on Wed Feb 25 21:10:37 MST 2015
mathseng, thanks for the sample code, it worked for me too.

I use LPCOpen core library for 13xx so I wrote example for the library too.

However, in order for this example to compile and work with lpcopne v2.05 for LPCXpresso IDE (the one I'm using, downloaded from here http://www.lpcware.com/content/nxpfile/lpcopen-software-development-platform-lpc13xx-packages), you will have to modify lpc_chip_13xx/inc/timer_13xx.h header file. Structure LPC_TIMER_T defined near the top of that file is missing PWMC register.

Change this:

    __IO uint32_t CTCR;             /*!< Count Control Register. The CTCR selects between Timer and Counter mode, and in Counter mode selects the signal and edge(s) for counting. */
} LPC_TIMER_T;


to this

    __IO uint32_t CTCR;             /*!< Count Control Register. The CTCR selects between Timer and Counter mode, and in Counter mode selects the signal and edge(s) for counting. */
    __IO uint32_t PWMC;             /*!< PWM Control Register. The PWMC configures match outputs as PWM outputs. Each match output can independently be PWM or can be controlled by EMR */
} LPC_TIMER_T;


Now you have access to PWMC (no API for that, so I set it directly in the example).
0 Kudos
Reply

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by josephfreiburg on Tue Jan 06 18:34:48 MST 2015
Hello mathseng,

I needed a quick pwm test program and found your code. It worked great! Thank you for sharing!
0 Kudos
Reply

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mathseng on Wed Jan 15 17:33:38 MST 2014
Hi,
I am using LPCXpresso LPC1347. I tried all the examples in this thread, and found that I could not get PWM out until I had the duty cycle in MR0 and the period value in some other MR. The examples showed the addresses, but no mention of the need to match MRx with MATx, and of course, they used different PIOs.

I also found that I could reliably change the duty cycle at any time, without stopping the timer, nor needing to rely on interrupt code. I have not checked the waveform for the actual cycle when MR(dutycycle) is updated with the new duty cycle value. I would expect that there is a possibility of one period where the output is fixed for the full period - this is not an issue with my application.

FWIW - attached is my code, which runs happily with either a new LPCXpresso 1347 project, or with FreeRTOS.

mathseng

drat - you always find out more answers, just after you say something.
0 Kudos
Reply

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by RodPinar on Fri Oct 19 13:08:44 MST 2012
You made my day.
thanks a lot
Rod.
0 Kudos
Reply

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Fri Oct 19 09:16:19 MST 2012
[B][U]Tutorials:[/U][/B]

LPCXpresso Introduction part 1 training video
http://www.youtube.com/watch?v=dV7rG2VdG9E&feature=relmfu

LPCXpresso Introduction part 2 training video
http://www.youtube.com/watch?v=cLvGwmJAA7k&feature=relmfu

Learn Eclipse the LPCXpresso way - from the developers ;)
http://www.youtube.com/watch?v=E6vSPloei-0

NXP LPCXpresso Homepage :D
http://ics.nxp.com/lpcxpresso/

and of course: LPCXpressoGettingStarted.pdf in your LPCXpresso directory  (...\LPCXpresso_4.3.0_1023\lpcxpresso)  :)
0 Kudos
Reply

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by RodPinar on Fri Oct 19 05:28:30 MST 2012
[FONT=Calibri]No, absolutely not. LPCXpresso installed the entire examplespretty good. Which one is the most relative to a blink Led? Where can I find Iguide “ step by step” to load the library need it to run at least one example.[/FONT]

[FONT=Calibri]Thank you Zero,[/FONT]

[FONT=Calibri]Rod. [/FONT]
0 Kudos
Reply

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Oct 18 13:09:12 MST 2012
Did LPCXpresso refuse to install example folder :confused:

If not, try ...\LPCXpresso_4.3.0_1023\lpcxpresso\Examples\NXP\LPC1000\LPC13xx_12bitADC ;)
0 Kudos
Reply

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by RodPinar on Thu Oct 18 12:40:07 MST 2012
[FONT=Calibri]I just was looking for some kind of information, and I foundyour topic interesting to me. I would like to start with some example code forLPCXpresso 1347.[/FONT]

[FONT=Calibri]The one you are talking about it is something that I WOULDLIKE TO DO JUST TO "START" programing with IDE. Power on a led andthem with pwm dimmer or blink it.[/FONT]

[FONT=Calibri]Do know any example code that you can recommend to me, anybook, etc.[/FONT]

[FONT=Calibri]Thank you[/FONT]

[FONT=Calibri]Rod.[/FONT]

[FONT=Calibri] [/FONT]
0 Kudos
Reply

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ECamino on Tue Oct 16 11:11:27 MST 2012
[FONT=Arial].[/FONT]
[COLOR=#3f7f5f][COLOR=#3f7f5f]
[/COLOR][/COLOR]
0 Kudos
Reply

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Tue Oct 16 10:11:48 MST 2012
#1 Delete this nonsense

GPIOSetDir(0,16,1);  // set port 0, pin 16 to output
#2 Change IOCON settings, just copying is wrong :rolleyes:

#3 Use Peripherals View to check: SYSAHBCLKCTRL, especially IOCON (bit16)

#4 Use Peripherals View to check: CT32B1
0 Kudos
Reply

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ECamino on Tue Oct 16 10:02:36 MST 2012
.
0 Kudos
Reply

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Tue Oct 16 09:24:15 MST 2012
This is a working 1343 sample (12ms cycle, 4 ms high), so don't forget to change PIO1.4
 LPC_SYSCON->SYSAHBCLKCTRL |= (1<<10);//enable clock CT32B1
 LPC_IOCON->PIO1_4= (2<<0)|(2<<3)|(1<<6)|(1<<7);//CT32B1_MAT3 & pullup & digital
 LPC_TMR32B1->MCR = (1<<7);            //MR2 reset
 LPC_TMR32B1->PR =  72000 -1;        //prescale:72000 = 1ms
 LPC_TMR32B1->PWMC= (1<<3);            //enable pwmmode MAT3
 LPC_TMR32B1->MR2 = 12 -1;            //set cycle -1
 LPC_TMR32B1->MR3 = (12*2/3)-1;        //set low width 2/3
 LPC_TMR32B1->TCR = 1;                //start timer
0 Kudos
Reply

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ECamino on Tue Oct 16 08:41:54 MST 2012
.
0 Kudos
Reply

2,793 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by karanmehta93 on Tue Oct 16 08:21:03 MST 2012
On what basis did you decide the MR2 and MR3 value? What is your clock signal(MHz) ?
To get a 30% duty cycle you will need 2/3 value of MR2 because the PWM gets high at MR3 match.
0 Kudos
Reply