Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.  * PWM - AVR.c
  3.  *
  4.  * Created: 10/13/2017 11:47:44 AM
  5.  * Author : Lamegaton
  6.  */
  7.  
  8. #define F_CPU 16000000UL
  9. #define dtime 1000/256
  10. #include <avr/io.h>
  11. #include <util/delay.h>
  12. #include <stdio.h>
  13.  
  14.  
  15. int main(void)
  16. {
  17.     /* Replace with your application code */
  18.     /*Initialize port and pin*/
  19.     DDRD |= (1<<DDRD6);
  20.     PORTD &= ~(1<<PORTD6);
  21.  
  22.     DDRB &= ~(1<<DDRB7);
  23.     PINB |= (1<<PINB7);
  24.    
  25.     // initialize "fast PWM","compare output mode (non-inverting)"
  26.     TCCR0A |= (1<<COM0A1) | (1<<WGM00) | (1<<WGM01);
  27.     // no-prescaling
  28.     TCCR0B |= (1<<CS00);
  29.     // setting pwm
  30.     uint8_t pwm = 0b00000000;
  31.     uint8_t up = 1;
  32.     /*main loop*/
  33.     while (1)
  34.     {
  35.        
  36.         if (!(PINB & (1<<PINB7))){
  37.             while((!(PINB & (1<<PINB7)))&&(pwm < 0xff)){
  38.                     pwm+=up;
  39.                     OCR0A = pwm;
  40.                     _delay_ms(dtime);
  41.             }
  42.         }
  43.         else{
  44.             while((pwm > 0x00)&&(PINB & (1<<PINB7))){
  45.                 pwm-=up;
  46.                 OCR0A = pwm;
  47.                 _delay_ms(dtime);
  48.             }
  49.  
  50.         /*
  51.         if (!(PINB & (1<<PINB7)))
  52.             PORTD |= (1<<PORTD6);//change it to increasing
  53.         else
  54.             PORTD &= ~(1<<PORTD6);//change it to decreasing
  55.         */
  56.         }
  57.     }
  58. }