Advertisement
miszczo

[AVR]ATtiny 13 software PWM 3 way (RGB LAMP)

Apr 3rd, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. /*
  2.  * main.c
  3.  *
  4.  *  Created on: 01-04-2013
  5.  *              15:05:56
  6.  *      Author: miszczo
  7. */
  8.  
  9.  
  10. #include <avr/io.h>
  11. #include <avr/interrupt.h>
  12. #include <util/delay.h>
  13. #define TIME 10
  14.  
  15. volatile uint8_t R = 0;
  16. volatile uint8_t G = 0;
  17. volatile uint8_t B = 0;
  18.  
  19.  
  20. int main(void)
  21. {
  22.     //wyłączenie komparatora
  23.     ADCSRB |= (1<<ACME);
  24.  
  25.     // wyłączenie adc;
  26.     PRR |= (1<<PRADC);
  27.  
  28.     // ustawienia portow
  29.     DDRB |= (1<<PB0) | (1<<PB1) | (1<<PB2) ;
  30.  
  31.  
  32.     //konfiguracja timera
  33.     TCCR0B |= (1<<CS00);        //preskaler 1
  34.     TIMSK0 |= (1<<TOIE0);       //zezwolenie na przerwanie przy przepelnieniu
  35.  
  36.     sei();
  37.     char kanter = 1;
  38.  
  39.  
  40.     while(1)
  41.     {
  42.         char i;
  43.  
  44.         for(i=0;i<255;i++)
  45.         {
  46.             if(kanter == 1)
  47.             {
  48.                 //R = 0
  49.                 //G = 0
  50.                 B = i;
  51.             }
  52.  
  53.             if(kanter == 2)
  54.             {
  55.                 //R = 0
  56.                 G = i;
  57.                 //B = 255
  58.             }
  59.  
  60.             if(kanter == 3)
  61.             {
  62.                 //R = 0
  63.                 //G =255
  64.                 B = 255 - i;
  65.             }
  66.  
  67.             if(kanter == 4)
  68.             {
  69.                 R = i;
  70.                 //G = 255
  71.                 //B = 0
  72.             }
  73.  
  74.             if(kanter == 5)
  75.             {
  76.                 //R = 255
  77.                 //G = 255
  78.                 B = i;
  79.             }
  80.  
  81.             if(kanter == 6)
  82.             {
  83.                 //R = 255
  84.                 G = 255-i;
  85.                 //B = 255
  86.             }
  87.  
  88.             if(kanter == 7)
  89.             {
  90.                 //R = 255
  91.                 //G = 0
  92.                 B = 255-i;
  93.             }
  94.  
  95.             if(kanter == 8)
  96.             {
  97.                 R = 255-i;
  98.                 //G = 0
  99.                 //B = 0
  100.             }
  101.  
  102.             _delay_ms(TIME);
  103.         }
  104.  
  105.         kanter++;
  106.         if(kanter == 9) kanter = 1;
  107.  
  108.     }
  109.  
  110.  
  111. }
  112.  
  113.  
  114. ISR ( TIM0_OVF_vect )
  115. {
  116.     static uint8_t licznik;
  117.  
  118.     if(licznik>=R) PORTB |= (1<<PB0); else PORTB &= ~(1<<PB0);
  119.     if(licznik>=G) PORTB |= (1<<PB1); else PORTB &= ~(1<<PB1);
  120.     if(licznik>=B) PORTB |= (1<<PB2); else PORTB &= ~(1<<PB2);
  121.  
  122.     licznik++;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement