2019-06-10 10:24 PM
Hello guys, last one week i've been trying to communicate between my mcu and a canbus generator. I couldnt receive that transmitted 8byte codes by my canbus generator. Here are my
codes about it. Can anyone help me to understand this ?
#include "main.h"
uint8_t alinan_deger; // the value taken from DATA
void main(void)
{
GPIO_Init (GPIOD, (GPIO_Pin_TypeDef)GPIO_PIN_2, GPIO_MODE_OUT_OD_LOW_FAST);
CAN_TxStatus_TypeDef status ;
CAN_Id_TypeDef Tx_IDE = CAN_Id_Standard;
CAN_RTR_TypeDef Tx_RTR = CAN_RTR_Data;
uint8_t Tx_DLC = 0;
uint32_t Tx_Id = 0;
uint8_t Tx_Data[8] = {0};
enableInterrupts();
while(1)
{
/* Transmit Parameters*/
Tx_Id = 0x222;
Tx_IDE = CAN_Id_Standard;
Tx_RTR = CAN_RTR_Data;
Tx_DLC = 1;
Tx_Data[0] = alinan_deger;
/* Sender send Frame */
status = CAN_Transmit(Tx_Id,Tx_IDE,Tx_RTR,Tx_DLC,Tx_Data);
if(alinan_deger==0x11)
{
GPIO_WriteHigh( GPIOD, GPIO_PIN_2 );
}
else
GPIO_WriteHigh (GPIOD, GPIO_PIN_3);
}
}
void CLK_Config(void)
{
/* Initialization of the clock */
/* Clock divider to HSI/8 */
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);//2Mhz
}
void CAN_Config(void)
{
CAN_InitStatus_TypeDef status = CAN_InitStatus_Failed;
/* Filter Parameters */
CAN_FilterNumber_TypeDef CAN_FilterNumber;
FunctionalState CAN_FilterActivation;
CAN_FilterMode_TypeDef CAN_FilterMode;
CAN_FilterScale_TypeDef CAN_FilterScale;
uint8_t CAN_FilterID1;
uint8_t CAN_FilterID2;
uint8_t CAN_FilterID3;
uint8_t CAN_FilterID4;
uint8_t CAN_FilterIDMask1;
uint8_t CAN_FilterIDMask2;
uint8_t CAN_FilterIDMask3;
uint8_t CAN_FilterIDMask4;
/* Init Parameters*/
CAN_MasterCtrl_TypeDef CAN_MasterCtrl;
CAN_Mode_TypeDef CAN_Mode;
CAN_SynJumpWidth_TypeDef CAN_SynJumpWidth;
CAN_BitSeg1_TypeDef CAN_BitSeg1;
CAN_BitSeg2_TypeDef CAN_BitSeg2;
uint8_t CAN_Prescaler;
/* CAN register init */
CAN_DeInit();
/* CAN init */
CAN_MasterCtrl=CAN_MasterCtrl_AllEnabled;
CAN_Mode = CAN_Mode_Normal;
CAN_SynJumpWidth = CAN_SynJumpWidth_1TimeQuantum;
CAN_BitSeg1 = CAN_BitSeg1_3TimeQuantum;
CAN_BitSeg2 = CAN_BitSeg2_2TimeQuantum;
CAN_Prescaler = 1; //Tq=(1+1)*0.5*10-6 =1us
status = CAN_Init(CAN_MasterCtrl, CAN_Mode, CAN_SynJumpWidth, CAN_BitSeg1, CAN_BitSeg2, CAN_Prescaler); /* CAN filter init */
CAN_FilterNumber = CAN_FilterNumber_0;
CAN_FilterActivation = ENABLE;
CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterScale = CAN_FilterScale_32Bit;
CAN_FilterID1=0;
CAN_FilterID2=0;
CAN_FilterID3=0;
CAN_FilterID4=0;
CAN_FilterIDMask1=0;
CAN_FilterIDMask2=0;
CAN_FilterIDMask3=0;
CAN_FilterIDMask4=0;
CAN_FilterInit(CAN_FilterNumber, CAN_FilterActivation, CAN_FilterMode,
CAN_FilterScale,CAN_FilterID1, CAN_FilterID2, CAN_FilterID3,
CAN_FilterID4,CAN_FilterIDMask1, CAN_FilterIDMask2,
CAN_FilterIDMask3, CAN_FilterIDMask4);
CAN_ITConfig(CAN_IT_FMP, ENABLE);
}
void Delay (uint16_t nCount)
{
/* Decrement nCount value */
for (; nCount != 0; nCount--);
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
CAN_Receive();
alinan_deger = CAN_GetReceivedData(0);