Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void pwm_init()
- {
- //Timer1
- //top=15000, prescaler=8, frec=50hz (T=20ms)
- ICR1=15000;
- TCCR1B|=(1<<CS11);
- //phase and frequency correction, icr1 = top
- TCCR1B|=(1<<WGM13);
- //set OC1A,OC2B on downcounting, clear on upcounting
- TCCR1A |= (1<<COM1A1)|(1<<COM1B1);
- //Timer 2
- //top=233, prescaler=1024, frec=50hz (T=20ms)
- OCR2A=233;//233
- OCR2B=200;//200
- TCCR2B|=(1<<CS22)|(1<<CS21)|(1<<CS20);
- //phase correction, OCR2x = top
- TCCR2A|=(1<<WGM22)|(1<<WGM20)|(1<<WGM21);
- //set OC1A,OC2B on downcounting, clear on upcounting
- TCCR2A |=(1<<COM2A1)|(1<<COM2B1);
- //make sure to make pin as output pin
- DDRD |= (1<<PD4);
- DDRD |= (1<<PD5);
- DDRD |= (1<<PD6);
- DDRD |= (1<<PD7);
- }
- int get_pulse(int degrees){
- return (int)((float)degrees*750/180+750);
- }
- int get_pulset2(int degrees){
- return (int)((float)degrees*12/180+12);
- }
- void set_pulse1(int degrees){
- OCR1A=get_pulse(degrees);
- }
- void set_pulse2(int degrees){
- OCR1B=get_pulse(degrees);
- }
- void set_pulse3(int degrees){
- OCR2A=get_pulset2(degrees);
- }
- void set_pulse4(int degrees){
- OCR2B=get_pulset2(degrees);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement