Skip to main content
added 34 characters in body
Source Link
mike
  • 347
  • 6
  • 20

What I did wrong was using |= instead of = . Apparently Arduino set some of these Bits on start, which didn't change. Now it works like a charm and I added a frequency choice, which works for at least 50,200,400,488 Hz. Everything in between should work aswell!

Working testcode for timer1 with serial input of HIGH-time:

#define PRESCALAR 8
#define PWM_FREQUENCY 50
#define TOP_VALUE F_CPU/(2*PRESCALAR*PWM_FREQUENCY)

uint16_t dutytime = 1000; // this gives idle throttle signal

void setup(void) {
  Serial.begin(115200);

  // TIMER 1
  DDRB |= (1<<DDB5); // D9 Output
 
  // set 1000ms to start motors in idle mode
  OCR1A = 1000L;
 
  // bits 7,5,3 PWM Output on pins A,B,C respectively (COMnx1)
  // bits 1,0 WGM1(1,0): phase correction mode
  TCCR1A = B10000010; // in hex: 0xAA for all 3 = B 1010 1010
  // bit 4,3 WGM_3,2: phase correction mode
  // bits 2,1,0 Prescalar: 8
  TCCR1B = B00010010; // in hex: 0x12; B 0001 0010
  ICR1 = TOP_VALUE;
}//setup


void loop(void) {
  // Serial.println(TOP_VALUE);
  dutytime = serial_input();
  if (dutytime) {
    OCR1A = dutytime;
  }
  delay(500);
}//loop


float serial_input() {
  double num = 0;
  while(Serial.available()) {
    delay(2);
    char ser = Serial.read();
    if(isDigit(ser)) {
      num = (num*10)+(ser-48);
    }else{ break; }
  }
  if (num) {Serial.println(num);}
  return num;
}

What I did wrong was using |= instead of = . Apparently Arduino set some of these Bits on start, which didn't change. Now it works like a charm and I added a frequency choice, which works for at least 50,200,400,488 Hz. Everything in between should work aswell!

Working testcode for timer1 with serial input of HIGH-time:

#define PRESCALAR 8
#define PWM_FREQUENCY 50
#define TOP_VALUE F_CPU/(2*PRESCALAR*PWM_FREQUENCY)

uint16_t dutytime = 1000; // this gives idle throttle signal

void setup(void) {
  Serial.begin(115200);

  // TIMER 1
  DDRB |= (1<<DDB5); // D9 Output
 
  // set 1000ms to start motors in idle mode
  OCR1A = 1000L;
 
  // bits 7,5,3 PWM Output on pins A,B,C respectively (COMnx1)
  // bits 1,0 WGM1(1,0): phase correction mode
  TCCR1A = B10000010; // in hex: 0xAA for all 3 = B 1010 1010
  // bit 4,3 WGM_3,2: phase correction mode
  // bits 2,1,0 Prescalar: 8
  TCCR1B = B00010010; // in hex: 0x12; B 0001 0010
  ICR1 = TOP_VALUE;
}//setup


void loop(void) {
  // Serial.println(TOP_VALUE);
  dutytime = serial_input();
  OCR1A = dutytime;
  delay(500);
}//loop


float serial_input() {
  double num = 0;
  while(Serial.available()) {
    delay(2);
    char ser = Serial.read();
    if(isDigit(ser)) {
      num = (num*10)+(ser-48);
    }else{ break; }
  }
  if (num) {Serial.println(num);}
  return num;
}

What I did wrong was using |= instead of = . Apparently Arduino set some of these Bits on start, which didn't change. Now it works like a charm and I added a frequency choice, which works for at least 50,200,400,488 Hz. Everything in between should work aswell!

Working testcode for timer1 with serial input of HIGH-time:

#define PRESCALAR 8
#define PWM_FREQUENCY 50
#define TOP_VALUE F_CPU/(2*PRESCALAR*PWM_FREQUENCY)

uint16_t dutytime = 1000; // this gives idle throttle signal

void setup(void) {
  Serial.begin(115200);

  // TIMER 1
  DDRB |= (1<<DDB5); // D9 Output
 
  // set 1000ms to start motors in idle mode
  OCR1A = 1000L;
 
  // bits 7,5,3 PWM Output on pins A,B,C respectively (COMnx1)
  // bits 1,0 WGM1(1,0): phase correction mode
  TCCR1A = B10000010; // in hex: 0xAA for all 3 = B 1010 1010
  // bit 4,3 WGM_3,2: phase correction mode
  // bits 2,1,0 Prescalar: 8
  TCCR1B = B00010010; // in hex: 0x12; B 0001 0010
  ICR1 = TOP_VALUE;
}//setup


void loop(void) {
  // Serial.println(TOP_VALUE);
  dutytime = serial_input();
  if (dutytime) {
    OCR1A = dutytime;
  }
  delay(500);
}//loop


float serial_input() {
  double num = 0;
  while(Serial.available()) {
    delay(2);
    char ser = Serial.read();
    if(isDigit(ser)) {
      num = (num*10)+(ser-48);
    }else{ break; }
  }
  if (num) {Serial.println(num);}
  return num;
}
Source Link
mike
  • 347
  • 6
  • 20

What I did wrong was using |= instead of = . Apparently Arduino set some of these Bits on start, which didn't change. Now it works like a charm and I added a frequency choice, which works for at least 50,200,400,488 Hz. Everything in between should work aswell!

Working testcode for timer1 with serial input of HIGH-time:

#define PRESCALAR 8
#define PWM_FREQUENCY 50
#define TOP_VALUE F_CPU/(2*PRESCALAR*PWM_FREQUENCY)

uint16_t dutytime = 1000; // this gives idle throttle signal

void setup(void) {
  Serial.begin(115200);

  // TIMER 1
  DDRB |= (1<<DDB5); // D9 Output
 
  // set 1000ms to start motors in idle mode
  OCR1A = 1000L;
 
  // bits 7,5,3 PWM Output on pins A,B,C respectively (COMnx1)
  // bits 1,0 WGM1(1,0): phase correction mode
  TCCR1A = B10000010; // in hex: 0xAA for all 3 = B 1010 1010
  // bit 4,3 WGM_3,2: phase correction mode
  // bits 2,1,0 Prescalar: 8
  TCCR1B = B00010010; // in hex: 0x12; B 0001 0010
  ICR1 = TOP_VALUE;
}//setup


void loop(void) {
  // Serial.println(TOP_VALUE);
  dutytime = serial_input();
  OCR1A = dutytime;
  delay(500);
}//loop


float serial_input() {
  double num = 0;
  while(Serial.available()) {
    delay(2);
    char ser = Serial.read();
    if(isDigit(ser)) {
      num = (num*10)+(ser-48);
    }else{ break; }
  }
  if (num) {Serial.println(num);}
  return num;
}