From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Master/Projekt Systementwicklung/OBD2_uart.c | 1486 ++++++++++++++++++++++++++ 1 file changed, 1486 insertions(+) create mode 100644 Master/Projekt Systementwicklung/OBD2_uart.c (limited to 'Master/Projekt Systementwicklung/OBD2_uart.c') diff --git a/Master/Projekt Systementwicklung/OBD2_uart.c b/Master/Projekt Systementwicklung/OBD2_uart.c new file mode 100644 index 0000000..455748b --- /dev/null +++ b/Master/Projekt Systementwicklung/OBD2_uart.c @@ -0,0 +1,1486 @@ +/** + * OBD2 Reader + * Avraham Aisenberg, Mike Andromalos + * Spring 2009- ECE 4760 + * Assistance from Bruce Land and Ruibing Wang + */ +/******************************************************************************************/ +/* Includes */ +#include +#include +#include +#include +#include +#include "lcd_lib.h" +#include + +/* Global/System Constants */ +#define TRUE 1 +#define FALSE 0 + +#define sbi(data,bit) (data |= (1<= 0x20 && rx_data <= 0x5F) { + rx_buffer[rx_buffer_index++] = rx_data; + } + } + else if (state == St_Rec_Speed){ + if(button_pressed == Left){ + state = St_Print_Menu; + LCDclr(); + menu = 1; + menu_cursor = 1; + trial_flag = 0; + timeOn = 0; + milliseconds = 0; + seconds = 0; + + LCDGotoXY(0,0); + LCDsendChar(7); + + kph = 0; + mph = 0; + rpm = 0; + mpg = 0; + } + if (rx_data == 0x3E) { + sscanf(rx_buffer,"%*s %*s %X",&kph); + LCDGotoXY(6,0); + if (menu == 8) { + mph = speed_convert(kph); + if ((mph > 0) && (trial_flag == 0)){ + LCDGotoXY(0,2); + sprintf(lcd_buffer, "TO BEGIN: SLOW TO 0"); + LCDstring(lcd_buffer, strlen(lcd_buffer)); + state = St_Trans_Speed; + } + else if ((mph == 0) && (trial_flag == 0)){ + LCDGotoXY(0,1); + if(mph <10) + sprintf(lcd_buffer, "MPH: %d", mph); + else if ((mph >= 10) && (mph < 100)) + sprintf(lcd_buffer, "MPH: %d", mph); + else + sprintf(lcd_buffer, "MPH: %d", mph); + LCDstring(lcd_buffer, strlen(lcd_buffer)); + + LCDGotoXY(0,2); + sprintf(lcd_buffer,"PROCEED WHEN READY"); + LCDstring(lcd_buffer, strlen(lcd_buffer)); + trial_flag = 1; + } + else if ((mph > 0) && (trial_flag == 1) && (timeOn == 0)) { + timeOn = 1; + } + else if ((mph >= 0) && (timeOn == 1)){ + if(mph < 60 && seconds <60){ + LCDGotoXY(0,1); + if(mph <10) + sprintf(lcd_buffer, "MPH: %d", mph); + else if ((mph >= 10) && (mph < 100)) + sprintf(lcd_buffer, "MPH: %d", mph); + else + sprintf(lcd_buffer, "MPH: %d", mph); + LCDstring(lcd_buffer, strlen(lcd_buffer)); + LCDGotoXY(0,2); + if(seconds < 10){ + if(milliseconds<10){ + sprintf(lcd_buffer, "Time: %u.00%u seconds",seconds,milliseconds);} + else if(milliseconds<100){ + sprintf(lcd_buffer, "Time: %u.0%u seconds",seconds,milliseconds);} + else{ + sprintf(lcd_buffer, "Time: %u.%u seconds",seconds,milliseconds);} + } + else { + if(milliseconds<10){ + sprintf(lcd_buffer, "Time: %u.00%u seconds",seconds,milliseconds);} + else if(milliseconds<100){ + sprintf(lcd_buffer, "Time: %u.0%u seconds",seconds,milliseconds);} + else{ + sprintf(lcd_buffer, "Time: %u.%u seconds",seconds,milliseconds);} + } + LCDstring(lcd_buffer, strlen(lcd_buffer)); + } + else if ((mph >= 60) || (seconds >= 60)){ + timeOn = 0; + LCDGotoXY(0,2); + if(seconds < 10){ + if(milliseconds<10){ + sprintf(lcd_buffer, "Time: %u.00%u seconds",seconds,milliseconds);} + else if(milliseconds<100){ + sprintf(lcd_buffer, "Time: %u.0%u seconds",seconds,milliseconds);} + else{ + sprintf(lcd_buffer, "Time: %u.%u seconds",seconds,milliseconds);} + } + else { + if(milliseconds<10){ + sprintf(lcd_buffer, "Time: %u.00%u seconds",seconds,milliseconds);} + else if(milliseconds<100){ + sprintf(lcd_buffer, "Time: %u.0%u seconds",seconds,milliseconds);} + else{ + sprintf(lcd_buffer, "Time: %u.%u seconds",seconds,milliseconds);} + } + LCDstring(lcd_buffer, strlen(lcd_buffer)); + LCDGotoXY(0,1); + if(mph <10) + sprintf(lcd_buffer, "MPH: %d", mph); + else if ((mph >= 10) && (mph < 100)) + sprintf(lcd_buffer, "MPH: %d", mph); + else + sprintf(lcd_buffer, "MPH: %d", mph); + LCDstring(lcd_buffer, strlen(lcd_buffer)); + trial_flag = 3; + } + } + rx_buffer_index = 0; + state = St_Print_Timer60; + } + else if(metric_flag == 0){ + mph = speed_convert(kph); + if(mph <10) + sprintf(lcd_buffer, " %d", mph); + else if ((mph >= 10) && (mph < 100)) + sprintf(lcd_buffer, " %d", mph); + else + sprintf(lcd_buffer, " %d", mph); + LCDstring(lcd_buffer, strlen(lcd_buffer)); + state = St_Trans_Rpm; + if(button_pressed == Left){ + state = St_Print_Menu; + LCDclr(); + menu = 1; + menu_cursor = 1; + LCDGotoXY(0,0); + LCDsendChar(7); + mph = 0; + rpm = 0; + mpg = 0; + } + } + else if(metric_flag == 1){ + if(kph <10) + sprintf(lcd_buffer, " %d", kph); + else if ((kph >= 10) && (kph < 100)) + sprintf(lcd_buffer, " %d", kph); + else + sprintf(lcd_buffer, " %d", kph); + LCDstring(lcd_buffer, strlen(lcd_buffer)); + state = St_Trans_Rpm; + if(button_pressed == Left){ + state = St_Print_Menu; + LCDclr(); + menu = 1; + menu_cursor = 1; + + LCDGotoXY(0,0); + LCDsendChar(7); + + mph = 0; + rpm = 0; + mpg = 0; + } + } + rx_buffer_index = 0; + } + else if (rx_data >= 0x20 && rx_data <= 0x5F) { + rx_buffer[rx_buffer_index++] = rx_data; + } + } + else if (state == St_Rec_Rpm){ + if (rx_data == 0x3E) { + sscanf(rx_buffer,"%*s %*s %X %X",&temp_rpm1,&temp_rpm2); + rpm = rpm_convert(temp_rpm1,temp_rpm2); + LCDGotoXY(16,0); + if(rpm<1000) + sprintf(lcd_buffer, " %d", rpm); + else + sprintf(lcd_buffer, "%d", rpm); + + LCDstring(lcd_buffer, strlen(lcd_buffer)); + rx_buffer_index = 0; + state = St_Trans_Volt; + if(button_pressed == Left){ + state = St_Print_Menu; + LCDclr(); + menu = 1; + menu_cursor = 1; + LCDGotoXY(0,0); + LCDsendChar(7); + mph = 0; + rpm = 0; + mpg = 0; + } + } + else if (rx_data >= 0x20 && rx_data <= 0x5F) { + rx_buffer[rx_buffer_index++] = rx_data; + } + } + else if (state == St_Rec_Mpg){ + if (rx_data == 0x3E) { + sscanf(rx_buffer,"%*s %*s %X %X",&temp_maf1,&temp_maf2); + maf = airflowrate_convert(temp_maf1,temp_maf2); + mpg = mpg_convert(mph,maf); + LCDGotoXY(16,1); + sprintf(lcd_buffer, "%2.1f", mpg); + LCDstring(lcd_buffer, strlen(lcd_buffer)); + rx_buffer_index = 0; + state = St_Trans_Speed; + } + else if (rx_data >= 0x20 && rx_data <= 0x5F) { + rx_buffer[rx_buffer_index++] = rx_data; + } + } + else if (state == St_Rec_Volt){ + if (rx_data == 0x3E){ + rx_buffer_index = 0; + state = St_Trans_Engine_Temp; + if(button_pressed == Left){ + state = St_Print_Menu; + LCDclr(); + menu = 1; + menu_cursor = 1; + LCDGotoXY(0,0); + LCDsendChar(7); + mph = 0; + rpm = 0; + mpg = 0; + } + } + if (rx_data >= 0x20 && rx_data <= 0x5F) { + rx_buffer[rx_buffer_index++] = rx_data; + } + if (rx_data == 0x56) { + LCDGotoXY(5,1); + sprintf(lcd_buffer, "%c%c%c%c", rx_buffer[0],rx_buffer[1],rx_buffer[2],rx_buffer[3]); + LCDstring(lcd_buffer, strlen(lcd_buffer)); + } + } + else if (state == St_Rec_Reset){ + if (rx_data == 0x3E) { + LCDGotoXY(5,3); + sprintf(lcd_buffer, "%s", rx_buffer); + LCDstring(lcd_buffer, strlen(lcd_buffer)); + rx_buffer_index = 0; + PORTB = 0xFF; + state = St_Print_Menu; + _delay_ms(1000); + LCDclr(); + LCDGotoXY(0,0); + LCDsendChar(7); + } + else if (rx_data >= 0x20 && rx_data <= 0x5F) { + rx_buffer[rx_buffer_index++] = rx_data; + } + } + else if (state == St_Rec_SN){ + if (about_flag == 1){ + LCDGotoXY(5,2); + LCDstring(serial_buffer, strlen(serial_buffer)); + state = St_Print_About; + } + else if(about_flag == 0){ + if (rx_data == 0x3E) { + menu_cursor = 2; + LCDGotoXY(5,2); + sprintf(serial_buffer, "%c%c%c%c%c%c%c%c%c%c%c%c", rx_buffer[0],rx_buffer[1],rx_buffer[2],rx_buffer[3],rx_buffer[4],rx_buffer[5],rx_buffer[6],rx_buffer[7],rx_buffer[8],rx_buffer[9],rx_buffer[10],rx_buffer[11]); + LCDstring(serial_buffer, strlen(serial_buffer)); + rx_buffer_index = 0; + about_flag = 1; + state = St_Print_About; + } + else if (rx_data >= 0x20 && rx_data <= 0x5F) { + rx_buffer[rx_buffer_index++] = rx_data; + } + } + } + else if (state == St_Rec_Clear){ + if (rx_data == 0x3E) { + if (rx_buffer[0] == 0x34) + state = St_Codes_Cleared; + else + state = St_Codes_Not_Cleared; + LCDclr(); + rx_buffer_index = 0; + } + else if (rx_data >= 0x20 && rx_data <= 0x5F) { + rx_buffer[rx_buffer_index++] = rx_data; + } + } + else if(state == St_Rec_Engine_Temp){ + if (rx_data == 0x3E) { + sscanf(rx_buffer,"%*s %*s %X",&temperature); + rx_buffer_index = 0; + if(metric_flag == 0){ + temperature = temp_convert(temperature - 40); + } + else + temperature = temperature - 40; + LCDGotoXY(15 ,1); + if (temperature <= 10) + sprintf(lcd_buffer,"%d",temperature); + else if (temperature<0 && temperature>10) + sprintf(lcd_buffer," %d", temperature); + else if((temperature >= 0) && (temperature <10)) + sprintf(lcd_buffer, " %d", (temperature)); + else if ((temperature >= 10) && (temperature < 100)) + sprintf(lcd_buffer, " %d", temperature); + else + sprintf(lcd_buffer, "%d", temperature); + + LCDstring(lcd_buffer, strlen(lcd_buffer)); + state = St_Trans_Speed; + } + else if (rx_data >= 0x20 && rx_data <= 0x5F) { + rx_buffer[rx_buffer_index++] = rx_data; + } + } + else if(state == St_Rec_Check_Engine){ + if (rx_data == 0x3E){ + sscanf(rx_buffer,"%X %X %X %X %X %X %X",&error1, &error2, &error3, &error4, &error5, &error6, &error7); + if (error1 = 0x43){ + state = St_Print_Check_Engine; + } + else { + LCDGotoXY(0,0); + sprintf(lcd_buffer,"********************"); + LCDstring(lcd_buffer, strlen(lcd_buffer)); + + LCDGotoXY(0,0); + sprintf(lcd_buffer,"* NO CODES *"); + LCDstring(lcd_buffer, strlen(lcd_buffer)); + + LCDGotoXY(0,0); + sprintf(lcd_buffer,"* DETECTED *"); + LCDstring(lcd_buffer, strlen(lcd_buffer)); + + LCDGotoXY(0,3); + sprintf(lcd_buffer,"********************"); + LCDstring(lcd_buffer, strlen(lcd_buffer)); + + _delay_ms(1000); + state = St_Print_Menu; + LCDclr(); + menu = 1; + menu_cursor = 1; + LCDGotoXY(0,0); + LCDsendChar(7); + } + rx_buffer_index = 0; + } + else if (rx_data >= 0x20 && rx_data <= 0x5F) { + rx_buffer[rx_buffer_index++] = rx_data; + } + } +} + +/******************************************************************************************/ +/** + * Main program + */ +int main(void) { + initialize(); + // Main program loop + while(1) { + if ((button_timer == 0) && (PINA != 15)){ + button_pressed = PINA; + button_timer = 0; + } + else + button_pressed = 15; + state_machine(); + } +} +/******************************************************************************************/ +void initialize(void){ + rx_buffer_index = 0; + DDRD = 0xFF; + PORTD = 0xFF; + + //init pushbuttonsf + DDRA = 0xF0; + PushState = Release; + + init_uart0_rxtx(); // Initialize UART TX + init_lcd(); + + state = St_Initial; + mph = 0; + rpm = 0; + mpg = 0; + kph = 0; + kpl = 0; + temp_rpm1 = 0; + temp_rpm2 = 0; + maf = 0; + temperature = 0; + milli_temp = 0; + seconds = 0; + about_flag = 0; + + menu = 1; + menu_cursor = 1; + button_pressed = 0; + metric_flag = 0; + trial_flag = 0; + timeOn = 0; + button_timer = 0; + + // set up timer 0 + OCR0A = 249; // clear after 250 counts + TIMSK0 = (1<