Practice of can bus
Practice of can bus
Hardware
II. Software
A. GPIO_ Configuration for Led (LED1->LED4)
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
//LED1 (PC9) LED2 (PC10) LED3 (PC11) LED4 (PC12)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_2| GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
B. UART3_ Configuration for monitoring
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(Open_USARTx_TX_GPIO_CLK,ENABLE);
RCC_AHB1PeriphClockCmd(Open_USARTx_RX_GPIO_CLK,ENABLE);
/*
* Open_USARTx_TX -> PA9 , Open_USARTx_RX -PA10
*/
GPIO_InitStructure.GPIO_Pin = Open_USARTx_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(Open_USARTx_TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Open_USARTx_RX_PIN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(Open_USARTx_RX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Open_USARTx_RTS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(Open_USARTx_RTS_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Open_USARTx_CTS_PIN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(Open_USARTx_CTS_GPIO_PORT, &GPIO_InitStructure);
#endif
/*
USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit
*/
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
#if defined HwFlowControl && !defined OpenUART4 && !defined OpenUART5
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
#else
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
#endif
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(Open_USARTx, &USART_InitStructure);
/* Enable the Open_USART Transmit interrupt: this interrupt is generated when the
Open_USARTx transmit data register is empty */
USART_ITConfig(Open_USARTx,USART_IT_RXNE,ENABLE);
USART_Cmd(Open_USARTx, ENABLE);
void USART_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef OpenCAN1
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
#else /* USE_CAN2 */
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
#endif /* OpenCAN1 */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
Example:
Diagnostic CAN ID
Request Id: 0x712
Tester send request via Request Id to ECU
Tester receives Id from ECU response and then show the value on monitor (PC) via UART3
int main(void)
{
GPIO_Configuration();
USART_Configuration();
NVIC_Config();
CAN_Config();
//show information on monitor (PC) via UART3
printf("\r\n****************************************************************\r\n");
printf("CAN-Bus Test \r\n");
printf("CAN-Bus Speed 100kHz \r\n");
/* Infinite loop */
while (1)
{
if( CanFlag == ENABLE )
{
CanFlag = DISABLE;
printf("CAN Receive Data \r\n");
printf("CAN ID %x \r\n",CAN_ID);
printf("CAN_DATA0 %x \r\n",CAN_DATA0);
printf("CAN_DATA1 %x \r\n",CAN_DATA1);
printf("CAN_DATA2 %x \r\n",CAN_DATA2);
printf("CAN_DATA3 %x \r\n",CAN_DATA3);
printf("CAN_DATA4 %x \r\n",CAN_DATA4);
printf("CAN_DATA5 %x \r\n",CAN_DATA5);
printf("CAN_DATA6 %x \r\n",CAN_DATA6);
printf("CAN_DATA7 %x \r\n",CAN_DATA7);
}
CanWriteData(0x712);
if( Display )
{
/*====LED-ON=======*/
GPIO_SetBits(GPIOB , GPIO_Pin_0);
GPIO_SetBits(GPIOB , GPIO_Pin_1);
GPIO_SetBits(GPIOB , GPIO_Pin_2);
GPIO_SetBits(GPIOB , GPIO_Pin_3);
}
else
{
/*====LED-OFF=======*/
GPIO_ResetBits(GPIOB , GPIO_Pin_0);
GPIO_ResetBits(GPIOB , GPIO_Pin_1);
GPIO_ResetBits(GPIOB , GPIO_Pin_2);
GPIO_ResetBits(GPIOB , GPIO_Pin_3);
}
Display = ~Display;
Delay(); /* delay 200ms */
Delay(); /* delay 200ms */
Delay(); /* delay 200ms */
Delay(); /* delay 200ms */
}
}
Refer to attached file
Practice
In this practice,
- Tester board will send request to ECU board read Wakeup button value and User button
value.
- ECU board receives request, Wakeup button value and User button value are stored in
“Data7” of frame.
- Tester board receives Wakeup button value and User button value from ECU board
response and then show the value on LED1 and LED2.
- Tester board will send request to read Wakeup button value and User button value for each
1 second.