/*=========================================================================
文件使用说明
作 者: 林圣欢
时间:2017-11-30
本文件适合ht-idaq18 rt-thread系统
温度can 扩展灯地址 0xeefff8 温度内容存放在temp_idaq[3]数值中 在用6个字节
压力can 扩展灯地址 0xeefff9 压力内容存放在press_idaq[3]数值中 在用6个字节
导航(经纬度)can 扩展灯地址 未定义 可以扩展( 0xeefffa )
采集 数据存放在
u16 VehSpeed; //车速
u16 EngSpeed; //转速
u8 ColdTemp; //发动机冷却剂温
u16 OilTemp; //发动机油温
u8 OilPres; //油压
u16 OilValve_Percent; //燃油使用率
u16 Battery_voltage; //电池电压
u8 Speed_Percent; //当前速度的百分比负载
需要在rt_application_init()函数中开启obd_caj1939_thread_entry线程
extern void obd_caj1939_thread_entry(void *parameter);
tid_caj1939_test = rt_thread_create("caj1939_test",
obd_caj1939_thread_entry, RT_NULL,
256, 9, 20);
if (tid_caj1939_test != RT_NULL)
rt_thread_startup(tid_caj1939_test);
=========================================================================*/
#include "stm32f10x.h"
//#include "stm32l1xx.h"
#include "board.h"
#include "stm32f10x.h"
#include "stm32f10x_fsmc.h"
#include "stm32f10x_adc.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_iwdg.h"
//#include <rtdevice.h>
#include <rtthread.h>
#include "sdcard.h"
#include <stdlib.h>
#include <string.h>
#include "time.h"
#include "CAJ1939.h"
//#include "integer.h"
//#include "ff.h"
//#include "diskio.h"
#if CAN_EN
//==========================================================================
u8 Source_Address = 0XF9; //存储原地址
u8 Target_Address = 0x00; //发送请求的目标地址
u8 PDU_PF = 0x00; //存储PDU_PF域变量
u8 PDU_PS = 0x00; //存储PDU_PS域变量
u8 PDU_SA = 0x00; //存储PDU_PA域变量
u8 PDU_TA = 0X00; //存储上请求发送的目的地址
u8 PGN_Low = 0x00; //存储参数组pdu_pf
u8 PGN_Mid = 0x00; //存储参数组的PDU_PS
u8 PGN_High = 0x00; //存储目前正在进行数据交换的PGN
u8 CA_Name[8] = {0x00}; //存储CA名字
u8 Can_Receive_Data[50][8]; //存储接收到的数据
u16 Number_Of_Byte = 0x00; //接收的数据的字节数目
u8 Sent_To_PCData[256]; //定义一个发到PC的参数数组
//u16 m_PGN;
CanTxMsg TxMessage; //定义一个发送结构体
CanRxMsg *RxMessage; //定义一个接收结构体
//==========================================================================
static J1939_PDU_T J1939PDU; //接收PDU报文
/*=========================================================================
函数名称: CAN_SetBps
功能描述: 设置波特率
输入参数: 波特率
输出参数: 无
作 者: 林圣欢
=========================================================================*/
void CAN_SetBps(CAN_BPS_TYPE BPS)
{
CAN_InitTypeDef CAN_InitStructure;
switch(BPS)
{
case BPS_500K:
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //1-4
CAN_InitStructure.CAN_BS1=CAN_BS1_6tq; //1-16
CAN_InitStructure.CAN_BS2=CAN_BS2_5tq; //1-8
CAN_InitStructure.CAN_Prescaler=6; //波特率36/(6*(1+9+2)=500k
break;
case BPS_250K:
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //1-4
CAN_InitStructure.CAN_BS1=CAN_BS1_6tq; //1-16
CAN_InitStructure.CAN_BS2=CAN_BS2_5tq; //1-8
CAN_InitStructure.CAN_Prescaler=12; //波特率为36/(12*(1+9+2)=250k
break;
case BPS_125K:
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //1-4
CAN_InitStructure.CAN_BS1=CAN_BS1_9tq; //1-16
CAN_InitStructure.CAN_BS2=CAN_BS2_2tq; //1-8
CAN_InitStructure.CAN_Prescaler=24; //波特率为36/(24*(1+9+2)=125k
break;
case BPS_100K:
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //1-4
CAN_InitStructure.CAN_BS1=CAN_BS1_14tq; //1-16
CAN_InitStructure.CAN_BS2=CAN_BS2_3tq; //1-8
CAN_InitStructure.CAN_Prescaler=20; //波特率为 36/(20*(1+14+3))=100k
break;
case BPS_33K:
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //1-4
CAN_InitStructure.CAN_BS1=CAN_BS1_11tq; //1-16
CAN_InitStructure.CAN_BS2=CAN_BS2_3tq; //1-8
CAN_InitStructure.CAN_Prescaler=72; //波特率为 36/(72*(1+11+3))=33.3k
break;
default:
return;
}
CAN_InitStructure.CAN_TTCM=DISABLE; //时间触发(启用时间触发模式时会替换最后两个字节)
CAN_InitStructure.CAN_ABOM=ENABLE; //自动离线管理
CAN_InitStructure.CAN_AWUM=ENABLE; //自动唤醒
CAN_InitStructure.CAN_NART=ENABLE; //ENABLE:错误不自动重传 DISABLE:重传
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal; //正常传输模式 CAN_Mode_Silent_LoopBack;//CAN_Mode_LoopBack;//
CAN_Init(CAN1,&CAN_InitStructure);
}
/*=========================================================================
函数名称: CAN_SetFilterStd
功能描述: 设置标准帧过滤器
输入参数: 过滤器号码,过滤的ID和屏蔽器
输出参数: 无
作 者:林圣欢
=========================================================================*/
void CAN_SetFilterStd(u8 number,u16 ID,u16 Mask)
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN 过滤器设置 */
CAN_FilterInitStructure.CAN_FilterNumber=number;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_16bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow=ID;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=Mask;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}
/*=========================================================================
函数名称: CAN_SetFilterExt
功能描述: 设置扩展帧过滤器
输入参数: 过滤器号码,过滤的ID和屏蔽器
输出参数: 无
作 者: 林圣欢
=========================================================================*/
void CAN_SetFilterExt(u8 number,u16 ID,u16 Mask)
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN 过滤器设置 */
CAN_FilterInitStructure.CAN_FilterNumber=number;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=ID;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=Mask;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}
/*=========================================================================
函数名称: CAN_SetFilterExt_Full(u8 number,u32 ID)
功能描述: 设置扩展帧过滤器全滤波29位ID
输入参数: 过滤器号码,过滤的
输出参数: 无
作 者: 林圣欢
=========================================================================*/
void CAN_SetFilterExt_Full(u8 number,u32 ID)
{
CAN_FilterInitTypeDef CAN_F
评论2