LowTech.com


ローテク、ローコストで面白いものに挑戦してみよう!


ARDUINO UNO's PWM

1. Timer0(8bit) PWM PWM outputs using timer 0 are output from pin 6 and 5. The duty cycle is controlled by OCR0A and OCR0B. PWM output 0A = pin6 = PORTD_bit6 : PWM duty register = OCR0A PWM output 0B = pin5 = PORTD_bit5 : PWM duty register = OCR0B When using timer 0, TCCR0A and TCCR0B must be set. Details of TCCR0A and TCCR0B set up are as follows.

The settings for PWM 0A and 0B outputs, Fast PWM(TOP=0xff), and prescaler=64 are as follows: setup(){ pinMode(6, OUTPUT); pinMode(5, OUTPUT); TCCR0A = 0; // Clear timer control register. TCCR0B = 0; // Clear timer control register. TCCR0A = 0b10100011; //1010: PWM 0A,0B output, 11: Fast PWM, TOP=FF TCCR0B = 0b00000011; //011: PSR=64 Period T=(1/16MHz/64)*256=1.024(ms) Freq.=1/T=976Hz OCR0A = 127; // PWM output 0A Duty = (127+1)/256 = 50% OCR0B = 63; // PWM output 0B Duty = (63+1)/256 = 25% } The duty ratio can be changed freely using OCR0A and OCR0B.

2. Timer2(8bit) PWM The PWM output setting using timer 2 is the same as timer 0 except that the prescaler setting is different. PWM output 2A = pin11 = PORTB_bit3 : PWM duty register = OCR2A PWM output 2B = pin3 = PORTD_bit3 : PWM duty register = OCR2B Details of TCCR2A and TCCR2B set up are as follows.

The settings for PWM 2A and 2B outputs, Fast PWM(TOP=0xff), and prescaler=64 are as follows: setup(){ pinMode(11, OUTPUT); pinMode(3, OUTPUT); TCCR2A = 0; // Clear timer control register. TCCR2B = 0; // Clear timer control register. TCCR2A = 0b10100011; //1010: PWM 2A,2B output, 11: Fast PWM, TOP=FF TCCR2B = 0b00000100; //100: PSR=64 Period T=(1/16MHz/64)*256=1.024(ms) Freq.=1/T=976Hz OCR2A = 127; // PWM output 0A Duty = (127+1)/256 = 50% OCR2B = 63; // PWM output 0B Duty = (63+1)/256 = 25% } The duty ratio can be changed freely using OCR2A and OCR2B.

3. Timer1(16bit) PWM The PWM output setting using timer 1 is set by TCCR1A and TCCR1B, and can generate various types of PWM. PWM output 1A = pin9 = PORTB_bit1 : PWM duty register = OCR1A PWM output 1B = pin10 = PORTB_bit2 : PWM duty register = OCR1B Details of TCCR1A and TCCR1B set up are as follows.

The settings for PWM 1A and 1B outputs, Fast PWM, TOP=ICR1, and prescaler(PSR)=1 are as follows: setup(){ pinMode(9, OUTPUT); pinMode(10, OUTPUT); TCCR1A = 0; // Clear timer control register. TCCR1B = 0; // Clear timer control register. TCCR1A = 0b10100010; //1010: PWM 1A,1B output, 10: Fast PWM mode TCCR1B = 0b00011001; //11: TOP=ICR1, 001: PSR=1 ICR1 = 64999; // Period T=(1/16MHz)*(ICR1+1) = 4.0625(ms) Freq.=1/T=246(Hz) OCR1A = 32499; // PWM Duty = 50% OCR1B = 16249; // PWM Duty = 25% } The duty ratio can be changed freely using OCR1A and OCR1B.

inserted by FC2 system