找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32使用CAN总线源码

[复制链接]
跳转到指定楼层
楼主
ID:294892 发表于 2018-3-21 10:08 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
CAN总线源码,有需要的童鞋可以下载

仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
CAN.rar (727.2 KB, 下载次数: 69)


单片机源程序如下:
  1. /*Include---------------------------*/
  2. #include"stm32f10x_lib.h"                //包含所有的头文件
  3. #include<stdio.h>

  4. //----------------函数声明--------------------
  5. void Delay_MS(u16 dly);
  6. void RCC_Configuration(void);
  7. void GPIO_Configuration(void);
  8. void NVIC_Configuration(void);

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

  10. TestStatus RxStatus;

  11. extern TestStatus CAN_Interrupt_Configuration(void);


  12. /*******************************************************************************
  13. * Function Name  : main
  14. * Description    : Main program.
  15. * Input          : None
  16. * Output         : None
  17. * Return         : None
  18. *******************************************************************************/
  19. int main(void)
  20. {
  21.         #ifdef DEBUG
  22.         debug();
  23.         #endif
  24.         //------------初始化------------
  25.         RCC_Configuration();
  26.         NVIC_Configuration();
  27.         GPIO_Configuration();
  28.         GPIO_SetBits(GPIOA, GPIO_Pin_3);
  29.         RxStatus = CAN_Interrupt_Configuration();
  30.         while(1)
  31.         {
  32.                  if(RxStatus == 1)
  33.                  {
  34.                         GPIO_SetBits(GPIOA, GPIO_Pin_3);
  35.                         Delay_MS(1000);
  36.                         GPIO_ResetBits(GPIOA, GPIO_Pin_3);
  37.                         Delay_MS(1000);       
  38.                   }
  39.                   else
  40.                   {
  41.                           GPIO_SetBits(GPIOA, GPIO_Pin_3);
  42.                         Delay_MS(1000);       
  43.                   }
  44.         }
  45.                
  46. }

  47. /*******************************************************************************
  48. * Function Name  : Delay_Ms
  49. * Description    : delay 1 ms.
  50. * Input          : dly (ms)
  51. * Output         : None
  52. * Return         : None
  53. *******************************************************************************/
  54. void Delay_MS(u16 dly)
  55. {
  56.         u16 i,j;
  57.         for(i=0;i<dly;i++)
  58.                 for(j=1000;j>0;j--);
  59. }

  60. /*******************************************************************************
  61. * Function Name  : RCC_Configuration
  62. * Description    : Configures the different system clocks.
  63. * Input          : None
  64. * Output         : None
  65. * Return         : None
  66. *******************************************************************************/
  67. void RCC1_Configuration(void)
  68. {
  69. //        ErrorStatus HSEStartUpStatus; //定义外部高速晶体启动状态枚举变量

  70. //     RCC_DeInit(); //复位RCC外部设备寄存器到默认值

  71. //     RCC_HSEConfig(RCC_HSE_ON); //打开外部高速晶振

  72. //     HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部高速时钟准备好
  73. //
  74. //     if(HSEStartUpStatus == SUCCESS) //外部高速时钟已经准备好

  75.   //    {
  76.   //           FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);  
  77.   //           FLASH_SetLatency(FLASH_Latency_2);

  78.               
  79. // /
  80. //             RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)时钟==SYSCLK

  81. //             RCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(PCLK2)钟==AHB时钟

  82. //            RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(PCLK1)钟==AHB1/2时钟

  83. //            RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  84.   //           RCC_PLLCmd(ENABLE); //使能PLL时钟

  85.       

  86.              //等待PLL时钟就绪

  87.   //           while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  88. //
  89. //             RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系统时钟= PLL时钟

  90.              //等待PLL时钟作为系统时钟

  91. //            while(RCC_GetSYSCLKSource() != 0x08);
  92. //                }

  93.                         //---------打开相应外设时钟--------------------
  94.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);        //使能APB2外设的GPIOA的时钟       
  95.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB, ENABLE);
  96.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);       

  97.        
  98. }
  99. void RCC_Configuration(void)
  100. {
  101.         //----------使用外部RC晶振-----------
  102.         RCC_DeInit();                        //初始化为缺省值
  103.         RCC_HSEConfig(RCC_HSE_ON);        //使能外部的高速时钟
  104.         while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);        //等待外部高速时钟使能就绪
  105.        
  106.         FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);        //Enable Prefetch Buffer
  107.         FLASH_SetLatency(FLASH_Latency_2);                //Flash 2 wait state

  108.     RCC_HCLKConfig(RCC_SYSCLK_Div1);
  109.     RCC_PCLK2Config(RCC_HCLK_Div1);
  110.     RCC_PCLK1Config(RCC_HCLK_Div1);
  111.     RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
  112.     while(RCC_GetSYSCLKSource() != 0x04);
  113.        
  114.         //---------打开相应外设时钟--------------------
  115.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);        //使能APB2外设的GPIOA的时钟       
  116.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB, ENABLE);
  117.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);         
  118. }

  119. /*******************************************************************************
  120. * Function Name  : GPIO_Configuration
  121. * Description    : 初始化GPIO外设
  122. * Input          : None
  123. * Output         : None
  124. * Return         : None
  125. *******************************************************************************/
  126. void GPIO_Configuration(void)
  127. {
  128.         GPIO_InitTypeDef        GPIO_InitStructure;                //声明一个结构体变量
  129.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;         //选择PA.3
  130.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         //管脚频率为50MHZ
  131.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;         //输出模式为推挽输出
  132.         GPIO_Init(GPIOA,&GPIO_InitStructure);                                 //初始化GPIOA寄存器               
  133. }
  134. ……………………

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

所有资料51hei提供下载:



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

使用道具 举报

沙发
ID:386783 发表于 2020-5-9 16:57 | 只看该作者
楼主 里面根本没有proteus的仿真
回复

使用道具 举报

板凳
ID:826991 发表于 2020-12-2 08:49 | 只看该作者
这个编译不过啊!
回复

使用道具 举报

地板
ID:826991 发表于 2020-12-2 08:57 | 只看该作者
关键是没有proteus仿真
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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