• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PWM capability of a microcontroller?

Status
Not open for further replies.

s3elan

New Member
Hi there I am 69 and probably left it a bit late to start learning about Microcontrollers. I am studying PWM at the moment but have got stuck with my understanding of how it works. I will be using a pic 12f683 for this project which is to do with fading LED’s from off to full brightness in about 2 secs. The Fade register is incremented in a linear way (adding 1 each cycle). The look up Table (8bit) converts it and returns an adjusted value to give a non-linear progression for CCPR1L allowing a consistent progressive fade ON of the LED's.


My question is that I understand that it is possible to have a 9 or 10 bit CCPR1L resolution by adding bits 5 & 4 of CCP1CON. Nowhere can I find out what the controlling factors are or how to initialise the Pic to do this. Also I have been unable to find Assembly code that shows how to write to the 9 or 10 bit CCPR1L : CCP1CON registers. I think I might be missing something. Sorry if it is a dim question. I can only understand Assembly code.

Following are my notes on PWM. They may be hopelessly wrong but I would really appreciate some help with this. Kind regards.


Pulse Width Modulation General Concept



PWM is a method of controlling current in a circuit. The current is switched on and off at a pre-determined frequency. The period defines this frequency and is decided by the circuit designer.

The ratio of ON during a single Period is called the Duty cycle.

¾ of maximum current would be 75% ON 25% OFF.


Pic Micro controller with PWM Module



Timing is a critical aspect of PWM. The Pic Oscillator frequency is fundamental to its operation as this controls the speed of the Timer count (TMR2). The Timer count duration can be extended using the Timer Prescaler (1:1, 1:4 & 1:16).


The Period commences with the first count of TMR2 and ends when TMR2 is equal to the set value of PR2 (0-255).


The Duty Cycle also starts with the first count of TMR2 but ends when TMR2 count matches CCPR1L (H) set value.


The first count of TMR2 sets the PWM Output pin high during the start of each Period.

When TRM2 matches CCPR1L (H) the PWM pin is set to low until the start of the next Period.


CCPR1L is set in software and can be written to at any time. At the start of a new Period CCPR1L is latched to CCPR1H which is read only and cannot be changed. The module uses this latched CCPR1H setting for the new duty cycle.



Timing


Timing must be decided by the circuit designer and is a critical part of the PWM module. The basic building block is the Oscillator Frequency (Fosc). (4Mhz)

From this we know that 1 Osc cycle = 1Sec / 4Mhz = 0.25uS


TMR2 requires 4 Osc cycles for 1 count = 4 x .025 = 1uS

We now know that to achieve a duty cycle CCPR1L resolution of 255 there will need to be 255 +1 TMR2 counts during each Period.

The minimum TMR2 count duration is 1uS. Therefore the period duration must be at least 256uS.

Producing a Period frequency of :- 1sec / 256uS = 3906Hz

Frequency requirements will vary depending on the circuit design. Motor control, LED switching etc. and can be adjusted with TMR2 prescaler settings
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top