merhaba arkadaşlar kodu düzenledim ancak kod çalışmıyor debug ta da normalde de ? nedenini bulamadım...
#include <stdio.h>
#include <string.h> // memset
#include <stm32f4xx.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h>
#include <misc.h>
#include "stm32f4xx_CAN.h"
// RedDragon 407 - CAN 125 Kbps RX and TX - sourcer32@gmail.com
// CAN1 PD0 (RX), PD1 (TX) J2, J3 ON
// CAN2 PB12 (RX), PB13 (TX) board miswired RX/TX switched
// J9-1 to J10-2, J9-2 to J10-1 fixes routing to SN65VD230
// CAN1_L to CAN2_L, CAN1_H to CAN2_H, GND already Common
// USART6 PG9 (RX), PG14 (TX) J13/J14 at 2-3
__IO uint8_t ADCConvertedValue[2];
int indexx=0;
double adcokunan;
void Led_Init(){
GPIO_InitTypeDef gpioStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
gpioStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
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|GPIO_Pin_14|GPIO_Pin_15, Bit_RESET);
}
//****************************************************************************
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* Enable CAN clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB1Periph_CAN2, ENABLE);
/* GPIO[B,D,G] clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOG, ENABLE);
}
//****************************************************************************
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Connect CAN pins to AF */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_CAN1); // CAN1_RX
GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_CAN1); // CAN1_TX
/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Connect CAN pins to AF */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_CAN2); // CAN2_RX
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_CAN2); // CAN2_TX
}
//****************************************************************************
//****************************************************************************
void CAN_Configuration(void)
{
RCC_ClocksTypeDef RCC_Clocks;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
RCC_GetClocksFreq(&RCC_Clocks);
/* CAN register init */
CAN_DeInit(CAN1);//Deinitializes the CAN peripheral registers to their default reset values.
CAN_DeInit(CAN2);//Deinitializes the CAN peripheral registers to their default reset values.
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM = DISABLE;//Enable or disable the time triggered communication mode. This parameter can be set either to ENABLE or DISABLE. [url=http://en.wikipedia.org/wiki/Time-Triggered_Protocol]http://en.wikipedia.org/wiki/Time-Triggered_Protocol[/url]
CAN_InitStructure.CAN_ABOM = DISABLE;//Enable or disable the automatic bus-off management. This parameter can be set either to ENABLE or DISABLE.
CAN_InitStructure.CAN_AWUM = DISABLE;//Enable or disable the automatic wake-up mode. This parameter can be set either to ENABLE or DISABLE.
CAN_InitStructure.CAN_NART = DISABLE;//Enable or disable the non-automatic retransmission mode. This parameter can be set either to ENABLE or DISABLE.
CAN_InitStructure.CAN_RFLM = DISABLE;//Enable or disable the Receive FIFO Locked mode. This parameter can be set either to ENABLE or DISABLE.
CAN_InitStructure.CAN_TXFP = DISABLE;//Enable or disable the transmit FIFO priority. This parameter can be set either to ENABLE or DISABLE.
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;//Specifies the CAN operating mode. This parameter can be a value of CAN_operating_mode
/* quanta 1+6+7 = 14, 14 * 24 = 336, 42000000 / 336 = 125000 */
/* CAN Baudrate = 125Kbps (CAN clocked at 42 MHz) Prescale = 24 */
/* Requires a clock with integer division into APB clock */
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; // 1+6+7 = 14, 1+14+6 = 21, 1+15+5 = 21 Specifies the maximum number of time quanta the CAN hardware is allowed to lengthen or shorten a bit to perform resynchronization. This parameter can be a value of CAN_synchronisation_jump_width
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;//Specifies the number of time quanta in Bit Segment 1. This parameter can be a value of CAN_time_quantum_in_bit_segment_1
CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq;//Specifies the number of time quanta in Bit Segment 2. This parameter can be a value of CAN_time_quantum_in_bit_segment_2
CAN_InitStructure.CAN_Prescaler = RCC_Clocks.PCLK1_Frequency / (14 * 125000); // quanta by baudrate
CAN_Init(CAN1, &CAN_InitStructure);
CAN_Init(CAN2, &CAN_InitStructure);
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; // IdMask or IdList
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; // 16 or 32
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000; // Everything, otherwise 11-bit in top bits Specifies the filter identification number (MSBs for a 32-bit configuration, first one for a 16-bit configuration). This parameter can be a value between 0x0000 and 0xFFFF
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;//Specifies the filter identification number (LSBs for a 32-bit configuration, second one for a 16-bit configuration). This parameter can be a value between 0x0000 and 0xFFFF
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;//Specifies the filter mask number or identification number, according to the mode (MSBs for a 32-bit configuration, first one for a 16-bit configuration). This parameter can be a value between 0x0000 and 0xFFFF
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;//Specifies the filter mask number or identification number, according to the mode (LSBs for a 32-bit configuration, second one for a 16-bit configuration). This parameter can be a value between 0x0000 and 0xFFFF
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; // Rx Specifies the FIFO (0 or 1) which will be assigned to the filter. This parameter can be a value of CAN_filter_FIFO
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //Enable or disable the filter. This parameter can be set either to ENABLE or DISABLE.
CAN_FilterInitStructure.CAN_FilterNumber = 0; // CAN1 [ 0..13] Specifies the filter which will be initialized. It ranges from 0 to 13.
CAN_FilterInit(&CAN_FilterInitStructure);
CAN_FilterInitStructure.CAN_FilterNumber = 14; // CAN2 [14..27] Specifies the filter which will be initialized. It ranges from 0 to 13.
CAN_FilterInit(&CAN_FilterInitStructure);
}
//****************************************************************************
//****************************************************************************
void CAN2RX(void)
{
CanRxMsg RxMessage;
if (CAN_MessagePending(CAN2, CAN_FIFO0)) //Returns the number of pending received messages.
{
memset(&RxMessage, 0, sizeof(RxMessage));
/* receive */
/* receive */
CAN_Receive(CAN2, CAN_FIFO0, &RxMessage);
adcokunan=RxMessage.Data[4];
printf("RX %04X - %02X - %02X %02X %02X %02X %02X %02X %02X %02X\n",
RxMessage.StdId, RxMessage.DLC,
RxMessage.Data[0],RxMessage.Data[1],RxMessage.Data[2],RxMessage.Data[3],
RxMessage.Data[4],RxMessage.Data[5],RxMessage.Data[6],RxMessage.Data[7]);
}
}
//****************************************************************************
void led(void)
{
if (5>adcokunan)
{
GPIO_WriteBit(GPIOD,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15,Bit_RESET);
}
else if (5<=adcokunan&&adcokunan<100)
{
GPIO_WriteBit(GPIOD,GPIO_Pin_12,Bit_SET);
GPIO_WriteBit(GPIOD,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15,Bit_RESET);
//for (indexx = 0; indexx < 5000; indexx++);
}
else if (100<=adcokunan&&adcokunan<150)
{
GPIO_WriteBit(GPIOD,GPIO_Pin_12|GPIO_Pin_13, Bit_SET);
GPIO_WriteBit(GPIOD,GPIO_Pin_14|GPIO_Pin_15, Bit_RESET);
}
else if (150<=adcokunan&&adcokunan<200)
{
GPIO_WriteBit(GPIOD,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14, Bit_SET);
GPIO_WriteBit(GPIOD,GPIO_Pin_15, Bit_RESET);
}
else
{
GPIO_WriteBit(GPIOD,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15, Bit_SET);
}
}
void CAN1TX(void)
{
CanTxMsg TxMessage;
// transmit */
TxMessage.StdId = 0x123;//Specifies the standard identifier. This parameter can be a value between 0 to 0x7FF.
TxMessage.ExtId = 0x00;//Specifies the extended identifier. This parameter can be a value between 0 to 0x1FFFFFFF
TxMessage.RTR = CAN_RTR_DATA;//Specifies the type of frame for the message that will be transmitted
TxMessage.IDE = CAN_ID_STD;//Specifies the type of identifier for the message that will be transmitted
TxMessage.DLC = 8;//Specifies the length of the frame that will be transmitted
TxMessage.Data[0] = 0x02;
TxMessage.Data[1] = 0x11;
TxMessage.Data[2] = 0x11;
TxMessage.Data[3] = 0x11;
while(1) // Do not want to exit
{
volatile uint32_t i;
static int j = 0;
uint8_t TransmitMailbox = 0;
TxMessage.Data[4] =ADCConvertedValue[0]; // Cycling
TxMessage.Data[5] = (j >> 8) & 0xFF;
TxMessage.Data[6] = (j >> 16) & 0xFF;
TxMessage.Data[7] = (j >> 24) & 0xFF;
j++;
TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);
i = 0;
while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFFFFFF)) // Wait on Transmit
{ //Checks the transmission status of a CAN Frame
i++;
CAN2RX(); // Pump RX
}
CAN2RX();
led();
}
}
//****************************************************************************
int main(void)
{
while(1)
{
Led_Init();
RCC_Configuration();
GPIO_Configuration();
CAN1TX();
CAN_Configuration();
CAN2RX();
puts("CAN Test");
}
}
konfigürasyonların while loop içerisinde olmaması gerekir.