Advertisement
dumle29

Untitled

Jun 4th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 KB | None | 0 0
  1.  
  2. #define F_CPU   8000000L //Running at 8Mhz
  3.  
  4. #include <avr/io.h>
  5. #include <avr/interrupt.h>
  6. #include <util/delay.h>
  7.  
  8. double dutyCycle = 0;
  9.  
  10. int main(void)
  11. {    
  12.     DDRB = (1 << PORTB0); // Set PB0 to an outpu
  13.      
  14.     TCCR0A = (1 << COM0A1) | (1 << WGM00) | (1 << WGM01); // Non inverting Fast PWM. Fast PWM (Mode 3)
  15.     TIMSK = (1 << TOIE0);
  16.      
  17.     OCR0A = (dutyCycle/100)*255; // Get the dutycycle setup
  18.      
  19.     sei(); // Setup external interupts
  20.      
  21.     TCCR0B = (1 << CS00); // No prescaler
  22.      
  23.     while(1)
  24.     {
  25.         _delay_ms(100); // Wait 100ms
  26.          
  27.         dutyCycle += 10; // Increase pwm with 10 percent
  28.          
  29.         if(dutyCycle > 100) // If dutycycle is 100 percent, reset it.
  30.         {
  31.             dutyCycle = 0;
  32.         }                        
  33.     }
  34. }
  35.  
  36. ISR(TIMER0_OVF_vect)
  37. {
  38.     OCR0A = (dutyCycle/100)*255; // Update dutycycle after every timer interupt.
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement