Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 1000000
- #include <avr/io.h>
- #include <util/delay.h>
- #include <math.h>
- #define OutputCompareRegister1 OCR1A
- #define OutputCompareRegister2 OCR1B
- #define PWMDDR DDRB
- #define PWMpin1 PB1
- #define PWMpin2 PB2
- uint8_t sine8bit[];
- void sineWave8bit()
- {
- for (int i=0; i<256; i++)
- {
- sine8bit[i]=128+(127*sin(2.0*M_PI*i/256));
- }
- }
- void init16bitPWM() //this function works for ATMega8 but needs to be configured for other AVR uCs (configure the registers and the bits)
- {
- PWMDDR |= 1<<PWMpin1;
- PWMDDR |= 1<<PWMpin2;
- TCCR1A |= 1<<WGM11 | 1<<COM1A1 | 1<<COM1B1;
- TCCR1B |= 1<<WGM12 | 1<<WGM13 | 1<<CS10;
- ICR1 = 255;
- OutputCompareRegister1 = 0;
- OutputCompareRegister2 = 0;
- }
- void changeDuty16bit(uint16_t duty, uint8_t channel)
- {
- switch (channel)
- {
- case 1:
- OutputCompareRegister1=duty;
- break;
- case 2:
- OutputCompareRegister2=duty;
- break;
- }
- }
- int main(void)
- {
- //Initialization
- init16bitPWM();
- sineWave8bit();
- //Variables
- int i;
- //Infinite loop
- while(1)
- {
- for (i=0;i<256;i++)
- {
- changeDuty16bit(sine8bit[i],1);
- _delay_us(5);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement