Home > other >  STM32F1 series CAN communication
STM32F1 series CAN communication

Time:11-25

I'm playing STM32F1 series microcontroller, CAN communications test when the single-chip computer CAN send the CAN data to use PC and CAN tools CAN receive a message, send message but the use of PC, single-chip microcomputer has not receive (after receiving light an LED lamps, but have been unsuccessful), who is a great god to solve, thank you!

CodePudding user response:


refer to the original poster fengjun2233 response:
I'm playing STM32F1 series microcontroller, CAN communications test when the single-chip computer CAN send the CAN data to use PC and CAN tools CAN receive a message, send message but the use of PC, single-chip microcomputer has not receive (after receiving light an LED lamps, but have been unsuccessful), who is a great god to solve, thank you!



Best above and procedures,

Said several ways:
1. STM32F1 configuration CAN communicate the loopback mode test, see if you receive interrupt and handler normal,
2. Check if PC CAN tool communication parameters and single chip microcomputer,
3. Send data in the PC, MCU is occupied when sending data bus, cause data not to go out,
4. Check the circuit, communication no problem, however, circuit, generally no problem,

For reference only.

CodePudding user response:

Fill above:

Code:
CAN model initialization
Void CAN_Mode_Init (u8 TSJW, u8 tbs2, u8 tbs1, under-16 BRP, u8 mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;

# if CAN_RX0_INT_ENABLE
NVIC_InitTypeDef NVIC_InitStructure;
# endif

RCC_APB1PeriphClockCmd (RCC_APB1Periph_CAN1, ENABLE);//open the CAN1 clock
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);//PA open port clock

GPIO_InitStructure. GPIO_Pin=GPIO_Pin_11;//PA11
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_IPU;//pull input mode
GPIO_Init (GPIOA, & amp; GPIO_InitStructure);

GPIO_InitStructure. GPIO_Pin=GPIO_Pin_12;//PA12
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_AF_PP;//multiplexing push-pull output
GPIO_InitStructure. GPIO_Speed=GPIO_Speed_50MHz;//IO port speed of 50 MHZ
GPIO_Init (GPIOA, & amp; GPIO_InitStructure);

//CAN unit set
CAN_InitStructure. CAN_TTCM=DISABLE;//the time triggered communication mode
CAN_InitStructure. CAN_ABOM=DISABLE;//software automatically offline management
CAN_InitStructure. CAN_AWUM=DISABLE; Wake up//sleep mode by software (remove CAN - & gt; SLEEP of MCR)
CAN_InitStructure. CAN_NART=ENABLE;//using message automatically send
CAN_InitStructure. CAN_RFLM=DISABLE;//message doesn't lock, new cover the old
CAN_InitStructure. CAN_TXFP=DISABLE;//priority is determined by the message identifier
CAN_InitStructure. CAN_Mode=mode;//mode setting
CAN_InitStructure. CAN_SJW=TSJW;//jump to resynchronize width (Tsjw) for Tsjw + 1 time unit CAN_SJW_1tq ~ CAN_SJW_4tq
CAN_InitStructure. CAN_BS1=tbs1;//Tbs1 range CAN_BS1_1tq ~ CAN_BS1_16tq
CAN_InitStructure. CAN_BS2=tbs2;//Tbs2 range CAN_BS2_1tq ~ CAN_BS2_8tq
CAN_InitStructure. CAN_Prescaler=BRP;//frequency coefficient (Fdiv) for BRP + 1
CAN_Init (CAN1, & amp; CAN_InitStructure);//initialize the CAN1

//configure filters
CAN_FilterInitStructure. CAN_FilterNumber=0;//filter 0
CAN_FilterInitStructure. CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure. CAN_FilterScale=CAN_FilterScale_32bit;//32-bit
CAN_FilterInitStructure. CAN_FilterIdHigh=0 x0000;////32-bit ID
CAN_FilterInitStructure. CAN_FilterIdLow=0 x0000;
CAN_FilterInitStructure. CAN_FilterMaskIdHigh=0 x0000;//32-bit MASK
CAN_FilterInitStructure. CAN_FilterMaskIdLow=0 x0000;
CAN_FilterInitStructure. CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//filter 0 associated with FIFO0
CAN_FilterInitStructure. CAN_FilterActivation=ENABLE;//active filter 0
CAN_FilterInit (& amp; CAN_FilterInitStructure);//filter initialization

# if CAN_RX0_INT_ENABLE
CAN_ITConfig (CAN1, CAN_IT_FMP0, ENABLE);//registered interruption allows FIFO0 news.

NVIC_InitStructure. NVIC_IRQChannel=USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure. NVIC_IRQChannelPreemptionPriority=1;//the main priority is 1
NVIC_InitStructure. NVIC_IRQChannelSubPriority=0;//time priority to 0
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init (& amp; NVIC_InitStructure);
# endif
}

Send function:
U8 CAN_Send_Msg (u8 * MSG, u8 len)
{
U8 mbox;
u16 i=0;
CanTxMsg TxMessage;
TxMessage. X12 StdId=0;//standard identifier of 0
TxMessage. X12 ExtId=0;//set to expand identifier (29)
TxMessage. IDE=0;//use the extension identifiers
TxMessage. RTR=0;//message type for the data frame and a frame eight
TxMessage. DLC=len;//send two information
for(i=0; iTxMessage. Data [I]=MSG [I];//the first frame information
Mbox=CAN_Transmit (CAN1, & amp; TxMessage);
i=0;
While ((CAN_TransmitStatus (CAN1, mbox)==CAN_TxStatus_Failed) & amp; & (i<0 i++ XFFF));//wait for sending end
If (i>=0 XFFF) return 1;
return 0;
}

Receive function:
U8 CAN_Receive_Msg (u8 * buf)
{
U32 I;
CanRxMsg RxMessage;
If (CAN_MessagePending (CAN1, CAN_FIFO0)==0) return 0;//not received data, directly from
CAN_Receive (CAN1 CAN_FIFO0, & amp; RxMessage);//read the data
for(i=0; iBuf [I]=RxMessage. Data [I];
Return RxMessage. DLC;
}

Main function:

Int main ()
{
U8 j=0;

U8 res;
Char_buf u8 tbuf [8], [8].
U8 rbuf [8].

SysTick_Init (72);
NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2);//the interrupt priority group points two groups
LED_Init();
USART1_Init (9600);
KEY_Init ();
CAN_Mode_Init (CAN_SJW_1tq, CAN_BS2_8tq, CAN_BS1_9tq, 4, CAN_Mode_Normal);//500 KBPS baud rate

While (1)
{

Res=CAN_Send_Msg (tbuf, 8);
If (res)
{
Printf (" Send Failed! \r\n");
}
The else
{
Printf (" send data: % s \ r \ n ", char_buf);
}

Res=CAN_Receive_Msg (rbuf);

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related