Friday, March 25, 2011

How to calculate PWM Period for PIC24

TUTURIAL FOR PIC24 Microcontroller

Okay.. this 4ns ( nano second / 1 X 10^-9) is really made my day.

It seem the timer make sense when multiply with 4ns.


At first, i tried to calculate Pulse-Width modulation Mode (PWM) for the timer1 of PIC24 but the period time is make no sense as i got 140us.


Coding to set your timer and interrupt :
********************************************************

_T1IP=4; //  set timer1 priority (4=dafault)
TMR1=0; //clear the time
PR1=34; //set the period register
T1CON=0x8020;  //check T1CON register (Prescale set to 1:64)
_T1IF=0; //clear interrupt flag
_T1IE=1;  //enable T1 interrupt source
********************************************************

After searching to the net and also looked at the datasheet for PIC24 family (mine is using PIC24FJ128G010) , the result are :

Frequency Oscillator,Fosc = 32Mhz
Frequency cycle clock,Fcy = Fosc/2 = 16Mhz
Time cycle clock,Tcy = 1/Fcy = 1/16Mhz = 62.5ns
Prescale : 1:64 (need toset at T1CON register for prescale)

PWM Period = [(PRy)+1]*Tcy*(Timer Prescale Value) = 140us

Where my PRy= 34 ( see at PR1 value above)

PWM Period = [(34)+1]*62.5ns*64=140us

That's how i get my PWM. Hence, current 140us or time require to transfer 2 byte of data using 115.2kbps line rate.

Hope it will help.


It only applicable to the engineering part.Hehe...



2 comments:

++ aD[d] ++ said...

xpaham

Shahmi said...

coding ape yg aku buat utk master project~