/* * CV * (or * knob) * to * MIDI * converter * * The controls go to the AVR chip's ADC pins, first control to AD1, second control to AD2, third to AD3 pitch wheel to AD0 * A switch goes to PIND2 which selects hi=38400 baud (serial port) lo=31500 baud (standard MIDI) * * Use with a 3.6864MHz crystal */ #include #include #include #define xADCSR (*(volatile unsigned char*)0x26) #define xUCSRA (*(volatile unsigned char*)0x2B) //#define xUCSRB (*(volatile unsigned char*)0x2A) #define debug 0 #define NUM_KNOBS 3 //number of controls/pots #define NUM_BUTTONS 5 //number of controls/pots #define POT_CHANNEL 0x00 //midi channel to use for controllers (pots) #define BUT_CHANNEL 0x01 //midi channel to use for effect selection (buttons) #define IDLE_LOOPS 400 //number of loops with no midi signal before we send status of all buttons/knobs void adc_init(void); void adc_init(void) { outp(0x00, DDRC); outp(0x00, PORTC); outp(0x02 | (1 << ADEN) | (1 << ADSC) | (0 << ADFR) , ADCSR); //set up ADC Prescaler Divide 4 mHz by 128 ADC enable //ADC Free Run SB 20 samples per hz } int main(void) { short ain_lo, ain_hi; //analog input high and low byte short ain; //analog in (full word) short val[NUM_KNOBS]; //array of current controller values short fx_on[NUM_BUTTONS]; //stores which fx are on/off short cur_pot; //current control number (0..NUM_KNOBS-1) that we're reading in the ADC short cur_but; //current button (0..NUM_BUTTONS-1) that we're reading from PINB short count,count2,idle_count; short last_baud; if (debug){ }else{ outp(0x00, DDRB); //portb as inputs for effect selection outp(0xFF, PORTB); //pull portB high outp(0<=NUM_BUTTONS) cur_but=0; /********************* ** READ CTRL VALUE ** **********************/ xADCSR |= 1<>3); //scale the 10 bit value to a 7 bit value (0..127) if (((val[cur_pot]-ain)>1) || ((val[cur_pot]-ain)<-1)){ //if this control's value has changed... val[cur_pot]=ain; //...update the array of control values idle_count=0; while ( !(xUCSRA & (1<=IDLE_LOOPS){ for (count=0;count=NUM_KNOBS) cur_pot=0; outp(cur_pot ,ADMUX); //set up mux to read next control count = 66; do{ } while(--count); // wait some cycles } //endless loop } //if debug } //main()