Advertisement
miszczo

[AVR]Software PWM ATtiny 13

Apr 2nd, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 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.  
  14. volatile uint8_t pwm;
  15.  
  16.  
  17. int main(void)
  18. {
  19.     //wyłączenie komparatora
  20.     ADCSRB |= (1<<ACME);
  21.  
  22.     // wyłączenie adc;
  23.     PRR |= (1<<PRADC);
  24.  
  25.     // ustawienia portow
  26.     DDRB |= (1<<PB0) | (1<<PB1) | (1<<PB2) | (1<<PB3);
  27.  
  28.  
  29.     //konfiguracja timera
  30.     TCCR0B |= (1<<CS00);        //preskaler 1
  31.     TIMSK0 |= (1<<TOIE0);       //zezwolenie na przerwanie przy przepelnieniu
  32.  
  33.     sei();
  34.     //char i;
  35.     while(1)
  36.     {
  37.         for(pwm=0;pwm<=255;pwm++) _delay_ms(20);
  38.         for(pwm=255;pwm>=0;pwm--) _delay_ms(20);
  39.  
  40.     }
  41.  
  42.  
  43. }
  44.  
  45.  
  46. ISR ( TIM0_OVF_vect )
  47. {
  48.     static uint8_t licznik;
  49.  
  50.     if(licznik>=pwm) PORTB |= (1<<PB3); else PORTB &= ~(1<<PB3);
  51.  
  52.     licznik++;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement