7_PWM/program PWM.c
2023-04-11 14:28:20 +03:00

42 lines
1013 B
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#define F_CPU 16000000UL //частота работы микроконтроллера
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
void timer1_init(int frequency, int dutycycle){
DDRD |= (1 << PORTD6); //OC1A (12)
DDRD |= (1 << PORTD5); //OC1B (11)
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
// Не инверсный режим работы и OC0B
TCCR1A |= (1 << COM1A1) | (0 << COM1A0);
//Fast PWM (Быстрый ШИМ)
TCCR1A |= (1 << WGM11) | (1 << WGM10);
TCCR1B |= (1 << WGM13) | (1 << WGM12);
//Предделитель - 1024
TCCR1B |= (1 << CS12) | (0 << CS11) | (1 << CS10);
//Частота = Частота_мк / (Предделитель * OCR1A)
OCR1A = F_CPU / 1024 / frequency;
//Скважность
OCR1B = OCR1A * dutycycle / 100;
}
int main(void){
cli();
timer1_init(); //после инициализации на выводе OC1B (11) будет ШИМ сигнал
sei();
while (1){
}
return 0;
}