/***********************************(abc931-1240.c)******
* CAN test (MCP25625BO) <ノード7> *
********************************************************/
#include "myProject.h"
#include "mcp_can_dfs.h"
#include "skMCP25xx.h"
// 共通変数の宣言
unsigned char R_len = 0; // CAN受信文字数
unsigned char RcvBuf[8]; // CAN受信データ
/********************************************************
* CAN送受信 *
*------------------------------------------------------*/
//--- CAN受信処理
void CAN_Receive(void){
unsigned long id ;
while (CAN_MSGAVAIL == mcp_checkReceive()) {
// 受信したメッセージを読み込む,
mcp_readMsgBuf(&R_len, (uint8_t *)RcvBuf);
// データフレームなら処理する
if (mcp_isRemoteRequest() == 0) {
id = mcp_getCanId() ;
switch (id) {
case 0x149 :
if(RcvBuf[0] & 1) IO_RA5_SetHigh();
else IO_RA5_SetLow();
break ;
}
}
}
}
/*---- CAN送信実施(標準、フレーム送信済み待ち)
* uint32_t id: 11ビット識別子
* uint8_t *bf: 送信するデータ列アドレス
* uint8_t n: 送信するバイト数 */
void CAN_Send(uint32_t id, uint8_t *bf, uint8_t n){
uint8_t res;
if(n == 0)
res = mcp_sendMsgBuf(id,CAN_STDID,CAN_RMTFRM,0,bf,1);
else
res = mcp_sendMsgBuf(id,CAN_STDID,CAN_DTFRM,n,bf,1);
}
/********************************************************
* Main application *
********************************************************/
void main(void){
uint8_t bf[8];
// initialize the device
SYSTEM_Initialize();
SSP1CON1bits.SSPEN = 1;
//INTERRUPT_GlobalInterruptEnable();
//INTERRUPT_PeripheralInterruptEnable();
IO_RA5_SetLow();
__delay_ms(1000);
// MCP25625BOによるCAN通信の初期化
// CANバス通信速度=100Kbps MCP25625のクロック=16MHz
while (CAN_OK != mcp_begin(CAN_100KBPS,MCP_16MHz)) {
__delay_ms(500);
IO_RA5_Toggle(); // エラーならLED点滅
}
IO_RA5_SetHigh() ;
// MASK0->Filter0->RXB0(オーバフローでRXB1)のみ使用
mcp_init_Mask(0, 0, 0x3ff) ; // 全て受付る
mcp_init_Filt(0, 0, 0x149) ; // ID:0x149のみ受け取る
while (1) {
// CAN受信有りなら
if(mcp_checkReceive()==CAN_MSGAVAIL){
CAN_Receive(); // データの取得と処理
}
// SWが押されていたら
if(IO_RA3_GetValue()==0){
bf[0]= 0x4F; bf[1]= 0x4B; // 'OK'を送る
CAN_Send(0x131, bf, 2); // id=0x131で送信
while(IO_RA3_GetValue()==0); // 離れるまで待つ
__delay_ms(100);
}
}
}
/*************** End of File **********************/
|