1wire2wayz Bidirectional
1wire2wayz Bidirectional
1wire2wayz Bidirectional
//***********************************************************
// Program discription
// Sends Mode 1 command set to ECU
// Stores Mode 1 data
// Cauclates disired values from stored data
// Outputs calculated data to LCD
//***********************************************************
//***********************************************************
// For more information on project visit:
// http://www.lukeskaff.com
//***********************************************************
// Dependent librarys
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <util/delay.h>
#define DATA_RPM_4 3
#define DATA_RPM_3 4
#define DATA_RPM_2 5
#define DATA_RPM_1 6
#define DATA_MPH_3 7
#define DATA_MPH_2 8
#define DATA_MPH_1 9
#define DATA_BLM_3 10
#define DATA_BLM_2 20
#define DATA_BLM_1 21
#define DATA_BLM_CELL 11
#define DATA_TPS_3 12
#define DATA_TPS_2 13
#define DATA_TPS_1 14
#define DATA_MAP_3 15
#define DATA_MAP_2 16
#define DATA_MAP_1 17
#define DATA_BATT_2 18
#define DATA_BATT_1 19
// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// ------------------------ Program Code Starts Here ----------------------------
// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
//******************************************************************
//* Function: LCD_Initialize *
//* Initialize LCD *
//******************************************************************
void LCD_Initialize(void)
{
Bit_Clear(E_PORT,E); // Bring enable pin low
//******************************************************************
//* Function: LCD_Write *
//* Output data passed in, in data var. to LCD *
//* - Commented out, not used anymore - *
//******************************************************************
/*void LCD_Write(unsigned char data, unsigned char select)
{
Bit_Set(E_PORT,E);
if(select==0xFF)
Bit_Set(RS_PORT,RS); //LCD Data mode
else
Bit_Clear(RS_PORT,RS); //LCD Instruction mode
LCD_PORT=data;
_delay_ms(1);
Bit_Clear(E_PORT,E);
_delay_ms(1);
}
*/
//******************************************************************
//* Function: LCD_D_Write *
//* Output data passed in, in data var. to LCD *
//******************************************************************
void LCD_D_Write(unsigned char data)
{
Bit_Set(E_PORT,E);
Bit_Set(RS_PORT,RS); //LCD Data mode
LCD_PORT=data;
LCD_Delay();
Bit_Clear(E_PORT,E);
LCD_Delay();
//******************************************************************
//* Function: LCD_I_Write *
//* Output data passed in, in data var. to LCD *
//******************************************************************
void LCD_I_Write(unsigned char data)
{
Bit_Set(E_PORT,E);
Bit_Clear(RS_PORT,RS); //LCD Instruction mode
LCD_PORT=data;
LCD_Delay();
Bit_Clear(E_PORT,E);
LCD_Delay();
//******************************************************************
//* Function: LCD_Delay *
//* Relay function used for LCD communication *
//******************************************************************
void LCD_Delay(void)
{
_delay_ms(1);
}
//******************************************************************
//* Function: USART_init *
//* Enable and Initialize UART *
//******************************************************************
void USART_init(void)
{
// Set the USART baudrate registers for 8192, UBRR=149
UBRR0H = 0;
UBRR0L = 149; // Set for 4800 for PC testing
//255 = 4800 baud
//149 = 8192 buad
//127 = 9600 baud
//******************************************************************
//* Function: Binary_Out *
//* Outputs any passed in variable to LCD in binary *
//* - Used for debugging - *
//* - Commented out, not used in final working code - *
//******************************************************************
/*
void Out_Binary(unsigned char data)
{
for(unsigned char bitn = 8 ; bitn > 0 ; bitn--)
{
if( data & ( 1 << (bitn-1) ) )
LCD_Write(0x31,0xFF); // Output 1 to LCD
else
LCD_Write(0x30,0xFF); // Output 0 to LCD
}
}
*/
//*******************************************************************
//* Function: USART_Receive *
//* Waits for incoming data on USART then returns it to the calling *
//* function *
//* - Commented out, not used in final working code - *
//*******************************************************************
/*
unsigned char USART_Receive(void)
{
// Wait for data to be received
while ( !(UCSR0A & (1<<RXC0)) );
//******************************************************************
//* Function: USART_Transmit *
//* Transmit data *
//* datastream array until array is full *
//******************************************************************
void USART_Transmit(unsigned char data)
{
// Wait for empty transmit buffer
while ( !( UCSR0A & (1<<UDRE0)) );
//******************************************************************
//* Function: Get_Car_Data *
//* Waits for incoming data on USART then puts received char into *
//* datastream array until array is full *
//******************************************************************
void Get_Car_Data(void)
{
unsigned int timeout;
//******************************************************************
//* Function: Flash_LEDs *
//* - Used for debugging - *
//* - Commented out, not used in final working code - *
//******************************************************************
/*
void Flash_LEDs()
{
for(int i=0 ; i<20 ; i++ )
{
PORTA=0x00;
_delay_ms(1000);
PORTA=0xFF;
_delay_ms(1000);
}
}
*/
//******************************************************************
//* Function: HEX_to_ASCII *
//* Convert inputed 8-bit HEX data to 4 ANSII digits for LCD *
//******************************************************************
void HEXtoASCII(unsigned int Hex_Input,unsigned char *digit4, unsigned char
*digit3, unsigned char *digit2, unsigned char *digit1)
{
// Clear varaibles
*digit4 = 0x00;
*digit3 = 0x00;
*digit2 = 0x00;
*digit1 = 0x00;
//******************************************************************
//* Function: Calculate_CarData *
//* Convert raw data from data stream to real numbers and put in *
//* ASCII format *
//******************************************************************
void Calculate_CarData()
{
unsigned char ignore;
unsigned int temp;
// Calculate RPM
temp = DataStream[DEF_RPM]*25;
HEXtoASCII(temp,&CarData_ASCII[DATA_RPM_4], &CarData_ASCII[DATA_RPM_3],
&CarData_ASCII[DATA_RPM_2], &CarData_ASCII[DATA_RPM_1]);
// Calculate MPH
temp = DataStream[DEF_MPH] / 1;
HEXtoASCII(temp,&ignore, &CarData_ASCII[DATA_MPH_3], &CarData_ASCII[DATA_MPH_2],
&CarData_ASCII[DATA_MPH_1]);
//******************************************************************
//* Function: Output_CarData() *
//* Output calcualted car data and trouble codes to LCD *
//******************************************************************
void Output_CarData()
{
unsigned char ignore;
unsigned int ErrorCode;
unsigned char bitn=0;
unsigned char code_2, code_1;
unsigned char error_bit, error_byte;
LCD_D_Write('R');
LCD_D_Write('P');
LCD_D_Write('M');
LCD_D_Write(' ');
LCD_D_Write(CarData_ASCII[DATA_RPM_4]);
LCD_D_Write(CarData_ASCII[DATA_RPM_3]);
LCD_D_Write(CarData_ASCII[DATA_RPM_2]);
LCD_D_Write(CarData_ASCII[DATA_RPM_1]);
LCD_D_Write('M');
LCD_D_Write('P');
LCD_D_Write('H');
LCD_D_Write(' ');
LCD_D_Write(CarData_ASCII[DATA_MPH_3]);
LCD_D_Write(CarData_ASCII[DATA_MPH_2]);
LCD_D_Write(CarData_ASCII[DATA_MPH_1]);
LCD_D_Write('B');
LCD_D_Write('L');
LCD_D_Write('M');
LCD_D_Write(' ');
LCD_D_Write(CarData_ASCII[DATA_BLM_3]);
LCD_D_Write(CarData_ASCII[DATA_BLM_2]);
LCD_D_Write(CarData_ASCII[DATA_BLM_1]);
LCD_D_Write('C');
LCD_D_Write('T');
LCD_D_Write('S');
LCD_D_Write(' ');
LCD_D_Write(CarData_ASCII[DATA_CTS_2]);
LCD_D_Write(CarData_ASCII[DATA_CTS_1]);
LCD_D_Write('C');
LCD_D_Write(' ');
LCD_D_Write('M');
LCD_D_Write('A');
LCD_D_Write('P');
LCD_D_Write(' ');
LCD_D_Write(CarData_ASCII[DATA_MAP_3]);
LCD_D_Write(CarData_ASCII[DATA_MAP_2]);
LCD_D_Write(CarData_ASCII[DATA_MAP_1]);
LCD_D_Write(' '); //Space
LCD_D_Write(' '); //Space
LCD_D_Write(' '); //Space
LCD_D_Write(' '); //Space
LCD_D_Write('T');
LCD_D_Write('P');
LCD_D_Write('S');
LCD_D_Write(' ');
LCD_D_Write(CarData_ASCII[DATA_TPS_3]);
LCD_D_Write(CarData_ASCII[DATA_TPS_2]);
LCD_D_Write(CarData_ASCII[DATA_TPS_1]);
LCD_D_Write('%');
LCD_D_Write('B');
LCD_D_Write('A');
LCD_D_Write('T');
LCD_D_Write('T');
LCD_D_Write(' ');
LCD_D_Write(CarData_ASCII[DATA_BATT_2]);
LCD_D_Write(CarData_ASCII[DATA_BATT_1]);
LCD_D_Write('V');
LCD_D_Write(' ');
LCD_D_Write(' ');
LCD_D_Write(' ');
//*************************
// Check for Trouble Codes
//*************************
for(error_byte = DEF_ERROR1 ; error_byte < DEF_ERROR5 ; error_byte++)
{
for(bitn = 0 ; bitn < 7 ; bitn++)
{
//If error bit in datastream is 1 then ouput trouble code
if(DataStream[error_byte] & ( 1 << (bitn) ) )
{
ErrorCode = (unsigned char)pgm_read_byte(&ErrorCode_Table[error_bit]);
}
}
//******************************************************************
//* Function: main *
//* Main control function *
//******************************************************************
int main (void)
{
DDRA = 0xFF; // Configure PORTA as output
DDRC = 0xFF; // Configure PORTC as output
DDRD = (1<<PD1)|(1<<PD5)|(1<<PD6)|(1<<PD7); // Configure PORTD
// Loop forever
while(1)
{
Get_Car_Data();
Calculate_CarData();
Output_CarData();
}
while(1)
;