/*
* PWM - AVR.c
*
* Created: 10/13/2017 11:47:44 AM
* Author : Lamegaton
*/
#define F_CPU 16000000UL
#define dtime 1000/256
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
int main(void)
{
/* Replace with your application code */
/*Initialize port and pin*/
DDRD |= (1<<DDRD6);
PORTD &= ~(1<<PORTD6);
DDRB &= ~(1<<DDRB7);
PINB |= (1<<PINB7);
// initialize "fast PWM","compare output mode (non-inverting)"
TCCR0A |= (1<<COM0A1) | (1<<WGM00) | (1<<WGM01);
// no-prescaling
TCCR0B |= (1<<CS00);
// setting pwm
uint8_t pwm = 0b00000000;
uint8_t up = 1;
/*main loop*/
while (1)
{
if (!(PINB & (1<<PINB7))){
while((!(PINB & (1<<PINB7)))&&(pwm < 0xff)){
pwm+=up;
OCR0A = pwm;
_delay_ms(dtime);
}
}
else{
while((pwm > 0x00)&&(PINB & (1<<PINB7))){
pwm-=up;
OCR0A = pwm;
_delay_ms(dtime);
}
/*
if (!(PINB & (1<<PINB7)))
PORTD |= (1<<PORTD6);//change it to increasing
else
PORTD &= ~(1<<PORTD6);//change it to decreasing
*/
}
}
}