void setup() {
// put your setup code here, to run once:
// 25kHz PWM frekansını üretip analogwrite fonksiyonuyla kullanabilmek için
// Set the main system clock to 8 MHz.
noInterrupts();
CLKPR = _BV(CLKPCE); // enable change of the clock prescaler
CLKPR = _BV(CLKPS0); // divide frequency by 2
interrupts();
// Configure Timer 0 for phase correct PWM @ 25 kHz.
TCCR0A = 0; // undo the configuration done by...
TCCR0B = 0; // ...the Arduino core library
TCNT0 = 0; // reset timer
TCCR0A = _BV(COM0B1) // non-inverted PWM on ch. B
| _BV(WGM00); // mode 5: ph. correct PWM, TOP = OCR0A
TCCR0B = _BV(WGM02) // ditto
| _BV(CS00); // prescaler = 1
OCR0A = 160; // TOP = 160 // potansiyometreyi 160a böl
// Timer 1.
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
TCCR1A = _BV(COM1A1) // non-inverted PWM on ch. A
| _BV(COM1B1) // same on ch. B
| _BV(WGM11); // mode 10: ph. correct PWM, TOP = ICR1
TCCR1B = _BV(WGM13) // ditto
| _BV(CS10); // prescaler = 1
ICR1 = 160;
// Timer 2.
TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
TCCR2A = _BV(COM2B1) // non-inverted PWM on ch. B
| _BV(WGM20); // mode 5: ph. correct PWM, TOP = OCR2A
TCCR2B = _BV(WGM22) // ditto
| _BV(CS20); // prescaler = 1
OCR2A = 160;
// 25kHz PWM üretildi, analogwrite fonksiyonu 25 kHz ile çalışacak
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(A0,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
float a=analogRead(A0);
a = map(a, 0, 1023, 0, 255);
analogWrite(11, a );
analogWrite(10, a);
analogWrite(6, LOW);
analogWrite(5, LOW);
}
Currently i use this code.It is a H-bridge DC motor using P-type MOSFETs on top and N-type MosFETs on bottom. But i don't know if this piece of code works for that frequency adjustment .Can you help me how to write the code for creating 25 khz pwm frequency.I'm beginner for the topic.
