stm32两个串口同时使用时,如果都将串口线接上,看不到发送
时间:10-02
整理:3721RD
点击:
小弟刚学习stm32,请各位大神指点下,我的串口程序如下:
#include "stm32f10x.h"
void Delay_MS(u16 dly);
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART1_Configuration(void);
void USART2_Configuration(void);
void NVIC_Config(void);
void Delay_MS(u16 dly)
{
u16 i,j;
for(i=0;i<dly;i++)
for(j=10000;j>0;j--);
}
void RCC_Configuration(void)
{
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
while(RCC_WaitForHSEStartUp()== ERROR);
FLASH_SetLatency(FLASH_Latency_2);
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while( RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08);
RCC_APB1PeriphClockCmd (RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOF|RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA,ENABLE);
}
void GPIO_Configuration(void)
{ GPIO_InitTypeDef GPIO_InitStructure; //声明一个结构体变量
GPIO_DeInit(GPIOA);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_2; //选择PC.10(TXD) 和 PC.11
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //管脚频率为50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //输出模式为复用推挽输出
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化GPIOC寄存器
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_3; //选择PC.10(TXD) 和 PC.11(RXD)
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //管脚频率为50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //输出模式为浮空输入
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7; //选择PC.10(TXD) 和 PC.11
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //管脚频率为50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //输出模式为复用推挽输出
GPIO_Init(GPIOF,&GPIO_InitStructure);
}
void USART1_Configuration(void)
{ USART_InitTypeDef U1;
USART_DeInit(USART1);
U1.USART_BaudRate =9600;
U1.USART_WordLength = USART_WordLength_9b;
U1.USART_StopBits = USART_StopBits_1;
U1.USART_Parity = USART_Parity_Even;
U1.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
U1.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &U1);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_ClearFlag(USART1,USART_FLAG_TXE);
}
void USART2_Configuration(void)
{ USART_InitTypeDef U1;
USART_DeInit(USART2);
U1.USART_BaudRate =9600;
U1.USART_WordLength = USART_WordLength_9b;
U1.USART_StopBits = USART_StopBits_1;
U1.USART_Parity = USART_Parity_Even;
U1.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
U1.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART2, &U1);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2,ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);
USART_ClearFlag(USART2,USART_FLAG_TXE);
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //采用组别2
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//配置串口中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//占先式优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //副优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//中断使能
NVIC_Init(&NVIC_InitStructure);//中断初始化
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;//配置串口中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//占先式优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //副优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//中断使能
NVIC_Init(&NVIC_InitStructure);//中断初始化
}
int main()
{
RCC_Configuration();
GPIO_Configuration();
USART1_Configuration();
USART2_Configuration();
NVIC_Config();
GPIO_ResetBits(GPIOF,GPIO_Pin_7);
while(1)
{
USART_SendData(USART1,0x31);
Delay_MS(100);
USART_SendData(USART2,0x32);
Delay_MS(100);
}
}
虽然开了中断,但是没用,就没写中断程序了,出现的问题是两个串口同时使用时,如果都讲串口线接上,看不到发送,如果直接一根串口线,可以看到发送的数据,想不明白是什么原因,麻烦大家了
#include "stm32f10x.h"
void Delay_MS(u16 dly);
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART1_Configuration(void);
void USART2_Configuration(void);
void NVIC_Config(void);
void Delay_MS(u16 dly)
{
u16 i,j;
for(i=0;i<dly;i++)
for(j=10000;j>0;j--);
}
void RCC_Configuration(void)
{
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
while(RCC_WaitForHSEStartUp()== ERROR);
FLASH_SetLatency(FLASH_Latency_2);
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while( RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08);
RCC_APB1PeriphClockCmd (RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOF|RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA,ENABLE);
}
void GPIO_Configuration(void)
{ GPIO_InitTypeDef GPIO_InitStructure; //声明一个结构体变量
GPIO_DeInit(GPIOA);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_2; //选择PC.10(TXD) 和 PC.11
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //管脚频率为50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //输出模式为复用推挽输出
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化GPIOC寄存器
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_3; //选择PC.10(TXD) 和 PC.11(RXD)
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //管脚频率为50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //输出模式为浮空输入
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7; //选择PC.10(TXD) 和 PC.11
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //管脚频率为50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //输出模式为复用推挽输出
GPIO_Init(GPIOF,&GPIO_InitStructure);
}
void USART1_Configuration(void)
{ USART_InitTypeDef U1;
USART_DeInit(USART1);
U1.USART_BaudRate =9600;
U1.USART_WordLength = USART_WordLength_9b;
U1.USART_StopBits = USART_StopBits_1;
U1.USART_Parity = USART_Parity_Even;
U1.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
U1.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &U1);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_ClearFlag(USART1,USART_FLAG_TXE);
}
void USART2_Configuration(void)
{ USART_InitTypeDef U1;
USART_DeInit(USART2);
U1.USART_BaudRate =9600;
U1.USART_WordLength = USART_WordLength_9b;
U1.USART_StopBits = USART_StopBits_1;
U1.USART_Parity = USART_Parity_Even;
U1.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
U1.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART2, &U1);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2,ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);
USART_ClearFlag(USART2,USART_FLAG_TXE);
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //采用组别2
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//配置串口中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//占先式优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //副优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//中断使能
NVIC_Init(&NVIC_InitStructure);//中断初始化
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;//配置串口中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//占先式优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //副优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//中断使能
NVIC_Init(&NVIC_InitStructure);//中断初始化
}
int main()
{
RCC_Configuration();
GPIO_Configuration();
USART1_Configuration();
USART2_Configuration();
NVIC_Config();
GPIO_ResetBits(GPIOF,GPIO_Pin_7);
while(1)
{
USART_SendData(USART1,0x31);
Delay_MS(100);
USART_SendData(USART2,0x32);
Delay_MS(100);
}
}
虽然开了中断,但是没用,就没写中断程序了,出现的问题是两个串口同时使用时,如果都讲串口线接上,看不到发送,如果直接一根串口线,可以看到发送的数据,想不明白是什么原因,麻烦大家了
没有用中断就关掉吧,两个接上有问题,跟驱动能力有关吗?有没有驱动电路,如何接的?