标题: 有关双CAN总线STM32F105系列例程源码 [打印本页]

作者: FENYUN518    时间: 2018-8-21 10:43
标题: 有关双CAN总线STM32F105系列例程源码
这是我做的双CAN总线例程,试验通过。

单片机源程序如下:
  1. /*
  2.         CAN1
  3.         USART1
  4.     CAN波特率、收发器 配置见CAN.h
  5.         stm32f10x.h中修改了外部晶振值8MHz
  6. */

  7. /* Includes ------------------------------------------------------------------*/
  8. #include "stm32f10x.h"
  9. #include "stm32f10x_rcc.h"
  10. #include "stm32f10x_flash.h"
  11. #include "stm32f10x_can.h"
  12. #include "usart.h"
  13. #include "CAN.h"
  14. #include <stdio.h>

  15. void RCC_Configuration(void);
  16. void LED_Config(void);
  17. void Delay(__IO uint32_t nCount);


  18. CanTxMsg TxMsg1={0xAB,0,CAN_ID_STD,CAN_RTR_DATA,8,{0xAB,0,0,0,0,0,0,0}};
  19. CanTxMsg TxMsg2={0xCD,0,CAN_ID_STD,CAN_RTR_DATA,8,{0xCD,0,0,0,0,0,0,0}};


  20. int main(void)
  21. {
  22.                           
  23.   /* System Clocks Configuration **********************************************/
  24.   RCC_Configuration();   
  25.   LED_Config();

  26.   // 串口配置                                                                                                               
  27.   USART_Configuration();

  28.   USART_STR(USART2,"++++++++++++++++++++++++\r\n");
  29.   USART_STR(USART2,"  CAN Study Board\r\n");
  30.   USART_STR(USART2,"++++++++++++++++++++++++\r\n");       
  31.                                                                                                                   
  32.                                                                                                   
  33.   // CAN1 配置
  34.   CAN1_Config(SET_CAN_SJW,SET_CAN_BS1,SET_CAN_BS2,SET_CAN_PRES);  
  35.                                                                                                               
  36.   Delay(2000);
  37.                                                          
  38.   // CAN2 配置
  39.   CAN2_Config(SET_CAN_SJW,SET_CAN_BS1,SET_CAN_BS2,SET_CAN_PRES);                                                                                          
  40.                                    
  41.                                                   
  42.   while (1)
  43.   {     
  44.           GPIO_ResetBits(GPIOC,GPIO_Pin_0);
  45.           GPIO_SetBits(GPIOC,GPIO_Pin_1);
  46.           GPIO_SetBits(GPIOC,GPIO_Pin_14);
  47.           GPIO_SetBits(GPIOC,GPIO_Pin_15);
  48.       Delay(5000);
  49.           Delay(5000);          
  50.           GPIO_SetBits(GPIOC,GPIO_Pin_0);
  51.           GPIO_ResetBits(GPIOC,GPIO_Pin_1);
  52.           GPIO_SetBits(GPIOC,GPIO_Pin_14);
  53.           GPIO_SetBits(GPIOC,GPIO_Pin_15);

  54.           CAN_SendData(CAN1,&TxMsg1);

  55.           Delay(5000);
  56.           Delay(5000);          
  57.           GPIO_SetBits(GPIOC,GPIO_Pin_0);
  58.           GPIO_SetBits(GPIOC,GPIO_Pin_1);
  59.           GPIO_ResetBits(GPIOC,GPIO_Pin_14);
  60.           GPIO_SetBits(GPIOC,GPIO_Pin_15);
  61.           Delay(5000);
  62.           Delay(5000);          
  63.           GPIO_SetBits(GPIOC,GPIO_Pin_0);
  64.           GPIO_SetBits(GPIOC,GPIO_Pin_1);
  65.           GPIO_SetBits(GPIOC,GPIO_Pin_14);
  66.           GPIO_ResetBits(GPIOC,GPIO_Pin_15);
  67.           Delay(5000);
  68.           Delay(5000);                                                                                       
  69.        
  70.           CAN_SendData(CAN2,&TxMsg2);                                                                                                                                     
  71.   }
  72. }


  73. void RCC_Configuration(void)
  74. {   
  75.   /* Setup the microcontroller system. Initialize the Embedded Flash Interface,  
  76.      initialize the PLL and update the SystemFrequency variable. */
  77.   SystemInit();
  78.                                                                                            
  79. }


  80. void Delay(__IO uint32_t nCount)
  81. {
  82.     uint8_t x;
  83.     for(; nCount != 0; nCount--)
  84.             for(x=0;x<100;x++);
  85. }

  86. void LED_Config(void)
  87. {
  88.   GPIO_InitTypeDef GPIO_InitStructure;

  89.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  90.   
  91.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_14|GPIO_Pin_15;                                  
  92.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  93.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  94.   GPIO_Init(GPIOC, &GPIO_InitStructure);                                         
  95. }


  96. #ifdef  USE_FULL_ASSERT

  97. /**
  98.   * @brief  Reports the name of the source file and the source line number
  99.   *   where the assert_param error has occurred.
  100.   * @param  file: pointer to the source file name
  101.   * @param  line: assert_param error line source number
  102. ……………………

  103. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
STM32_CAN500K USART_LED.rar (458.73 KB, 下载次数: 188)




作者: oliverlau2k11    时间: 2018-8-23 10:34
好东西,谢谢分享
作者: askloser    时间: 2019-2-13 10:03
謝謝分享
作者: fengbin    时间: 2019-5-5 16:53
可以发送下吗
905036191@qq.com
谢谢
作者: h070230053    时间: 2019-7-29 15:49

謝謝分享
作者: h070230053    时间: 2019-7-29 16:34
初学者,楼主可以分享吗
457268908@qq.com
谢谢
作者: jflbr    时间: 2019-7-29 21:01
不错不错   .
作者: 辅导费分    时间: 2020-3-26 22:40
好东西,谢谢分享
作者: jjwangxu2008    时间: 2020-4-10 09:04
好东西,谢谢分享
作者: 李天泽    时间: 2020-4-10 10:19
好东西




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1