403A的串口3,检测中断程序已经执行,但没发出数据。
用STM32程序是正常的,说明硬件没问题。
不知道问题都出在那儿,可UART5却是正常的。
初始化程序:
void usart3_init(u32 bound){
//GPIO端口设置
GPIO_InitType GPIO_InitStructure;
USART_InitType USART_InitStructure;
NVIC_InitType NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOB, ENABLE); //使能GPIOB时钟
//USART3_TX GPIOB.10
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_1; //PB1
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP; //推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOC.12
//USART3_TX GPIOB.10
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_10; //PB10
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
//USART3_RX GPIOB.11初始化
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_11;//PB11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_PU; //上拉输入
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_USART3,ENABLE);//使能USART3时钟
//Usart3 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
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(USART3, &USART_InitStructure); //初始化串口3
USART_INTConfig(USART3, USART_INT_RDNE, ENABLE);//开启串口接受中断
USART_Cmd(USART3, ENABLE); //使能串口3
RE1 = 1; //转为接收模式
}
中断程序:
//串口3中断(RS485)程序
void USART3_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART3, USART_INT_TRAC) != RESET) //数据发送完成
{
fc3 ++;
if(fc3 < fd3) USART_SendData(USART3, fa3[fc3]);
else {
USART_ClearITPendingBit(USART3, USART_INT_TRAC); //清除中断标志位
RE1 = 1; //转为接收模式
USART_INTConfig(USART3, USART_INT_RDNE, ENABLE);//开启串口接受中断
jcd3 = 0;
tpt3 = 3;
}
}
if(USART_GetITStatus(USART3, USART_INT_RDNE) != RESET) //接收到数据
{
res = USART_ReceiveData(USART3); //读取接收到的数据
if(jcd3 < 64) {
jie3[jcd3] = res; //记录接收到的值
jcd3 ++; //接收数据增加1
}
tpr3 = 3;
}
}
发送程序:
//====温湿度传感器数据读出====//
void input_f(u8 dd) {
while(tpt3);
fa3[0] = dd;
fa3[1] = 0x03;
fa3[2] = 0;
fa3[3] = 0;
fa3[4] = 0;
fa3[5] = 2;
crcjs(fa3,6);
fa3[6] = crclo;
fa3[7] = crchi;
fd3 = 8;
fc3 = 0;
RE1 = 0; //RS485发送模式
USART_INTConfig(USART3, USART_INT_TRAC, ENABLE); //开启发送完成中断
USART_SendData(USART3,fa3[fc3]);
}
|