STM8S105C6T6 UART2串口,能接收数据,不能发送数据,哪位大神能帮忙看看。现在单片机自发自收没有问题,串口自发自收也没有问题,但是两个连接起来就单片机只能接收数据,发送数据,串口无接收
工具:USB转TTL
以下是程序代码:
void UART2_GPIO_init()
{
GPIO_DeInit(GPIOD);
//GPIO_Init(GPIOD,GPIO_PIN_5,GPIO_MODE_IN_PU_NO_IT);//串口通讯TX
GPIO_Init(GPIOD,GPIO_PIN_5,GPIO_MODE_OUT_PP_LOW_FAST);
//GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(GPIOD,GPIO_PIN_6, GPIO_MODE_IN_FL_NO_IT);//串口通讯RX
}
void UART2_ConfigurationInit(void)
{
UART2_GPIO_init();
UART2_DeInit();
UART2_Init((u32)9600,
UART2_WORDLENGTH_8D,
UART2_STOPBITS_1,
UART2_PARITY_NO,
UART2_SYNCMODE_CLOCK_DISABLE,
UART2_MODE_TXRX_ENABLE); //波特率 9600 8位数据 1个停止位 没有奇偶校验 关闭SCK 同意串口接受和发送
UART2_ITConfig(UART2_IT_RXNE_OR,ENABLE);//开启读中断,接收数据寄存器非空或溢出错误中断
//UART2_ITConfig(UART2_IT_TXE,ENABLE);//开启写中断
UART2_Cmd(ENABLE);//启用串口
}
void UART2_Close()
{
UART2_Cmd(DISABLE);
UART2_ITConfig(UART2_IT_RXNE_OR,DISABLE);//关读中断,接收数据寄存器非空或溢出错误中断
UART2_ITConfig(UART2_IT_TXE,DISABLE);//关写中断
}
void Send_Data(u8 Data)
{
/*while((UART2_GetFlagStatus(UART2_FLAG_TXE)==RESET));
//待发送寄存器为空
UART2_SendData8((unsigned char)Data);
while((UART2_GetFlagStatus(UART2_FLAG_TC)==RESET));
//传输完成的标志
*/
unsigned int j;
while((UART2_GetFlagStatus(UART2_FLAG_TXE)==RESET));
//待发送寄存器为空
UART2_SendData8(Data);
while((UART2_GetFlagStatus(UART2_FLAG_TC)==RESET));
//UART2_ClearFlag(UART2_FLAG_TC);
//j=50;
//while(j--);
}
//发送字符串Send_Str("STM8SUART \r\n")
void Send_Str(unsigned char *Data)
{
while(*Data!='\0')
{
Send_Data(*Data);
Data++;
}
}
INTERRUPT_HANDLER(UART2_TX_IRQHandler, 20)//发送数据中断
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
// 写操作自动清中断,因此可以不用显式清中断
//UART2_ClearITPendingBit(UART2_IT_TXE);
}
/**
* @brief UART2 RX interrupt routine.
* @param None
* @retval None
*/
INTERRUPT_HANDLER(UART2_RX_IRQHandler, 21)//接收数据中断
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
UART2_ClearITPendingBit(UART2_IT_RXNE);
Uart2RecData = UART2_ReceiveData8();
Send(Uart2RecData);
*/
// 读操作自动清中断,因此可以不用显式清中断
// 注意这里的中断名是RXNE,不是RXNE_OR
Uart2RecData = UART2_ReceiveData8();
UART2_SendData8(Uart2RecData);
UART2_ClearITPendingBit(UART2_IT_RXNE);
}
|