init.c
来自「串口利用定时器中断接受不同的帧」· C语言 代码 · 共 274 行
C
274 行
#include "main.h"
typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
//#define countof(a) (sizeof(a) / sizeof(*(a)))//用来计算数组的长度
#define TxBufferSize (countof(TxBuffer) - 1)
/*下面的定义 用于中断发送数据 Uart_send_counter为要发送的字节数,*******
eg 要发送数组a【】的内容
#define countof(a) (sizeof(a) / sizeof(*(a))) 可以获a【】的字节数,******
*Uart_send_pointer被指向要发送的数组地址 Uart0_send_pointer=a
函数 USART_SendData(USARTx, *Uart_send_pointer++);来发送数据,每发送一次
Uart_send_counter减一至到0*******************************************/
u8 Uart_send_counter; //Uart_send()函数发送的字节数
vu8 *Uart_send_pointer;//发送的数组指针
u8 Uart0_rev_buff[];
u32 Uart0_rev_count=0;
u32 CCR1_Val=1000;
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : 配置时钟到 72m
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
/* 时钟复位 */
RCC_DeInit();
/* 使能HSE(外部时钟) */
RCC_HSEConfig(RCC_HSE_ON);
/* 等外部时钟稳定 */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* TIM1使能 */
/* TIM2使能 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* TIM3使能 */
/* TIM4使能 */
/* 是能用到的外设的时钟-----一定要把用到的写上 主意是apb1还是apb2*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : 配置用到的gpio
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/****************串口一的引脚的定义 注意看是不是重映射****************************************/
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : 中断优先级配置
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* 中断向量ram中的位置base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* 中断向量flash中的位置 base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* 配置优先级组 注意看0,1,2,3的区别*/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* 使能uart1中断设置其优先级 */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* 使能tim2中断设置其优先级 */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* Function Name : Uart_config
* Description : 串口配置
* Input : None
* Output : None
* Return : None
*******************************************************************************/
/* USART1 configuration ------------------------------------------------------*/
/* USART configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
注意 所有用到的串口可以用一个配置即可
*/
void Uart_config(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* 配置usart1 */
USART_Init(USART1, &USART_InitStructure);
/* 使能接受和发送中断*/
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/*使能uart1 */
USART_Cmd(USART1, ENABLE);
}
/*******************************************************************************
* Function Name : TIM_config
* Description : 定时器配置
* Input : None可以更改为溢出中断
* Output : None
* Return : None
*******************************************************************************/
void TIM_config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// TIM_OCInitTypeDef TIM_OCInitStructure;
/* Time base configuration[初始化时间基单元] */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 0xff;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_DeInit(TIM2);
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Prescaler configuration [配置TIM的预分频数]*/
// TIM_PrescalerConfig(TIM2, 36, TIM_PSCReloadMode_Immediate);//TIM2,36分频,预分频数立即载入
#if 0
/* [配置输出比较模式:通道1]*/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;//TIM输出比较计时模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出允许
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;//脉冲CCR1_Val
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//极性HIGH
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);//关闭TIM2在CCR1寄存器上的预载入寄存器
#endif
/* TIM IT enable [使能TIM2比较中断]*/
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM_ITConfig(TIM2, TIM_IT_Update , ENABLE);
/* TIM2 enable counter */
// TIM_Cmd(TIM2, ENABLE);
}
/*******************************************************************
* Function Name : Uart_InterruptsendN 不带传输字节
* Description : 中断发送字符串
* Input : 参数一:uart1 2 3 4.。
参数二:要发送的字符串地址指针
* Output : 无
* Return : 无
*******************************************************************************/
void Uart_InterruptsendN(USART_TypeDef *USARTx,u8* Uart0_sended)
{
Uart_send_counter=countof(Uart0_sended);
Uart_send_pointer=Uart0_sended;
USART_SendData(USARTx, *Uart_send_pointer++);
USART_ITConfig(USARTx, USART_IT_TXE, ENABLE);
//UART_ITConfig(UARTx,UART_IT_TxEmpty, ENABLE);
}
/*******************************************************************
* Function Name : Uart_InterruptsendY 带字节传输
* Description : 中断发送字符串
* Input : 参数一:uart1 2 3 4.。
参数二:要发送的字符串地址指针
参数三:要传输的字节数
* Output : 无
* Return : 无
*******************************************************************************/
void Uart_InterruptsendY(USART_TypeDef *USARTx,vu8* Uart0_sended,u8 NbOfBytes )
{
Uart_send_counter=NbOfBytes;
Uart_send_pointer=Uart0_sended;
USART_SendData(USARTx, *Uart_send_pointer++);
USART_ITConfig(USARTx, USART_IT_TXE, ENABLE);
//UART_ITConfig(UARTx,UART_IT_TxEmpty, ENABLE);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?