2022-07-12 20:14:59 +02:00
|
|
|
#include <avr/io.h>
|
|
|
|
#include <util/delay.h>
|
|
|
|
|
2022-07-14 16:35:11 +02:00
|
|
|
#define PIN_LED 0b1000
|
|
|
|
#define PIN_BUTTON 0b0001
|
|
|
|
#define PIN_TRIM 0b0001
|
2022-07-13 01:32:12 +02:00
|
|
|
|
|
|
|
void init_ports(void) {
|
2022-07-14 16:35:11 +02:00
|
|
|
// all outputs, swallows noise
|
|
|
|
DDRB = 0xff;
|
|
|
|
DDRC = 0xff;
|
|
|
|
DDRD = 0xff;
|
2022-07-13 01:32:12 +02:00
|
|
|
|
|
|
|
DDRD |= PIN_LED;
|
2022-07-14 16:35:11 +02:00
|
|
|
PORTB |= PIN_BUTTON; // pull-up
|
|
|
|
DDRB &= ~PIN_BUTTON;
|
|
|
|
DDRC &= ~PIN_TRIM;
|
2022-07-13 01:32:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-14 16:35:11 +02:00
|
|
|
/*
|
|
|
|
int analog_read(void) {
|
|
|
|
ADCSRA |= ADEN;
|
|
|
|
ADCSRA |= ADSC;
|
|
|
|
}*/
|
2022-07-13 01:32:12 +02:00
|
|
|
|
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-14 16:35:11 +02:00
|
|
|
if (!(PINB & PIN_BUTTON)) {
|
|
|
|
init_pwm();
|
|
|
|
pwm_fade_in(0x7f >> 5, 1);
|
|
|
|
_delay_ms(300);
|
|
|
|
OCR2B = 0x7f >> 3;
|
|
|
|
_delay_ms(150);
|
|
|
|
pwm_fade_out(32, 4);
|
|
|
|
_delay_ms(50);
|
|
|
|
pwm_fade_out(0, 4);
|
|
|
|
}
|
2022-07-13 01:32:12 +02:00
|
|
|
deinit_pwm();
|
|
|
|
PORTD &= ~PIN_LED;
|
2022-07-12 20:14:59 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|