📄 main.c
字号:
/**************************************************************
** 精英STM32开发板
** 版本:V1.0
** 功能: 不断查询串口状态,看串口是否收到数据
串口1配置如下
115200,8,N,1
本串口程序是接收到什么字符就返回什么字符
使用我提供的串口调试工具,选择【串口超级终端】波特率设置115200
***************************************************************/
/* Includes ------------------------------------------------------------------*/
#define SYS_GLOBALS
#include "include.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void USART1_InitConfig(uint32 BaudRate);
/* Private functions ---------------------------------------------------------*/
#define TBUF_SIZE 256 /*** Must be a power of 2 (2,4,8,16,32,64,128,256,512,...) ***/
#define RBUF_SIZE 256 /*** Must be a power of 2 (2,4,8,16,32,64,128,256,512,...) ***/
/*----------------------------------------------------------------------------
*----------------------------------------------------------------------------*/
#if TBUF_SIZE < 2
#error TBUF_SIZE is too small. It must be larger than 1.
#elif ((TBUF_SIZE & (TBUF_SIZE-1)) != 0)
#error TBUF_SIZE must be a power of 2.
#endif
#if RBUF_SIZE < 2
#error RBUF_SIZE is too small. It must be larger than 1.
#elif ((RBUF_SIZE & (RBUF_SIZE-1)) != 0)
#error RBUF_SIZE must be a power of 2.
#endif
/*----------------------------------------------------------------------------
*----------------------------------------------------------------------------*/
//数据缓冲队列
struct buf_st
{
unsigned int in;// Next In Index 下一个数据输入指针
unsigned int out;// Next Out Index 下一个数据输出指针
char buf[RBUF_SIZE];// Buffer 数据缓冲队列
};
//声明1个接收数据缓冲队列
static struct buf_st rbuf =
{
0, 0,
};
#define SIO_RBUFLEN ((unsigned short)(rbuf.in - rbuf.out))
//声明1个发送数据缓冲队列
static struct buf_st tbuf =
{
0, 0,
};
#define SIO_TBUFLEN ((unsigned short)(tbuf.in - tbuf.out))
static unsigned int tx_restart = 1; // NZ if TX restart is required
//考虑到效率问题,串口初始化用库函数
//串口收发函数仍然直接操作寄存器
/*----------------------------------------------------------------------------
USART1_IRQHandler
Handles USART1 global interrupt request.
串口中断函数
*----------------------------------------------------------------------------*/
void USART1_IRQHandler_Fun(void)
{
volatile unsigned int IIR;
struct buf_st* p;
//读取串口状态
IIR = USART1->SR;
//串口接收中断
if (IIR & USART_FLAG_RXNE)
{
//必须清除中断标志
USART1->SR &= ~USART_FLAG_RXNE; // clear interrupt
p = &rbuf;
//接收数据缓冲未满继续放数据进缓冲区
if (((p->in - p->out) & ~(RBUF_SIZE - 1)) == 0)
{
p->buf[p->in & (RBUF_SIZE - 1)] = (USART1->DR & 0x1FF);
p->in++;
}
}
//串口发送中断
if (IIR & USART_FLAG_TXE)
{
//必须清除中断标志
USART1->SR &= ~USART_FLAG_TXE; // clear interrupt
p = &tbuf;
//发送数据缓冲还有数据继续从发送缓冲区取数据
if (p->in != p->out)
{
USART1->DR = (p->buf[p->out & (TBUF_SIZE - 1)] & 0x1FF);
p->out++;
tx_restart = 0;
}
else//发送数据缓冲空表示数据发送结束
{
tx_restart = 1;
USART1->CR1 &= ~USART_FLAG_TXE; // disable TX interrupt if nothing to send
}
}
}
/*------------------------------------------------------------------------------
buffer_Init
initialize the buffers
初始化接收缓冲、发送缓冲
*------------------------------------------------------------------------------*/
void buffer_Init(void)
{
tbuf.in = 0; // Clear com buffer indexes
tbuf.out = 0;
tx_restart = 1;
rbuf.in = 0;
rbuf.out = 0;
}
/*------------------------------------------------------------------------------
SenChar
transmit a character
发送1个字节数据
*------------------------------------------------------------------------------*/
int SendChar(int c)
{
struct buf_st* p = &tbuf;
// If the buffer is full, return an error value
//如果发送缓冲满,直接返回
if (SIO_TBUFLEN >= TBUF_SIZE)
return (-1);
//向发送缓冲填充数据
p->buf[p->in & (TBUF_SIZE - 1)] = c; // Add data to the transmit buffer.
p->in++;
//如果发送中断禁止,那么开启发送中断
if (tx_restart)
{
// If transmit interrupt is disabled, enable it
tx_restart = 0;
USART1->CR1 |= USART_FLAG_TXE; // enable TX interrupt
}
return (0);
}
/*------------------------------------------------------------------------------
GetKey
receive a character
接收1个字节
*------------------------------------------------------------------------------*/
int GetKey(void)
{
struct buf_st* p = &rbuf;
if (SIO_RBUFLEN == 0)
return (-1);
return (p->buf[(p->out++) & (RBUF_SIZE - 1)]);
}
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System Clocks Configuration */
RCC_Configuration();//配置系统时钟
GPIO_Configuration();//配置GPIO
/* NVIC configuration */
NVIC_Configuration();//配置中断
buffer_Init();//串口收发缓冲初始化
//配置串口
USART1_InitConfig(115200);
printf("Interrupt driven Serial I/O Example\r\n\r\n");
while (1)
{
// Loop forever
unsigned char c;
printf("Press a key. ");//提示从串口输入1个字符
c = getchar();
printf("\r\n");
printf("You pressed '%c'.\r\n\r\n", c);
} // end while
}
//串口初始化函数
void USART1_InitConfig(uint32 BaudRate)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//使能串口的RCC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
//串口使用的GPIO口配置
/* 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);
//配置串口
USART_InitStructure.USART_BaudRate = BaudRate;
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;
/* Configure USART1 */
USART_Init(USART1, &USART_InitStructure);//配置串口1
/* Enable USART1 Receive interrupts 使能串口接收中断*/
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//串口发送中断在发送数据时开启
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);//使能串口1
//串口中断配置
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);//使能外部时钟
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();//等待外部时钟就绪
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer 启用预取缓冲器 */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state FLASH设置2个等待周期*/
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK 设置系统设置*/
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK PCLK2时钟=主时钟*/
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 PCLK1时钟为主时钟1/2*/
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz 设置时钟为72M*/
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL 使能PLL*/
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready 等待PLL工作稳定*/
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source 选择PLL做为系统时钟源*/
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source 准备就绪,开始干活*/
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO_LED clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能GPIO时钟
//LED_init-------------------------------------------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |
GPIO_Pin_9 |
GPIO_Pin_10 |
GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure); //设置GPIO为输出
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
//#ifdef VECT_TAB_RAM
#if defined (VECT_TAB_RAM)
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); //设置中断向量在RAM中
//#elif defined(VECT_TAB_FLASH_IAP)
// NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x2000);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //设置中断向量在FLASH中
#endif
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -