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>
- #define OutputCompareRegister1 OCR1A
- #define OutputCompareRegister2 OCR1B
- #define PWMDDR DDRB
- #define PWMpin1 PB1
- #define PWMpin2 PB2
- 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 = 65535;
- 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();
- //Variables
- int i;
- //Infinite loop
- while(1)
- {
- for (i=0;i<65536;i++)
- {
- changeDuty16bit(i,1);
- _delay_ms(10);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement