無線傳感組網技術
【主要程序文件】:
//******************************SJA1000初始化***********************************
void SJA1000_Init(void)
{
uchar i,a,b,i_temp=0,TempData,j_temp=0;
CLI();
SJA100HardWareRest(); //硬件復位
//Read_SJA1000(CAN_IR); //讀中斷寄存器,清除中斷位
SJAEntryResetMode(); //進入復位模式
DelayUs(5);
SR_status = Read_SJA1000(CAN_SR); //讀狀態(tài)
//進入Pelican模式
Write_SJA1000(CAN_CDR,0xc0); //選擇Pelican模式,CBP=1,激活RX0; 關閉TX1中斷與時鐘輸出。
SR_status = Read_SJA1000(CAN_SR); //讀狀態(tài)
DelayUs(5);
//自檢模式,自發(fā)自收檢測
//Write_SJA1000(CAN_MOD,0x04); //配置模式寄存器,雙濾波,自發(fā)自收
//接收所有報文
Write_SJA1000(CAN_AMR0,0xff); //配置驗收屏蔽AMR0=0FFH
Write_SJA1000(CAN_AMR1,0xff); //配置驗收屏蔽AMR1=0FFH
Write_SJA1000(CAN_AMR2,0xff); //配置驗收屏蔽AMR2=0FFH
Write_SJA1000(CAN_AMR3,0xff); //配置驗收屏蔽AMR3=0FFH
Write_SJA1000(CAN_ACR0,0x00); //配置驗收代碼ACR1=0:
Write_SJA1000(CAN_ACR1,0x00); //配置驗收代碼ACR1=0:
Write_SJA1000(CAN_ACR2,0x00); //配置驗收代碼ACR1=0:
Write_SJA1000(CAN_ACR3,0x00); //配置驗收代碼ACR3=0;
Write_SJA1000(CAN_TXERR,0x00);
Write_SJA1000(CAN_RXERR,0x00);
Write_SJA1000(CAN_ECC,0x00);
Write_SJA1000(CAN_RBSA,0x00);
//設置波特率(16MHZ晶振,125Kb)
Write_SJA1000(CAN_BTR0,0x03); //配置總線定時--1255kbps 0x6e03
Write_SJA1000(CAN_BTR1,0x1C); // 0xffaf
Write_SJA1000(CAN_OCR,0xaa); //配置輸出控制
Write_SJA1000(CAN_EWLR,0xff); //配置錯誤報警限制為255
Write_SJA1000(CAN_CMR,0x0c); // 清楚數(shù)據溢出和釋放接收緩沖器
// 退出復位模式,進入工作模式
SJAQuitResetMode();
SR_status = Read_SJA1000(CAN_SR); //讀狀態(tài)
//Write_SJA1000(CAN_TXB+4,ID3); //配置發(fā)送緩沖區(qū)的ID3-
// Write_SJA1000(CAN_IER,0x01); //配置SJA10000中斷-錯誤報警/發(fā)送/接收中斷0x07
Write_SJA1000(CAN_IER,0x81); //使能SJA1000接收中斷位,和總線錯誤中斷位
SEI();
}
//******************************SJA1000發(fā)送程序*********************************
void send_sja1000(void)
{
uchar s,i,canstatus,canstatus1,canstatus2,canstatus3,canstatus4;
//CLI();
canstatus= Read_SJA1000(CAN_SR); //讀狀態(tài)
canstatus1 = Read_SJA1000(CAN_MOD); //讀模式
canstatus2 =Read_SJA1000(15); //TX錯誤計數(shù)
canstatus3 =Read_SJA1000(14); //RX錯誤計數(shù)
canstatus4 =Read_SJA1000(12); //錯誤代碼捕捉
for( i=0;i5;i++ );
{
canstatus = Read_SJA1000(CAN_SR);
}
if((canstatus 0x0C)== 0x0C) //查詢SJA1000是否處于接收狀態(tài),當SJA1000不處于接收狀態(tài)時才可繼續(xù)執(zhí)行
{ //SR.3=0,發(fā)送請求未處理完,等待直到SR.3=1
goto SJA1000; //SR.2=0,發(fā)送緩沖器被鎖
}
else
{
LEDSysMode = 0;
return;
}
SJA1000:
//發(fā)送數(shù)據寫進發(fā)送緩沖區(qū)中
WriteSJARegBlock(16,Send_CAN_Info_ID,5); //擴展幀,向發(fā)送緩沖區(qū)寫入5個識別碼
Write_SJA1000(21,DataText[0]); //擴展幀,向發(fā)送緩沖區(qū)寫入8個數(shù)據
Write_SJA1000(22,DataText[1]);
Write_SJA1000(23,DataText[2]);
Write_SJA1000(24,DataText[3]);
Write_SJA1000(25,DataText[4]);
Write_SJA1000(27,DataText[6]);
Write_SJA1000(26,DataText[5]);
Write_SJA1000(28,DataText[7]);
// 啟動發(fā)送
Write_SJA1000(CAN_CMR,0x01);//使能發(fā)送請求,發(fā)送數(shù)據。(0x03,單次發(fā)送,有錯誤不重新發(fā)送;0x01發(fā)送請求命令,當錯誤時可重發(fā);0x10產生一次自接收性質的報文發(fā)送)
SEI();
DataText[7]=0x00;
while((canstatus0x08) ==0) //檢測是否發(fā)送完畢
{
canstatus = Read_SJA1000(CAN_SR); //讀狀態(tài)
}
LEDSysMode = 1;
}
【總結】:
本系統(tǒng)主要是RF無線和CAN總線兩大塊,功能都能實現(xiàn)。部分代碼如上所示,若網友希望進一步了解,可以站內信聯(lián)系。
原帖地址:http://bbs.21ic.com/icview-584206-1-1.html
評論