Issue with analogWriteResolutoin

Status
Not open for further replies.

mcparker

Member
I posted the original code and I have still having issues getting analogWriteResolution. In the code below, if I change the PWM or drive Frequency off of the ideal for the Teensy 3.6 CPU the motors do not turn on. Everytime i try and change the Resolution and the corresponding variables. The system does not turn on. Any ideas?


Situation: Powering two DC Motors thru an H-Bridge

Code:
// Motor A

/*
 * Frequency to Match
20. kHz
+ pulse 24micro s
- pulse 26micro s
 */
//TURN ON SERIAL CONNECTION 

const int enA = 10;//FTM0 Timer
const int in1 = 8;
const int in2 = 7;

// Motor B

const int enB = 3;//FTM0 Timer
const int in3 = 4;
const int in4 = 5;

// Set Up Test Variables
/* Resolution Bits = 11 , pwmValue = 2047, freq = 29296.875 
 * Resolution Bits = 8 (default), pwmValue = 255
 */
 
 
float  Duty = 6.0;
float driveFreq = 234375.00; //or 20.0*1000 or 234375
int Res = 8;//8
int pwmValue = 255;//255 , 2047
int Max = 3;   
int Counter = 0;

//time on $ time off

unsigned long  Time_On = 48.5*1000; //Motors On variable long & math calc to avoid clock issues
float  Time_Off = 5000; //Motors Off
  
void setup()

{
  
  Serial.begin(9600);
  while(!Serial){
  }
  
  analogWriteResolution(Res);  
  analogWriteFrequency(enA,driveFreq);
  analogWriteFrequency(enB,driveFreq);
  
  //Button Input
  pinMode(11, INPUT); 
  pinMode(12, OUTPUT);
  //Motor Outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  
//Setting Up Teensy PWM Output Frequencies
//https://www.pjrc.com/teensy/td_pulse.html
 
}

void Forward(float Duty, unsigned long Time_On, int pwmValue )
{
  float Work = (pwmValue/12.00)*Duty; //.XX added to force the float calculation
  pinMode(13,OUTPUT); //LED ON
  digitalWrite (13, HIGH);
 // Serial.println (Duty);
 // Serial.println(Work);
  
 // Turn on motor A

  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);

  // Set Working Voltage Percentage out of possible range 0~255

  analogWrite(enA, Work);

  // Turn on motor B

  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);

  // Set Working Voltage Percentage out of possible range 0~255
  
  analogWrite(enB, Work );
  
  delay (Time_On);
  
  // Now turn off motors

  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

  digitalWrite (13, LOW); //LED Off
}

void loop()
{ 
 while (digitalRead(11) != HIGH && Counter < Max)
  {    
    Forward ( Duty, Time_On, pwmValue );
    delay(Time_Off); 
    Counter++;
    Serial.println (Counter);  
  }
 Serial.println(Counter); 
 Serial.println("Work Stoppage...Max Cycles Reached.. so hard to say goodbye"); 
 Serial.flush();
 for (int i=0; i<100; i++) 
  {
   Serial.print("\n");
   }
}

*/
 
The system does not turn on.

I don't have your system here, nor do I even know what your system is. So even if I know everything about this code (I do not), how am I supposed to know whether it will turn on your system?

If you say clearly what you want the PWM pins to do, I can try running your code and watch the PWM pins with my oscilloscope or multimeter. A smaller, simpler program dealing only with the PWM pins would take less work to give you an answer too. But the main thing missing here is any sort of way to know what you want the PWM pins to really do. Without that, really can't help much.
 
IMG_0993.JPG

2 x DC Motors connected thru an H-Bridge on two encoders with a 12 V DC power supply.

The rest of the wires are for the buttons and display.

Thank you for the help.
 
I removed the L298N from the system and drove the motors straight from the Teensy pins (goal of a 20kHz drive frequency). I used the 24 Mhz CPU speed with 10 bit resolution with an ideal frequency of 23437.5 Hz to get as close to the 20kHz as possible.The change in the resolution and drive frequency worked when driving straight from the board.

Is there a reason the motor driver would cause this issue?
 
Status
Not open for further replies.
Back
Top