J1850 Implement
http://avrobdii.googlecode.com/svn/trunk/code/J1850.c /*
Copyright (C) Trampas Stern name of author This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ /*******************************************************************
* File: J1850.c
*
* Copyright ©, Trampas Stern. All Rights Reserved.
* Date: 5/19/2006 9:34:55 PM
*******************************************************************/
#include "scanner.h"
#include "time.h" #define PWM_UNKNOWN 0
#define PWM_BUS_ERROR 1
#define PWM_SOF 2
#define PWM_DATA 3
#define PWM_EOD 4
#define PWM_EOF 5
#define PWM_IDLE 6 #define PWM_BIT_US 24 //us for bit time
#define PWM_EOD_US 51 //us for End of Data time
#define PWM_EOF_US 72 //us for End of Frame time
#define PWM_IDLE_US 96 //us till idle #define TP1 8
#define TP2 16
#define TP3 24
#define TP4 48
#define TP5 72
#define TP7 32 #define PWM_OUT(x) {if (x) BIT_SET(PORTB,5); else BIT_CLEAR(PORTB,5);}
#define PWM_IN BIT_TEST(ACSR,5) #define J1850_ARRAY_N 16
#define J1850_ARRAY_PKTS 4 //must be power of 2
UINT8 J1850Data[ J1850_ARRAY_PKTS ][ J1850_ARRAY_N ];
UINT8 J1850DataBytes[ J1850_ARRAY_PKTS ];
UINT8 J1850DataTime[ J1850_ARRAY_PKTS ]; volatile UINT8 J1850_dataRead;
volatile UINT8 J1850_dataWrite; volatile UINT8 PWMState; volatile UINT8 *ptrWrite; volatile UINT8 NumPktBytes; /****************
Basic Operation
The J1850 comes in two flavors a PWM (pluse width modulation) and VPW
(variable pluse width modulation). For our initial system what we
are going to do is have the analog comparitor trigger on rising and
falling edges. Then we will have this trigger the timer/caputre (TC1).
This will tell allow us to measure the pulse widths.
*****************/ /******************************************************************
** J1850Init
*
* DESCRIPTION:
*
* Create: 5/19/2006 9:35:25 PM - Trampas Stern
*******************************************************************/
INT J1850Init( void )
{
//Set up data direction registers
BIT_SET( DDRB, ); //set OUT_VPW as output
BIT_SET( DDRB, ); //set OUT_PWM as output //ENABLE PWM
//BIT_SET(DDRC,2); //set pin as ouput
//BIT_CLEAR(PORTC,2); //drive pin low BIT_CLEAR( PORTB, ); //Turn off OUT_VPW
BIT_CLEAR( PORTB, ); //Turn off OUT_PWM //setup the analog comparator
ADCSRB = 0x00; //use negative input on compartor
ACSR = 0x08; //Output capture mode all disconnected
TCCR1A = 0x00; //noise canceler on (bit 7)
// prescaller clk/8==tick every half microSecond
TCCR1B = 0x82; //ignore the force output compare
TCCR1C = 0x00; //set count to zero
TCNT1 = 0x00; //The timer is reset on the rising edge of bits
//the EOD,EOF,and Idle time are based on end of last bit
// Thus we have to add bit time to each
//double due to timer runing twice as fast
OCR1A = ( PWM_EOD_US + PWM_BIT_US ) * ; //EOD time
OCR1B = ( PWM_EOF_US + PWM_BIT_US ) * ; //EOF time
OCR1C = ( PWM_IDLE_US + PWM_BIT_US ) * ; //IDLE time TIMSK1 = 0x0E; J1850_dataWrite = ;
J1850_dataRead = ;
NumPktBytes = ;
ptrWrite = J1850Data[ J1850_dataWrite ];
PWMState = PWM_UNKNOWN;
return NO_ERROR;
} ISR( TIMER1_COMPA_vect )
{
if ( BIT_TEST( ACSR, ) == ) //passive bus state
{
if ( PWMState < PWM_EOD )
{
PWMState = PWM_EOD;
if ( NumPktBytes > )
{
J1850DataBytes[ J1850_dataWrite ] = NumPktBytes;
J1850_dataWrite++;
J1850_dataWrite = J1850_dataWrite & ( J1850_ARRAY_PKTS - );
ptrWrite = J1850Data[ J1850_dataWrite ];
}
NumPktBytes = ;
}
}
else
{
PWMState = PWM_BUS_ERROR;
}
} ISR( TIMER1_COMPB_vect )
{
if ( BIT_TEST( ACSR, ) == ) //passive bus state
{
PWMState = PWM_EOF;
}
else
{
PWMState = PWM_BUS_ERROR;
}
} ISR( TIMER1_COMPC_vect )
{
if ( BIT_TEST( ACSR, ) == ) //passive bus state
{
PWMState = PWM_IDLE;
}
else
{
PWMState = PWM_BUS_ERROR;
}
// Note we do not want the timer to overflow so
// we reset it to some nominal amount.
//TCNT1=48*2+1;
} /******************************************************************
** Analog Comparator ISR
*
* DESCRIPTION:
*
* Create: 6/4/2006 9:21:43 AM - Trampas Stern
*******************************************************************/
ISR( ANALOG_COMP_vect )
{
static UINT8 data = ;
static UINT8 bits = ; UINT8 t; //read timer1
t = TCNT1L;
if ( BIT_TEST( ACSR, ) != )
{
//rising edge
TCNT1L = 0x00;
if ( PWMState > PWM_DATA )
{
J1850DataTime[ J1850_dataWrite ] = t;
} }
else //we have falling edge
{
//Falling edge
if ( t <= && t >= )
{
PWMState = PWM_DATA;
data = data << ;
if ( t <= )
data = data | 0x01;
bits++;
if ( bits > )
{
*ptrWrite = data;
NumPktBytes = NumPktBytes + ;
if ( NumPktBytes < J1850_ARRAY_N )
{
ptrWrite++;
//if (ptrWrite>(J1850Data + (4*16)))
//{
// ptrWrite--;
//}
}
bits = ;
}
}
if ( t >= && t <= )
{
PWMState = PWM_SOF;
bits = ;
}
} } /******************************************************************
** J1850test
*
* DESCRIPTION:
*
* Create: 6/4/2006 9:33:15 AM - Trampas Stern
*******************************************************************/
INT J1850test( void )
{
UBYTE i; if ( J1850_dataWrite != J1850_dataRead )
{
printf_P( PSTR( "\nPWMSTATE=%d\n" ), PWMState );
printf_P( PSTR( "J1850_DATA %d(us) " ),
J1850DataTime[ J1850_dataRead ] / );
for ( i = ; i < J1850DataBytes[ J1850_dataRead ]; i++ )
{
UINT8 d;
d = J1850Data[ J1850_dataRead ][ i ];
printf_P( PSTR( "%X " ), d );
}
J1850_dataRead++;
J1850_dataRead = J1850_dataRead & ( J1850_ARRAY_PKTS - ); } return NO_ERROR;
} /******************************************************************
** PWM_get
*
* DESCRIPTION: Gets a PWM packet
*
* Create: 6/11/2006 7:05:01 PM - Trampas Stern
*******************************************************************/
INT pwm_get( UINT8 *ptr, UINT8 count, UINT16 time_out_ms )
{
TIME ptrTime;
UINT8 i = ;
//get current time
GetTime( &ptrTime ); while ( !done )
{
if ( J1850_dataWrite != J1850_dataRead )
{
//printf_P(PSTR("\nPWMSTATE=%d\n",PWMState);
//printf_P(PSTR("J1850_DATA %d(us) ",J1850DataTime[J1850_dataRead]/2);
for ( i = ; i < J1850DataBytes[ J1850_dataRead ] && i < count; i++ )
{
//UINT8 d;
*ptr++ = J1850Data[ J1850_dataRead ][ i ];
//printf_P(PSTR("%X ",d);
}
J1850_dataRead++;
J1850_dataRead = J1850_dataRead & ( J1850_ARRAY_PKTS - ); } //check timne
if ( GetElaspMs( ptrTime ) > time_out_ms )
{
done = ;
}
} //while return i;
} /******************************************************************
** pwm_put
*
* DESCRIPTION:
*
* Create: 6/11/2006 7:10:50 PM - Trampas Stern
*******************************************************************/
INT pwm_put( UINT8 *data, UINT8 count, UINT16 time_out_ms )
{ TIME ptrTime;
UINT8 done = ;
UINT8 i = ;
INT8 bit;
UINT8 time;
//get current time
GetTime( &ptrTime ); while ( !done )
{
//first check that the PWM bus is idle
if ( PWMState >= PWM_IDLE )
{
//disable the receive ISRs
TIMSK1 = 0x00;
ACSR = 0x00; //Transmit SOF
TCNT1L = 0x00;
PWM_OUT( );
while ( TCNT1L < TP7 )
;
if ( PWM_IN != )
{
//enable reciever
ACSR = 0x08;
TIMSK1 = 0x0E;
return ;
}
while ( TCNT1L < ( TP7 * ) )
;
//drive low
PWM_OUT( );
while ( TCNT1L < ( TP4 * ) )
;
TCNT1L = 0x00;
PWM_OUT( ); //now lets drive the bits
for ( i = ; i < count; i++ )
{
UINT8 t;
t = *data++;
for ( bit = ; bit >= ; bit-- )
{
UINT8 b;
b = ( t >> bit ) & 0x01;
time = TP1;
if ( b == )
{
time = TP2;
}
while ( TCNT1L < ( time ) )
;
if ( PWM_IN != )
{
//drive low
PWM_OUT( );
//enable reciever
ACSR = 0x08;
TIMSK1 = 0x0E;
return ;
}
while ( TCNT1L < ( time * ) )
;
PWM_OUT( );
while ( TCNT1L < ( TP3 * ) )
;
TCNT1L = 0x00;
PWM_OUT( );
}
}
} //if PWMstate
//check time
if ( GetElaspMs( &ptrTime ) > time_out_ms )
{
done = ;
}
} //while(!done)
//drive low
PWM_OUT( );
//enable reciever
ACSR = 0x08;
TIMSK1 = 0x0E;
return i;
} //does CRC calculations
UINT8 crc( UINT8 *data, UINT8 len )
{
UINT8 result;
UINT8 i;
UINT8 mask;
UINT8 j;
UINT8 temp;
UINT8 poly; result = 0xFF;
for ( i = ; i < len; i++ )
{
mask = 0x80;
temp = data[ i ];
for ( j = ; j < ; j++ )
{
if ( temp & mask ) //bit is 1
{
poly = 0x1c;
if ( result & 0x80 )
{
poly = ;
}
result = ( ( result << ) | 0x01 ) ^ poly; }
else
{
poly = ;
if ( result & 0x80 )
{
poly = 0x1D;
}
result = ( ( result << ) ) ^ poly; }
mask = mask >> ;
}
}
return ~result;
}
J1850 Implement的更多相关文章
- SAE J1850 VPW Implement
---恢复内容开始--- OBDII Interface Project When I can ever find enough time away from schoolwork, I try to ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- svn: E200007: Runner for 'org.tmatesoft.svn.core.wc2.SvnMerge' command have not been found; probably not yet implement in this API.
myeclipse分支合并主干(分支->team->合并->选择主干)的时候出现这个错误: svn: E200007: Runner for 'org.tmatesoft.svn.c ...
- Leetcode Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- The easy way to implement a Red-Black tree
Red-Black trees are notorious for being nightmares of pointer manipulation. Instructors will show th ...
- How to implement a neural network
神经网络的实践笔记 link: http://peterroelants.github.io/posts/neural_network_implementation_part01/ 1. 生成训练数据 ...
随机推荐
- 20155202 2016-2017-2 《Java程序设计》第5周学习总结
20155202 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 第八章:异常处理 java中所有错误会包装成对象,可以尝试(try)执行程序并捕捉(catc ...
- es6笔记(4) Set数据结构
概要 介绍: 集合是由一组无序且唯一的项组成的,这个数据结构使用了与有限集合相同的数学概念,应用在计算机的数据结构中. ES6提供了数据结构Set.它类似于数组,但是没有重复的值. 特点: key与v ...
- Loadrunner如何进行有效的IP欺骗
柠檬班的清风同学某天紧急求助如何搞IP欺骗,端午节后,抽时间把这个事情搞定啦!跟大家详细的讲讲IP欺骗的运用和理解. 一.什么是IP欺骗 给你客户端的IP地址加个马甲,让服务器端识别不到是同一个IP地 ...
- 以python代码解释fork系统调用
import os print('Process (%s) start...' % os.getpid()) # Only works on Unix/Linux/Mac: pid = os.fork ...
- 【ARTS】01_06_左耳听风-20181217~1223
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- 修改history记录数门限
你的 Bash 命令历史保存的历史命令的数量可以在 ~/.bashrc 文件里设置.在这个文件里,你可以找到下面两行: HISTSIZE=1000 HISTFILESIZE=2000 HISTSIZE ...
- 善用backtrace解决大问题【转】
转自:https://www.2cto.com/kf/201107/97270.html 一.用途: 主要用于程序异常退出时寻找错误原因 二.功能: 回溯堆栈,简单的说就是可以列出当前函数调用关系 三 ...
- tensorflow中的kernel/Adam 变量的来源
原因是使用Adam优化函数时,Adam函数会创建一个Adam变量,目的是保存你使用tensorflow创建的graph中的每个可训练参数的动量, words/_word_embeddings:0 bi ...
- Python 爬虫入门之爬取妹子图
Python 爬虫入门之爬取妹子图 来源:李英杰 链接: https://segmentfault.com/a/1190000015798452 听说你写代码没动力?本文就给你动力,爬取妹子图.如果 ...
- LOJ 10138 -「一本通 4.5 例 1」树的统计
树链剖分模板题,详见这篇博客.