Advertisement
Guest User

avr atmega32 rgb controller

a guest
May 23rd, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/delay.h>
  3.  
  4. int maxValue(int x) {
  5.     if (x > 255) {
  6.         return 255;
  7.     }
  8.  
  9.     return x;
  10. }
  11.  
  12. int minValue(int x) {
  13.     if (x < 0) {
  14.         return 0;
  15.     }
  16.  
  17.     return x;
  18. }
  19.  
  20. int main(void) {
  21.     /*
  22.     WGM10, WGM11 - A
  23.     WGM12, WGM13 - B
  24.     */
  25.     TCCR0|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(0<<CS02)|(0<<CS01)|(1<<CS00); // 8 bit
  26.     // (1<<COM1A1)|(1<<COM1B1) - 16bit -> 2x8bit
  27.     // fast PWM, ICR1 = TOP
  28.     TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);
  29.     // (0<<CS12)|(0<<CS11)|(1<<CS10) - prescaler 1
  30.     TCCR1B|=(1<<WGM13)|(1<<WGM12)|(0<<CS12)|(0<<CS11)|(1<<CS10);
  31.     ICR1=255;
  32.  
  33.     DDRB |= (1 << PB3);
  34.     DDRD |= (1 << PD5) | (1 << PD4);
  35.     DDRA = 0x00; // in
  36.    
  37.     PORTA = 0xFF;
  38.  
  39.     int r = 255;
  40.     int g = 255;
  41.     int b = 255;
  42.  
  43.     OCR0 = r;   // PB3, R
  44.     OCR1A = g;  // PD5, G
  45.     OCR1B = b;  // PD4, B
  46.  
  47.     int constN = 255 / 7;
  48.     int lastButton = 0;
  49.  
  50.     while (1) {
  51.         if (PINA == lastButton) {
  52.             continue;
  53.         }
  54.  
  55.         lastButton = PINA;
  56.  
  57.         switch (PINA) {
  58.             // R
  59.             case 0b11111110:
  60.                 r += constN;
  61.                 r = maxValue(r);
  62.                 OCR0 = r;
  63.                 break;
  64.             case 0b11111101:
  65.                 r -= constN;
  66.                 r = minValue(r);
  67.                 OCR0 = r;
  68.                 break;
  69.            
  70.             // G
  71.             case 0b11111011:
  72.                 g += constN;
  73.                 g = maxValue(g);
  74.                 OCR1A = g;
  75.                 break;
  76.             case 0b11110111:
  77.                 g -= constN;
  78.                 g = minValue(g);
  79.                 OCR1A = g;
  80.                 break;
  81.            
  82.             // B
  83.             case 0b11101111:
  84.                 b += constN;
  85.                 b = maxValue(b);
  86.                 OCR1B = b;
  87.                 break;
  88.             case 0b11011111:
  89.                 b -= constN;
  90.                 b = minValue(b);
  91.                 OCR1B = b;
  92.                 break;
  93.         }
  94.     }
  95.  
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement