Advertisement
Guest User

Untitled

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