Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/delay.h>
- #define MIN(a, b) (a < b) ? a : b
- #define MAX(a, b) (a > b) ? a : b
- #define BTN(id) (0xFF ^ (1 << id))
- #define RGBDOWN(dest, src) {\
- src = MAX(src - 1, 0);\
- dest = levels[src];\
- break;\
- }
- #define RGBUP(dest, src) {\
- src = MIN(src + 1, 7);\
- dest = levels[src];\
- break;\
- }
- int16_t levels[] = {
- 255 - 0,
- 255 - 255 * 0.1429,
- 255 - 255 * 0.2857,
- 255 - 255 * 0.4286,
- 255 - 255 * 0.5714,
- 255 - 255 * 0.7142,
- 255 - 255 * 0.8571,
- 255 - 255
- };
- int main(void) {
- TCCR0|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(0<<CS02)|(0<<CS01)|(1<<CS00);
- TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);
- TCCR1B|=(1<<WGM13)|(1<<WGM12)|(0<<CS12)|(0<<CS11)|(1<<CS10);
- ICR1=255;
- DDRB |= (1 << PB3);
- DDRD |= (1 << PD5) | (1 << PD4);
- DDRA = 0x00;
- PORTA = 0xFF;
- int rLvl = 0;
- int gLvl = 0;
- int bLvl = 0;
- OCR0 = levels[rLvl];
- OCR1B = levels[gLvl];
- OCR1A = levels[bLvl];
- uint32_t counter = 0;
- uint32_t lastDebounceTime = 0;
- uint32_t debounceDelay = 10000;
- int buttonState = 0;
- int lastButtonState = 0;
- while(1) {
- counter++;
- int reading = PINA;
- if (reading != lastButtonState) {
- lastDebounceTime = counter;
- }
- if ((counter - lastDebounceTime) > debounceDelay) {
- if (reading != buttonState) {
- buttonState = reading;
- switch (PINA) {
- case BTN(0): RGBDOWN(OCR0, rLvl);
- case BTN(1): RGBUP(OCR0, rLvl);
- case BTN(2): RGBDOWN(OCR1B, gLvl);
- case BTN(3): RGBUP(OCR1B, gLvl);
- case BTN(4): RGBDOWN(OCR1A, bLvl);
- case BTN(5): RGBUP(OCR1A, bLvl);
- }
- }
- }
- lastButtonState = reading;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement