问题:想实现2个STM32串口通讯,实现一个发数据,一个接数据,把
2个STM32的TX连RX,RX连TX。数据就是接不到。
接收程序:
#include "stm32f10x_lib.h"
#include"stdio.h"
void RCC_Configuaration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); //使能USART1,GPIOA时钟
}
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
}
void NVIC_Init_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; //抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道使能
NVIC_Init(&NVIC_InitStructure); //中断优先级初始化
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1_RX    A.10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_5); //灭
// GPIO_ResetBits(GPIOB,GPIO_Pin_5); //初始化PA10
}
void USART_Configuration(u32 bound)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = bound; //波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长为8位
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); //初始化串口
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开启接收中断
USART_Cmd(USART1, ENABLE);
}
void Uart1_PutChar(u8 ch) //发送函数
{
USART_SendData(USART1,ch);
while(USART_GetITStatus(USART1,USART_FLAG_TXE) == SET);
}
void delay(u32 z)
{
u32 x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
int main()
{
vu32 i;
RCC_Configuaration();
NVIC_Configuration();
NVIC_Init_Configuration();
GPIO_Configuration();
USART_Configuration(1200);
while(1)
{
GPIO_SetBits(GPIOB,GPIO_Pin_5); //亮
}
}
void USART1_IRQHandler(void) //中断函数
{
u8 temp_trx;
GPIO_ResetBits(GPIOB,GPIO_Pin_5); //LED灭
if(USART_GetITStatus(USART1,USART_IT_RXNE) !=RESET )
{
temp_trx = USART_ReceiveData(USART1);
delay(1);
Uart1_PutChar(temp_trx);
}
}
|