久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標(biāo)題:
stm32f103c8用串口2接受字符串,串口1發(fā)出對(duì)應(yīng)數(shù)組的問(wèn)題
[打印本頁(yè)]
作者:
GCC123
時(shí)間:
2021-11-22 23:36
標(biāo)題:
stm32f103c8用串口2接受字符串,串口1發(fā)出對(duì)應(yīng)數(shù)組的問(wèn)題
以下是我自己寫的,感覺串口配置出問(wèn)題了,串口1發(fā)送沒問(wèn)題;但是串口二的接收字符串后馬上串口1發(fā)送數(shù)組不行。有沒有大佬幫忙修改一下
#include "usart.h"
#include "led.h"
#include "SysTick.h"
#include "string.h"
#include "DFPlayer.h"
/*******************************************************************************
* 函 數(shù) 名 : USART1_Init
* 函數(shù)功能 : USART1初始化函數(shù)
* 輸 入 : bound:波特率
* 輸 出 : 無(wú)
*******************************************************************************/
void USART1_Init(u32 bound)
{
//GPIO端口設(shè)置
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; //復(fù)用推挽輸出
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 初始化設(shè)置
USART_InitStructure.USART_BaudRate = bound;//波特率設(shè)置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長(zhǎng)為8位數(shù)據(jù)格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個(gè)停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//無(wú)奇偶校驗(yàn)位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無(wú)硬件數(shù)據(jù)流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發(fā)模式
USART_Init(USART1, &USART_InitStructure); //初始化串口1
USART_Cmd(USART1, ENABLE); //使能串口1
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//開啟相關(guān)中斷
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中斷通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//搶占優(yōu)先級(jí)3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子優(yōu)先級(jí)3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據(jù)指定的參數(shù)初始化VIC寄存器、
}
/*******************************************************************************
* 函 數(shù) 名 : USART1_Send_Data
* 函數(shù)功能 : USART1發(fā)送函數(shù)
* 輸 入 : buf
* 輸 入 : len
* 輸 出 : 無(wú)
*******************************************************************************/
void USART1_Send_Data(u8 *buf,uint16_t len)
{
u8 t=0;
for(t=0;t<len;t++) //循環(huán)發(fā)送數(shù)據(jù)
{
USART_SendData(USART1,buf[t] );
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
}
/*******************************************************************************
* 函 數(shù) 名 : uart2_init
* 函數(shù)功能 : IO端口及串口2,時(shí)鐘初始化函數(shù) A2,A3
* 輸 入 : 無(wú)
* 輸 出 : 無(wú)
*******************************************************************************/
void USART2_Init(u32 bt)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure; //聲明一個(gè)結(jié)構(gòu)體變量,用來(lái)初始化GPIO
//使能串口的RCC時(shí)鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //使能UART3所在GPIOB的時(shí)鐘
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);
//串口發(fā)送中斷在發(fā)送數(shù)據(jù)時(shí)開啟
//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端發(fā)送過(guò)來(lái)的字符
int Receive_Flag = 0; //接收消息標(biāo)志位
int Receive_sum = 0; //數(shù)組下標(biāo)
void USART2_IRQHandler(void) //中斷
{
if(USART_GetITStatus(USART2,USART_IT_RXNE) == 1) //USART_FLAG_RXNE判斷數(shù)據(jù),== 1則有數(shù)據(jù)
{
if(Receive_sum > 49) //數(shù)組能存放50個(gè)字節(jié)的數(shù)據(jù)
{
USART_ReceiveString[49] = '\0'; //數(shù)據(jù)字節(jié)超過(guò)50位時(shí),將最后一位設(shè)置為\0
Receive_Flag = 1; //接收標(biāo)志位置1,停止接收數(shù)據(jù)
Receive_sum = 0; //數(shù)組下標(biāo)置0
}
if(Receive_Flag == 0) //接收標(biāo)志位等于0,開始接收數(shù)據(jù)
{
USART_ReceiveString[Receive_sum] = USART_ReceiveData(USART2); //通過(guò)USART2串口接收字符
Receive_sum++; //數(shù)組下標(biāo)++
}
if(Receive_sum >= 2) //數(shù)組下標(biāo)大于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; //接收標(biāo)志位置1,停止接收數(shù)據(jù)
Receive_sum = 0; //數(shù)組下標(biāo)置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); //接收后先清空標(biāo)志位
}
}
復(fù)制代碼
作者:
人工置頂員
時(shí)間:
2021-11-24 03:32
頂一下
作者:
munuc_w
時(shí)間:
2021-11-24 17:28
發(fā)送數(shù)據(jù)需要時(shí)間,收到一個(gè)字節(jié)數(shù)據(jù)立即用另一個(gè)口發(fā)送,都是用中斷,或都是用查詢,就易出問(wèn)題,主要是發(fā)送占用CPU時(shí),收到一個(gè)字節(jié)數(shù)據(jù)沒有及時(shí)取走,就會(huì)被下一個(gè)接收的數(shù)據(jù)覆蓋,或接收口占用CPU,同樣會(huì)導(dǎo)致發(fā)送不能正常工作。
作者:
xws245925587
時(shí)間:
2021-11-25 13:59
你這樣處理不好,對(duì)于接受,開一個(gè)乒乓緩存吧,fifo1接受一幀后,切換到fifo2接受,并把fifo1的數(shù)據(jù)發(fā)出去
歡迎光臨 (http://www.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
成人性生交大片免费看中文带字幕
|
中文字幕视频网
|
欧美成年黄网站色视频
|
日韩av一区二区在线观看
|
欧美综合在线观看
|
久久久免费观看视频
|
中文字幕成人av
|
超碰8
|
国产一级免费视频
|
国产美女一区
|
国产精品99久久久久
|
午夜电影一区二区
|
香蕉一区二区
|
狠狠av
|
最新国产在线
|
国产伦精品一区二区三区照片91
|
密室大逃脱第六季大神版在线观看
|
日韩亚洲视频
|
国产日韩欧美在线观看
|
草久免费视频
|
日本三级网址
|
人人看人人射
|
亚洲一区二区三区视频在线
|
久久高清亚洲
|
国产精品久久国产愉拍
|
久久99精品久久久久久
|
久久欧美精品
|
99这里只有精品视频
|
亚洲精品一区二区三区蜜桃久
|
黄色片网此
|
国产亚洲精品精品国产亚洲综合
|
国产999精品久久久久久
|
国产精品1区2区
|
精品国产乱码久久久久久丨区2区
|
九九热免费在线观看
|
欧美日韩一区二区在线
|
色婷婷亚洲国产女人的天堂
|
国产成人aⅴ
|
看片91
|
亚洲美女网站
|
91视频网
|