#include "stm32f10x.h"
unsigned char TxTest;
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
}
void GPIO_Configuration(void) //用重映射功能,
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //RX
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //TX
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap2_CAN1,ENABLE);
}
unsigned char CAN_Configuration(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure; //初始化
CanTxMsg TxMessage; //发送
volatile u8 TxMailBox = 0;
u32 i;
CAN_DeInit(CAN1); //缺省模式
CAN_StructInit(&CAN_InitStructure);
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = DISABLE;
CAN_InitStructure.CAN_AWUM = DISABLE;
CAN_InitStructure.CAN_NART = DISABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = DISABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
CAN_InitStructure.CAN_Prescaler = 12;
CAN_Init(CAN1,&CAN_InitStructure); //CAN初始化有返回值 #define CANINITFAILED ((uint8_t)0x00)
//Error =CAN_Init(&CAN_InitStructure);
// if (Error == 0)
// return FAILED; //判断初始化 是否成功 不成功我们会有灯显示
//CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE); //有一个消息就中断,FIFO0中断
CAN_FilterInitStructure.CAN_FilterNumber = 1;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* (注:标准帧数据帧、标准远程帧和扩展远程帧均被过滤)*/
TxMessage.StdId = 0x321;
TxMessage.ExtId =0x01; //扩展帧
TxMessage.IDE = CAN_ID_STD;
TxMessage.RTR = CAN_RTR_DATA; //数据帧
TxMessage.DLC = 2;
TxMessage.Data[0] = 0x12;
TxMessage.Data[1] = 0x34;
CAN_Transmit(CAN1,&TxMessage);
TxMailBox = CAN_Transmit(CAN1,&TxMessage);
i=0;
while((CAN_TransmitStatus(CAN1,TxMailBox)!=CANTXOK) && (i != 0xff))
{
i++;
}
if(i==0xff)
{
return 0;
}
else
{
return 1;
}
}
void Delay(vu32 times)
{
for(;times!=0;times--);
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();;
CAN_Configuration();
while(1)
{
TxTest = CAN_Configuration();
if(TxTest==1)
{
GPIO_SetBits(GPIOC,GPIO_Pin_5);
Delay(1000000);
GPIO_ResetBits(GPIOC,GPIO_Pin_5);
Delay(1000000);
}
if(TxTest==0)
{
GPIO_SetBits(GPIOC,GPIO_Pin_6);
Delay(1000000);
GPIO_ResetBits(GPIOC,GPIO_Pin_6);
Delay(1000000);
}
}
}
只有发送程序,没有接收,发送成功,PC5对应LED亮灭,发送失败,PC6对应LED亮灭,现在是发送失败,不知道那里有问题,还请高手讲解。
用的是103 |