Go Down

Topic: generating same frequency on 4 pins in funduino uno (Read 2977 times) previous topic - next topic

ena123

I am using FUnduino (having atmega328P)  uno to control 4 brushless motors and for that i need to generate 50Hz frequency on 4 pwm pins as by dafualt 500Hz are generated on PWM pins.  after surfing i found that by using a libraray that is setpinfrequency, i can set whatever frequency i like on any pwm pin, but that library is for 8 bit timers. for getting same frequency on 4 pins i also would have to use 16-bit timers, any idea how can i change frequency on 16 bit timers?

CrossRoads

50 Hz is pretty slow, can do it blink without delay style and use direct port manipulation to change 4 IO pins together.
Every 10mS, change the output from High to Low to High to ...
Code: [Select]

unsigned long previousMicros;
unsigned long 10mS = 10000UL; // 50Hz = 20mS period, 1/2 that is 10mS = 10000uS

void setup(){
pinMode (7, OUTPUT); // D7,6,5,4 as outputs
pinMode (6, OUTPUT);
pinMode (5, OUTPUT);
pinMode (4, OUTPUT);
}

void loop(){
if (micros() >= previousMicros){
previousMillis = previousMillis + 10mS;
PIND = 0b11110000; // toggle outputs by writing 1 to input register
} // end time check

} // end loop
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

ena123

The BLDC motors i'm using do not work on any frequency excpet 50 Hz.

ena123

Mmm the above code shows a list of errors?  :~

CrossRoads

Rats - forgot to fix these:

previousMillis = previousMillis + 10mS;

to

previousMicros = previousMicros + 10mS;

What else? I am  not someplace where I can compile it myself, so you may have to tweak it a little.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

MAS3

@ena123:

The errors tell you what is wrong, and in what line this error was detected.
If you do not understand the errors, copy the list of errors to the board so someone who does understand what they say can help you out with it.

I copied CrossRoads code into IDE and found out that the error was as he mentioned the previousMillis should be previousMicros, and that the name of a variable started with a number (10mS), which seems to be a no-no.
This is the corrected sketch that does not get any compile errors:

Code: [Select]
unsigned long previousMicros;
unsigned long TenmS = 10000UL; // 50Hz = 20mS period, 1/2 that is 10mS = 10000uS

void setup(){
pinMode (7, OUTPUT); // D7,6,5,4 as outputs
pinMode (6, OUTPUT);
pinMode (5, OUTPUT);
pinMode (4, OUTPUT);
}

void loop(){
if (micros() >= previousMicros){
previousMicros = previousMicros + TenmS;
PIND = 0b11110000; // toggle outputs by writing 1 to input register
} // end time check

} // end loop
Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html


ena123

i need to get 50 Hz on 4 pwm pins so that i can use simple commands to change duty cycle between my desired range that is 1ms to 2ms to control speed of 4 brushless dc motors,i used the following library to get 50Hz on my desired pwm pins http://forum.arduino.cc/index.php?topic=117425.0, and it works fine except that now code doesn't seem to enter in the for loop to change dutycyle between my desired range . if i write pwmWrite command outside loop by giving only one brightness value (duty cycle) then it works fine.
here is my code.

#include <PWM.h>             
int32_t frequency = 50; //frequency (in Hz)

void setup()
{
  //initialize all timers except for 0, to save time keeping functions
  InitTimers();

  //sets the frequency for the specified pin
   SetPinFrequency(3, frequency);
   SetPinFrequency(5, frequency);
   SetPinFrequency(9, frequency);
   SetPinFrequency(10, frequency);
}

void loop()
{
  for (int brightness=13; brightness<=26; brightness++)
{
  pwmWrite(3, brightness);
  pwmWrite(5, brightness);
  pwmWrite(9, brightness);
  pwmWrite(10, brightness);
  delay(30);     
}
for (int brightness=26; brightness>=13; brightness--)
{
  pwmWrite(3, brightness);
  pwmWrite(5, brightness);
  pwmWrite(9, brightness);
  pwmWrite(10, brightness);
  delay(30);     
}
}

Go Up