SouthernAtHeart:
I've spent an hour reading through pages like "secretOfArduinoPWM", "PwmFrequency", "TimerPWNCheatsheet", About all I could get was that something called TCCR1B affects the timer1 which operates pin 9. This is WAY over my head. I've seen different examples, but I don't even know which of them to start with.
That's a good start. Each of the three timers has two PWM outputs (A and B). For Pin 9 you have to look at which timer controls it:
That spreadsheet shows that on an UNO the Pin 9 PWM comes from Timer 1 output A (T1A) and on the ATmega it's known as Port B Pin 1 (PB 1). If you switch to a Mega you need to change to Timer 2 output B (T2B) which on the ATmega is known as Port H Pin 6 (PH 6).
Knowing you want Timer/Counter 1 you read that section of the datasheet. It talks about the Timer Counter Registers: TCCR1A, TCCR1B, and TCCR1C (they had too many bit to fit in one byte so they made it into three registers).
TIMSK1: The Interrupt Mask Register. Since you aren't using interrupts this remains 0.
TIFR1: Interrupt Flag Register. Since you aren't using interrupts this isn't important.
Since it is a 16-bit counter the other registers are two bytes each:
TCNT1: The actual timer/counter count
OCR1A: The 'A' Output Compare Register that is associated with Pin 9
OCR1B: The 'B' Output Compare Register associated with a different pin.
ICR1: The Input Capture Register. In out case we are using it to set the TOP of the counting range so the counter doesn't go all the way to 65535.
thanks. Yes, I got the VNH2SP30 for that very reason, that it goes up to 20Khz.
I don't have a scope, but I do have a Fluke meter that measures frequency. I've never used that function, but I'll have to test it out, it'll be good learning.
Hi guys!
Your answers are great and very useful as I have the very same VNH2SP30 chip on my motomonster Hbridge (sparkfun https://www.sparkfun.com/products/10182)
but I would like to drive two motors! I have read in the code that "Channel B is irrelevant" and I'm lost...
Could you please tell me what to implement in order to have D9 and D10 ultrasonic's POWER
Thanks Coding Badly for your quick reply! and commented code 8)
Wonderful!
It seems that because the 2 pins are sharing the same timer (PWM T1), it's quite short to implement: excellent! I woudn't have dared to mess with the code: registry frighten me!
I'll have a try now with my Nano ATMega 328
To be compliant with the rest of my code, can I use this (map and constrain) without decreasing the overall performance?
void analogWriteSAHA( uint8_t value )
{
// My variable value varies from 0 to 255
// but awaited range in OCR1A is from 0 to 799 and nothing else!
OCR1A = constrain(map(value, 0, 255, 0, 799), 0, 799);
}
This website has been reported as unsafe
Phishing threat:
This site might try to trick you into disclosing your login, password or other sensitive information by disguising itself as another website you may trust.
Instead of hosting images off-site please attach them to your post. Click Modify to change your post, click Addition Options, click Browse to select the file, then click Save.
Mat13:
The direction leds (green/red) are less bright... than usual.
When a motor is connected, the leds don't light at all...
Test with two LEDs (and series resistors) using the Fade example. If the LEDs get dimmer / brighter then at least you know the pulse width modulation is working.
Thanks but unfortunatly, it was already in the code... there must be something else somewhere else :~
maybe in the wiring.c or .h files? or a previous tweak i may have done in serial file...
I'll do a reinstall!
and meanwhile I'll ask a friend to do the same
// the setup routine runs once when you press reset:
void setup() {
analogWriteSAH_Init;
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}
Mat13:
Thansk to Coding Badly's help,
here is the video demonstrating that the ultrasonic PWM eliminates the buzzing sound of the driven DCmotor!
You are welcome. Thank you for the video. It illustrates the problem beautifully.
For the record, that is actually a good way to ensure PWM is working correctly for a new core (a piezoelectric buzzer is a less expensive tool than a motor+driver). Just listen for the distinctive frequency and all is good.