generation of 400Hz PWM with same duty cycle as input

we have a signal module attached to my experiment which generates 1khz pwm signal but for my experiment to run a small BLDC motor it will accept only 400Hz as pwm signal. we tried to generate 400HZ with same duty cycle as input 1 KHz PWm signal for BLDC Motor using arduino MEGA 2560 but the output pulse is not at 400 HZ. int pwm_output=10;
int k;
int x=0;
int y=0;
static double duty;

static double freq;
static double timeperiod;

static long ONTime = 0;

static long OFFTime = 0;

static long tempPulse;

void setup() {
// put your setup code here, to run once:
pinMode(9,INPUT);
pinMode(pwm_output,OUTPUT);
TCCR2B = (TCCR2B & 0b11111000) | 0x05;
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:

ONTime = 0;

OFFTime = 0;

tempPulse = pulseIn(9,HIGH);

if(tempPulse>ONTime){

ONTime = tempPulse;

}

tempPulse = pulseIn(9,LOW);

if(tempPulse>OFFTime){

OFFTime = tempPulse;

}

Serial.println(ONTime);
Serial.println(OFFTime);
freq = ((double) 1000000)/(double (ONTime+OFFTime));
Serial.println(freq);
duty = (100*(ONTime/(double (OFFTime+ONTime))));
k= duty*2500/100;
i=2500;
for (j=0;j<k;j++)
{
digitalWrite(10,HIGH);
}
for (j=k;j<i;j++)
{
digitalWrite(10,LOW);
}

}

Please imagine that someone is reading this who has no idea what you are talking about. What module? What motor?

but not able to get 400 hz in arduino MEga

Why not? What did you try?

we have a signal module attached to my experiment which generates 1khz pwm signal but for my experiment to run a small BLDC motor it will accept only 400Hz as pwm signal. we tried to generate 400HZ with same duty cycle as input 1 KHz PWm signal for BLDC Motor using arduino MEGA 2560 but the output pulse is not at 400 HZ. int pwm_output=10;
int k;
int x=0;
int y=0;
static double duty;

static double freq;
static double timeperiod;

static long ONTime = 0;

static long OFFTime = 0;

static long tempPulse;

void setup() {
// put your setup code here, to run once:
pinMode(9,INPUT);
pinMode(pwm_output,OUTPUT);
TCCR2B = (TCCR2B & 0b11111000) | 0x05;
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:

ONTime = 0;

OFFTime = 0;

tempPulse = pulseIn(9,HIGH);

if(tempPulse>ONTime){

ONTime = tempPulse;

}

tempPulse = pulseIn(9,LOW);

if(tempPulse>OFFTime){

OFFTime = tempPulse;

}

Serial.println(ONTime);
Serial.println(OFFTime);
freq = ((double) 1000000)/(double (ONTime+OFFTime));
Serial.println(freq);
duty = (100*(ONTime/(double (OFFTime+ONTime))));
k= duty*2500/100;
i=2500;
for (j=0;j<k;j++)
{
digitalWrite(10,HIGH);
}
for (j=k;j<i;j++)
{
digitalWrite(10,LOW);
}

}

@avjspraneeth, please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

And please answer the 4 questions in Reply #1

…R

@avjspraneeth: Please use code tags.

Read this before posting a programming question

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

How to use this forum