CAN总线源码,有需要的童鞋可以下载
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
CAN.rar
(727.2 KB, 下载次数: 69)
单片机源程序如下:
- /*Include---------------------------*/
- #include"stm32f10x_lib.h" //包含所有的头文件
- #include<stdio.h>
- //----------------函数声明--------------------
- 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; //定义外部高速晶体启动状态枚举变量
- // RCC_DeInit(); //复位RCC外部设备寄存器到默认值
- // RCC_HSEConfig(RCC_HSE_ON); //打开外部高速晶振
- // HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部高速时钟准备好
- //
- // if(HSEStartUpStatus == SUCCESS) //外部高速时钟已经准备好
- // {
- // FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- // FLASH_SetLatency(FLASH_Latency_2);
-
- // /
- // RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)时钟==SYSCLK
- // RCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(PCLK2)钟==AHB时钟
- // RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(PCLK1)钟==AHB1/2时钟
- // RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- // RCC_PLLCmd(ENABLE); //使能PLL时钟
-
- //等待PLL时钟就绪
- // while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- //
- // RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系统时钟= PLL时钟
- //等待PLL时钟作为系统时钟
- // while(RCC_GetSYSCLKSource() != 0x08);
- // }
- //---------打开相应外设时钟--------------------
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能APB2外设的GPIOA的时钟
- 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); //使能外部的高速时钟
- while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); //等待外部高速时钟使能就绪
-
- 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);
-
- //---------打开相应外设时钟--------------------
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能APB2外设的GPIOA的时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);
- }
- /*******************************************************************************
- * Function Name : GPIO_Configuration
- * Description : 初始化GPIO外设
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure; //声明一个结构体变量
- 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黑下载附件…………
复制代码
所有资料51hei提供下载:
|