Max and min PWM period in 16/32bits counters

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Max and min PWM period in 16/32bits counters

1,753 次查看
emimad
Contributor III

Hi friends!

I am using a LPC11u3x microcontroller with 16 and 32 bits PWM counters to manage RGB leds.

The 32bit counter is working properly at 10kHz but 16bit ones can't get that frequency.

Anyway, I don't need 10kHz for this application, of course, but I configured a 200Hz frequency and the counters didn't work properly neither.

This is my config function:

 

 

#define PWM_FREQ_HZ 10000

#define PWM_DUTY 50



timerFreq = Chip_Clock_GetSystemClockRate();

Chip_TIMER_Disable(LPC_TIMER32_0); //disable to modify registers
Chip_TIMER_Disable(LPC_TIMER16_0);
Chip_TIMER_Disable(LPC_TIMER16_1);

Chip_TIMER_Init(LPC_TIMER32_0);
Chip_TIMER_Init(LPC_TIMER16_0);
Chip_TIMER_Init(LPC_TIMER16_1);


// This configures MRx to act as PWM register -->  GPIO (MAT0..3)
// Value written to MATx_pin will determine when GPIO goes high
// which basically determines PWM duty cycle
LPC_TIMER32_0->PWMC = (1 << 3); //P0.11 MAT3 
LPC_TIMER16_0->PWMC = (1 << 0); //P0.8 MAT0
LPC_TIMER16_1->PWMC = (1 << 1); //P0.22 MAT1

// pr = SystemCoreClock / (4 * 1000000) - 1; // 12 Mhz - 1 = 11 for prescaler
// Chip_TIMER_PrescaleSet(LPC_TIMER16_0, pr); //  C16Bx need prescaler to not overload
// Chip_TIMER_PrescaleSet(LPC_TIMER16_1, pr); // 

// We set TIMER32_0 to reset TC on MR1 match --> usar otro que no sea el del PWM
// this effectively makes value written to MR1 -->
// to determine PWM signal frequency
Chip_TIMER_ResetOnMatchEnable(LPC_TIMER32_0, 0); //MR0
Chip_TIMER_ResetOnMatchEnable(LPC_TIMER16_0, 1); //MR1
Chip_TIMER_ResetOnMatchEnable(LPC_TIMER16_1, 0); //MR0

periodRate = timerFreq / PWM_FREQ_HZ;
Chip_TIMER_SetMatch(LPC_TIMER32_0, 0, periodRate); //MR0
Chip_TIMER_SetMatch(LPC_TIMER16_0, 1, periodRate); //MR1
Chip_TIMER_SetMatch(LPC_TIMER16_1, 0, periodRate); //MR0

dutyMatchCount = ((periodRate + 1) * PWM_DUTY) / 100;
Chip_TIMER_SetMatch(LPC_TIMER32_0, 3, dutyMatchCount); //MR3
Chip_TIMER_SetMatch(LPC_TIMER16_0, 0, dutyMatchCount); //MR0
Chip_TIMER_SetMatch(LPC_TIMER16_1, 1, dutyMatchCount); //MR1

// Just in case, set MR1, MR2 to 0, which makes them do nothing basically.
Chip_TIMER_SetMatch(LPC_TIMER32_0, 1, 0);
Chip_TIMER_SetMatch(LPC_TIMER32_0, 2, 0);

Chip_TIMER_SetMatch(LPC_TIMER16_1, 2, 0);

 

My question is, what is the maximum and minimum frequencies reachable by the 32 and 16 bits PWM counters taking account of a 12MHz oscillator.

At 1000Hz, all the counters are working fine.

0 项奖励
回复
4 回复数

1,738 次查看
emimad
Contributor III

Thank you @xiangjun_rong ,

in the case of LPC11u34, it only has MR0 and MR1 for the C16B0. So I do this setup:

 

 

LPC_TIMER16_0->PWMC = (1 << 0);
Chip_TIMER_ResetOnMatchEnable(LPC_TIMER16_0, 1);

timerFreq = Chip_Clock_GetSystemClockRate();
periodRate = timerFreq / PWM_FREQ_HZ;
Chip_TIMER_SetMatch(LPC_TIMER16_0, 1, periodRate);

dutyMatchCount = ((periodRate + 1) * PWM_DUTY) / 100;
Chip_TIMER_SetMatch(LPC_TIMER16_0, 0, dutyMatchCount); //MR0

 

 

According to this, the min/max frequencies allowed with a 12MHz crystal would be:

Max => periodRate = 1 => freq = 12MHz

Min => periodRate = 0xFFFF => freq = 12Mhz/[(2^16)] = 183.1 Hz

 

Chip_Clock_GetSystemClockRate() returns the value of the crystal? I haven't setup any PLL multiplier or so on, I only use SystemCoreClockUpdate() in the first line of the main().

 

I tried with a 200Hz frequency and it didn't work.

0 项奖励
回复

1,723 次查看
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

You said "I tried with a 200Hz frequency and it didn't work.", I suppose that the PWM frequency is  not the same as what you expected, can you tell us the actual PWM frequency? If you figure out the PWM driving clock frequency based on the actual PWM frequency, what is the PWM module driving clock frequency?

BR

XiangJun Rong

0 项奖励
回复

1,722 次查看
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

As the following Figure, the CT32 and CT16 modules all use the PCLK(Peripheral clock), in other words, it is the system clock, pls check your system clock frequency.

BR

XiangJun Rong

 

xiangjun_rong_0-1651714173203.png

 

0 项奖励
回复

1,741 次查看
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

Regarding the maximum PWM frequency,for the CT32B and CT16B timer, the CT32 and CT16 all timer counts the PCLK clock as tick, the PCLK clock frequency is 50MHz, if we set the modulo as 2 and prescaler as 1, the maximum PWM frequency is 50Mhz/2=25MHz.

Regarding the minimum PWM frequency, for the CT32B and CT16B timer, the CT32 and CT16 all timer counts the PCLK clock as tick, the PCLK clock frequency is 50MHz(at most), let me discuss case by case:

CT32B:

if we set the modulo as 0xFFFF_FFFF and prescaler as 0xFFFF_FFFF, the minimum PWM frequency is 50Mhz/[(2**32)*(2**32)]=

CT16B:

if we set the modulo as 0xFFFF and prescaler as 0xFFFF, the minimum PWM frequency is 50Mhz/[(2**16)*(2**16)]=0.01164Hz.

If you use 12MHz rather than 50Mhz, pls replace 50Mhz with 12MHz, recompute the PWM frequency.

Pls note that the LPC11U32 has 4 independent timers CT32B0, CT32B1, CT16B0 and CT16B1. Each of the Timer has 4 match register:Match0, Match1, Match2, Match3, only one can control the period or frequency, the other 3 can control the PWM duty cycle of 3 PWM signals.

Hope it can help you

BR

XiangJun Rong

 

 

0 项奖励
回复