/* Accelerometer This file is a part of a project created on Lund Institute of Technology by Mattias Kindborg Rikard Ekström */ #include // To get access to the names of the PORTS, REGISTERS on the AVR #include // To be able to handle the Watchdog Timer #include // To get access to various commands, for example sei() #include // To get access to interrupt handling #include // Accessing mathematics #include // Delay function #include #include #include //----------------------------------------------------------------------------------------------------------- // DEFINITIONS /* GENERAL */ #define bit_write(register, pinNr, value) (value ? (register |= 1<= 201) { bit_write(TIMSK, 5, 0); // Disable ICP displayResults(); } break; case 2: // 0-402 m if (distance >= 402) { bit_write(TIMSK, 5, 0); // Disable ICP displayResults(); } break; case 3: // 0-100 km/h if (speed >= 27.77778) { // = 100 km/h bit_write(TIMSK, 5, 0); // Disable ICP displayResults(); } break; case 4: if (speed <= 0.2777778) { // 100-0 km/h bit_write(TIMSK, 5, 0); // Disable ICP displayResults(); } break; case 5: if ((fabs(acc) > maxG) && (fabs(acc) <= 2*g)) { // Save largest G maxG = fabs(acc); } if ((time/16000000) > 9) { bit_write(TIMSK, 5, 0); // Disable ICP displayResults(); } break; } } else { // Falling edge T1_current = ICR1; // Save T1 bit_write(TCCR1B, 6, 1); // Trig on rising edge next time if (T1_current < T2_current) { // Wrap T1 = (65535 - T2_current) + T1_current; } else { // No wrap T1 = T1_current - T2_current; } // Calculations double T2_double; T2_double = T2; if((fabs(acc) > triggValue || time != 0) && (startSampling == 2)) { speed += (acc * (T2_double/16000000)); // In m/s distance += (fabs(speed) + fabs(acc) * (T2_double/16000000) / 2) * (T2_double/16000000); time += T2; // Large counter value } } bit_write(PORTD, 4, 0); // ??? Test time in interrupt } //----------------------------------------------------------------------------------------------------------- // MENUE /* INITIATE THE MENUE */ void initMenue(void) { bit_write(DDRB, buttonPressed, input); // Define INT2 as input bit_write(DDRB, menueButton1, input); // Define menueButton1 as input bit_write(DDRB, menueButton2, input); // Define menueButton2 as input bit_write(DDRB, menueButton3, input); // Define menueButton3 as input bit_write(PORTB, buttonPressed, 1); // Activate pull up bit_write(PORTB, menueButton1, 1); // Activate pull up bit_write(PORTB, menueButton2, 1); // Activate pull up bit_write(PORTB, menueButton3, 1); // Activate pull up //bit_write(GICR, 6, 1); // Enable external interrupt 0 (on pin PD2) GICR |= 0x20; MCUCSR |= 0x40; // menueIndex = 1; char buf[20]; strcpy_P (buf, menue[0]); LCD_putString(0, buf); } // LCD /* MENUE BUTTON PRESSED */ INTERRUPT(SIG_INTERRUPT2) { ///bit_write(PORTD, 7, 1); char pressedButtonIndex; pressedButtonIndex = (PINB & 0x38)>>3; char buf[20]; if (pressedButtonIndex == 4) { // Backward in menue switch(menueIndex) { case 1: menueIndex = 6; strcpy_P (buf, menue[5]); break; case 2: menueIndex = 1; strcpy_P (buf, menue[0]); break; case 3: menueIndex = 2; strcpy_P (buf, menue[1]); break; case 4: menueIndex = 3; strcpy_P (buf, menue[2]); break; case 5: menueIndex = 4; strcpy_P (buf, menue[3]); break; case 6: menueIndex = 5; strcpy_P (buf, menue[4]); break; } LCD_putString(0, buf); } else if (pressedButtonIndex == 2) { // Forward in menue switch(menueIndex) { case 1: menueIndex = 2; strcpy_P (buf, menue[1]); break; case 2: menueIndex = 3; strcpy_P (buf, menue[2]); break; case 3: menueIndex = 4; strcpy_P (buf, menue[3]); break; case 4: menueIndex = 5; strcpy_P (buf, menue[4]); break; case 5: menueIndex = 6; strcpy_P (buf, menue[5]); break; case 6: menueIndex = 1; strcpy_P (buf, menue[0]); break; } LCD_putString(0, buf); } else if (pressedButtonIndex == 1) { // Select startSampling = 0; time = 0; speed = 0; distance = 0; maxG = 0; switch(menueIndex) { case 1: // 0-201 m strcpy_P (buf, menue[6]); LCD_putString(64, buf); TIMSK |= 0x20; // Enable ICP break; case 2: // 0-402 strcpy_P (buf, menue[6]); LCD_putString(64, buf); TIMSK |= 0x20; // Enable ICP break; break; case 3: // 0-100 km/h strcpy_P (buf, menue[6]); LCD_putString(64, buf); TIMSK |= 0x20; // Enable ICP break; case 4: // 100-0 km/h strcpy_P (buf, menue[6]); LCD_putString(64, buf); speed = 27.77778; // Startspeed = 100 km/h TIMSK |= 0x20; // Enable ICP break; case 5: // Max. G-force strcpy_P (buf, menue[6]); LCD_putString(64, buf); TIMSK |= 0x20; // Enable ICP break; case 6: // Set trig value break; } } //bit_write(PORTD, 7, 0); } //----------------------------------------------------------------------------------------------------------- // MAIN int main(void) { /* Initieringar */ initLCD(); initMenue(); /*bit_write(DDRC, 2, input); bit_write(DDRC, 3, input); bit_write(DDRC, 4, input); bit_write(DDRC, 5, input); bit_write(PORTC, 2, input); bit_write(PORTC, 3, input); bit_write(PORTC, 4, input); bit_write(PORTC, 5, input);*/ //welcome(); initICP(); //TIMSK = 0x20; //triggValue = 0.1; //acc = 0.0; //T1 = 18473; //T2 = 37700; /* Testutskrift */ //char buf[16]; //strcpy_P (buf, menue[0]); //LCD_putString(0, buf); sei(); while(1) { } }