วันจันทร์ที่ 9 ตุลาคม พ.ศ. 2560

CLASS D AMPLIFIER ARDUINO





/* Arduino Uno Audio PWM Modulator
* by Matt "Sigurthr" Giordano. 9 Jan 2014.
* https://www.youtube.com/watch?v=KNjaSJa6mf4
* Modified by Andriy Makukha. 30 Mar 2016.
* https://www.youtube.com/watch?v=KNSZfAbhE0A
* Audio input on Analog 0, PWM output on Digital 6.
*/

int duty;
int raw;
void setup(){
TCCR0B &= B11111000;
TCCR0B |= B00000001;
// PWM Boost pins 5 & 6 to 64KHz

// ADC Boost Start - sets ADC clock to 1MHz
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

// set ADC prescaler to 16
sbi(ADCSRA,ADPS2) ;
cbi(ADCSRA,ADPS1) ;
cbi(ADCSRA,ADPS0) ;
// ADC Boost End
pinMode(7, OUTPUT);
}

void loop(){
raw = analogRead (0);

// Normal code
//duty = raw>>2;
//analogWrite(6, duty);

// Amplification code (NOTE: this wil reduce the sound quality)
duty = (raw>>1)-127;
analogWrite(6, max(0,min(duty, 255)));

// Crazy code
//duty = raw/3-40;
//analogWrite(6, (duty > 127) ? 255: 0);
}
แสดงน้อยลง
Sompong Tungmepol
/* Arduino Uno Audio PWM Modulator
by Matt "Sigurthr" Giordano. 1/9/2014.
Audio Input on Analog 0, PWM output on Digital 6.
I/O circuitry as follows:
Input: capacitor couple iPod audio through 4.7uF.
Bias Input pin to 2.5V with one 10k resistor to 5v,
and another 10k resistor to ground to form a voltage divider.
Output goes to a RC filter made of a series 560 ohm resistor
and a parallel 0.1uF capacitor.
Capacitor couple this node to amplifier of choice.
*/
int duty;
int raw;
int bias = 192;
void setup(){
TCCR0B &= B11111000;
TCCR0B |= B00000001;
// PWM Boost pins 5 & 6 to 64KHz

// ADC Boost Start - sets ADC clock to 1MHz
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

// set ADC prescaler to 16
sbi(ADCSRA,ADPS2) ;
cbi(ADCSRA,ADPS1) ;
cbi(ADCSRA,ADPS0) ;
// ADC Boost End
}
void loop(){
raw = analogRead (0);
duty = bias + ((raw - 512)/4);
analogWrite(6, duty);
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น