研究
STM32的双can有段时间了 一直有个问题一直没搞定,我初始化设置成500K,当 can总线上为500K时程序不会自动配置成相同的波特率.当总线上为125K是就会自动配置成125K 请大神们看看要怎么弄,
以下是我的部分代码:
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "can.h"
int main(void)
{
Bsp_Init();
while(1)
{
c1Sendc2Data();
c2Sendc1Data();
if (CAN_MessagePending(CAN1,CAN_FIFO0)==0)
检测有没有报文 没有就配置成125K
{
CAN_Mode_Init(CAN_SJW_1tq,CAN_BS1_16tq,CAN_BS2_1tq,8,CAN_Mode_Normal);
Delay(1);
}
IWDG_ReloadCounter();
}
}
void Bsp_Init(void)
{
Delay_Init();
Delay(3); //ÑÓʱ3Ãë µÈ´ýµçÔ´ÉϵçÎȶ¨ºó µ¥Æ¬»úÔÙÕý³£¹¤×÷¡£
LED_Init();
BEEP_Init();
KEY_Init();
SFQpw=1;
LED=1;
BEEP=1;
SFQout=1;
SFQin=1;
// USART_Configura
tion();
CAN_Mode_Init(CAN_SJW_1tq,CAN_BS1_16tq,CAN_BS2_1tq,4,CAN_Mode_Normal);
这里初始化设置can波特率设为500K
// NVIC_Configuration();
// CAN_Filter_config();
// IWDG ...
IWDG_Configuration();
}
如果检测不到,报文然后配置成125k。这个完全是由程序控制啊。
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
// CAN_FilterInitTypeDef CAN_FilterInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* 打开GPIO时钟、AFIO时钟,CAN时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
/* CAN1 RX PB8 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* CAN1 TX PB9 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//使用PA8 9引脚的第二功能
GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE); // CAN1 remap
/* CAN1 Enabling interrupt */
NVIC_InitStructure.NVIC_IRQChannel=CAN1_RX0_IRQn;
//STM32有2个3级深度的接收缓冲区:FIFO0和FIFO1
//如果是来自FIFO0的接收中断,则用CAN1_RX0_IRQn中断来处理。
//与这个 对应 CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE); // 启动 CAN1_RX0_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* CAN BaudRate = RCC_APB1PeriphClock/(CAN_SJW+CAN_BS1+CAN_BS2)/CAN_Prescaler */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
CAN_InitStructure.CAN_TTCM=DISABLE;
CAN_InitStructure.CAN_ABOM=ENABLE;//ABOM 自动离线管理
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_Mode=CAN_Mode_LoopBack;
// #define SET_CAN_SJW CAN_SJW_1tq
//#define SET_CAN_BS1 CAN_BS1_4tq // 8
//#define SET_CAN_BS2 CAN_BS2_4tq // 7
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_4tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_4tq;
//#define SET_CAN_PRES 8 // 波特率分频器 9-250K 18-125K
//CAN_InitStructure.CAN_Prescaler=pres;
//CAN波特率 BaudRate = 36MHz/(CAN_SJW+CAN_BS1+CAN_BS2)/CAN_Prescaler
//CAN波特率 BaudRate = 4MHz/CAN_Prescaler
switch(BRtXK)//通信速度:1000K,500K,250K,125K,100K,50K,20K,10K,5K_//11C14:10K_5K不行,1768_5K不行
{
//uQuantumPrescaler为 6bit 所以应该<63
case 1000:
CAN_InitStructure.CAN_Prescaler=4; //4000K/4
break;
case 500:
CAN_InitStructure.CAN_Prescaler=8; //4000k/8
break;
case 250:
CAN_InitStructure.CAN_Prescaler=16; //4000k/16
break;
case 125:
CAN_InitStructure.CAN_Prescaler=32; //4000k/32
break;
case 100:
CAN_InitStructure.CAN_Prescaler=40; //4000k/40
break;
case 50:
CAN_InitStructure.CAN_Prescaler=80; //4000k/80
break;
case 20:
CAN_InitStructure.CAN_Prescaler=200; //4000k/200
break;
case 10:
CAN_InitStructure.CAN_Prescaler=400; //4000k/400
break;
case 5:
CAN_InitStructure.CAN_Prescaler=800; //4000k/800
break;
// uQuantumPrescaler=80; //1768无 CANBAUD_25k
// uQuantumPrescaler=800; //1768无 CANBAUD_2k5
}
CAN_Init(CAN1,&CAN_InitStructure); // CAN1
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE); // 启动 CAN1_RX0_IRQn
冗余双CAN现场总线STM32F105开发板KeilC++源程序
http://www.stmcu.org/module/forum/thread-612916-1-1.html
一周热门 更多>