找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4482|回复: 9
打印 上一主题 下一主题
收起左侧

有关双CAN总线STM32F105系列例程源码

  [复制链接]
跳转到指定楼层
楼主
ID:147641 发表于 2018-8-21 10:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这是我做的双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, 下载次数: 184)



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:243168 发表于 2018-8-23 10:34 | 只看该作者
好东西,谢谢分享
回复

使用道具 举报

板凳
ID:446581 发表于 2019-2-13 10:03 | 只看该作者
謝謝分享
回复

使用道具 举报

地板
ID:47548 发表于 2019-5-5 16:53 | 只看该作者
可以发送下吗
905036191@qq.com
谢谢
回复

使用道具 举报

5#
ID:158966 发表于 2019-7-29 15:49 | 只看该作者

謝謝分享
回复

使用道具 举报

6#
ID:158966 发表于 2019-7-29 16:34 | 只看该作者
初学者,楼主可以分享吗
457268908@qq.com
谢谢
回复

使用道具 举报

7#
ID:118473 发表于 2019-7-29 21:01 | 只看该作者
不错不错   .
回复

使用道具 举报

8#
ID:231807 发表于 2020-3-26 22:40 | 只看该作者
好东西,谢谢分享
回复

使用道具 举报

9#
ID:86450 发表于 2020-4-10 09:04 | 只看该作者
好东西,谢谢分享
回复

使用道具 举报

10#
ID:703927 发表于 2020-4-10 10:19 | 只看该作者
好东西
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表