Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.
The Pulse Width Modulator is one among four modes in which the Programable Counter Array (PCA) can be programmed.
You can have up to five independent running PWM modules.
Though the PCA timer is a 16-bit counter, the PWM uses only 8-bit register CL mapped in SFR space at 0E9h.
Another limitation is that all five modules will have the same frequency of output because they all share the PCA timer.
What you basically need is loading two registers : CCAPL and CCAPH for each PWM channel if in use.
Next you have to select the input source for PCA 16-bit counter.
In the end you must set PWM and ECOM bits in CCAPM register to enable PWM mode for each of the five channels if in use.
The PCA timer is determined from the CPS1 and CPS0 bits in the CMOD register (SFR 0D9h) and can be programmed to run at:
- 1/6 the peripheral frequency
- 1/2 the peripheral frequency
- the timer 0 overflow (running either is 13-bits, 16-bits or mode 2 8-bit autoreload)
- the input on the ECI pin (P1.2)
Peripheral frequency is your crystal oscilator frequency divided by 2 or not (CKCON0 SFR).
You can get even lower one by using then the 8-bit prescaler divider (CKRL SFR settings)
How things are going inside ?
The CL register is incremented at a rate according with the selected input source.
As long as the contents of this register is less than the value of CCAPL register, the CEX output on P1 will be low.
When it's equal or greater, then the CEX output will be high.
As soon as CL register overflows from 0FFh to 00h, CCAPL register is reloaded with the value CCAPH holds.
If CCAPL value is equal with CCAPH, which usually is, then you got a nice PWM at CEX output, continously as long as ECOM bit is set.
If you want to change the duty cycle, that can be done easy by changing the value of CCAPH register (the update is done clean without glitches at the next rollover of CL register).
You can use this Excel file to determine register values for PCA and Timers in order to obtain a specific signal (period and duty cycle) according to the application's oscillator frequency and internal clock mode (X1 or X2).