7_PWM/program PWM.c
2023-04-11 15:02:16 +03:00

48 lines
1.2 KiB
C
Raw Permalink 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 intfrequency,int fracfrequency,int dutycycle){
DDRD |= (1 << PORTD6); //OC1A (12)
DDRD |= (1 << PORTD5); //OC1B (11)
//Частота
int frequency = intfrequency + fracfrequency / 10000;
OCR1A = F_CPU / 1024 / frequency;
//Скважность
uint8_t tempOCR1B = OCR1A * dutycycle / 100;
OCR1B = tempOCR1B;
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
// Не инверсный режим работы и OC0B
uint8_t tempTCCR1A |= (1 << COM1A1) | (0 << COM1A0);
//Fast PWM (Быстрый ШИМ)
tempTCCR1A |= (1 << WGM11) | (1 << WGM10);
uint8_t tempTCCR1B |= (1 << WGM13) | (1 << WGM12);
//Предделитель - 1024
tempTCCR1B |= (1 << CS12) | (0 << CS11) | (1 << CS10);
//Присваивание регистров
TCCR1A = tempTCCR1A;
TCCR1B = tempTCCR1B;
}
int main(void){
cli();
timer1_init(); //после инициализации на выводе OC1B (11) будет ШИМ сигнал
sei();
while (1){
}
return 0;
}