最近小弟學習STM32編程,購買了一個模塊(WiFi-ESP8266),將引腳接入UART4來驅動,進行收發數據,后面發現,發出去的數據和接收的數據一模一樣,感覺不對勁,因為按照模塊說明收到數據后,返回值和發送的是有區別的,但是找了好多資料沒有找到問題,請各位有經驗的大哥指教;如下是代碼:(使用的是STM32F103VET6芯片)
這個是UART4的初始化:
void UART4_INIT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PB11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4,ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4,DISABLE);
USART_InitStructure.USART_BaudRate = 115200;
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;
USART_Init(UART4, &USART_InitStructure); ;
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
//USART_ITConfig(UART4, USART_IT_TC, ENABLE);
USART_Cmd(UART4, ENABLE);
USART_ClearFlag(UART4, USART_FLAG_TC);
}
這個是發送函數
void WiFi_SendData(u8 *str,unsigned int count)
{
if(count>0)
{
Sendstr1 = str;
Send_Count1 = count;
//GPIO_SetBits(RS485_RTS_PORT, RS485_RTS);
USART_ITConfig(UART4, USART_IT_TXE, ENABLE);
}
}
中斷函數:
void UART4_IRQHandler(void) //中斷函數
{
unsigned char data=0;
unsigned char sbuf_temp = 0;
/******************/
if(uart.recetime == 0)
{
uart.len = 0;
uart.errorbit = 0 ;
RS485_Read_Count=0;
RS485_Read_OK=0;
}
uart.recetime = 100;//這個是作為是否接收完成的標示,在別的地方用一個定時器控制
/*******************/
if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) //判斷是否有接收到數據
{
data = USART_ReceiveData(UART4);
sbuf_temp = data;
uart.revice_buffer[uart.len] = sbuf_temp
uart.len++;
RS485_Receive_Time = 0;
if(uart.len > 80 -2)
{
uart.errorbit = 1 ;
uart.len = 76 ;//·¢ÏÖ′íÎó
}
}
if(USART_GetITStatus(UART4, USART_IT_TXE) != RESET) //發送數據
{
if(Send_Count1)
{
USART_SendData(UART4,*Sendstr1++);
Send_Count1--;
}
else
{
USART_ITConfig(UART4, USART_IT_TXE, DISABLE);//發送完畢,停止發送
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//轉變成,等等接收
RS485_Send_Time=1;//接收完成標示
}
}
if(USART_GetFlagStatus(UART4,USART_FLAG_ORE)==SET)
{
USART_ClearFlag(UART4,USART_FLAG_ORE);
USART_ReceiveData(UART4);
}
}
|