#include #include #define PIN_LED 0b1000 #define PIN_BUTTON 0b0001 #define PIN_TRIM 0b0001 void init_ports(void) { // all outputs, swallows noise DDRB = 0xff; DDRC = 0xff; DDRD = 0xff; DDRD |= PIN_LED; PORTB |= PIN_BUTTON; // pull-up DDRB &= ~PIN_BUTTON; DDRC &= ~PIN_TRIM; } 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); } } /* int analog_read(void) { ADCSRA |= ADEN; ADCSRA |= ADSC; }*/ int main(void) { init_ports(); while (1) { 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); } deinit_pwm(); PORTD &= ~PIN_LED; } return 0; }