您的位置 首页 汽车

STM32 串口中止接纳数据

includestm32f10xh***********************************************************************************

#include “stm32f10x.h”

/***********************************************************************
***********************************************************************/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void delay(vu32 nCount)
{
for(; nCount != 0; nCount–);
}
/***********************************************************************
************************************************************************/

main()
{
RCC_Configuration();//体系时钟装备

NVIC_Configuration();//中止装备

GPIO_Configuration();//GPIO口装备

while(1) //LED灯循环亮灭,串口循环发送ASCII“9”
{
delay(5000000);
GPIO_WriteBit(GPIOD,GPIO_Pin_2,Bit_RESET);
USART_SendData(USART1,9);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
{
}
delay(5000000);
GPIO_WriteBit(GPIOD,GPIO_Pin_2,Bit_SET);
}
}
/*****************************************************************************
*****************************************************************************/
//体系时钟装备
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//使能串口1的GPIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD, ENABLE); //pa.9 pa.10 led0
}
/*****************************************************************************
*****************************************************************************/
//串口GPIO口装备
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

/* LED0*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);

// GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
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);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //悬浮输入
GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = 9600; //设定波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //传输数据位数
USART_InitStructure.USART_StopBits = USART_StopBits_1; //中止位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); //初始化串口1
USART_Cmd(USART1,ENABLE); //串口1使能

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能串口1 读中止

}

/*****************************************************************************
*****************************************************************************/
//中止装备
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //先占优先权2,从优先级2位
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //开串口中止1
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; //指定抢占优先等级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //指定相应优先等级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*****************************************************************************
*****************************************************************************/
void USART1_IRQHandler(void) //串口接纳中止,并将接纳到得数据发送出
{
u16 temp_trx;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//判别 是否 接纳中止
{
temp_trx = USART_ReceiveData(USART1);
USART_SendData(USART1,temp_trx);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);//判别 发送标志
}
}
/*****************************************************************************
*****************************************************************************/

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/317552.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部