2022-07-12 20:14:59 +02:00
|
|
|
#include <avr/io.h>
|
|
|
|
#include <util/delay.h>
|
|
|
|
|
2022-07-13 01:32:12 +02:00
|
|
|
#define PIN_LED 0b1000;
|
|
|
|
|
|
|
|
void init_ports(void) {
|
|
|
|
DDRB = 0x00;
|
|
|
|
DDRC = 0x00;
|
|
|
|
DDRD = 0x00;
|
|
|
|
|
|
|
|
DDRD |= PIN_LED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_pwm(void) {
|
|
|
|
// page 84
|
|
|
|
// 7-6: ouput A, 5-4: output B, 3-2: res, 1-0: waveform
|
|
|
|
TCCR2A = 0b00100011;
|
|
|
|
// 7-6: force output A/B, 5-4: res, 3: waveform, 2-0: clock source
|
|
|
|
TCCR2B = 0b01000001;
|
|
|
|
// duty
|
|
|
|
OCR2B = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void deinit_pwm(void) {
|
|
|
|
TCCR2A = 0;
|
|
|
|
TCCR2B = 0;
|
|
|
|
OCR2B = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pwm_fade_in(int until, int step) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < until; i += step) {
|
|
|
|
OCR2B = i;
|
|
|
|
_delay_ms(0.5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pwm_fade_out(int until, int step) {
|
|
|
|
int i;
|
|
|
|
for (i = OCR2B; i > until; i -= step) {
|
|
|
|
OCR2B = i;
|
|
|
|
_delay_ms(0.25);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 20:14:59 +02:00
|
|
|
int main(void) {
|
2022-07-13 01:32:12 +02:00
|
|
|
init_ports();
|
2022-07-12 20:14:59 +02:00
|
|
|
|
|
|
|
while (1) {
|
2022-07-13 01:32:12 +02:00
|
|
|
init_pwm();
|
|
|
|
pwm_fade_in(0x7f, 1);
|
|
|
|
_delay_ms(300);
|
|
|
|
OCR2B = 0xff;
|
|
|
|
_delay_ms(150);
|
|
|
|
pwm_fade_out(0, 4);
|
|
|
|
deinit_pwm();
|
|
|
|
PORTD &= ~PIN_LED;
|
|
|
|
_delay_ms(2000);
|
2022-07-12 20:14:59 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|