EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

help - lpc2103 timer0 capture code / timer1 pwm output code

Started by hjiongh April 18, 2010
Hi,
I have a question. Now I'm using LPC2103. I use timer0 to capture PWMInput . Frequency range is from 1Hz to 4KHz. I use timer1 and timer2 to PWM output , 100Hz, 50% duty cycle.

My Fpclk is 11059200*4 .

Capture code:
void Timer_Capture_Init(void)
{
PINSEL0 &= 0xffffcfff;
PINSEL0 |= 0x00002000; // CAP0.2, P0.6 , Freq

T0PR = 0x00; // count timer freq = Fpclk/(PRval + 1)
T0CCR = 0x180; // CAP0.2 falling edge;interrupt on CAP0.2 falling edge
T0TC = 0;
T0TCR = 0x01;
VICVectCntl2=0x24;
VICVectAddr2 = (INT32U)Timer0_ISR;
VICIntEnable |= 0x00000010;
}

PWMOutput:
//--------------------------
// Timer_PWM_Output_Init
//--------------------------
//! \brief timer pwm output function init
//!
//! \param none
//!
//! \return none
//--------------------------
void Timer_PWM_Output_Init(void)
{
PINSEL0 &= 0xffff3fff; // MAT2.0, P0.7 , Flow_PWM
PINSEL0 |= 0x00008000;

PINSEL1 &= 0xffffff3f; // MAT1.2, P0.19, AI_PWM
PINSEL1 |= 0x00000080;

// MAT1.2 AI_PWM, 100Hz, duty cycle can be set, default is 50%
T1TC = 0;
T1PR = 0;
T1MCR = 0x80; // set MR2 match TC value and clear TC value
T1EMR = 0x300; // Toggle the corresponding external match bit/output.

T1MR1 = ?

......
}

My question is : in Timer_PWM_Output_Init(), how to setup T1MR1?

Hope anyone can help me , thank you very much!

Best Regards,
Vincent

An Engineer's Guide to the LPC2100 Series

--- In l..., "hjiongh" wrote:
>
> Hi,
> I have a question. Now I'm using LPC2103. I use timer0 to capture PWMInput . Frequency range is from 1Hz to 4KHz. I use timer1 and timer2 to PWM output , 100Hz, 50% duty cycle.
>
> My Fpclk is 11059200*4 .
>
> Capture code:
> void Timer_Capture_Init(void)
> {
> PINSEL0 &= 0xffffcfff;
> PINSEL0 |= 0x00002000; // CAP0.2, P0.6 , Freq
>
> T0PR = 0x00; // count timer freq = Fpclk/(PRval + 1)
> T0CCR = 0x180; // CAP0.2 falling edge;interrupt on CAP0.2 falling edge
> T0TC = 0;
> T0TCR = 0x01;
> VICVectCntl2=0x24;
> VICVectAddr2 = (INT32U)Timer0_ISR;
> VICIntEnable |= 0x00000010;
> }
>
> PWMOutput:
> //--------------------------
> // Timer_PWM_Output_Init
> //--------------------------
> //! \brief timer pwm output function init
> //!
> //! \param none
> //!
> //! \return none
> //--------------------------
> void Timer_PWM_Output_Init(void)
> {
> PINSEL0 &= 0xffff3fff; // MAT2.0, P0.7 , Flow_PWM
> PINSEL0 |= 0x00008000;
>
> PINSEL1 &= 0xffffff3f; // MAT1.2, P0.19, AI_PWM
> PINSEL1 |= 0x00000080;
>
> // MAT1.2 AI_PWM, 100Hz, duty cycle can be set, default is 50%
> T1TC = 0;
> T1PR = 0;
> T1MCR = 0x80; // set MR2 match TC value and clear TC value
> T1EMR = 0x300; // Toggle the corresponding external match bit/output.
>
> T1MR1 = ?
>
> ......
> }
>
> My question is : in Timer_PWM_Output_Init(), how to setup T1MR1?
>
> Hope anyone can help me , thank you very much!
>
> Best Regards,
> Vincent
>
Set up MR0 such that the entire PWM period is generated. If you have 1 uS clocks and you want 100 Hz, this value is set to 0.01 / 0.000001 or 10,000 Every 10,000 counts, the timer will reset to zero. There is no need to generate an interrupt. MCR sets this up.

Now set up MR1 to 5000 to get a 50% duty cycle. The output will go high when the count reaches 5000 and then reset when the timer is reset by MR0 and MCR. Do the PINSEL thing to get MAT output.

You might want to look at the PWM1CON register (you need to set it up). The UM recommends using MR3 for controlling the entire cycle (instead of MR0 as described above) simply because PWM3 isn't pinned out. That leaves the possibility of 3 PWM outputs from Timer1. All will have the same frequency but individual duty cycles.

Richard

--- In l..., "Vincent" wrote:
>
> Hi, Richard,
>
> Thanks for your response.
> My understanding is:
> // MAT2.0 Flow_Output, frequency default value is 100Hz, range :1Hz---4KHz, duty cycle is 50%
> T2TC = 0;
> T2PR = 0;
> T2MCR = 0x02; // set MR0 match TC value and clear TC value
> T2EMR = 0x30; // Toggle the corresponding external match bit/output.
> T2MR1 = Fpclk/100; // pwm output frequency = 100Hz
> T2TCR = 0x01; // enable timer1
>
> My VPB clk is Fpclk, PR=0, so timer2 clk is Fpclk, each count period of timer2 counter is 1/Fpclk, If my final Frequency is Y(Hz) , and must spend Tx(1/Fpclk) time .
> so formula is T/Fpclk = 1/Y ; T = Fpclk/Y.
> T2MR1 = Fpclk/Y; // Y is frequency that want to setup.
>
> But how to setup duty cycle base on my understanding? If the frequency is invariable , only should set duty cycle?
>
> Best Regards,
> Vincent

Typical PWM is to set a fixed frequency and vary the duty cycle. But there is no reason that you can't vary the frequency and still hold 50%.

Assuming MR0 controls frequency and MR1 controls duty cycle with MAT1 being the output, just vary both together. When you double the frequecy by reducing MR0 by 1/2, maintain the duty cycle by cutting MR1 in half, as well.

Think of it this way: MR0 is the maximum value for the timer. So this register sets the frequency by which the timer clock is divided. The timer counts clocks from 0 to whatever the MR0 setting is and starts over.

MR1 is set to half of MR0 so as the timer counts up, when the value gets to MR1, the MAT output is set high. When the counter resets, the output is set low. As long as MR1 is half of MR0, the duty cycle will be 50% regardless of the frequency.

Don't forget the argument to use MR3 for controlling frequency. It's a good one. It's just easier to write in terms of MR0 and MR1.

Richard


The 2024 Embedded Online Conference