|
CAN總線源碼,有需要的童鞋可以下載
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
CAN.rar
(727.2 KB, 下載次數(shù): 69)
2018-3-22 01:38 上傳
點(diǎn)擊文件名下載附件
CAN
單片機(jī)源程序如下:
- /*Include---------------------------*/
- #include"stm32f10x_lib.h" //包含所有的頭文件
- #include<stdio.h>
- //----------------函數(shù)聲明--------------------
- void Delay_MS(u16 dly);
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void NVIC_Configuration(void);
- typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
- TestStatus RxStatus;
- extern TestStatus CAN_Interrupt_Configuration(void);
- /*******************************************************************************
- * Function Name : main
- * Description : Main program.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- int main(void)
- {
- #ifdef DEBUG
- debug();
- #endif
- //------------初始化------------
- RCC_Configuration();
- NVIC_Configuration();
- GPIO_Configuration();
- GPIO_SetBits(GPIOA, GPIO_Pin_3);
- RxStatus = CAN_Interrupt_Configuration();
- while(1)
- {
- if(RxStatus == 1)
- {
- GPIO_SetBits(GPIOA, GPIO_Pin_3);
- Delay_MS(1000);
- GPIO_ResetBits(GPIOA, GPIO_Pin_3);
- Delay_MS(1000);
- }
- else
- {
- GPIO_SetBits(GPIOA, GPIO_Pin_3);
- Delay_MS(1000);
- }
- }
-
- }
- /*******************************************************************************
- * Function Name : Delay_Ms
- * Description : delay 1 ms.
- * Input : dly (ms)
- * Output : None
- * Return : None
- *******************************************************************************/
- void Delay_MS(u16 dly)
- {
- u16 i,j;
- for(i=0;i<dly;i++)
- for(j=1000;j>0;j--);
- }
- /*******************************************************************************
- * Function Name : RCC_Configuration
- * Description : Configures the different system clocks.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void RCC1_Configuration(void)
- {
- // ErrorStatus HSEStartUpStatus; //定義外部高速晶體啟動狀態(tài)枚舉變量
- // RCC_DeInit(); //復(fù)位RCC外部設(shè)備寄存器到默認(rèn)值
- // RCC_HSEConfig(RCC_HSE_ON); //打開外部高速晶振
- // HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部高速時(shí)鐘準(zhǔn)備好
- //
- // if(HSEStartUpStatus == SUCCESS) //外部高速時(shí)鐘已經(jīng)準(zhǔn)備好
- // {
- // FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- // FLASH_SetLatency(FLASH_Latency_2);
-
- // /
- // RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)時(shí)鐘==SYSCLK
- // RCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(PCLK2)鐘==AHB時(shí)鐘
- // RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(PCLK1)鐘==AHB1/2時(shí)鐘
- // RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- // RCC_PLLCmd(ENABLE); //使能PLL時(shí)鐘
-
- //等待PLL時(shí)鐘就緒
- // while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- //
- // RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系統(tǒng)時(shí)鐘= PLL時(shí)鐘
- //等待PLL時(shí)鐘作為系統(tǒng)時(shí)鐘
- // while(RCC_GetSYSCLKSource() != 0x08);
- // }
- //---------打開相應(yīng)外設(shè)時(shí)鐘--------------------
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能APB2外設(shè)的GPIOA的時(shí)鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);
-
- }
- void RCC_Configuration(void)
- {
- //----------使用外部RC晶振-----------
- RCC_DeInit(); //初始化為缺省值
- RCC_HSEConfig(RCC_HSE_ON); //使能外部的高速時(shí)鐘
- while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); //等待外部高速時(shí)鐘使能就緒
-
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //Enable Prefetch Buffer
- FLASH_SetLatency(FLASH_Latency_2); //Flash 2 wait state
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
- RCC_PCLK2Config(RCC_HCLK_Div1);
- RCC_PCLK1Config(RCC_HCLK_Div1);
- RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
- while(RCC_GetSYSCLKSource() != 0x04);
-
- //---------打開相應(yīng)外設(shè)時(shí)鐘--------------------
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能APB2外設(shè)的GPIOA的時(shí)鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);
- }
- /*******************************************************************************
- * Function Name : GPIO_Configuration
- * Description : 初始化GPIO外設(shè)
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure; //聲明一個(gè)結(jié)構(gòu)體變量
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //選擇PA.3
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //管腳頻率為50MHZ
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //輸出模式為推挽輸出
- GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化GPIOA寄存器
- }
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
|
|