标题:
STM32串口通信的程序问题,如何修改错误
[打印本页]
作者:
查理九世
时间:
2019-7-25 18:02
标题:
STM32串口通信的程序问题,如何修改错误
写了一段STM32单片机库函数双机通信运行的程序,在一个板上按键,则另一个板上的电机运动,反过来也同理,可为什么不能实现同时按两块板同时接受和发送,只能一块板发一块板收,如果要反过来控制还需要复位一下,程序是错在哪里呢?该如何改呢?求大佬指教主函数
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "timer.h"
uchar X=0;//占空比初始值百分比
int flag1=0;
int flag2=0;
int flag3=0;
int flag4=0;
int main(void)
{
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart2_init(115200); //串口初始化为115200
LED_Init(); //LED端口初始化
TIM3_PWM_Init(100,0); //PWM波产生函数
OLED_Init(); //OLED初始化
OLED_ShowString(0,0,"duty cycle:"); //OLED屏显示
OLED_ShowNum(80,0,X,3,12);
USART2_IRQHandler();
while(1)
{
if(Key1==0) flag1=1; //按键1按下发送1
if(Key1==1&&flag1==1)
{
USART_SendData(USART2,1);
flag1=0;
}
if(Key2==0) flag2=1;
if(Key2==1&&flag2==1)
{
USART_SendData(USART2,2); //按键2按下发送2
flag2=0;
}
if(Key3==0) flag3=1;
if(Key3==1&&flag3==1)
{
USART_SendData(USART2,3); //按键3按下发送3
flag3=0;
}
if(Key4==0) flag4=1;
if(Key4==1&&flag4==1)
{
USART_SendData(USART2,4); //按键4按下发送4
flag4=0;
}
}
}
复制代码
usart函数
#include "sys.h"
#include "usart.h"
#include "string.h"
#include "led.h"
//加入以下代码,支持printf函数,而不需要选择use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
_sys_exit(int x)
{
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
USART1->DR = (u8) ch;
return ch;
}
#endif
#if EN_USART1_RX //如果使能了接收
//串口2中断服务程序
//注意,读取USARTx->SR能避免莫名其妙的错误
u8 USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.
//接收状态
//bit15, 接收完成标志
//bit14, 接收到0x0d
//bit13~0, 接收到的有效字节数目
u16 USART_RX_STA=0; //接收状态标记
void uart2_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //使能USART2,GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//USART2_TX GPIOA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
//USART2_RX GPIOA.3初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级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(USART2, &USART_InitStructure); //初始化串口1
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(USART2, ENABLE); //使能串口1
}
int ii=0;
#endif
void USART2_IRQHandler(void) //串口2中断服务程序
{
u8 Res;
uchar X;
#if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
OSIntEnter();
#endif
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
while(1)
{
TIM3->CCR3=X; //PWM波,占空比
Res=USART_ReceiveData(USART2); //接受另一块板发送的数据
if(Res==1)
{
X=50; //电机开始以50%的占空比运行
OLED_ShowNum(80,0,X,3,12);
}
if(Res==2)
{
X=0;
OLED_ShowNum(80,0,X,3,12);
}
if(Res==4)
{
if(X!=100)
{
X=X+10; //不会单独加10直接加到100?
OLED_ShowNum(80,0,X,3,12); //OLED屏显示
}
}
if(Res==3)
{
if(X!=0)
{
X=X-10;
OLED_ShowNum(80,0,X,3,12);
}
}
}
}
#if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
OSIntExit();
#endif
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1