// Motor Chip Selection const uint8_t MotorChipVersion = 2; // 1 - VNH2SP30, used on Scout boards through Rev 10 // 2 - VNH5019, used on Scout boards from Rev 11
int16_t getSpeedCommand_fromSerial(uint8_t val){ // Serial speed commands should be 0 to 127. val = constrain(val, 0, 127); // MODIFIED VERSION FOR 8-BIT TIMER // Multiply by 2 to scale our speed value (from 0 to 127) to our PWM duty cycle range (0 to 255) if (val == 127) return 255; else return val * 2;}
Since you now mention using Fast PWM, am I correct in assuming the Phase Corect was only chosen so that you could more precisely set the PWM frequency?
I noticed the Motor_PWM_Top definition but I have commented this out as you only seemed to use this for setting ICR1 and as this is not used with Timer0 and the value of TOP is predefined as 255.Am I correct with this?
The multiplcation of the speed I did change to 2 but I am confused with the code you have shown.I had changed the code as below ...You have an extra line which is not in the code from Github and checks for the max valueif (val == 127) return 255; Is it necessary?
// TIMER 0 - MOTOR PWM// ----------------------------------------------------------------------------------------------------------------------------------------------> // We use Timer 0 to generate ~7.8KHz PWM on Arduino pins 5 & 6 (PD5 and PD6). Ken Shirriff has a good tutorial on the various types of PWM possible: http://www.righto.com/2009/07/secrets-of-arduino-pwm.html // We want: // - Waveform Generation Mode bits (WGM0 2:0) - split across TCCR0A and TCCR0B. These set the PWM modes, we want Mode 3 - Fast PWM with a fixed TOP value of 0xFF (255). See page 109 // - Clock Select (CS0 2:0) - last three bits of TCCR0B which set the prescaler, we want prescaler of 8. See page 111. // - Compare Match Output A & B bits (COM0A, COM0B): Two bits each, in TCCR0A register, these enable/disable PWM on the associated pins. See page 107. // The pins we are using are Arduino pins 5 & 6 or in other words, Atmega port D pin 5 and 6. // To control the duty cycle (set the motor speed), we write to OCR0A or OCR0B a value between 0-TOP (0-255) //OCR0A = 0-TOP; // To set duty cycle on Arduino pin 6 //OCR0B = 0-TOP; // To set duty cycle on Arduino pin 5 // The frequency is determined by the PWM mode (Fast PWM mode in our case), the clock frequency, the prescaler, and our TOP value. // Formula for Fast PWM is (Clock/Prescaler/TOP) = 16,000,000 / 8 / 255 = 7,843 Hz // Low frequencies result in "motor whine" which is annoying and doesn't sound realistic on a model. The VNH2SP30/VNH5019 motor drivers used on this device have a maximum PWM frequency input of 20Khz, though this // is not absolute. Higher frequencies cause less efficient operation and create more heat. Human hearing typically extends to about 20Khz, so above that the PWM should be inaudbile. // When using the 8-bit Timer 0 the best we can do is ~7.8KHz, which will still be audible but not terrible. // When controlled via serial the speed is passed as a 7-bit speed number (meaning values from 0-127), so we must multiply them by 2 to scale them to the PWM range of 0-255 for OCR0A and OCR0B. TCCR0A = 0xA3; // COM0 A1:A0, COM0 B1:B0 = 10 (connect non-inverted PWM to pins), WGM0 2:0 = 011 (Mode 3, Fast PWM with TOP = 0xFF) TCCR0B = 0x02; // CS1 2:0 = 010 (Prescaler = 8) TIFR0 = 0x07; // Start off with all Timer 0 flags clear (by setting bits to 1 in TIFR0 register) TIMSK0 = 0x00; // Disable Timer 0 interrupts (by clearing TIMSK0 bits). PWM is generated in hardware and we won't need interrupts enabled for it. TCNT0 = 0; // Reset TCNT0 OCR0A = 0; // Start off with duty cycle for both motors at 0 for safety. OCR0B = 0; //
It takes a while for me to understand code but I normally get there, however writing from scratch is a complete no no.Going through all the routines is interesting as I am learning quite a bit.
Almost certainly the issue is a short on the motor driver chip M2 during reflow. This is not hard to do, and I have done it myself on occasion.