- //S选择模式 高速 接地 静默模式 接高电 PA15
- //PA11 CANRX PA12 CANTX
- //can总线io口配置初始化
- void can_io_init(void)
- {
-
- GPIO_InitTypeDef GPIO_Structure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- // RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
-
- GPIO_Structure.GPIO_Pin=GPIO_Pin_12; //管脚
- GPIO_Structure.GPIO_Speed=GPIO_Speed_50MHz; //输出速度
- GPIO_Structure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_Init(GPIOA,&GPIO_Structure); //初始化PA12
-
- GPIO_Structure.GPIO_Pin=GPIO_Pin_11; //管脚
- GPIO_Structure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Structure.GPIO_Mode=GPIO_Mode_IPU; //上拉输入
- GPIO_Init(GPIOA,&GPIO_Structure); //初始化PA11
-
- GPIO_Structure.GPIO_Pin=GPIO_Pin_15; //管脚
- GPIO_Structure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Structure.GPIO_Mode=GPIO_Mode_Out_PP; //推挽输出
- GPIO_Init(GPIOA,&GPIO_Structure); //初始化PA15
- GPIO_ResetBits(GPIOA,GPIO_Pin_15); //写低电平
- }
- //can总线工作模式设置
- void can_structure(void)
- {
- //主控MCR寄存器设置 can工作模式
- CAN_InitTypeDef CAN_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE); //打开can1时钟总线
- CAN_DeInit(CAN1); //清除can通讯寄存器
-
- CAN_StructInit(&CAN_InitStructure);
- CAN_InitStructure.CAN_TXFP=DISABLE; //优先级由报文标识符决定
- CAN_InitStructure.CAN_RFLM=DISABLE; //三个邮箱收满时 溢出时覆盖原有的报文
- CAN_InitStructure.CAN_NART=ENABLE; //报文发送失败时会不断的继续发送
- CAN_InitStructure.CAN_AWUM=DISABLE; //检测到报文,自动唤醒can
- CAN_InitStructure.CAN_ABOM=DISABLE;// 离线过程 对INRQ置1 在清0初始化can寄存器如果还是检测到隐性就自动退出
- CAN_InitStructure.CAN_TTCM=DISABLE;//禁止时间触发通讯模式
- CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;//正常模式
- //can 波特率设置 位时序 baud=1/正常位时间 baud=1mbps
- CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//重新同步跳跃宽度为1个时间单位
- CAN_InitStructure.CAN_BS1=CAN_BS1_7tq;//时间段1占用8个时间单位
- CAN_InitStructure.CAN_BS2=CAN_BS2_4tq; //时间段2占用7个时间单位
- CAN_InitStructure.CAN_Prescaler=6; //分频系数5
- CAN_Init(CAN1,&CAN_InitStructure); //初始化can1
- }
- //can滤波器设置函数 接收
- void Filter_Sturcture(void)
- {
- CAN_FilterInitTypeDef CAN_FilterStructure;
- CAN_FilterStructure.CAN_FilterNumber=0; //过滤器0
- CAN_FilterStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//标识符屏蔽位
- CAN_FilterStructure.CAN_FilterScale=CAN_FilterScale_32bit; //32 位宽
- CAN_FilterStructure.CAN_FilterIdHigh=0x0000;////32 位 标识符
- CAN_FilterStructure.CAN_FilterIdLow=0x0000;
- CAN_FilterStructure.CAN_FilterMaskIdHigh=0x0000;//32 位 MASK屏蔽寄存器
- CAN_FilterStructure.CAN_FilterMaskIdLow=0x0000;
-
- CAN_FilterStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;// FIFO0
- CAN_FilterStructure.CAN_FilterActivation=ENABLE; //激活过滤器 0
- CAN_FilterInit(&CAN_FilterStructure); //初始化过滤器0
- CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE); //FIFO0使能can通讯中断 接收 //发送CAN_IT_TME
- //发送中断
- CAN_ITConfig(CAN1,CAN_IT_TME,ENABLE); //发送邮箱空中断
- // CAN_ITConfig(CAN1,CAN_IT_FF0,ENABLE);
- }
- void can_nvic_structure(void)
- {
- // CAN1_RX1_IRQn处理FIFO1中断
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //中断组别1
- NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN1_RX0_IRQn;//设置can总线中断线FIF0
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;//抢占优先级0
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;//响应优先级
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //使能中断
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel=USB_HP_CAN1_TX_IRQn;//设置can总线中断线
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;//抢占优先级0
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;//响应优先级
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //使能中断
- NVIC_Init(&NVIC_InitStructure);
- //USB_LP_CAN1_RX0_IRQn互联型 如上
- }
- //can初始化函数
- void can_Init(void)
- {
- can_io_init();
- can_structure();
- Filter_Sturcture();
- can_nvic_structure();
- }
- //发送数据函数
- //return 0;发送成功
- // 其他;失败
- u8 can_senddate(u8 *date,u8 len)
- {
- u8 i=0,j=0,mbox=0;
- //设定标识符 共11位 取值范围 0~0x7FF
- //决定该帧优先级 数值越小 优先级越高
- CanTxMsg CanTxMsgStructure;
- CanTxMsgStructure.StdId=0x314; //标准标识符
- CanTxMsgStructure.ExtId=0;//设定扩展标识符 共18bit 取值范围 0~0x3FFFF
- CanTxMsgStructure.IDE=CAN_ID_STD; //标准格式
- CanTxMsgStructure.RTR=CAN_RTR_Data; //数据帧
- CanTxMsgStructure.DLC=len; //数据长度
- for(i=0;i<len;i++)
- {
- CanTxMsgStructure.Data[i]=date[i];//can总线上装载数据
- }
- mbox=CAN_Transmit(CAN1,&CanTxMsgStructure); //发送数据
- while((CAN_TransmitStatus(CAN1, mbox)==CAN_TxStatus_Failed)&&(j<0XFFF)) j++;
- if(j>0xFFF) return 1;
- return 0;
- }
- //接收中断函数
- void USB_LP_CAN1_RX0_IRQHandler(void) //PA11 PA12
- {
- CanRxMsg RxMessage;
- int i=0;
- RxMessage.StdId=0x00;
- RxMessage.ExtId=0x00;
- RxMessage.IDE=0;
- RxMessage.DLC=0;
- RxMessage.FMI=0;
- RxMessage.Data[0]=0x00;
- RxMessage.Data[1]=0x00;
- RxMessage.Data[2]=0x00;
- RxMessage.Data[3]=0x00;
- RxMessage.Data[4]=0x00;
- RxMessage.Data[5]=0x00;
- RxMessage.Data[6]=0x00;
- RxMessage.Data[7]=0x00;
- CAN_Receive(CAN1,CAN_Filter_FIFO0,&RxMessage); //接受数据
- for(i=0;i<8;i++)
- {
- recive_buf[i]=RxMessage.Data[i];
- }
-
- Rx_finish_Flag=1;
- }
- //发送中断函数
- void USB_HP_CAN1_TX_IRQHandler(void) //CAN TX
- {
- if (CAN_GetITStatus(CAN1,CAN_IT_TME)!= RESET)
- {
- // LED4=!LED4;
- CAN_ClearITPendingBit(CAN1,CAN_IT_TME);
- }
- }
- //can数据查询收到
- u8 can_recive_check(void)
- {
- u32 i=0;
- CanRxMsg CanRxMsgStructure;
- //检测有无数据收到
- CanRxMsgStructure.StdId=0x00;
- CanRxMsgStructure.ExtId=0x00;
- CanRxMsgStructure.IDE=0;
- CanRxMsgStructure.DLC=0;
- CanRxMsgStructure.FMI=0;
- CanRxMsgStructure.Data[0]=0x00;
- CanRxMsgStructure.Data[1]=0x00;
- CanRxMsgStructure.Data[2]=0x00;
- CanRxMsgStructure.Data[3]=0x00;
- CanRxMsgStructure.Data[4]=0x00;
- CanRxMsgStructure.Data[5]=0x00;
- CanRxMsgStructure.Data[6]=0x00;
- CanRxMsgStructure.Data[7]=0x00;
- CAN_Receive(CAN1, CAN_Filter_FIFO0, &CanRxMsgStructure);//读取数据
- for(i=0;i<8;i++)
- {
- recive_buf[i]=CanRxMsgStructure.Data[i];
- }
- return CanRxMsgStructure.DLC;
- }
复制代码
芯片型号 STM32F103VCT6
会不会是因为PA11和PA12IO口有其他功能复用了。求解答感谢
|