#include #include #include #define ADC_VREF_TYPE 0x40 char sensor_val[3]={0,0,0}; //float sensor; //char pwm_val =0; signed char jerk; /* char simulation_mode=1; // External Interrupt 0 service routine // NEED TO CHANGE THE INTERUPT REGISTER TO "ANY EDGE" WHEN I GO TO THE PROTO BOARD interrupt [EXT_INT0] void ext_int0_isr(void) { (simulation_mode==0)?(simulation_mode=1):(simulation_mode=0); delay_ms(100); } void accelerometer_value(){ //sensor_val[2]=sensor_val[1]; sensor_val[1]=sensor_val[0]; sensor_val[0]=read_adc(0)>>2; //sensor[1]=sensor[0]; //sensor[0]=sensor_val[0]; sensor = 1*sensor_val[0]; //+ .15*sensor_val[1] + .05*sensor_val[2];// //exp smoothing commented out... adjust 1 to .8 in front of sensor_val[0] jerk = sensor_val[0]-sensor_val[1]; //printf("\n\rSensor Value:\t%d\tInverse\t%d",sensor,~sensor); //delay_ms(100); printf("[%d;%d]\r",jerk,sensor_val[0]); } void potentiometer_value(){ //sensor_val[2]=sensor_val[1]; sensor_val[1]=sensor_val[0]; sensor_val[0]=read_adc(7)>>2; //sensor[1]=sensor[0]; //sensor[0]=sensor_val[0]; sensor = 1*sensor_val[0]; //+ .15*sensor_val[1] + .05*sensor_val[2];// //exp smoothing commented out... adjust 1 to .8 in front of sensor_val[0] jerk = sensor_val[0]-sensor_val[1]; //printf("\n\rSensor Value:\t%d\tInverse\t%d",sensor,~sensor); //delay_ms(100); printf("[%d;%d]\r",jerk,sensor_val[0]); } */ // Read the AD conversion result unsigned int read_adc(unsigned char adc_input) { ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); // Delay needed for the stabilization of the ADC input voltage delay_us(10); // Start the AD conversion ADCSRA|=0x40; // Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCW; } void main(void) { /////////////////////////////////////////////////////////////////// // USART initialization // Communication Parameters: 8 Data, 1 Stop, No Parity // USART Receiver: Off // USART Transmitter: On // USART Mode: Asynchronous // USART Baud Rate: 9600 /////////////////////////////////////////////////////////////////// UCSRA=0x00; UCSRB=0x08; UCSRC=0x86; UBRRH=0x00; UBRRL=0x33; //////////////////////////////////////////////////////////////////// // External Interrupt(s) initialization // INT0: On // INT0 Mode: Rising Edge // INT1: Off // INT2: Off //////////////////////////////////////////////////////////////////// GICR|=0x40; MCUCR=0x03; MCUCSR=0x00; GIFR=0x40; //////////////////////////////////////////////////////////////////// // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off //////////////////////////////////////////////////////////////////// ACSR=0x80; SFIOR=0x00; // Asembly syntax to enable global interupts #asm("sei") //////////////////////////////////////////////////////////////////// // ADC initialization // ADC Clock frequency: 62.500 kHz // ADC Voltage Reference: AVCC pin // ADC Auto Trigger Source: None //////////////////////////////////////////////////////////////////// ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x87; while (1) { /* Running the same program in a funtion that is determined by an interupt which tell what pin to ADC from //sensor_val[2]=sensor_val[1]; sensor_val[1]=sensor_val[0]; sensor_val[0]=read_adc(3)>>2; //sensor[1]=sensor[0]; //sensor[0]=sensor_val[0]; sensor = 1*sensor_val[0] + .15*sensor_val[1] + .05*sensor_val[2];// //exp smoothing commented out... adjust 1 to .8 in front of sensor_val[0] jerk = sensor_val[0]-sensor_val[1]; //printf("\n\rSensor Value:\t%d\tInverse\t%d",sensor,~sensor); //delay_ms(100); printf("[%d;%d]\r",jerk,sensor_val[0]); switch(simulation_mode) { case 0x00: potentiometer_value(); case 0x01: accelerometer_value(); } */ sensor_val[2]=sensor_val[1]; sensor_val[1]=sensor_val[0]; sensor_val[0]=read_adc(0)>>2; //sensor = 1*sensor_val[0] + .15*sensor_val[1] + .05*sensor_val[2]; //exp smoothing commented out... adjust 1 to .8 in front of sensor_val[0] jerk = sensor_val[0]-sensor_val[1]; printf("[%d;%d]\r",sensor_val[0],jerk); }; }