⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 STM32 CAN通信例程
💻 C
字号:


#include "stm32f10x_lib.h"

typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;


ErrorStatus HSEStartUpStatus;

void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void CAN_ReceiveMessage(void);

void delay(vu32 nCount);

/*******************************************************************************
* Function Name  : main
* Description    : Main program
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{

#ifdef DEBUG
  debug();
#endif


  RCC_Configuration();

  NVIC_Configuration();
  
  GPIO_Configuration();

  delay(0xFFFFFF);
  
  CAN_ReceiveMessage();
  
  while (1)
  {
  }
}

void delay(vu32 nCount)
{
for(; nCount !=0;nCount--);
}

/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{
  
  RCC_DeInit();//设为缺省值
 
  RCC_HSEConfig(RCC_HSE_ON);
  
  HSEStartUpStatus = RCC_WaitForHSEStartUp();

  if(HSEStartUpStatus == SUCCESS)
  {
  
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    
    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1); 
  
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1); 

    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);

    /* Select HSE as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);

    /* Wait till HSE is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x04)
    {
    }
  }
  
  /* GPIOA and GPIOC and GPIOD clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |RCC_APB2Periph_AFIO, ENABLE);

  /* CAN Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  // For STM3210B-LK1 use PC.04 -PC.07
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  /* Configure CAN pin: RX */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
  
  /* Configure CAN pin: TX */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //
  GPIO_Init(GPIOD, &GPIO_InitStructure);

  GPIO_PinRemapConfig(GPIO_Remap2_CAN, ENABLE); 
}

/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures the NVIC and Vector Table base address.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

#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 ReceiveMessage */
  NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN_RX0_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure); 
}


 void CAN_ReceiveMessage(void)
 {

  CAN_InitTypeDef        CAN_InitStructure;
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;
  CanRxMsg RxMessage;
 
  CAN_DeInit();			  						//将外设重设为缺省值
  CAN_StructInit(&CAN_InitStructure);  			//将其中每个参数按缺省值写入

  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锁定模式
  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;			//时间单位长度为5
  CAN_Init(&CAN_InitStructure);					//根据指定参数初始化CAN
  
  CAN_FilterInitStructure.CAN_FilterNumber=0;		//初始化过滤器0
  CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;	// 过滤器初始化为标识符屏蔽模式
  CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;  //一个32位过滤器
  CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;					// 过滤器标识符,32位位宽时为高字段,16位时为第一个
  CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;		  //  过滤器标识符,32位位宽时为低字段,16位时为第二个
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;		//过滤器屏蔽标识符或过滤器标识符,32位位宽时为高字段,16位时为第一个
  CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;		  //过滤器屏蔽标识符或过滤器标识符 ,16位时为第二个
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;			//
  CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;		 //使能过滤器
  CAN_FilterInit(&CAN_FilterInitStructure);	//根据CAN_FilterInitstruct中指定参数初始化CAN寄存器

  CAN_ITConfig(CAN_IT_FMP0, ENABLE);

  RxMessage.StdId=0x00;	//接收返回标识符为0x00
  RxMessage.IDE=CAN_ID_STD;//接收使用标准标识符
  RxMessage.DLC=0;		   //接收消息传输帧长度为0
  RxMessage.Data[0]=0x00;  //接收消息数据0为0x00
  RxMessage.Data[1]=0x00;	//接收消息数据为0x00
  CAN_Receive(CAN_FIFO0, &RxMessage);  //CAN_FIFO0接收数据

  if ((RxMessage.StdId!=0x11)&&(RxMessage.IDE!=CAN_ID_STD)&& (RxMessage.DLC!=2) 			

  && ((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xCAFE))   
  {
  	LD1_ON;
  }
  else
  {
  	LD2_ON;
  }
  CAN_ITConfig(CAN_IT_FMP0,DISABLE);   // 消息挂号中断屏蔽失能
}


#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif
/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -