STM32F205ZET6单片机,使用PA11和PA12进行CAN通信,收发器无论是ISO1050还是TJA1050,都不成功,发送信息时,CAN_TransmitStatus函数一直返回Pending,请问是为什么啊?
另外,使用CAN分析仪——收发器——PA11,PA12连接时,单片机的PA11和PA12引脚是有波形的,,两个引脚都有方波,TXD上的波形密集些,RXD上面的好像只有一个低脉冲,占空比很高,
主要是使用ISO1050芯片通讯,但是就无法发送数据,也没有办法进入到接收终端之中,中断函数我自认为配置好了,和库函数的配置是一样的,
ISO1050 电路为:有120欧终端电阻,CAN_TX和CAN_RX接上拉电阻472,CANH和CANL之间有120欧终端电阻,并且各串接了一个零欧电阻,
大家交流交流?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
main函数:
int main(void)
{
//&*******************************************
//以下程序段只是测试核心板上的指示灯能否正常亮起
RCC_ClocksTypeDef RCC_Clocks;
//&**** SysTick end of count event each 10ms *****&/
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
NVIC_Config();
LED_Config();
CAN_Config();
while(1)
{
CAN_Polling();
}
}
void CAN_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* CAN GPIOs configuration **************************************************/
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* Connect CAN pins to AF1112 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1);
/* Enable CAN clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
// Configure CAN RX and TX pins //
//PA11 RX-上拉输入,PA12 TX-复用推挽输出//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 |GPIO_Pin_12;
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(GPIOA, &GPIO_InitStructure);
}
void CAN_Polling(void)
{
uint32_t i=0;
uint8_t TransmitMailbox=0;
/* CAN configuration ********************************************************/
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM = DISABLE; //time triggered communication mode
CAN_InitStructure.CAN_ABOM = DISABLE; //automatic bus-off management
CAN_InitStructure.CAN_AWUM = DISABLE; //automatic wake-up mode
CAN_InitStructure.CAN_NART = DISABLE; //non-automatic retransmission mode
CAN_InitStructure.CAN_RFLM = DISABLE; //Receive FIFO Locked mode
CAN_InitStructure.CAN_TXFP = ENABLE; //transmit FIFO priority
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; //Specifies the maximum number of time quanta
/*主板的时钟使用外部告诉时钟,25MHz,CAN clocked(APB1) at 30 MHz, set CAN Baudrate = 125KBps
than, how to calculate the Tq*/
// CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq; //Specifies the number of time quanta in Bit Segment 1
// CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq; //Specifies the number of time quanta in Bit Segment 2
// CAN_InitStructure.CAN_Prescaler = 16; // Specifies the length of a time quantum
//
// /* 主板的时钟使用外部高速时钟,25MHz Baudrate = 500 Kbps ,CAN clocked (APB1) at 30MHz*/
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 4;
CAN_Init(CAN1, &CAN_InitStructure);
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber = 0;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;// CAN_FilterMode_IdList
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; //FIFO0
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* Transmit Structure preparation */
TxMessage.StdId = 0x00; //standard ID
TxMessage.ExtId = 0x1234; //extend ID
TxMessage.RTR = CAN_RTR_DATA; //data frame
TxMessage.IDE = CAN_ID_EXT; //standard ID
TxMessage.DLC = 2; //length of data
TxMessage.Data[0]= 0xDE;
TxMessage.Data[1]= 0xCA;
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); // finish sending Interrupt CAN_IT_TME, Enable FIFO 0 message pending Interrupt
//CAN_Transmit(CAN1,&TxMessage);
TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);
//CAN_TransmitStatus 是transmit pending
if((CAN_TransmitStatus(CAN1, TransmitMailbox) == CAN_TxStatus_Ok))
{
GPIO_ResetBits(GPIOE, GPIO_Pin_3);
GPIO_ResetBits(GPIOE, GPIO_Pin_2);
}
i = 0;
//一定时间内不断发送,直到:1、发送成功==CANTXOK,2、i=0xFFFF
while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK ) && (i != 0xFFFF)) // CAN_TxStatus_Failed
{
i++; //发送失败
//GPIO_ResetBits(GPIOE, GPIO_Pin_2);
}
i = 0;
while((CAN_MessagePending(CAN1, CAN_FIFO0) <1 ) && (i != 0xFFFF))
{
i++; //MessagePending 不是FIFO 1,也不是FIFO 0
//GPIO_ResetBits(GPIOE, GPIO_Pin_2);
}
// GPIO_ResetBits(GPIOE, GPIO_Pin_2);//发送失败
}
//
/**
* @brief Configures the NVIC for CAN.
* @param None
* @retval None
*/
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel=CAN1_RX0_IRQn; //中断请求 CAN1_RX0_IRQn
//the highest Priority
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
贴出来了
我只能回去帮你看,我在外地出差。
首先,很感谢你的热心批评指导,我以后会注意的了。现在我大概是确认了不是外围电路的问题,我是使用的核心板加底板的形式的,我使用核心板和另一个CAN收发器模块,TJA1050,也是无法收发,程序还是一直在发送的时候一直处于Pending,而且没有办法进入接收中断,但是其他的中断是正常的,比如定时器中断,,,这么说的话,我的核心板是没有问题的吧?但是程序为什么执行不下去?是不是CAN应用在STM32F205上的应用和在STM32F103的应用不一样?因为以前没有使用过205系列的。
一周热门 更多>