calculation frequency

Status
Not open for further replies.

medastar

Newbie level 6
Joined
Jul 3, 2013
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
78
here is my code,and i was expect 500hz from the formula of

Crystal Frequency
PWM Freq = ----------------------------------------- but i couldnt found what i want can anyone help me where is the error pls.
(PR2 + 1) * (Timer2 Prescaler) * 4

#include<18F4431.h>
#fuses INTRC, NOWDT, NOPROTECT, NOBROWNOUT, NOPUT, NOLVP,NOMCLR
#use delay(clock=8000000)
#define POWER_PWM_PERIOD 1999
//=======================================


void main()
{


float x =1;




setup_timer_2(T2_DIV_BY_16, 249, 1); // 500 Hz PWM freq with 8 MHz osc


// Setup the 4 Power PWM channels as ordinary pwm channels.
//setup_power_pwm_pins(PWM_ODD_ON, PWM_ODD_ON, PWM_ODD_ON, PWM_ODD_ON);
setup_power_pwm_pins(PWM_COMPLEMENTARY,PWM_COMPLEMENTARY,PWM_COMPLEMENTARY,PWM_COMPLEMENTARY);
// Mode = Free Run
// Postscale = 2 (1-16) Timebase output postscaler
// TimeBase = 0 (0-65355) Initial value of PWM Timebase
// Period = 154 (0-4095) Max value of PWM TimeBase
// Compare = 0 (Timebase value for special event trigger)
// Compare Postscale = 1 (Postscaler for Compare value)
// Dead Time

setup_power_pwm(PWM_FREE_RUN, 1, 0, POWER_PWM_PERIOD, 0, 1,0);
output_toggle(PIN_D1);


while(1)
{
output_toggle(PIN_D1);
x =0.50;

set_power_pwm0_duty((int16)((POWER_PWM_PERIOD *4) * x)); // 50%
set_power_pwm2_duty((int16)((POWER_PWM_PERIOD *4) * x)); // 50%
delay_ms(500);

}


}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…