⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iso.h

📁 AVR ISO9141 OBDII Souce code
💻 H
字号:
/*************************************************************************
**  AVR ISO 9141/14230-2 Interface
**  by Michael Wolf
**
**  Released under GNU GENERAL PUBLIC LICENSE
**
**  contact: webmaster@mictronics.de
**  homepage: www.mictronics.de
**
**  Revision History
**
**  when         what  who			why
**
**************************************************************************/
#ifndef __ISO_H__
#define __ISO_H__

/*** CONFIG START ***/

#define ISO_K_LINE_PORT	PORTD	// K line output port
#define ISO_K_LINE_DIR 	DDRD	// K line direction register
#define ISO_K_LINE_PIN	PIND	// K line input port
#define ISO_K_LINE_OUT	7			// K line output pin
#define ISO_K_LINE_IN		2			// K line input pin

#define ISO_L_LINE_PORT	PORTB	// L line output port
#define ISO_L_LINE_DIR 	DDRB	// L line direction register
#define ISO_L_LINE_OUT	0			// L line output pin

#define ISO_INIT_LED_PORT	PORTD	// bus init LED output port
#define ISO_INIT_LED_DIR 	DDRD	// bus init LED direction register
#define ISO_INIT_LED_OUT	4			// bus init LED output pin

#define	ISO_K_LINE_OUT_NEG	// define K line output level inverted by hardware
#define	ISO_L_LINE_OUT_NEG	// define L line output level inverted by hardware
#define	ISO_K_LINE_IN_NEG		// define K line input level inverted by hardware

/*** CONFIG END ***/

#ifdef ISO_K_LINE_OUT_NEG
	#define iso_k_high() ISO_K_LINE_PORT &=~ _BV(ISO_K_LINE_OUT)
	#define iso_k_low() ISO_K_LINE_PORT |= _BV(ISO_K_LINE_OUT)
	#define is_k_out_high() bit_is_set(ISO_K_LINE_PORT, ISO_K_LINE_OUT)
#else
	#define iso_k_high() ISO_K_LINE_PORT |= _BV(ISO_K_LINE_OUT)
	#define iso_k_low() ISO_K_LINE_PORT &=~ _BV(ISO_K_LINE_OUT)
	#define is_k_out_high() bit_is_clear(ISO_K_LINE_PORT, ISO_K_LINE_OUT)
#endif


#ifdef ISO_K_LINE_IN_NEG
#define is_k_in_high() bit_is_clear(ISO_K_LINE_PIN, ISO_K_LINE_IN)
#else
#define is_k_in_high() bit_is_set(ISO_K_LINE_PIN, ISO_K_LINE_IN)
#endif


#ifdef ISO_L_LINE_OUT_NEG
	#define iso_l_high() ISO_L_LINE_PORT &=~ _BV(ISO_L_LINE_OUT)
	#define iso_l_low() ISO_L_LINE_PORT |= _BV(ISO_L_LINE_OUT)
#else
	#define iso_l_high() ISO_L_LINE_PORT |= _BV(ISO_L_LINE_OUT)
	#define iso_l_low() ISO_L_LINE_PORT &=~ _BV(ISO_L_LINE_OUT)
#endif


#define bus_init_led_on() ISO_INIT_LED_PORT |= _BV(ISO_INIT_LED_OUT)
#define bus_init_led_off() ISO_INIT_LED_PORT &=~ _BV(ISO_INIT_LED_OUT)

// define error return codes
#define ISO_RETURN_CODE_UNKNOWN    0
#define ISO_RETURN_CODE_OK         1
#define ISO_RETURN_CODE_BUS_BUSY   2
#define ISO_RETURN_CODE_BUS_ERROR  3
#define ISO_RETURN_CODE_DATA_ERROR 4
#define ISO_RETURN_CODE_NO_DATA    5
#define ISO_RETURN_CODE_DATA       6
#define ISO_RETURN_CODE_INIT_ERROR 7

// convert microseconds to counter values
#define us2cnt(us) ((unsigned int)((unsigned long)(us) / (1000000L / (float)((unsigned long)MCU_XTAL / 64L))))

// convert milliseconds to counter values
#define ms2cnt(ms)   ((unsigned int) us2cnt((unsigned long)(ms) * 1000))

/* Timing parameters

	Names are taken from ISO9141 and ISO14230-2

*/
// 5 Baud Init
#define TIME_5_BAUD_BIT ms2cnt(200)	// ms; bit hold time
#define TIME_W1_MIN ms2cnt(60)	// ms; min time end of address to start of sync pattern
#define TIME_W1_MAX ms2cnt(300)	// ms; max time end of address to start of sync pattern
#define TIME_W2_MIN ms2cnt(5)		// ms; min time end of sync to start key byte 1
#define TIME_W2_MAX ms2cnt(20)	// ms; max time end of sync to start key byte 1
#define TIME_W3_MIN ms2cnt(0)		// ms; min time key byte 1 to key byte 2
#define TIME_W3_MAX ms2cnt(20)	// ms; max time key byte 1 to key byte 2
#define TIME_W4_MIN ms2cnt(25)	// ms; min time key byte 2 to inverse addr
#define TIME_W4_MAX ms2cnt(50)	// ms; max time key byte 2 to inverse addr
#define TIME_W5_MIN ms2cnt(300)	// ms; min bus idle time before 5 Baud init addr byte

// Fast Init
#define TIME_TiniL	ms2cnt(25)	// ms; wake up pattern low time
#define TIME_TiniH	ms2cnt(25)	// ms; wake up pattern high time
#define TIME_FASTINIT_P2_MAX 13 // ms; P2_MAX_DEF / 4ms receive routine timeout

// Message timing
#define TIME_P1_MAX			ms2cnt(20)	// ms; max inter byte time response
#define TIME_P2_MAX_DEF	ms2cnt(50)	// ms; default max time request to response
#define TIME_P3_DEF			ms2cnt(55)	// ms; default time response to new request
#define TIME_P4_DEF			ms2cnt(8)		// ms; default inter byte time tester

#define PRESCALER_1    1
#define PRESCALER_8    2
#define PRESCALER_64   3
#define PRESCALER_256  4
#define PRESCALER_1024 5

#define T0_PRESCALER		PRESCALER_8  // Timer0 resolution = 1us @ 7.3728MHz
#define T1_PRESCALER		PRESCALER_64  // Timer1 resolution = 8us @ 7.3728MHz

// 4ms no data timeout
#define TIME_4ms ms2cnt(4)

// baud rate value for 10,4kBaud
#define N_10400  (unsigned char)((unsigned long)MCU_XTAL / ((unsigned long)10400 * (unsigned long)8))

volatile struct{
  unsigned char uart_buffer;                                                      
  unsigned char uart_status;
  unsigned char uart_reload;
  signed char uart_bit_cnt;
	unsigned char uart_n_baud;
	unsigned char keybyte1;
	unsigned char keybyte2;
  }iso_glob;

/* ISO UART status flags in iso_global */
#define   RDY   0x01
#define   TD    0x02                             
#define   BUSY  0x04
#define   ERROR 0x08
#define   AUTOBAUD 0x10
#define		BAUDMEAS 0x20

/* define bit macros */
#define SETBIT(x,y) (x |= (y)) 		// Set bit y in byte x
#define CLEARBIT(x,y) (x &= (~y)) // Clear bit y in byte x
#define CHECKBIT(x,y) (x & (y)) 	// Check bit y in byte x

/* Header message format - see ISO14230-2 */
#define FMT_NO_ADDR			0x00
#define FMT_CARB				0x40
#define FMT_ADDR      	0x80
#define FMT_MSG_LENGTH  0x3F


/* Functions */
extern void iso_hardware_init(void);
extern char iso_5_baud_init(unsigned char addr);
extern char iso_fast_init(void);

extern void iso_uart_init(void);
extern void iso_uart_putc(char data);
extern unsigned char iso_checksum(unsigned char *msg_buf, unsigned int nbytes);
extern char iso_send_msg(unsigned char *msg_buf, unsigned int nbytes);
extern unsigned int iso_recv_msg(unsigned char *msg_buf);

static inline void timer1_start(unsigned char val)
{
    TCCR1B = val;
    TCNT1 = 0;
}

static inline void timer1_stop(void)
{
    TCCR1B = 0x00;
}

static inline void timer1_set(unsigned int val)
{
    TCNT1 = val;
}


static inline void timer0_start(unsigned char val)
{
    TCCR0 = val;
    TCNT0 = 0;
}

static inline void timer0_stop(void)
{
    TCCR0 = 0x00;
}

static inline void timer0_set(unsigned char val)
{
    TCNT0 = val;
}
#endif // __ISO_H__

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -