找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2933|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

STM32使用CAN總線源碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:294892 發(fā)表于 2018-3-21 10:08 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
CAN總線源碼,有需要的童鞋可以下載

仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
CAN.rar (727.2 KB, 下載次數(shù): 69)


單片機(jī)源程序如下:
  1. /*Include---------------------------*/
  2. #include"stm32f10x_lib.h"                //包含所有的頭文件
  3. #include<stdio.h>

  4. //----------------函數(shù)聲明--------------------
  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; //定義外部高速晶體啟動(dòng)狀態(tài)枚舉變量

  70. //     RCC_DeInit(); //復(fù)位RCC外部設(shè)備寄存器到默認(rèn)值

  71. //     RCC_HSEConfig(RCC_HSE_ON); //打開(kāi)外部高速晶振

  72. //     HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部高速時(shí)鐘準(zhǔn)備好
  73. //
  74. //     if(HSEStartUpStatus == SUCCESS) //外部高速時(shí)鐘已經(jīng)準(zhǔn)備好

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

  78.               
  79. // /
  80. //             RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)時(shí)鐘==SYSCLK

  81. //             RCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(PCLK2)鐘==AHB時(shí)鐘

  82. //            RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(PCLK1)鐘==AHB1/2時(shí)鐘

  83. //            RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  84.   //           RCC_PLLCmd(ENABLE); //使能PLL時(shí)鐘

  85.       

  86.              //等待PLL時(shí)鐘就緒

  87.   //           while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  88. //
  89. //             RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系統(tǒng)時(shí)鐘= PLL時(shí)鐘

  90.              //等待PLL時(shí)鐘作為系統(tǒng)時(shí)鐘

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

  93.                         //---------打開(kāi)相應(yīng)外設(shè)時(shí)鐘--------------------
  94.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);        //使能APB2外設(shè)的GPIOA的時(shí)鐘       
  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);        //使能外部的高速時(shí)鐘
  104.         while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);        //等待外部高速時(shí)鐘使能就緒
  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.         //---------打開(kāi)相應(yīng)外設(shè)時(shí)鐘--------------------
  115.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);        //使能APB2外設(shè)的GPIOA的時(shí)鐘       
  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外設(shè)
  122. * Input          : None
  123. * Output         : None
  124. * Return         : None
  125. *******************************************************************************/
  126. void GPIO_Configuration(void)
  127. {
  128.         GPIO_InitTypeDef        GPIO_InitStructure;                //聲明一個(gè)結(jié)構(gòu)體變量
  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. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:386783 發(fā)表于 2020-5-9 16:57 | 只看該作者
樓主 里面根本沒(méi)有proteus的仿真
回復(fù)

使用道具 舉報(bào)

板凳
ID:826991 發(fā)表于 2020-12-2 08:49 | 只看該作者
這個(gè)編譯不過(guò)!
回復(fù)

使用道具 舉報(bào)

地板
ID:826991 發(fā)表于 2020-12-2 08:57 | 只看該作者
關(guān)鍵是沒(méi)有proteus仿真
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表