PWM, step by step while configuring the SFR

Status
Not open for further replies.

PA3040

Advanced Member level 3
Joined
Aug 1, 2011
Messages
883
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Activity points
6,936
It is much appreciated if somebody can teach me PWM, step by step while configuring the SFR
my MCU friend is 16f877a language ASM

 

check this discussion.... its very big but has a lot of information. but somewhere in middle of discussion you have pwm
its c programming but very useful....

https://www.edaboard.com/threads/215581/

---------- Post added at 17:26 ---------- Previous post was at 17:21 ----------

**broken link removed**

---------- Post added at 17:31 ---------- Previous post was at 17:26 ----------

**broken link removed** has the information you want....
 
Thanks Mr:ckshivaram

while we continue this

Generates a Pulse-Width Modulated (PWM) signal on the CCP1 and CCP2 pins

Duty cycle, period and resolution determined by the following registers

PR2--------Period Register

T2CON-----Timer2 Control

CCPRxL-----Duty Cycle Register

CCPxCON---CCP Control Register

what will be the next
 


Hey man
Today i am also here to ask aabout step by step pwm tutorial . I dont know that where do u want to use this but i want to use it in inverter . I want to learn 50 Hz with pwm control . Actually what i want to say is a fixed volt ups . as the load increased on the out put of ups the pwm increased as the load decreased the pwm decrease to make the voltage fixed at 230 volt from 12v input. Just like TL-494 or SG3524.
Can anybody help me to do this with modified sine wave or sine wave????????
 

See this link,
2KWatts PIC16F84/ADC0831 Power Inverter
Link
**broken link removed**

I ported this to PIC16F676 and changed from 60Hz to 50Hz. PWM is in software.
From webpage
 

Attachments

  • I2KFiles.zip
    97.8 KB · Views: 192

One way you can do it (I've done like this) is:
Set the CCP module in compare mode. Load the value of your duty cycle (keep it within 95%). Turn one MOSFET on. Then wait for the compare flag to be raised (by polling or interrupt). Then load the value of the period and then wait for the flag to be raised. Then turn both MOSFETs off. Then, again load the duty cycle value. Wait for flag to be raised. Then turn the other MOSFET on. Load the value of the period, wait for flag, then turn both MOSFETs off. Remember to clear the flag in each case. In the middle of this somewhere, check the output voltage and if it is lower than the required set voltage (220v, eg), increase the value of the duty cycle register. Otherwise, increase the duty cycle voltage. While waiting for the flag, you can also check the battery voltage and load level.

Hope this helps.
Tahmid.
 
Hi All

I read all step by step and now I am trying to configure PWM

BSF TRISC,2 ;Configure pin as input

BCF T2CON,TMR2ON ;turn off timer2

CLRF TMR2 ; Clear Timer2

I think now need to Set up Period and Duty Cycle
Am I right?

If I ask, why we clear TMR2
 


If you load the period register PR2, you don't need to clear TMR2. It will be cleared automatically when PR2 = Timer2. So, you just need to load an appropriate duty cycle value and an appropriate period value.

Hope this helps.
Tahmid.
 

I add another step

BSF TRISC,2 ;Configure pin as input

BCF T2CON,TMR2ON ;turn off timer2


Set up Period and Duty Cycle

MOVLW b’01111111’ ;
MOvWF PR2 ;Load a Period Value
MOVLW b’00011111’
MOVWF CCPR1L ;Load Duty Cycle Value

I think now I am in correct track
Am I right?

can any body explain step by step above, also use image without pointing link
 
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Hi

here PR1 + 1 I think 1 mean constant
 

Attachments

  • pwm3.GIF
    5 KB · Views: 166

Yes, 1 is the constant integer "one". You have to add this to the value of PR2 when calculating using the mentioned formula.

Hope this helps.
Tahmid.
 
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Thanks Tahmid & BigDog

I think one is constant otherwise it dose not have period, No PWM frequency and above formula contains multiply by 4
let me know why hear 4
 

Because, the frequency of operation of the PIC is 1/4 that of the oscillator. So, you use a 20MHz crystal, your PIC operates at 5MHz. Since frequency is 1/4 of the oscillator, time is 4 times as time_period = 1/frequency

Hope this helps.
Tahmid.
 
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Hi All
The PWM duty cycle is specified by writing to the CCPRxL register and to the DCxB1CxB0
(CCPxCON<5:4>) bits. Up to 10-bit resolution is available: the CCPRxL contains the eight MSbs
and CCPxCON<5:4> contains the two LSbs. This 10-bit value is represented by DCxB9CxB0.
The following equation is used to calculate the PWM duty cycle:

the above paragraph says "Up to 10-bit resolution is available"
What is this resolution
can somebody explain it
 

10 bit resolution means 2 pow 10 i.e. the count will be from 0 to 1023. if 5v is the vreference then that means 0V is represented by 0(0000) and 5v is represented by 1023 (FFFF)
 

the above paragraph says "Up to 10-bit resolution is available"
What is this resolution
can somebody explain it

Resolution describes the available precision of a parameter, in this case time duration or period.

An example:

Lets assume a PWM frequency of 1kHz which has a period of 1mS.

The 10-bits can represent 1024 unique values, therefore if the period of the entire PWM cycle is 1mS, you can control the resolution of the duty cycle (ON State) of the PWM signal by 1mS/1024 = 977nS ~ 1uS.

A 10-bit value of "0000000000" represents 0% duty cycle, no pulse or always in the OFF State, while a 10-bit value of "1111111111" represents 100% duty cycle or always in the ON State.

A value of "0111111111" represents a 50% duty cycle with a ON State duration of approximately 500uS.

Incrementing or decrementing by 1-bit will effective change the duty cycle by a duration of 1uS.

Does this help you understand?

BigDog
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…