Advertisement
RybaSG

PWM

Aug 5th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <avr/io.h>     // dołączenie głównego systemowego  pliku nagłówkowego
  2. #include <avr/interrupt.h>
  3. #include <util/delay.h>
  4.  
  5. volatile uint8_t pwm;
  6.  
  7. int main()
  8. {
  9.  
  10.     DDRB |= (1<<PB3); // kierunek wyjściowy dla OC0
  11.  
  12.     // konfiguracja fast PWM
  13.     TCCR0 |= (1<<WGM01) | (1<<WGM00); // tryb fast PWM
  14.     TCCR0 |= (1<<COM01);              // clear OC0 on compare match
  15.     TCCR0 |= (1<<CS00);               // preskaler na 1
  16.     OCR0 = 128;                   // wypełnienie około 50%
  17.  
  18.     // programowy PWM
  19.     DDRA |= (1<<PA0); // wyjście PWM
  20.  
  21.     TCCR2 |= (1<<WGM21); // tryb CTC
  22.     TCCR2 |= (1<<CS20);// preskaler 1
  23.     OCR2 = 77; // ustalenie częstotliwości (256kHz)
  24.     TIMSK |= (1<<OCIE2); // zezwolenie na Compare Match Interrupt
  25.  
  26.  
  27.  
  28.  
  29.  
  30.     sei();
  31.     while(1)
  32.     {
  33.     pwm = 127;
  34.     }
  35.  
  36.     //uint8_t i;
  37.  
  38.     /*while(1)
  39.     {
  40.         // pętla rozjaśniająca
  41.         for( i = 0 ; i<255; i++)
  42.         {
  43.             OCR0 = i;
  44.             _delay_ms(3);
  45.         }
  46.         // pętla gasząca
  47.         for( i = 255; i > 0; i--)
  48.         {
  49.             OCR0 = i;
  50.             _delay_ms(3);
  51.         }
  52.  
  53.     }*/
  54.  
  55. }
  56.  
  57. ISR ( TIMER2_COMP_vect )
  58. {
  59.     static uint8_t counter;
  60.  
  61.  
  62.  
  63.     if(counter>=pwm)
  64.     {
  65.         //PORTA |= (1<<PA0);
  66.         PORTA &= ~(1<<PA0);
  67.     }
  68.     else
  69.     {
  70.         //PORTA &= ~(1<<PA0);
  71.         PORTA |= (1<<PA0);
  72.     }
  73.  
  74.     counter++;
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement