This library works fine, only I would need to get float or double frequency. For example set the PWM freq on 109 Hz and then to do steps plus or minus by at least 0.5 Hz. But the best if I can change frequency on 109.1, then 109.2 Hz and so on.
I want to have two buttons: plus and minus and after push the button you will go down or up from 109 Hz on 108.5 or 109.5 Hz, and after next button push again +/- 0.5 Hz. But the best for me to have steps by 0.1 Hz.
Problem is if I can set frequency in float or double value.
When I changed int32_t frequency = 109 as float frequency, it does not help.
Can anybody help me please?
Or is there any different way how I can make steps in 0.1 or 0.5 Hz by pushing plus or minus button?
For example for me is the best
Thank you
Basic view on PWM example (without buttons)
#include <PWM.h>
int motor = 9; // Pin with PWM output
int32_t frequency = 109; // freq in Hz
void setup()
{
InitTimersSafe();
bool success = SetPinFrequencySafe(motor, frequency);
if(success) {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
}
void loop()
{
pwmWrite(motor, 128);
}
delay(30);
}
The code is clearly very incomplete. For instance, it's not clear how the SetPinFrequencySafe() function is defined and which variable type it accepts as its second argument, i.e. frequency. If that's an integer, then obviously you'll never be able to set the frequency as a fraction - it'll always be whole numbers only.
An Arduino Uno would be more than capable of achieving PWM resolution in the 0.0001 Hz increments in that range. Do you have equipment to measure the PWM frequency in that resolution?
Frequency will drive rpm of VDC motor. So yes, you can measure rpm of motor very precisely.
1 Hz = 10 rpm. I need to get the speed 33,33 behind the gearbox that is 14:1
That means motor has to have speed 466,62 rpm, that means 46,662 Hz Thats why I need to have very soft steps
Try this one out. Put a potentiometer on A0, and the output PWM on D10
int analogPin = A0;
int val = 0;
uint16_t freqTop = 0;
void setup() {
// put your setup code here, to run once:
PWM_init();
}
void loop() {
// put your main code here, to run repeatedly:
val = analogRead(analogPin);
freqTop = map(val, 0, 1023, 2080, 6250);
OCR1A = freqTop;
OCR1B = freqTop/2;
delay(50);
}
void PWM_init(void)
{
/***************************
Initialize the PWM hardware
***************************/
// set OC1B output pin
DDRB |= 1<<PORTB2;
// set TIMER1 for Fast PWM mode, non-inverted output on OC1B pin
TCCR1A = 1<<WGM11 | 1<<WGM10 | 1<<COM1B1 | 0<<COM1B0;
TCCR1B = 1<<WGM13 | 1<<WGM12;
// disable interrupts
TIMSK1 = 0<<OCIE1B | 0<<OCIE1A | 0<<TOIE1;
// set TOP value to generate initial 40 Hz [250 KHz / 6250]
OCR1A = (6250 - 1);
// set initial duty cycles at 50%
OCR1B = 6250/2;
// start TIMER1 at 250 KHz [DIV64]
TCCR1B = 1<<WGM13 | 1<<WGM12 | 0<<CS12 | 1<<CS11 | 1<<CS10;
return;
}
You can change the value of val however you want (through buttons, potentiometer, etc). This example uses potentiometer on A0. As long as its between 0 and 1023, you should be able to get 40 Hz to 120 Hz on the PWM
No particular reason except for resolution available for the PWM timer. You can play with the number since it TIMER1 is 16 bit which gives you a lot of possible combinations
There are only five values of prescale you can use on Timer1: 1, 8, 64, 256, 1024
You usually pick the smallest prescale that keeps your TOP value below 17 bits (you only have 16 bits for the value of TOP.
You can't use prescale 1 because it only goes down to 16MHz / 65536 = 244 Hz.
Prescale 8 (2 MHz clock) goes down to 30.5 Hz so that is what I would have used. TOP = 43667 for 45.8 Hz and TOP = 18348 for 109 Hz so you would have 25,319 steps between 45.8 Hz and 109 Hz.
Prescale 64 (250 kHz clock) goes down to 3.8 Hz which is overkill for your purposes (45.8-109 Hz).