標(biāo)題:
STM32使用CAN總線源碼
[打印本頁]
作者:
wxc_3021159
時間:
2018-3-21 10:08
標(biāo)題:
STM32使用CAN總線源碼
CAN總線源碼,有需要的童鞋可以下載
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
CAN.rar
(727.2 KB, 下載次數(shù): 69)
2018-3-22 01:38 上傳
點(diǎn)擊文件名下載附件
CAN
下載積分: 黑幣 -5
單片機(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(); //等待外部高速時鐘準(zhǔn)備好
//
// if(HSEStartUpStatus == SUCCESS) //外部高速時鐘已經(jīng)準(zhǔn)備好
// {
// 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); //配置系統(tǒng)時鐘= PLL時鐘
//等待PLL時鐘作為系統(tǒng)時鐘
// while(RCC_GetSYSCLKSource() != 0x08);
// }
//---------打開相應(yīng)外設(shè)時鐘--------------------
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能APB2外設(shè)的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);
//---------打開相應(yīng)外設(shè)時鐘--------------------
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能APB2外設(shè)的GPIOA的時鐘
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; //聲明一個結(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提供下載:
作者:
ryop
時間:
2020-5-9 16:57
樓主 里面根本沒有proteus的仿真
作者:
cookidog
時間:
2020-12-2 08:49
這個編譯不過啊!
作者:
cookidog
時間:
2020-12-2 08:57
關(guān)鍵是沒有proteus仿真
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1