m0_37760062
华丽丽的小许
2017-09-21 03:18

stm32串口发送可以进入中断但没有数据显示

2
  • stm32
  • 数据
  • usart1
  • 串口

程序功能是:若接收到数据0x55,则发送接收到的数据(0x55)。

但现在问题是:通过串口助手发送0x55,已经进入接收中断接受到数据,然后通过SendData函数发送数据,能进入发送中断,但是串口助手却没有显示0x55(好像是数据并没有发送出去)

/*头文件*/
#ifndef HEAD_H
#define HEAD_H

#include
#include
#include
#include
#include
#include "stm32f10x_flash.h"
//#include
//#include

void NVIC_Configuration(void);
void RCC_Configuration(void);
void Usart_Initial(void);
void gpio_Init(void);
void Delayms(uint16_t ms);

extern uint16_t rece;

#endif

/*函数*/
#include "head.h"

void NVIC_Configuration(void){ //ÖжÏÓÅÏȼ¶ÉèÖÃ
NVIC_InitTypeDef NVIC_InitStruc;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

NVIC_InitStruc.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruc.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruc.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruc.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStruc);

}

void RCC_Configuration(void){
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(RCC_WaitForHSEStartUp() == SUCCESS) {
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY==RESET)) {}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource()!=0x08){}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
}

void Usart_Initial(void){
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockStructure;

USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Parity  = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;

// USART_ClockStructure.USART_Clock = USART_Clock_Disable;
// USART_ClockStructure.USART_CPHA = USART_CPHA_2Edge;
// USART_ClockStructure.USART_CPOL = USART_CPOL_Low;
// USART_ClockStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockStructInit(&USART_ClockStructure);

USART_Init(USART1,&USART_InitStructure);
USART_ClockInit(USART1,&USART_ClockStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

// USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
// USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);
// USART_WakeUpConfig(USART1, USART_WakeUp_IdleLine);
USART_Cmd(USART1,ENABLE);

}

void gpio_Init(void){

GPIO_InitTypeDef GPIO_InitSTA;
GPIO_InitTypeDef GPIO_InitSTB;

/* GPIO_PinLockConfig(GPIOB,GPIO_Pin_12);*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOB,ENABLE);

GPIO_InitSTA.GPIO_Mode = GPIO_Mode_Out_PP;  //TxD1
GPIO_InitSTA.GPIO_Pin = GPIO_Pin_9;
GPIO_InitSTA.GPIO_Speed = GPIO_Speed_50MHz; 
GPIO_Init(GPIOA,&GPIO_InitSTA);

GPIO_InitSTA.GPIO_Mode = GPIO_Mode_IN_FLOATING;  //RxD1
GPIO_InitSTA.GPIO_Pin = GPIO_Pin_10;

// GPIO_InitSTA.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitSTA);

GPIO_InitSTB.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitSTB.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
GPIO_InitSTB.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitSTB);

GPIO_SetBits(GPIOA,GPIO_Pin_9 | GPIO_Pin_10);     
GPIO_SetBits(GPIOB,GPIO_Pin_12 | GPIO_Pin_13);    

}

void Delayms(uint16_t ms){
uint16_t i = 0;
uint16_t j = 0;

for(j = 0;j < ms;j ++){
  for(i = 0;i < 12000;i ++){

  }  
}

}

/*主程序,串口usart1中断函数*/
#include "head.h"
#include "stdio.h"
#include "stm32f10x_it.h"

uint16_t rece = 0;

int main(void){

NVIC_Configuration();
RCC_Configuration();
Usart_Initial();
gpio_Init();

USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
GPIO_WriteBit(GPIOB,GPIO_Pin_12 | GPIO_Pin_13,Bit_RESET); 
while(1){

// GPIO_WriteBit(GPIOB,GPIO_Pin_12 | GPIO_Pin_13,Bit_SET);
// Delayms(1000);
GPIO_WriteBit(GPIOB,GPIO_Pin_12 | GPIO_Pin_13,Bit_RESET);
Delayms(1000);
// USART_SendData(USART1,0x55);
// Delayms(1000);
// while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET){};
}
}

void USART1_IRQHandler(void)
{
/*
if(USART_GetITStatus(USART1, USART_IT_PE) != RESET)
{
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
rece = USART_ReceiveData(USART1);
}
*/

if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
  GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_SET); //若发送0x55,引脚电平会变

// USART_SendData(USART1, 0xff);
// while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET){};
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Disable USART1 RXNE Interrupt */
rece = USART_ReceiveData(USART1);

USART_ClearFlag(USART1,USART_IT_RXNE);
// USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
if(rece == 0x55){
// GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_SET);
USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
USART_SendData(USART1,rece);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET){};
// printf("hello");
}
}
}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答