I have extracted the following code from the multiwii project. It generates a ~490-500hz pwm signal on pin 6 which is used to drive an esc.
I want to change this frequency down to 50Hz to drive a servo instead. But ive had next to no experience at this level of avr programming, and dont want to loose anymore days to it, if its a dead end. (ie not possible) (i have tried reading various websites on the topic)
void setup() {
pinMode(6, OUTPUT);
TCCR4E |= (1<<ENHC4); // enhanced pwm mode
TCCR4B &= ~(1<<CS41); TCCR4B |= (1<<CS42)|(1<<CS40); // prescaler to 16
TCCR4D |= (1<<WGM40); TC4H = 0x3; OCR4C = 0xFF; // phase and frequency correct mode & top to 1023 but with enhanced pwm mode we have 2047
TCCR4C |= (1<<COM4D1)|(1<<PWM4D); // connect pin 6 to timer 4 channel D
}
void loop() {
//write out
int value = 1500;
TC4H = value>>8;
OCR4D = (value&0xFF);
}
here is the new code so far working off the solution provided below:
void setup() {
pinMode(6, OUTPUT);
TCCR4B &= ~(_BV(CS43) | _BV(CS42) | _BV(CS41) | _BV(CS40));
TCCR4B |= _BV(CS43) | _BV(CS41);
TCCR4D &= ~(_BV(WGM41) | _BV(WGM40));
TC4H = 624 >> 8; // B10 0x2
OCR4C = 624 & 0xff;//B1110000 0x70
TCCR4C |= (1<<COM4D1)|(1<<PWM4D);
}
void loop() {
//write out
//30 - 60 gives 1000 to 2000 us, but that only allows 30 increments between ??
int value = 30;
TC4H = value >> 8;
OCR4D = value & 0xff;
}
the wave form i want: