关于GZ16的mscan08的标准can的配置?[原创]
- UID
- 131735
- 性别
- 男
|
kuimulang1019@sina.com
我的油箱,关于CAN2.0的GZ16我正在开发,初始化始终不行,请哥哥姐姐们给予支持!!
QQ104793361 |
|
|
|
|
|
- UID
- 131735
- 性别
- 男
|
#include
#include "main.h"
#define BAUDRATE_125k 0x0000
#define BAUDRATE_250k 0x0001
#define BAUDRATE_500k 0x0002
#define COMMSTATUS_INIT 0x0000
#define COMMSTSTUS_RCVOPEN 0x1111
#define COMMSTATUS_RCVEND 0x2222
#define COMMSTSTUS_SNDOPEN 0x3333
#define COMMSTSTUS_SNDEND 0x4444
static unsigned short s_wCANCommBuffer[100];
static unsigned short s_wCANCountSend;
static unsigned short s_wCANCommStatus;
static unsigned short s_wCANCountTimeout;
static unsigned short* s_pCANRcvPointer;
static unsigned short* s_pCANRcvEndPointer;
static unsigned short* s_pCANSndPointer;
static unsigned short* s_pCANSndEndPointer;
static void InitCAN(void)
{
CMCR0 = 0x01;
CMCR1 = 0x00;
CIDAR0 = 0x00;
CIDAR1 = 0x00;
CIDAR2 = 0x00;
CIDAR3 = 0x00;
CIDMR0 = 0xff;
CIDMR1 = 0xff;
CIDMR2 = 0xff;
CIDMR3 = 0xff;
CIDAC = 0x00;
switch(GetBautRate())
{
case BAUDRATE_125k: CBTR0 = 0x49; CBTR1 = 0x00; break;
case BAUDRATE_250k: CBTR0 = 0x49; CBTR1 = 0x00; break;
case BAUDRATE_500k: CBTR0 = 0x49; CBTR1 = 0x00; break;
default:break;
}
CRIER = 0x00;
CTCR = 0x00;
CMCR0 = 0x00;
}
static void InitCommStatus(void)
{
s_wCANCountTimeout = 50000;
s_wCANCountSend = 14;
s_wCANCommStatus = COMMSTATUS_INIT;
}
static void CANInitReceive(void) //接受初始化
{
CIDAR0 = 0xff;
CIDAR1 = 0xe0;
CIDAR2 = 0x00;
CIDAR3 = 0x00;
CIDMR0 = 0xff;
CIDMR1 = 0xff;
CIDMR2 = 0xff;
CIDMR3 = 0xff;
CRFLG = 0xff;
}
void OnInitCAN(void)
{
InitCAN();
InitCommStatus();
}
static void CANSendPackage(void)
{
unsigned short wCountSend;
// if(s_pCANSndPointer>=s_pCANSndEndPointer) return;
// wCountSend = (unsigned short)(s_pCANSndEndPointer-s_pCANSndPointer);
// if(wCountSend>8) wCountSend = 8;
wCountSend = 0x08;
if(CTFLG_TXE0) //判断是否为空
{
IDR00 = 0xff; //标准数据桢
IDR10 = 0xe0;
IDR20 = 0x00;
IDR30 = 0x00;
DLR0 = wCountSend&0x0f; //长度
DSR00 = 0xff; //要发送的内容
DSR10 = 0x00;
DSR20 = 0xff;
DSR30 = 0x00;
DSR40 = 0xff;
DSR50 = 0x00;
DSR60 = 0xff;
DSR70 = 0x00;
// DSR00 = s_pCANSndPointer[0]&0xff;
// DSR10 = s_pCANSndPointer[1]&0xff;
// DSR20 = s_pCANSndPointer[2]&0xff;
// DSR30 = s_pCANSndPointer[3]&0xff;
// DSR40 = s_pCANSndPointer[4]&0xff;
// DSR50 = s_pCANSndPointer[5]&0xff;
// DSR60 = s_pCANSndPointer[6]&0xff;
// DSR70 = s_pCANSndPointer[7]&0xff;
TBPR0 = 0x01; //优先级
CTFLG_TXE0 = 1; //启动发送
s_pCANSndPointer += wCountSend; //指针后移
}
}
void OnTaskCAN(void) //每10ms调用一次
{
// unsigned short wCRC;
// unsigned short wSndCount;
// if(s_wCANCommStatus != COMMSTSTUS_SNDEND) return;
// s_pCANSndPointer = s_wCANCommBuffer;
// s_pCANSndEndPointer = s_wCANCommBuffer+8;
// s_wCANCountTimeout = 5000;
// s_wCANCommStatus = COMMSTSTUS_SNDOPEN;
// if(s_wCANCommStatus != COMMSTSTUS_SNDOPEN) return;
// wCRC = CalculateCRC(s_wCANCommBuffer,s_pCANRcvEndPointer-s_wCANCommBuffer);
// if(wCRC==0)
{
// wSndCount = OnTaskTxData(s_wCANCommBuffer);
// s_pCANSndPointer = s_wCANCommBuffer;
// s_pCANSndEndPointer = s_wCANCommBuffer+wSndCount;
// wCRC = CalculateCRC(s_wCANCommBuffer,wSndCount);
// *s_pCANSndEndPointer++ = wCRC%256;
// *s_pCANSndEndPointer++ = wCRC/256;
// s_wCANCountTimeout = 5000;
// s_wCANCommStatus = COMMSTSTUS_SNDOPEN;
}
}
void OnPollingCAN(void) //此函数每1ms调用一下
{
unsigned short wSndCount;
unsigned short wAddress;
if(s_wCANCountTimeout>0)
{
s_wCANCountTimeout--;
if(s_wCANCountTimeout==0)
{
InitCAN();
InitCommStatus();
}
}
switch(s_wCANCommStatus)
{
case COMMSTATUS_INIT:
// CANInitReceive();
s_wCANCountTimeout = 2000;
s_wCANCommStatus = COMMSTSTUS_SNDOPEN;
s_pCANSndPointer = s_wCANCommBuffer; //发送指针初始地址
s_pCANSndEndPointer = s_wCANCommBuffer+8; //发送指针结束地址
break;
case COMMSTSTUS_RCVOPEN:
if(CRFLG_RXF)
{
s_pCANRcvPointer[0] = REC_DSR0;
s_pCANRcvPointer[1] = REC_DSR1;
s_pCANRcvPointer[2] = REC_DSR2;
s_pCANRcvPointer[3] = REC_DSR3;
s_pCANRcvPointer[4] = REC_DSR4;
s_pCANRcvPointer[5] = REC_DSR5;
s_pCANRcvPointer[6] = REC_DSR6;
s_pCANRcvPointer[7] = REC_DSR7;
s_pCANRcvPointer += REC_DLR;
}
if(s_wCANCommBuffer[1]==0x03)
{
s_pCANRcvEndPointer = s_wCANCommBuffer+8;
PTE_PTE0 = 1;
if(s_pCANRcvPointer>=s_pCANRcvEndPointer)
{
s_wCANCountTimeout = 5000;
s_wCANCommStatus = COMMSTATUS_RCVEND;
// wAddress = GetAddress();
// if(s_wCANCommBuffer[3] != wAddress)
// {
// InitCAN();
// InitCommStatus();
// }
}
}
if(s_wCANCommBuffer[1]==0x10)
{
wSndCount = (s_pCANRcvPointer[4]<<8) + s_pCANRcvPointer[5];
if(wSndCount*2!=s_pCANRcvPointer[6]) break;
s_pCANRcvEndPointer = s_wCANCommBuffer+wSndCount*2+9;
}
break;
case COMMSTATUS_RCVEND:
s_wCANCountTimeout = 5000;
s_wCANCommStatus = COMMSTSTUS_SNDOPEN;
break;
case COMMSTSTUS_SNDOPEN:
if(s_pCANSndPointer!=s_wCANCommBuffer) //等待发送结束
{
if(s_wCANCountSend)
{
s_wCANCountSend--;
break;
}
}
s_wCANCountSend = 14;
s_wCANCountTimeout = 5000;
CANSendPackage(); //发送
if(s_pCANSndPointer>=s_pCANSndEndPointer) //重新发送
{
PTE_PTE0 = 1;
s_wCANCommStatus = COMMSTATUS_INIT;
}
break;
case COMMSTSTUS_SNDEND:
break;
default:
OnInitCAN();
break;
}
}
发送不行,不知道问题到底出在哪里,请斑竹和大虾门给予支持,多谢!!!
|
|
|
|
|
|
- UID
- 145738
- 性别
- 男
|
大家有人试成功过MC33889的STOP 模式吗!试了N 天了, 就是进不去!哪位朋友可以帮忙提供一下设置, 不胜感激! |
|
|
|
|
|
- UID
- 145738
- 性别
- 男
|
|
|
|
|
|