一直忙着这个串口转CAN的工程,好久没有更新,今天把CAN的双机中断通信的代码上传。
发送程序:
#include "stm32f10x.h"
#include "delay.h"
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
ErrorStatus HSEStartUpStatus;
vu32 ret = 0; /* for return of the interrupt handling */
unsigned char TestTx; //发送成功标志
unsigned char CAN_MessageSend(void); //报文发送函数
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure PE.02, PE.03, PE.04 and PE.05 as Output push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE ); //重影射CAN IO脚到 PB8,PB9
/* Configure CAN pin: RX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure CAN pin: TX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOE, GPIO_Pin_2);
GPIO_SetBits(GPIOE, GPIO_Pin_3);
GPIO_SetBits(GPIOE, GPIO_Pin_4);
GPIO_SetBits(GPIOE, GPIO_Pin_5);
}
//系统中断管理
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
//配置系统时钟,使能各外设时钟
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA
|RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
|RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE
|RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO
|RCC_APB2Periph_SPI1, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 |RCC_APB1Periph_USART3|RCC_APB1Periph_TIM2 , ENABLE );
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); //使能CAN的时钟
}
//配置所有外设
void Init_All_Periph(void)
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
// USART1_Configuration();
}
int main(void)
{
Init_All_Periph();
while(1)
{
/* CAN transmit at 100Kb/s and receive by polling in normal mode */
TestTx = CAN_MessageSend();
if (TestTx == 1)
{
GPIO_ResetBits(GPIOE,GPIO_Pin_5);
delay_nms(1000); // LED1闪烁一次,发送一个字符
GPIO_SetBits(GPIOE,GPIO_Pin_5);
delay_nms(1000);
}
}
}
unsigned char CAN_MessageSend(void)
{
CAN_InitTypeDef CAN_InitStructure;
CanTxMsg TxMessage;
volatile u8 TransmitMailbox=0;
u32 i;
/* 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; //禁止接收FIFO锁定模式,当接收FIFO的报文未被读出,下一个收到的报文会覆盖原有的报文
CAN_InitStructure.CAN_TXFP=DISABLE; //禁止发送FIFO优先级,优先级由报文的标识符来决定
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;//CAN硬件工作在正常模式
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//重新同步跳跃宽度1个时间单位
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;//时间段1为8个时间单位
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;//时间段2为7个时间单位
CAN_InitStructure.CAN_Prescaler=5; // (pclk1/((1+8+7)*9)) = 36Mhz/[(CAN_SJW_1tq+CAN_BS1_8tq+CAN_BS2_7tq)*(CAN_Prescaler)] = 100Kbits
// 设定了一个时间单位的长度
CAN_Init(CAN1,&CAN_InitStructure);//初始化CAN
/* transmit 1 message */
TxMessage.StdId=0x00;
TxMessage.ExtId=0x1234;
TxMessage.IDE=CAN_ID_EXT;//使用标准格式,扩展格式为:TxMessage.IDE=CAN_ID_EXT;
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)) //等待CAN总线传输数据成功
{
i++;
}
if(i==0xff)//发送超时,但发送不成功
{
GPIO_ResetBits(GPIOE,GPIO_Pin_4);
delay_nms(1000); // LED1闪烁一次,发送一个字符
GPIO_SetBits(GPIOE,GPIO_Pin_4);
delay_nms(1000);
return 0;
}
return 1;
}
接收程序:
#include "stm32f10x.h"
//#include "USART.h"
#include "delay.h"
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
vu32 ret = 0; /* for return of the interrupt handling */
ErrorStatus HSEStartUpStatus;
void CAN_Interrupt_MessageReceive(void);//中断方式
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Configure CAN pin: RX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure CAN pin: TX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE ); //重影射CAN IO脚到 PB8,PB9
GPIO_SetBits(GPIOE, GPIO_Pin_2);
GPIO_SetBits(GPIOE, GPIO_Pin_3);
GPIO_SetBits(GPIOE, GPIO_Pin_4);
GPIO_SetBits(GPIOE, GPIO_Pin_5);
}
//系统中断管理
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* enabling interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//配置系统时钟,使能各外设时钟
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA
|RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
|RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE
|RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO
|RCC_APB2Periph_SPI1, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4
|RCC_APB1Periph_USART3|RCC_APB1Periph_TIM2
, ENABLE );
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
}
//配置所有外设
void Init_All_Periph(void)
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
// USART1_Configuration();
}
int main(void)
{
Init_All_Periph();
/* CAN transmit at 100Kb/s*/
CAN_Interrupt_MessageReceive();
while(1)
{
if(ret == 1)
{
GPIO_ResetBits(GPIOE,GPIO_Pin_3);
delay_nms(1000); // LED1闪烁一次,发送一个字符
GPIO_SetBits(GPIOE ,GPIO_Pin_3);
delay_nms(1000);
ret = 0;//复位标志
}
}
}
/*******************************************************************************
* Function Name : CAN_Interrupt
* Description : Configures the CAN, transmit and receive using interrupt.
* Input : None
* Output : None
* Return : PASSED if the reception is well done, FAILED in other case
*******************************************************************************/
void CAN_Interrupt_MessageReceive(void) //CAN中断接收配置程序
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* 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_8tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=5; //波特率=1/[(1+8+7)*5/36000000]=450kHZ
CAN_Init(CAN1,&CAN_InitStructure);
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=0;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow=0x91a4;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xffff;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0xffff;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* CAN FIFO0 message pending interrupt enable */
CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
}
/*******************************************************************************
* Function Name : USB_LP_CAN_RX0_IRQHandler
* Description : This function handles USB Low Priority or CAN RX0 interrupts
* requests.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USB_LP_CAN1_RX0_IRQHandler(void)//CAN接受中断服务程序
{
CanRxMsg RxMessage;
RxMessage.StdId=0x00;
RxMessage.ExtId=0x00;
RxMessage.IDE=0;
RxMessage.DLC=1;
RxMessage.FMI=0;
RxMessage.Data[0]=0x00;
RxMessage.Data[1]=0x00;
CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);
if((RxMessage.ExtId==0x1234) && (RxMessage.IDE==CAN_ID_EXT)
&& (RxMessage.DLC==2) && ((RxMessage.Data[1]|RxMessage.Data[0]<<8)==0xDECA))
{
// GPIO_ResetBits(GPIOE,GPIO_Pin_5);
// delay_nms(1000); // LED1闪烁一次,发送一个字符
// GPIO_SetBits(GPIOE,GPIO_Pin_5);
// delay_nms(1000);
ret = 1;
}
else
{
ret = 0;
}
}
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |