//#include <avr/io.h>
//#include <avr/interrupt.h>
/*
#define SERVO_PIN PL3 // Assuming PWM pin is connected to PL3 (Arduino Mega digital pin 46)
void setup() {
// Set PWM pin as output
DDRL |= (1 << SERVO_PIN);
// Configure Timer3 for PWM mode (Fast PWM with TOP = ICR3)
TCCR3A |= (1 << WGM31) | (1 << COM3A1); // Fast PWM, non-inverting mode
TCCR3B |= (1 << WGM33) | (1 << WGM32); // Fast PWM, TOP = ICR3
TCCR3B |= (1 << CS31); // Set prescaler to 8, 16 MHz / 8 = 2 MHz (0.5 us per tick)
// Set ICR3 to determine PWM frequency (20 ms period for servo control)
ICR3 = 40000; // 2 MHz / 40000 = 50 Hz (20 ms period)
// Set OCR3A to determine PWM pulse width (1.5 ms)
OCR3A = 4000; // 1.5 ms / 0.5 us = 3000 ticks
// Enable Timer3 overflow interrupt (optional)
// TIMSK3 |= (1 << TOIE3);
// Enable global interrupts (optional)
// sei();
}
void loop() {
// No need for anything here if you want the pulse to be continuous
}
*/
#include <avr/io.h>
int increaseFact = 1;
void setupPWM() {
// Set Fast PWM mode with ICR1 as TOP
TCCR1A |= (1 << WGM11); // Mode 14: Fast PWM, TOP is ICR1
TCCR1B |= (1 << WGM12) | (1 << WGM13); // Mode 14: Fast PWM, TOP is ICR1
// Set prescaler to 8
TCCR1B |= (1 << CS11);
// Set ICR1 to define TOP value for 20 ms period
ICR1 = 39999; // (16 MHz / (8 * 50 Hz)) - 1
// Set duty cycle (OCR1A) for 1.333 ms pulse width
OCR1A = 1000; // (1.333 ms / 20 ms) * 39999
// Set output pin for PWM (e.g., OC1A on PB1/Pin 11)
DDRB |= (1 << PB5); // Set PB1 as output
// Enable PWM on OC1A (Clear on Compare Match, set at BOTTOM)
TCCR1A |= (1 << COM1A1);
}
int main() {
setupPWM();
while (1) {
// Main loop
}
return 0;
}
/*
void setup(){
//setupPWM();
}
void loop(){
setupPWM();
}*/