用了2个节点,1个节点发送0xaa给节点2,2节点收到发送0x55给一节点,过程中用串口跟踪个寄存器状态!
节点1上传给串口的数据:00 01 02 01 01 aa 03 01 a6 节点1程序: //====================================================== unsigned char xdata *SJA_BasicAddr;
unsigned char PC_BUF;
sbit LED = P1^6; sbit BEEP = P1^7;
#define OK 0 #define ERROR 1
//======================================================
extern bit SJA_TestCommunication(void); extern bit SJA_EnterRestModel(void); extern bit SJA_QuitResetModel(void); extern void SJA_SetInterrupt(unsigned char interruptMode); extern void SJA_SetBaudRate(unsigned char baudRateNum); extern void SJA_SetObject(unsigned char acr, unsigned char amr); extern void SJA_SetOutandClock(unsigned char outControl, unsigned char clockOut); void MCU_Init(void); void Delay(unsigned int Delay_time); bit SJA_Init(void); void send(unsigned char S_Data);
//------------------------------------------------------------------- bit SJA_TestCommunication(void) { SJA_BasicAddr = REG_TEST; *SJA_BasicAddr = 0xAA; if (*SJA_BasicAddr == 0xAA) { return OK; } else { return ERROR; }
}
//-------------------------------------------------------------------
bit SJA_EnterRestModel(void) { unsigned char temp; SJA_BasicAddr = REG_CONTROL; temp = *SJA_BasicAddr; *SJA_BasicAddr = temp |RR_CTR; // 置位复位请求位 if ((*SJA_BasicAddr & RR_CTR) == 1) { return OK; } else { return ERROR; } }
//------------------------------------------------------------------- bit SJA_QuitResetModel(void) { unsigned char temp;
SJA_BasicAddr = REG_CONTROL; temp = *SJA_BasicAddr; *SJA_BasicAddr = temp & (~RR_CTR);
if ((*SJA_BasicAddr & RR_CTR) == 1) { return ERROR; } else { return OK; }
}
//------------------------------------------------------------------- //参数说明: //返回值: 无 //说明: 该函数用于设定再系统晶体为16MHz时常用的 // 标准波特率的值。 //------------------------------------------------------------------- unsigned char code SJA_BTR_TABLE[] = { 0xbf, 0xff, // 5Kbps时的波特率 0x67, 0x2f, // 10Kbps时的波特率 0x53, 0x2f, // 20Kbps时的波特率 0x87, 0xff, // 40Kbps时的波特率 0x47, 0x2f, // 50Kbps时的波特率 0x83, 0xff, // 80Kbps时的波特率 0x43, 0x2f, // 100Kbps时的波特率 0x03, 0x1c, // 125Kbps时的波特率 0x81, 0xfa, // 200Kbps时的波特率 0x01, 0x1c, // 250Kbps时的波特率 0x80, 0xfa, // 400Kbps时的波特率 0x00, 0x1c, // 500Kbps时的波特率 0x80, 0xb6, // 666Kbps时的波特率 0x00, 0x16, // 800Kbps时的波特率 0x00, 0x14 // 1000Kbps时的波特率 };
void SJA_SetBaudRate(unsigned char baudRateNum) { unsigned char btr0, btr1; btr0 = SJA_BTR_TABLE[2*baudRateNum]; btr1 = SJA_BTR_TABLE[2*baudRateNum+1];
SJA_BasicAddr = REG_BTR0; *SJA_BasicAddr = btr0; SJA_BasicAddr = REG_BTR1; *SJA_BasicAddr = btr1; }
//------------------------------------------------------------------- void SJA_SetObject(unsigned char acr, unsigned char amr) { SJA_BasicAddr = REG_ACR; *SJA_BasicAddr = acr; SJA_BasicAddr = REG_AMR; *SJA_BasicAddr = amr; }
//------------------------------------------------------------------- void SJA_SetOutandClock(unsigned char outControl, unsigned char clockOut) { SJA_BasicAddr = REG_OUTCTR; *SJA_BasicAddr = outControl;
SJA_BasicAddr = REG_CDR; *SJA_BasicAddr = clockOut; }
//------------------------------------------------------------------- // //------------------------------------------------------------------- bit SJA_Init(void) { while (SJA_TestCommunication() == ERROR); // 测试连接 while (SJA_EnterRestModel() == ERROR); SJA_SetBaudRate(6); SJA_SetObject(0x01, 0xfc); SJA_SetOutandClock(0xaa, 0x48); while (SJA_QuitResetModel() == ERROR);
return 0; }
//------------------------------------------------------------------------- void MCU_Init(void) { PC_BUF = 0x77; TMOD = 0x20; TH1 = 0xfd; TL1 = 0xfd; TR1 = 1; SCON = 0xd8; PCON = 0x00; EA = 1; ES = 1; }
void Send(unsigned char S_Data) { SBUF = S_Data; while(TI == 0); TI =0; }
//-------------------------------------------------------------------------- void Delay(unsigned int Delay_time) { unsigned int i; unsigned char t;
for (i=Delay_time; i>0; i--) for (t=100; t>0; t--) { ; } }
//---------------------------------------------------------------------------------------- void main(void) { unsigned char temp; Delay(1000); MCU_Init(); if (SJA_Init() == 0) { send(0x00); }
LED =0; BEEP = 0; Delay(1000); LED = 1; BEEP = 1; send(0x01);
while (1) { SJA_BasicAddr = REG_STATUS; //检查发送缓冲区状态 while ((*SJA_BasicAddr & TBS_STA) != TBS_STA); send(0x02);
SJA_BasicAddr = REG_TxBuffer1; *SJA_BasicAddr = 0x01; temp = *SJA_BasicAddr; send(temp); SJA_BasicAddr = REG_TxBuffer2; *SJA_BasicAddr= 0x01; temp = *SJA_BasicAddr; send(temp); SJA_BasicAddr = REG_TxBuffer3; *SJA_BasicAddr = 0xaa; temp = *SJA_BasicAddr; send(temp);
SJA_BasicAddr = REG_COMMAND; *SJA_BasicAddr = TR_CMD; temp = *SJA_BasicAddr; send(0x03); send(temp);
SJA_BasicAddr = REG_STATUS; temp = *SJA_BasicAddr; send(temp); while ((*SJA_BasicAddr & TCS_STA) != TCS_STA); temp = *SJA_BasicAddr; send(0x05); send (0x04);
SJA_BasicAddr = REG_STATUS; while ((*SJA_BasicAddr & RBS_STA) != RBS_STA); send(0x04); SJA_BasicAddr = REG_RxBuffer1; temp = *SJA_BasicAddr; send(temp); SJA_BasicAddr = REG_RxBuffer2; temp = *SJA_BasicAddr; send(temp); SJA_BasicAddr = REG_RxBuffer3; temp = *SJA_BasicAddr; send(temp);
LED =0; BEEP = 0; Delay(1000); LED = 1; BEEP = 1;
SJA_BasicAddr = REG_COMMAND; *SJA_BasicAddr = RRB_CMD;
SJA_BasicAddr = REG_STATUS; while ((*SJA_BasicAddr & RBS_STA) != RBS_STA); send(0x05); }
节点2上传给串口数据:00 01 d1 03 cf 50 B8 04 05 节点2程序: //====================================================== unsigned char xdata *SJA_BasicAddr;
unsigned char PC_BUF;
sbit LED = P1^6; sbit BEEP = P1^7;
#define OK 0 #define ERROR 1
//======================================================
extern bit SJA_TestCommunication(void); extern bit SJA_EnterRestModel(void); extern bit SJA_QuitResetModel(void); extern void SJA_SetInterrupt(unsigned char interruptMode); extern void SJA_SetBaudRate(unsigned char baudRateNum); extern void SJA_SetObject(unsigned char acr, unsigned char amr); extern void SJA_SetOutandClock(unsigned char outControl, unsigned char clockOut); void MCU_Init(void); void Delay(unsigned int Delay_time); bit SJA_Init(void); void send(unsigned char S_Data);
//------------------------------------------------------------------- bit SJA_TestCommunication(void) { SJA_BasicAddr = REG_TEST; *SJA_BasicAddr = 0xAA; if (*SJA_BasicAddr == 0xAA) { return OK; } else { return ERROR; }
}
//-------------------------------------------------------------------
bit SJA_EnterRestModel(void) { unsigned char temp; SJA_BasicAddr = REG_CONTROL; temp = *SJA_BasicAddr; *SJA_BasicAddr = temp |RR_CTR; // 置位复位请求位 if ((*SJA_BasicAddr & RR_CTR) == 1) { return OK; } else { return ERROR; } }
//------------------------------------------------------------------- bit SJA_QuitResetModel(void) { unsigned char temp;
SJA_BasicAddr = REG_CONTROL; temp = *SJA_BasicAddr; *SJA_BasicAddr = temp & (~RR_CTR);
if ((*SJA_BasicAddr & RR_CTR) == 1) { return ERROR; } else { return OK; }
}
//------------------------------------------------------------------- //参数说明: //返回值: 无 //说明: 该函数用于设定再系统晶体为16MHz时常用的 // 标准波特率的值。 //------------------------------------------------------------------- unsigned char code SJA_BTR_TABLE[] = { 0xbf, 0xff, // 5Kbps时的波特率 0x67, 0x2f, // 10Kbps时的波特率 0x53, 0x2f, // 20Kbps时的波特率 0x87, 0xff, // 40Kbps时的波特率 0x47, 0x2f, // 50Kbps时的波特率 0x83, 0xff, // 80Kbps时的波特率 0x43, 0x2f, // 100Kbps时的波特率 0x03, 0x1c, // 125Kbps时的波特率 0x81, 0xfa, // 200Kbps时的波特率 0x01, 0x1c, // 250Kbps时的波特率 0x80, 0xfa, // 400Kbps时的波特率 0x00, 0x1c, // 500Kbps时的波特率 0x80, 0xb6, // 666Kbps时的波特率 0x00, 0x16, // 800Kbps时的波特率 0x00, 0x14 // 1000Kbps时的波特率 };
void SJA_SetBaudRate(unsigned char baudRateNum) { unsigned char btr0, btr1; btr0 = SJA_BTR_TABLE[2*baudRateNum]; btr1 = SJA_BTR_TABLE[2*baudRateNum+1];
SJA_BasicAddr = REG_BTR0; *SJA_BasicAddr = btr0; SJA_BasicAddr = REG_BTR1; *SJA_BasicAddr = btr1; }
//------------------------------------------------------------------- void SJA_SetObject(unsigned char acr, unsigned char amr) { SJA_BasicAddr = REG_ACR; *SJA_BasicAddr = acr; SJA_BasicAddr = REG_AMR; *SJA_BasicAddr = amr; }
//------------------------------------------------------------------- void SJA_SetOutandClock(unsigned char outControl, unsigned char clockOut) { SJA_BasicAddr = REG_OUTCTR; *SJA_BasicAddr = outControl;
SJA_BasicAddr = REG_CDR; *SJA_BasicAddr = clockOut; }
//------------------------------------------------------------------- // //------------------------------------------------------------------- bit SJA_Init(void) { while (SJA_TestCommunication() == ERROR); // 测试连接 while (SJA_EnterRestModel() == ERROR); SJA_SetBaudRate(6); SJA_SetObject(0x02, 0xfc); SJA_SetOutandClock(0xaa, 0x48); while (SJA_QuitResetModel() == ERROR);
return 0; }
//------------------------------------------------------------------------- void MCU_Init(void) { PC_BUF = 0x77; TMOD = 0x20; TH1 = 0xfd; TL1 = 0xfd; TR1 = 1; SCON = 0xd8; PCON = 0x00; EA = 1; ES = 1; }
void Send(unsigned char S_Data) { SBUF = S_Data; while(TI == 0); TI =0; }
//-------------------------------------------------------------------------- void Delay(unsigned int Delay_time) { unsigned int i; unsigned char t;
for (i=Delay_time; i>0; i--) for (t=100; t>0; t--) { ; } }
//---------------------------------------------------------------------------------------- void main(void) { unsigned char temp; Delay(1000); MCU_Init(); if (SJA_Init() == 0) { send(0x00); }
LED =0; BEEP = 0; Delay(1000); LED = 1; BEEP = 1; send(0x01);
while (1) { SJA_BasicAddr = REG_STATUS; temp = *SJA_BasicAddr; send(temp); while ((*SJA_BasicAddr & RBS_STA) != RBS_STA); send(0x03);
SJA_BasicAddr = REG_RxBuffer1; temp = *SJA_BasicAddr; send(temp); SJA_BasicAddr = REG_RxBuffer2; temp = *SJA_BasicAddr; send(temp); SJA_BasicAddr = REG_RxBuffer3; temp = *SJA_BasicAddr; send(temp);
LED =0; BEEP = 0; Delay(1000); LED = 1; BEEP = 1;
SJA_BasicAddr = REG_COMMAND; *SJA_BasicAddr = RRB_CMD; send(0x04);
SJA_BasicAddr = REG_STATUS; while ((*SJA_BasicAddr & RBS_STA) != RBS_STA); send(0x05);
SJA_BasicAddr = REG_STATUS; //检查发送缓冲区状态 while ((*SJA_BasicAddr & TBS_STA) != TBS_STA); send(0x02);
SJA_BasicAddr = REG_TxBuffer1; *SJA_BasicAddr = 0x02; SJA_BasicAddr = REG_TxBuffer2; *SJA_BasicAddr= 0x01; SJA_BasicAddr = REG_TxBuffer3; *SJA_BasicAddr = 0x55;
SJA_BasicAddr = REG_COMMAND; *SJA_BasicAddr = TR_CMD;
SJA_BasicAddr = REG_STATUS; //等待发送完毕 while ((*SJA_BasicAddr & TCS_STA) != TCS_STA); send(0x03);
}
}
问题出在那里呢?
|
|