[编辑] 程序流程图
主程序
文件:TB 串口实验主程序流程图.png 中断服务程序
文件:TB 串口实验中断服务程序流程图.png
[编辑] 代码实现及分析
[编辑] main.c
[编辑] main函数
int main(void) {
/* 初始化TB开发板上的LED3 */ TB_LEDInit(LED3);
/* 初始化TB开发板上的COM(USART1) */ TB_COMInit();
while (1) { } }
[编辑] TB_COM.c
[编辑] 串口初始化函数TB_COMInit
/*-----------------------------------------------------------------------------------------------------
// 功能描述 初始化TB开发板上的COM口(USART1) // 输入参数 无 // 返回值 无
-----------------------------------------------------------------------------------------------------*/ void TB_COMInit(void) {
GPIO_InitTypeDef GPIO_InitStructure; /* RCC 配置
-----------------------------------------------------------------*/ /* 使能 GPIOA 和 AFIO 时钟,USART1 Tx 和 PA9 复用,USART1 Rx 和 PA10 复用 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); /* 使能 USART1 时钟 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* GPIO 配置
----------------------------------------------------------------*/ /* 配置 USART1 Tx 为复用推挽输出模式 */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* 配置 USART1 Rx 为浮空输入模式 */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOA, &GPIO_InitStructure); /* NVIC 配置
----------------------------------------------------------------*/ /* 使能 USART1 中断 */ NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* USART1 配置
---------------------------------------------------------------*/ /* USART1 配置如下: - 波特率9600 - 字长8位 - 停止位1位 - 不使用校验 - 不使用硬件流控制 (RTS 和 CTS 信号)
- 收发使能 */ 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; USART_Init(USART1, &USART_InitStructure); /* 使能 USART1 发送中断:当 USART1 发送数据寄存器空,产生该中断 */ USART_ITConfig(USART1, USART_IT_TXE, ENABLE); /* 使能 USART1 接收中断:当 USART1 接收数据寄存器非空,产生该中断 */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); /* 使能 USART1 */ USART_Cmd(USART1, ENABLE); }
[编辑] vectors.c
[编辑] USART1中断服务程序
uint8_t TX[] = %uint8_t Counter = 0;
void Delay(__IO uint32_t nCount);
/*-----------------------------------------------------------------------------------------------------
// 功能描述 该函数处理 USART1 总的中断请求 // 输入参数 无 // 返回值 无
-----------------------------------------------------------------------------------------------------*/ void USART1_IRQHandler(void) { if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
uint16_t c; TB_LEDOn(LED3); c = USART_ReceiveData(USART1); if(c == '\\r') { USART_SendData(USART1, '\\n'); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); } USART_SendData(USART1, c); Delay(0xffff); TB_LEDOff(LED3); } if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) { TB_LEDOn(LED3); USART_SendData(USART1, TX[Counter++]); if(Counter == sizeof(TX)) USART_ITConfig(USART1, USART_IT_TXE, DISABLE); Delay(0xffff); EM_TB_LEDOff(LED3); } }
/*----------------------------------------------------------------------------------------------------- // 功能描述 插入一段延迟
// 输入参数 nCount:指定延迟时间长度 // 返回值 无
-----------------------------------------------------------------------------------------------------*/ void Delay(__IO uint32_t nCount) { for(; nCount != 0; nCount--); }