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

📄 main.c

📁 基于stm32f103rb微处理器的can总线发送程序
💻 C
字号:
/*******************************************************************************
* 程序说明:can总线测试,工作在正常模式,接受数据
* 作者:xiaocai_cqu
* 版本:v1.0
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include <stm32f10x_lib.h>

/* Local includes ------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;

/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
vu32 ret,ret1; /* for return of the interrupt handling */
volatile TestStatus TestRx;
ErrorStatus HSEStartUpStatus;
CanRxMsg RxMessage;
/* Private functions ---------------------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
extern void lcd_init     (void);
extern void lcd_clear    (void);
extern void lcd_putchar  (char c);
extern void set_cursor   (int column, int line);
extern void lcd_print     (char *string);
TestStatus CAN_Interrupt(void);	

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

#ifdef DEBUG
  debug();
#endif

  /* System Clocks Configuration */
  RCC_Configuration();

  /* NVIC Configuration */
  NVIC_Configuration();
  
  /* GPIO ports pins Configuration */
  GPIO_Configuration();
  
//  /* CAN transmit at 100Kb/s and receive by polling in loopback mode*/
//  TestRx = CAN_Polling();//发送标准数据帧
//
//  if (TestRx ==!FAILED)  //如果发送标准数据帧成功,则点亮led3
//  {
//    /* Turn on led connected to PB.10 pin (LD3) */
//    GPIO_SetBits(GPIOB, GPIO_Pin_10);
//  }
  /* CAN transmit at 500Kb/s and receive by interrupt in loopback mode*/
  TestRx =CAN_Interrupt();//发送扩展数据帧
      
	if (TestRx ==!FAILED)    //如果中断发送扩展数据帧成功,点亮led4
    {
     //int i=0;
	 /* Turn on led connected to PB.11 pin (LD4) */
     GPIO_SetBits(GPIOB, GPIO_Pin_11);
	 lcd_init  ();
  	 lcd_clear ();
  	 lcd_print ("Receive succeed!");
  	 //string str;
	 //str=RxMessage.Data; 
//	 set_cursor(0, 1);
//	 lcd_putchar((char)(RxMessage.Data[0]));
//   	 for(i=0;i<8;i++)
//	 {
//	  set_cursor(i+1,1);
//	  lcd_putchar((char)(RxMessage.Data[i])); 
//	 }
    }   
  while(1)
  {	
  }
}

/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{
//  /* RCC system reset(for debug purpose) */
//  RCC_DeInit();
//
//  /* Enable HSE */
//  RCC_HSEConfig(RCC_HSE_ON);
//
//  /* Wait till HSE is ready */
//  HSEStartUpStatus = RCC_WaitForHSEStartUp();
//
//  if(HSEStartUpStatus == SUCCESS)
//  {
//    /* HCLK = SYSCLK */
//    RCC_HCLKConfig(RCC_SYSCLK_Div1); 
//  
//    /* PCLK2 = HCLK */
//    RCC_PCLK2Config(RCC_HCLK_Div1); 
//
//    /* PCLK1 = HCLK/2 */
//    RCC_PCLK1Config(RCC_HCLK_Div2);
//
//    /* Enable Prefetch Buffer */
//    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
//
//    /* Select HSE as system clock source */
//    RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
//
//    /* Wait till HSE is used as system clock source */
//    while(RCC_GetSYSCLKSource() != 0x04)
//    {
//    }
//  }
//  
//  /* GPIOB clock enable */
//  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);	//|RCC_APB2Periph_AFIO
//
//  /* CAN Periph clock enable */
//  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);

  /* RCC system reset(for debug purpose) */
  RCC_DeInit();

  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);/****************** External 8 MHz ************************************/

  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();

  if (HSEStartUpStatus == SUCCESS)
  {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);

    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);/****************** HCLK 72 MHz ************************************/

    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);/****************** PCLK2 72 MHz ************************************/

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

    /* ADCCLK = PCLK2/6 */
    RCC_ADCCLKConfig(RCC_PCLK2_Div6);/****************** ADC 12 MHz ************************************/


    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);/****************** PLL 8*9=72 MHz *************/

    /* Enable PLL */
    RCC_PLLCmd(ENABLE);

    /* Wait till PLL is ready */
    while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {}

    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);/*************SYSCLK 72 MHz*******************************/

    /* Wait till PLL is used as system clock source */
    while (RCC_GetSYSCLKSource() != 0x08)
    {}
	//使能时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | 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;
//  
//  //端口重映射到PB8(Rx)及PB9(Tx)
//  GPIO_PinRemapConfig(GPIO_Remap1_CAN,ENABLE);
//  
//  /* Configure PB.10 and PB.11 as Output push-pull 用作led灯*/
//  GPIO_InitStructure.GPIO_Pin =GPIO_Pin_10 | GPIO_Pin_11;
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//  GPIO_Init(GPIOB, &GPIO_InitStructure);
//  
//  
//  /* Configure CAN pin: RX-PB8*/
//  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//  GPIO_Init(GPIOB, &GPIO_InitStructure);
//  
//  /* Configure CAN pin: TX-PB9*/ 
//  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//  GPIO_Init(GPIOB, &GPIO_InitStructure);  
//  //使能时钟
//  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO , ENABLE);

  GPIO_InitTypeDef GPIO_InitStructure;
  
  /* Configure PB.10 and PB.11 as Output push-pull 用作led灯*/
  GPIO_InitStructure.GPIO_Pin =GPIO_Pin_10 | GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  
  /* Configure CAN pin: RX-PB8*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  /* Configure CAN pin: TX-PB9*/ 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);  
  
  //端口重映射到PB8(Rx)及PB9(Tx)
  GPIO_PinRemapConfig(GPIO_Remap1_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 interrupt */
//  NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN_RX0_IRQChannel;//can发送终端通道
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//  NVIC_Init(&NVIC_InitStructure);
}

/*******************************************************************************
* Function Name  : CAN_Interrupt
* Description    : Configures the CAN and transmit,and receive by interruption
* Input          : None
* Output         : None
* Return         : PASSED if the reception is well done, FAILED in other case
*******************************************************************************/
TestStatus CAN_Interrupt(void)
{
  CAN_InitTypeDef        CAN_InitStructure;
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;
  
    
  /* CAN register init */
  CAN_DeInit();
  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_10tq;
  CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
  CAN_InitStructure.CAN_Prescaler=4; //72MHz/2=36MHz=PCLK1 / 4 => 9000KHz / (1+10+7) => 500KHz
  CAN_Init(&CAN_InitStructure);

  /* CAN filter init */
  CAN_FilterInitStructure.CAN_FilterNumber=1;//选择将要被初始化的过滤器,其值为0-13
  CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//选择需要被初始化的模式:标识/屏蔽模式,或者标识列表模式(CAN_FilterMode_IdList)
  CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;//配置过滤器规模:一个32位或者2个16位
  CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;//选择过滤器的标识号(对应一个32位配置的高16位 MSBs)
  CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;//选择过滤器的标识号(对应一个32位配置的低16位 LSBs)
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//根据模式用来选择过滤器屏蔽号或者标识号(对应一个32位配置的高16位 MSBs)
  CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;//根据模式用来选择过滤器屏蔽号或者标识号(对应一个32位配置的低16位 LSBs)
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;//分配FIFO 0给过滤器1
  CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//can过滤器激活
  CAN_FilterInit(&CAN_FilterInitStructure);//配置滤波器

  //CAN_WakeUp();
  //CAN_ITConfig(CAN_IT_FMP0, ENABLE);//使能can FIFO 0报文挂起中断(属于接收中断的一种,详见编程指导P271页)

//  /* transmit 1 message */
//  TxMessage.StdId=0x0000;
//  TxMessage.ExtId=0x0000;
//  TxMessage.IDE=CAN_ID_STD;//使用标准标识号
//  TxMessage.RTR=CAN_RTR_DATA;
//  TxMessage.DLC=8;
//  TxMessage.Data[0]=0x01;
//  TxMessage.Data[1]=0x02;
//  TxMessage.Data[2]=0x03;
//  TxMessage.Data[3]=0x04;
//  TxMessage.Data[4]=0x05;
//  TxMessage.Data[5]=0x06;
//  TxMessage.Data[6]=0x07;
//  TxMessage.Data[7]=0x08;
//  CAN_Transmit(&TxMessage);
//
  /* initialize the value that will be returned */
 // ret=0xFF;
       
  /* receive message with interrupt handling */
//  while((ret==0xFF))//当ret不等于255(表明中断已经放生,ret的值在中断服务函数中得以改变),退出循环
//    {
//	}
    /* receive */
  RxMessage.StdId=0x0000;
  RxMessage.ExtId=0x0000;
  RxMessage.IDE=0;//标准标志符
  RxMessage.RTR=0;//远程帧
  RxMessage.DLC=8;//发送数据长度
  RxMessage.FMI=0;//过滤器匹配序列号
  RxMessage.Data[0]=0x00;//接受字节0
  RxMessage.Data[1]=0x00;//接受字节1
  RxMessage.Data[2]=0x00;//接受字节2
  RxMessage.Data[3]=0x00;//接受字节3
  RxMessage.Data[4]=0x00;//接受字节4
  RxMessage.Data[5]=0x00;//接受字节5
  RxMessage.Data[6]=0x00;//接受字节6
  RxMessage.Data[7]=0x00;//接受字节7

  while (!CAN_MessagePending(CAN_FIFO0));	 //等待FIFO 0中接收到有报文
  CAN_Receive(CAN_FIFO0, &RxMessage);


  /* disable interrupt handling */
 //CAN_ITConfig(CAN_IT_RQCP0, DISABLE);//发送失败,关闭中断
 

  return (TestStatus)1;//返回ret,当ret为非零时,表示发送成功;若为0则表示发送失败
}

#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert 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 + -