久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標題:
stm32f103c8用串口2接受字符串,串口1發出對應數組的問題
[打印本頁]
作者:
GCC123
時間:
2021-11-22 23:36
標題:
stm32f103c8用串口2接受字符串,串口1發出對應數組的問題
以下是我自己寫的,感覺串口配置出問題了,串口1發送沒問題;但是串口二的接收字符串后馬上串口1發送數組不行。有沒有大佬幫忙修改一下
#include "usart.h"
#include "led.h"
#include "SysTick.h"
#include "string.h"
#include "DFPlayer.h"
/*******************************************************************************
* 函 數 名 : USART1_Init
* 函數功能 : USART1初始化函數
* 輸 入 : bound:波特率
* 輸 出 : 無
*******************************************************************************/
void USART1_Init(u32 bound)
{
//GPIO端口設置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
/* 配置GPIO的模式和IO口 */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX //串口輸出PA9
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //復用推挽輸出
GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化串口輸入IO */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX //串口輸入PA10
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //模擬輸入
GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化GPIO */
//USART1 初始化設置
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); //初始化串口1
USART_Cmd(USART1, ENABLE); //使能串口1
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//開啟相關中斷
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中斷通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//搶占優先級3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子優先級3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據指定的參數初始化VIC寄存器、
}
/*******************************************************************************
* 函 數 名 : USART1_Send_Data
* 函數功能 : USART1發送函數
* 輸 入 : buf
* 輸 入 : len
* 輸 出 : 無
*******************************************************************************/
void USART1_Send_Data(u8 *buf,uint16_t len)
{
u8 t=0;
for(t=0;t<len;t++) //循環發送數據
{
USART_SendData(USART1,buf[t] );
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
}
/*******************************************************************************
* 函 數 名 : uart2_init
* 函數功能 : IO端口及串口2,時鐘初始化函數 A2,A3
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void USART2_Init(u32 bt)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure; //聲明一個結構體變量,用來初始化GPIO
//使能串口的RCC時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //使能UART3所在GPIOB的時鐘
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
//串口使用的GPIO口配置
// Configure USART2 Rx (PB.11) as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure USART2 Tx (PB.10) as alternate function push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置串口
USART_InitStructure.USART_BaudRate = bt;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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;
// Configure USART
USART_Init(USART2, &USART_InitStructure);//配置串口
// Enable USART1 Receive interrupts 使能串口接收中斷
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
//串口發送中斷在發送數據時開啟
//USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
// Enable the USART3
USART_Cmd(USART2, ENABLE);//使能串口
//串口中斷配置
//Configure the NVIC Preemption Priority Bits
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
// Enable the USART Interrupt
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
char USART_ReceiveString[50]; //接收PC端發送過來的字符
int Receive_Flag = 0; //接收消息標志位
int Receive_sum = 0; //數組下標
void USART2_IRQHandler(void) //中斷
{
if(USART_GetITStatus(USART2,USART_IT_RXNE) == 1) //USART_FLAG_RXNE判斷數據,== 1則有數據
{
if(Receive_sum > 49) //數組能存放50個字節的數據
{
USART_ReceiveString[49] = '\0'; //數據字節超過50位時,將最后一位設置為\0
Receive_Flag = 1; //接收標志位置1,停止接收數據
Receive_sum = 0; //數組下標置0
}
if(Receive_Flag == 0) //接收標志位等于0,開始接收數據
{
USART_ReceiveString[Receive_sum] = USART_ReceiveData(USART2); //通過USART2串口接收字符
Receive_sum++; //數組下標++
}
if(Receive_sum >= 2) //數組下標大于2
{
if(USART_ReceiveString[Receive_sum-2] == '\r' && USART_ReceiveString[Receive_sum-1] == '\n' )
{
USART_ReceiveString[Receive_sum-1] = '\0';
USART_ReceiveString[Receive_sum-2] = '\0';
Receive_Flag = 1; //接收標志位置1,停止接收數據
Receive_sum = 0; //數組下標置0
if(strcmp(USART_ReceiveString,"batteriesHarmful") == 0)
{
led1=!led1;
}
if(strcmp(USART_ReceiveString,"cansRecyclable") == 0)
{
Uart_DFPlayer(0x03 , 0x02) ;
}
if(strcmp(USART_ReceiveString,"cartonRecyclable") == 0)
{
Uart_DFPlayer(0x03 , 0x03) ;
}
if(strcmp(USART_ReceiveString,"cigaretteDry") == 0)
{
Uart_DFPlayer(0x03 , 0x04) ;
}
if(strcmp(USART_ReceiveString,"tissueDry") == 0)
{
Uart_DFPlayer(0x03 , 0x05) ;
}
Receive_Flag = 0;
}
}
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //接收后先清空標志位
}
}
復制代碼
作者:
人工置頂員
時間:
2021-11-24 03:32
頂一下
作者:
munuc_w
時間:
2021-11-24 17:28
發送數據需要時間,收到一個字節數據立即用另一個口發送,都是用中斷,或都是用查詢,就易出問題,主要是發送占用CPU時,收到一個字節數據沒有及時取走,就會被下一個接收的數據覆蓋,或接收口占用CPU,同樣會導致發送不能正常工作。
作者:
xws245925587
時間:
2021-11-25 13:59
你這樣處理不好,對于接受,開一個乒乓緩存吧,fifo1接受一幀后,切換到fifo2接受,并把fifo1的數據發出去
歡迎光臨 (http://www.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
99国内精品久久久久久久
|
日韩一区二区在线视频
|
欧美一区二区三区免费在线观看
|
日本公妇乱淫xxxⅹ 国产在线不卡
|
久久精品国产亚洲
|
精品日韩一区
|
免费看黄视频网站
|
国产三级一区二区
|
久久久久久久久久久高潮一区二区
|
h在线免费观看
|
精品一区二区三区在线观看国产
|
午夜精品
|
91麻豆精品国产91久久久久久
|
亚洲天天干
|
少妇久久久
|
美女视频久久
|
久久成人久久
|
欧美在线视频一区
|
午夜精品在线观看
|
先锋av资源网
|
91九色在线观看
|
99精品一区二区
|
国产中文字幕网
|
成人精品免费视频
|
亚洲欧美中文日韩在线v日本
|
天天干夜夜操
|
伊人伊人伊人
|
欧美日韩电影在线
|
日韩在线一区二区
|
精品福利在线
|
黄色一级电影免费观看
|
欧美在线综合
|
亚洲精品在线播放
|
亚洲毛片
|
偷拍自拍第一页
|
久久免费看
|
日本天堂视频在线观看
|
色桃网
|
免费激情av
|
精区3d动漫一品二品精区
|
久久亚洲一区二区
|