Guest User

Untitled

a guest
Apr 9th, 2019
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. // 9.6 MHz, built in resonator
  2. #define F_CPU 9600000
  3. #define LED PB1
  4. #define LED1 PB0
  5.  
  6.  
  7. #include <avr/io.h>
  8. #include <util/delay.h>
  9.  
  10.  
  11. void pwm_setup (void)
  12. {
  13.     // Set Timer 0 prescaler to clock/8.
  14.     // At 9.6 MHz this is 1.2 MHz.
  15.     TCCR0B |= (1 << CS01);
  16.    
  17.  
  18.     // Set to 'Fast PWM' mode
  19.     TCCR0A |= (1 << WGM01) | (1 << WGM00);
  20.  
  21.     // Clear OC0B output on compare match, upwards counting.
  22.     TCCR0A |= (1 << COM0B1);
  23. }
  24.  
  25. void pwm_write (int val)
  26. {
  27.     OCR0B = val;
  28. }
  29.  
  30. int main (void)
  31. {
  32.     int pwm = 0;
  33.     int count = 0;
  34.    
  35.     // LED is an output.
  36.     DDRB |= (1 << LED);
  37.     DDRB |= (1 << LED1);  
  38.  
  39.     pwm_setup();
  40.  
  41.     while (1) {
  42.        
  43.         // Get the ADC value
  44.         //adc_in = adc_read();
  45.         // Now write it to the PWM counter
  46.         pwm_write(pwm);
  47.         _delay_ms(5);
  48.        
  49.         if (pwm==255)
  50.         {
  51.             count = 1;
  52.         }
  53.         if (pwm==0)
  54.         {
  55.             count = 0;
  56.         }
  57.        
  58.         if (count == 0)
  59.         {
  60.             pwm++;
  61.         }
  62.         if (count == 1)
  63.         {
  64.             pwm--;
  65.         }
  66.        
  67.        
  68.  
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment