#include "stm32f10x.h"
/* RCC时钟配置 */
void RCC_config(void)
{
ErrorStatus HSEStartUpStatus;
/* RCC寄存器设置为默认配置 */
RCC_DeInit();
/* 打开外部高速时钟 */
RCC_HSEConfig(RCC_HSE_ON);
/* 等待外部高速时钟稳定 */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* 设置HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* 设置PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* 设置PCLK1 = HCLK / 2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
// /* 设置FLASH代码延时 */
// FLASH_SetLatency(FLASH_Latency_2);
// /* 使能预取址缓存 */
// FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* 设置PLL时钟源为HSE倍频9 72MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* 使能PLL */
RCC_PLLCmd(ENABLE);
/* 等待PLL稳定 */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/* 设置PLL为系统时钟源 */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* 等待系统时钟源切换到PLL */
while(RCC_GetSYSCLKSource() != 0x08);
}
}
/* GPIO配置 */
void GPIO_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 时钟配置 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
/* RX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* TX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/* 中断嵌套控制器配置 */
void NVIC_config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 选择中断分组2 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* 设置CAN接收中断 */
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/* CAN总线配置 */
void CAN_config(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
/* CAN1默认参数 */
CAN_DeInit(CAN1);
/* 将结构体填入默认参数 */
CAN_StructInit(&CAN_InitStructure);
/* 关闭时间触发模式 */
CAN_InitStructure.CAN_TTCM = DISABLE;
/* 关闭自动离线管理 */
CAN_InitStructure.CAN_ABOM = ENABLE;
/* 关闭自动唤醒 */
CAN_InitStructure.CAN_AWUM = ENABLE;
/* 自动重传 */
CAN_InitStructure.CAN_NART = DISABLE;
/* 禁止FIFO溢出时覆盖原报文 */
CAN_InitStructure.CAN_RFLM = DISABLE;
/* 关闭优先级取决于ID */
CAN_InitStructure.CAN_TXFP = DISABLE;
/* 正常模式 */
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
/* 设置波特率 */
CAN_InitStructure.CAN_SJW = CAN_SJW_2tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;
CAN_InitStructure.CAN_Prescaler = 4;
/* 初始化CAN总线 */
CAN_Init(CAN1, &CAN_InitStructure);
/* 过滤器配置 */
CAN_FilterInitStructure.CAN_FilterNumber = 0;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0020;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0007;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0020;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* 接收挂起中断 */
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
/* 毫秒延时函数 */
void delay_ms(uint16_t time)
{
uint16_t i = 0;
while(time--)
{
i = 12000;
while(i--);
}
}
CanTxMsg TxMessage;
/* 主函数 */
int main(void)
{
uint8_t TransmitMailbox = 0;
/* RCC时钟配置 */
RCC_config();
/* GPIO配置 */
GPIO_config();
/* CAN总线配置 */
CAN_config();
/* 中断嵌套控制器配置 */
NVIC_config();
while(1)
{
TxMessage.StdId = 1;
TxMessage.ExtId = 0;
TxMessage.IDE = CAN_ID_STD;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.DLC = 8;
TxMessage.Data[0] = 0x11;
TxMessage.Data[1] = 0x22;
TxMessage.Data[2] = 0x33;
TxMessage.Data[3] = 0x44;
TxMessage.Data[4] = 0x55;
TxMessage.Data[5] = 0x66;
TxMessage.Data[6] = 0x77;
TxMessage.Data[7] = 0x88;
TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);
while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK));
delay_ms(1000);
}
}
void USB_LP_CAN1_RX0_IRQHandler(void)
{
CanRxMsg RxMessage;
volatile CanRxMsg Message;
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
Message.StdId = RxMessage.StdId;
Message.ExtId = RxMessage.ExtId;
Message.IDE = RxMessage.IDE;
Message.RTR = RxMessage.RTR;
Message.DLC = RxMessage.DLC;
Message.Data[0] = RxMessage.Data[0];
Message.Data[1] = RxMessage.Data[1];
Message.Data[2] = RxMessage.Data[2];
Message.Data[3] = RxMessage.Data[3];
Message.Data[4] = RxMessage.Data[4];
Message.Data[5] = RxMessage.Data[5];
Message.Data[6] = RxMessage.Data[6];
Message.Data[7] = RxMessage.Data[7];
}
上一篇:STM32之ADC单通道连续例程
下一篇:STM32之CAN总线原理
推荐阅读最新更新时间:2024-03-16 16:25