Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 8000000L //Running at 8Mhz
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- double dutyCycle = 0;
- int main(void)
- {
- DDRB = (1 << PORTB0); // Set PB0 to an outpu
- TCCR0A = (1 << COM0A1) | (1 << WGM00) | (1 << WGM01); // Non inverting Fast PWM. Fast PWM (Mode 3)
- TIMSK = (1 << TOIE0);
- OCR0A = (dutyCycle/100)*255; // Get the dutycycle setup
- sei(); // Setup external interupts
- TCCR0B = (1 << CS00); // No prescaler
- while(1)
- {
- _delay_ms(100); // Wait 100ms
- dutyCycle += 10; // Increase pwm with 10 percent
- if(dutyCycle > 100) // If dutycycle is 100 percent, reset it.
- {
- dutyCycle = 0;
- }
- }
- }
- ISR(TIMER0_OVF_vect)
- {
- OCR0A = (dutyCycle/100)*255; // Update dutycycle after every timer interupt.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement