Home > other >  Help!!!!!! McP2515 message sent out!!!!!!
Help!!!!!! McP2515 message sent out!!!!!!

Time:10-27

Written in C language, the code is as follows:
# include "MCU. H"
# include "STR. H"
# include "SPI. H"
# include "UART. H"
# include "UART1. H"
# include "EXTIN. H"

# define Inst_ReadRX_Base 1001 x90//mn0
# define Inst_Reset 0 xc0/0000/1100
# define Inst_Read 0 x03//0000 0011
# define Inst_Write 0 x02//0000 0010
# define Inst_WriteTX_Base 0 x40/ABC/0100 0
# define Inst_RTS_Base 0 x80//1000 NNN
# define Inst_Read_State 0 xa0/0000/1010
# define Inst_RX_State 0 xb0/0000/1011
# define Inst_Fix 0 x05/0101/0000

/* * * * * * * * * * * * * * * * * * *
TXB0 send buffer
* * * * * * * * * * * * * * * * * * */
# define TXB0CTRL 0 x30
# define TXRTSCTRL 0 x0d
# define TXB0SIDH 0 x31
# define TXB0SIDL 0 x32
# define TXB0DLC 0 x35
# define TXB0D0 0 x36
# define TXB0D1 0 x37
# define TXB0D2 0 x38
# define TXB0D3 0 x39
# define TXB0D4 0 x3a
# define TXB0D5 0 x3b
# define TXB0D6 0 x3c
# define TXB0D7 0 x3d
/* * * * * * * * * * * * * * * * * * * * *
The sequential
* * * * * * * * * * * * * * * * * * * * */
# define CNF1 0 x2a
# define CNF2 0 x29
# define CNF3 0 x28
/* * * * * * * * * * * * * * * * * * * * *
Interrupt
* * * * * * * * * * * * * * * * * * * * */
# define CANINTE 0 x2b
# define CANINTF 0 x2c
/* * * * * * * * * * * * * * * * * * * * *
Control and status register
* * * * * * * * * * * * * * * * * * * * */
# define CANCTRL 0 x0f
# define CANSTAT 0 x0e

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
SPI read and write functions that area
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Function: implementation of MCP soft reset
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void the Reset () {
RT_SPI_CSOn ();
RT_SPI_Clr ();
RT_SPI_Write (Inst_Reset);
While (RT_SPI_Busy ()) {}
RT_SPI_CSOff ();
Puts (" \ nreset MCP ");
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Function: read and output the corresponding register values
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void the Read (int add) {
RT_SPI_CSOn ();
RT_SPI_Clr ();
RT_SPI_Write (Inst_Read);
RT_SPI_Write (add);
RT_SPI_Write (0 XFF);
while(! RT_SPI_DataRdy ()) {}
Puts (xtoa (RT_SPI_Read_ ()));
While (RT_SPI_Busy ()) {}
RT_SPI_CSOff ();
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Function: to the appropriate register assignment
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void the Write (int the add, int val) {
RT_SPI_CSOn ();
RT_SPI_Clr ();
RT_SPI_Write (Inst_Write);
RT_SPI_Write (add);
RT_SPI_Write (val).
While (RT_SPI_Busy ()) {}
RT_SPI_CSOff ();
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Function: read and returns the value of the corresponding register
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Int Read_ (int add) {
Int outcome;
RT_SPI_CSOn ();
RT_SPI_Clr ();
RT_SPI_Write (Inst_Read);
RT_SPI_Write (add);
RT_SPI_Write (0 XFF);
while(! RT_SPI_DataRdy ()) {}
Puts (xtoa (outcome=RT_SPI_Read_ ()));
While (RT_SPI_Busy ()) {}
RT_SPI_CSOff ();
Return outcome;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Functions: register a modification of the
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void Fix (int the add, int hide, int val) {
RT_SPI_CSOn ();
RT_SPI_Clr ();
RT_SPI_Write (Inst_Fix);
RT_SPI_Write (add);
RT_SPI_Write (hide);
RT_SPI_Write (val).
While (RT_SPI_Busy ()) {}
RT_SPI_CSOff ();
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Function: MCP mode setting
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void MCP_ModeSet (int mode) {
The switch (mode) {
Case 1://Setting
Fix (0 x0f, 0 xe0-0xfc, 0 b10000000);
Puts (" \ nSet to the Setting Mode! \n");
break;
Case 2://Normal
Fix (0 x0f, 0 xe0-0xfc, 0 b00000000);
Puts (" \ nNormal mode \ n ");
break;
Case 3://Sleeping
Fix (0 x0f, 0 xe0-0xfc, 0 b00100000);
break;
Case 4://Listening
Fix (0 x0f, 0 xe0-0xfc, 0 x01100000);
break;
Case 5://Cycle
Fix (0 x0f, 0 xe0-0xfc, 0 x01000000);
break;
}
}

Void user_interrupt () {
Puts (" \ ninterrupt!!!!!! \n");
RT_EXINT_ClrAll ();
RT_EXINT_Set (1, 0);
}

Void main () {
RT_UART1_Off ();
RT_SPI_ModeSet (0);
Puts (" \ nM2 initialized ");

RT_EXINT_Set (1, 0);
RT_EXINT_ClrAll ();

//to soft reset the MCP2515
The Reset ();

//whether to enter configuration mode is not assured, again write to make sure to enter configuration mode
MCP_ModeSet (1);

//check
//while (Read_ (0 x0f) & amp; {0 x87==0 x87)
//puts (" \ n!!!!!!!!!!!!!!!! Real setting mode!!!!!!!!!!!!!!!! \ n ");
//}

//set the timing in configuration mode, empty unnecessary function
Write (CNF1, 0 b00000100);//SJW=1, BRP=4
Write (CNF2, 0 b10110001);//sampling time, PS1=7, PRSEG=1
Write (CNF3, 0 b01000101);//PS2=6
Write (TXRTSCTRL, 0 x00);
//return to normal mode
MCP_ModeSet (2);
//check the normal mode
//while (Read_ (0 x0f) & amp; {0 x07==0 x07)
//puts (" \ n!!!!!!!!!!!!!!!! Real normal mode!!!!!!!!!!!!!!!! \n");
//}

//initialize the buffer
Write (TXB0D0, 0 x00);
Write (TXB0D1, 0 x00);
Write (TXB0D2, 0 x00);
Write (TXB0D3, 0 x00);
Write (TXB0D4, 0 x00);
Write (TXB0D5, 0 x00);
Write (TXB0D6, 0 x00);
Write (TXB0D7, 0 x00);

//set the interrupt enable, and interrupt initialization
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related