STM32L151串口中断发送数据
我在做STM32L151串口中断发送数据时,初始化程序总是有问题,请帮我看看。void USART1_INIT(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
/* Enable USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //PA9-TX PA10-RX
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;
/* Connect PXx to USARTx_Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9,GPIO_AF_USART1);//PA9-TX
/* Connect PXx to USARTx_Rx */
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);//PA10-RX
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_10;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/* USART configuration */
USART_Init(USART1,&USART_InitStructure);
/* Enable the USART Receive interrupt */
USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //程序总是死在这里
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable USART */
USART_Cmd(USART1, ENABLE);
}
中断函数
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
}
if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
}
}
还有点不明白,这几个定义都表示什么意思?
#define USART_IT_PE ((uint16_t)0x0028)
#define USART_IT_TXE ((uint16_t)0x0727)
#define USART_IT_TC ((uint16_t)0x0626)
#define USART_IT_RXNE ((uint16_t)0x0525)//接收中断使能
#define USART_IT_IDLE ((uint16_t)0x0424)
#define USART_IT_LBD ((uint16_t)0x0846)
#define USART_IT_ORE_RX ((uint16_t)0x0325) /* In case interrupt is generated if the RXNEIE bit is set */
#define USART_IT_CTS ((uint16_t)0x096A)
#define USART_IT_ERR ((uint16_t)0x0060)
#define USART_IT_ORE_ER ((uint16_t)0x0360) /* In case interrupt is generated if the EIE bit is set */
#define USART_IT_NE ((uint16_t)0x0260)
#define USART_IT_FE ((uint16_t)0x0160)
回复:STM32L151串口中断发送数据
使能发送中断是USART_IT_TXE还是USART_IT_TC?使能接收中断有USART_IT_RXNE,为什么没有USART_IT_TXNE呢?
这个比较难理解。
RE:STM32L151串口中断发送数据
USART_Init(USART1, &USART_InitStructure);USART_ITConfig(USART1, USART_IT_RXNE,ENABLE); USART_Cmd(USART1, ENABLE);
只用这三句就可以使用中断了。
不要使能发送中断
下面这两句去掉:
////////////////////////////////
USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //程序总是死在这里while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
页:
[1]