stm32f te can haberleşme problemi?

koray692

Üye
Katılım
3 Tem 2010
Mesajlar
49
Puanları
1
Yaş
34
merhaba arkadaşlar normal modda iki stm kartını haberleştirmeye çalışıyorum kod aşşağıda ancak debug yaptığımda RxMessage.Data[0] ya 0x58 yerleşiyor ve sonra hiç değişmiyor...derleyici olarak keil kullanıyorum. (mcp2551 kullandım)


#include "stm32f4xx.h"
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_can.h>
#include <stm32f4xx_gpio.h>

#define CAN_GPIO_PORT GPIOD
#define CAN_RX_SOURCE GPIO_PinSource0 //PD0
#define CAN_TX_SOURCE GPIO_PinSource1 //PD1
#define CAN_AF_PORT GPIO_AF_CAN1
#define CANx CAN1

void CanBusInit(void);

CanTxMsg TxMessage;
CanRxMsg RxMessage;

int main()
{
int i=0;
CanBusInit();

TxMessage.StdId=0x321;
TxMessage.ExtId=0x01;
TxMessage.IDE=CAN_ID_STD;
TxMessage.RTR=CAN_RTR_DATA;
TxMessage.DLC=1;
TxMessage.Data[0]=0;

CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);


CAN_Transmit(CANx,&TxMessage);
CAN_Receive(CANx,CAN_FIFO0,&RxMessage);

// RxMessage.StdId=;
// RxMessage.ExtId=;
// RxMessage.IDE=;
// RxMessage.RTR=;
// RxMessage.DLC=;
// RxMessage.Data=;
// RxMessage.FMI=;

while(1)
{
for(i=0;i<5000000;i++);
CAN_Transmit(CANx,&TxMessage);
CAN_Receive(CANx,CAN_FIFO0,&RxMessage);
TxMessage.Data[0]=RxMessage.Data[0]+1;
if(RxMessage.Data[0]>5)
{
GPIO_WriteBit(GPIOD,GPIO_Pin_12|GPIO_Pin_13, Bit_SET);

}
}

}

void CanBusInit(void)
{

GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CANFilter_InitStructure;

/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
/* CAN configuration ********************************************************/
/* Enable CAN clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);


/* Connect CAN pins to AF9 */
GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_RX_SOURCE, CAN_AF_PORT);
GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT);

/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin = CAN_RX_SOURCE | CAN_TX_SOURCE;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(CAN_GPIO_PORT, &GPIO_InitStructure);

/* CAN register init */
CAN_DeInit(CANx);

/* CAN cell init */
CAN_InitStructure.CAN_TTCM = DISABLE; // time-triggered communication mode = DISABLED
CAN_InitStructure.CAN_ABOM = DISABLE; // automatic bus-off management mode = DISABLED
CAN_InitStructure.CAN_AWUM = DISABLE; // automatic wake-up mode = DISABLED
CAN_InitStructure.CAN_NART = ENABLE; // non-automatic retransmission mode = ENABLE (To prevent endless spam)
CAN_InitStructure.CAN_RFLM = DISABLE; // receive FIFO locked mode = DISABLED
CAN_InitStructure.CAN_TXFP = DISABLE; // transmit FIFO priority = DISABLED
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

/* CAN Baudrate */
CAN_InitStructure.CAN_BS1 = CAN_BS1_14tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 4;
CAN_Init(CANx, &CAN_InitStructure);

CANFilter_InitStructure.CAN_FilterActivation = ENABLE;
CANFilter_InitStructure.CAN_FilterNumber = 14;
CANFilter_InitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CANFilter_InitStructure.CAN_FilterScale = CAN_FilterScale_16bit;
CANFilter_InitStructure.CAN_FilterIdHigh = 0x00;
CANFilter_InitStructure.CAN_FilterIdLow = 0x00;
CANFilter_InitStructure.CAN_FilterMaskIdHigh = 0x00;
CANFilter_InitStructure.CAN_FilterMaskIdLow = 0x00;
CANFilter_InitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
CAN_FilterInit(&CANFilter_InitStructure);
}

void Led_Init(){

GPIO_InitTypeDef gpioStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

gpioStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13;
gpioStructure.GPIO_Mode = GPIO_Mode_OUT;
gpioStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &gpioStructure);

GPIO_WriteBit(GPIOD,GPIO_Pin_12|GPIO_Pin_13, Bit_RESET);
}
 

Forum istatistikleri

Konular
129,711
Mesajlar
929,164
Kullanıcılar
452,419
Son üye
EnjAli

Yeni konular

Geri
Üst