//Program for controlling Duty Cycle and motor Voltage //By: Clayton Campagna, Kevin LaBeau, Sarah Rizzio //9/22/04 #include #include #include #include "sio.c" int AD_convert(int analog_value); void PWM_setup(void); void AD_setup(void); void IO_setup(void); void IO_update(void); void CLK_setup(); int count = 0; // a count value to be output on port D int analog_input=0; // -------------- PWM Waveform Stuff ---------- void PWM_setup(void){ //using OCR2 DDRD = 0xFF; //set as output TCCR2 = _BV(WGM21)|_BV(WGM20)|_BV(COM21)|_BV(CS20); //fast PWM OCR2 = 0xFF; //100% duty cycle } void AD_setup(void){ ADMUX = 0xC0; // set the input to channel 0 ADCSRA = 0xE0;// turn on ADC and set to free running } int AD_read(){ return (ADCW >> 2); } // -------------- IO Stuff -------------------- void IO_setup(void){ DDRD = 0xFF; // all of port D is outputs ***CHANGES*** AD_setup(); // enable the AD inputs PWM_setup(); // setup the PWM outputs DDRA = (unsigned char)0; // set port A to inputs } void IO_update(void){// This routine will run once per interrupt for updates OCR2 = count; // Controls PWM wave outint((analog_input / 100)); // Display the voltage value to the screen outln(" "); analog_input = 0; // put feedback control stuff here } // -------------- Clock/interrupt stuff --------------------- #define CLK_ms 10// set the updates for every 10ms unsigned int CNT_timer1; // the delay time volatile unsigned int CLK_ticks = 0; // the current number of ms volatile unsigned int CLK_seconds = 0; // the current number of seconds SIGNAL(SIG_OVERFLOW1){ // The interrupt calls this function CLK_ticks += CLK_ms; analog_input += AD_read(); if(CLK_ticks >= 1000){ // The number of interrupts between output changes CLK_ticks = CLK_ticks - 1000; CLK_seconds++; IO_update(); } TCNT1 = CNT_timer1; } void CLK_setup(){ // Start the interrupt service routine TCCR1A = (0< 255) count = 255; outln("counter incremented"); } else if(c == '-'){ if(--count < 0) count = 0; outln("counter decremented"); } else if(c == 'p'){ outint(count); } else if(c == 'h'){ outln("HELP: + -> increase, - ->decrease, p -> output count, q -> quit"); } else if(c == 'q'){ break; } } sio_cleanup(); return 1; }