STM32的can问题
用的103VE + usb_can来调试,可是我只有发送。都失败了, usb_can那边得不到数据。
/* Includes ------------------------------*/
#include "stm32f10x.h"
#include "misc.h"
#include "stm32f10x_can.h"
/* Private typedef -----------------------*/
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
__IO uint32_t ret = 0; /* for return of the interrupt handling */
volatile TestStatus TestRx;
/* Private functions ---------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
TestStatus CAN_Polling(void);
TestStatus CAN_Interrupt(void);
/****************************************************************************
* 名 称:int main(void)
* 功 能:主函数
* 入口参数:无
* 出口参数:无
* 说 明:
* 调用方法:无
****************************************************************************/
int main(void)
{
RCC_Configuration(); //系统时钟设置及外设时钟使能
NVIC_Configuration(); //中断源配置
GPIO_Configuration(); //LED控制初始化
/* CAN 50KB/s---正常通信模式 */
TestRx = CAN_Polling();
if (TestRx == FAILED)
{
GPIO_SetBits(GPIOB, GPIO_Pin_5); //测试失败LED1 亮
}
else
{
GPIO_SetBits(GPIOD, GPIO_Pin_3);//测试成功LED3亮
}
while (1);
}
/****************************************************************************
* 名 称:void RCC_Configuration(void)
* 功 能:系统时钟配置为72MHZ, 外设时钟配置
* 入口参数:无
* 出口参数:无
* 说 明:
* 调用方法:无
****************************************************************************/
void RCC_Configuration(void){
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
| RCC_APB2Periph_GPIOD| RCC_APB2Periph_GPIOE , ENABLE);
}
/****************************************************************************
* 名 称:void GPIO_Configuration(void)
* 功 能:LED初始化
* 入口参数:无
* 出口参数:无
* 说 明:
* 调用方法:无
****************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //状态LED1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //通用推挽输出模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出模式最大速度50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_3; //状态LED2,LED3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //通用推挽输出模式
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap1_CAN1 , ENABLE); //端口复用为CAN1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PB8:CAN-RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //输入上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PB9:CAN-TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/****************************************************************************
* 名 称:void NVIC_Configuration(void)
* 功 能:中断源配置
* 入口参数:无
* 出口参数:无
* 说 明:
* 调用方法:无
****************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; //CAN1 RX0中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/***********************************************************/
/*****************CAN 的查询方式***************************/
TestStatus CAN_Polling(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
CanTxMsg TxMessage;
uint32_t i = 0;
uint8_t TransmitMailbox = 0;
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
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_2tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_3tq;
CAN_InitStructure.CAN_Prescaler=120; //36M36/(1+2+3)/120 = 50k
CAN_Init(CAN1, &CAN_InitStructure);
/* transmit 1 message */
TxMessage.StdId=1;
TxMessage.IDE=CAN_ID_STD;
TxMessage.RTR=CAN_RTR_DATA;
TxMessage.DLC=2;
TxMessage.Data[0]=0xDE;
TxMessage.Data[1]=0xCA;
TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);
i=0;
while((CAN_TransmitStatus(CAN1,TransmitMailbox)!=CANTXOK) && (i != 0xff))
{
i++;
}
if(i==0xff)//发送超时,但发送不成功
{
return FAILED;
}
return PASSED; /* Test Passed */
}