Advertisement
Guest User

Untitled

a guest
Jan 14th, 2012
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #define F_CPU 1000000
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4.  
  5. #define OutputCompareRegister1 OCR1A
  6. #define OutputCompareRegister2 OCR1B
  7.  
  8. #define PWMDDR DDRB
  9.  
  10. #define PWMpin1 PB1
  11. #define PWMpin2 PB2
  12.  
  13. void init16bitPWM() //this function works for ATMega8 but needs to be configured for other AVR uCs (configure the registers and the bits)
  14. {
  15.     PWMDDR |= 1<<PWMpin1;
  16.     PWMDDR |= 1<<PWMpin2;
  17.    
  18.    
  19.     TCCR1A |= 1<<WGM11 | 1<<COM1A1 | 1<<COM1B1;
  20.     TCCR1B |= 1<<WGM12 | 1<<WGM13 | 1<<CS10;
  21.    
  22.     ICR1 = 65535;
  23.    
  24.     OutputCompareRegister1 = 0;
  25.     OutputCompareRegister2 = 0;
  26. }
  27.  
  28. void changeDuty16bit(uint16_t duty, uint8_t channel)
  29. {
  30.     switch (channel)
  31.     {
  32.     case 1:
  33.         OutputCompareRegister1=duty;
  34.         break;
  35.     case 2:
  36.         OutputCompareRegister2=duty;
  37.         break;
  38.     }
  39. }
  40.  
  41. int main(void)
  42. {
  43.     //Initialization
  44.     init16bitPWM();
  45.    
  46.     //Variables
  47.     int i;
  48.    
  49.     //Infinite loop
  50.     while(1)
  51.     {
  52.        
  53.         for (i=0;i<65536;i++)
  54.         {
  55.             changeDuty16bit(i,1);
  56.             _delay_ms(10);
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement