STM32F407串口3一直不好使!!!!

2019-07-20 21:41发布

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "beep.h"
#include "key.h"


//ALIENTEK ̽Ë÷ÕßSTM32F407¿ª·¢°å ʵÑé4
//´®¿ÚͨÐÅʵÑé -¿âº¯Êý°æ±¾
//¼¼ÊõÖ§³Ö£ºwww.openedv.com
//ÌÔ±¦µêÆÌ£ºhttp://eboard.taobao.com
//¹ãÖÝÊÐÐÇÒíµç×ӿƼ¼ÓÐÏÞ¹«Ë¾  
//×÷ÕߣºÕýµãÔ­×Ó @ALIENTEK




int main(void)
{

u8 t;
u8 len;
u16 times=0;  
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//ÉèÖÃϵͳÖжÏÓÅÏȼ¶·Ö×é2

uart_init(115200); //´®¿Ú³õʼ»¯²¨ÌØÂÊΪ115200
delay_init(168);  //ÑÓʱ³õʼ»¯
//   LED_Init();      //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú  
while(1)
{
  if(USART_RX_STA&0x8000)
  {        
   len=USART_RX_STA&0x3fff;//µÃµ½´Ë´Î½ÓÊÕµ½µÄÊý¾Ý³¤¶È
   printf(" Äú·¢Ë͵ÄÏûϢΪ: ");
   for(t=0;t<len;t++)
   {
    USART_SendData(USART3, USART_RX_BUF[t]);         //&Iuml;ò&acute;&reg;&iquest;&Uacute;1·&cent;&Euml;&Iacute;&Ecirc;&yacute;&frac34;&Yacute;
    while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);//&micro;&Egrave;&acute;&yacute;·&cent;&Euml;&Iacute;&frac12;á&Ecirc;&oslash;
   }
   printf(" ");//&sup2;&aring;&Egrave;&euml;&raquo;&raquo;&ETH;&ETH;
   USART_RX_STA=0;
  }else
  {
   times++;
   if(times%5000==0)
   {
    printf(" ALIENTEK &Igrave;&frac12;&Euml;÷&Otilde;&szlig;STM32F407&iquest;&ordf;·&cent;°&aring; &acute;&reg;&iquest;&Uacute;&Ecirc;&micro;&Ntilde;é ");
    printf("&Otilde;&yacute;&micro;&atilde;&Ocirc;&shy;×&Oacute;@ALIENTEK ");
   }
   if(times%200==0)printf("&Ccedil;&euml;&Ecirc;&auml;&Egrave;&euml;&Ecirc;&yacute;&frac34;&Yacute;,&Ograve;&Ocirc;&raquo;&Oslash;&sup3;&micro;&frac14;ü&frac12;á&Ecirc;&oslash; ");  
   if(times%30==0)LED0=!LED0;//&Eacute;&Aacute;&Euml;&cedil;LED,&Igrave;á&Ecirc;&frac34;&Iuml;&micro;&Iacute;&sup3;&Otilde;&yacute;&Ocirc;&Uacute;&Ocirc;&Euml;&ETH;&ETH;.
   delay_ms(10);   
  }
}
}




#include "sys.h"
#include "usart.h"
//////////////////////////////////////////////////////////////////////////////////   
//&Egrave;&ccedil;&sup1;&ucirc;&Ecirc;&sup1;&Oacute;&Atilde;ucos,&Ocirc;ò°ü&Agrave;¨&Iuml;&Acirc;&Atilde;&aelig;&micro;&Auml;&Iacute;·&Icirc;&Auml;&frac14;&thorn;&frac14;&acute;&iquest;&Eacute;.
#if SYSTEM_SUPPORT_UCOS
#include "includes.h"     //ucos &Ecirc;&sup1;&Oacute;&Atilde;   
#endif

#if 1
#pragma import(__use_no_semihosting)            
//±ê×&frac14;&iquest;&acirc;&ETH;è&Ograve;&ordf;&micro;&Auml;&Ouml;§&sup3;&Ouml;&ordm;&macr;&Ecirc;&yacute;                 
struct __FILE
{
int handle;
};

FILE __stdout;      
//&para;¨&Ograve;&aring;_sys_exit()&Ograve;&Ocirc;±&Uuml;&Atilde;&acirc;&Ecirc;&sup1;&Oacute;&Atilde;°&euml;&Ouml;÷&raquo;ú&Auml;&pound;&Ecirc;&frac12;   
_sys_exit(int x)
{
x = x;
}
//&Ouml;&Oslash;&para;¨&Ograve;&aring;fputc&ordm;&macr;&Ecirc;&yacute;
int fputc(int ch, FILE *f)
{  
while((USART3->SR&0X40)==0);//&Ntilde;&shy;&raquo;··&cent;&Euml;&Iacute;,&Ouml;±&micro;&frac12;·&cent;&Euml;&Iacute;&Iacute;ê±&Iuml;   
USART3->DR = (u8) ch;      
return ch;
}
#endif

#if EN_USART3_RX   //&Egrave;&ccedil;&sup1;&ucirc;&Ecirc;&sup1;&Auml;&Uuml;&Aacute;&Euml;&frac12;&Oacute;&Ecirc;&Otilde;
//&acute;&reg;&iquest;&Uacute;1&Ouml;&ETH;&para;&Iuml;·&thorn;&Icirc;&ntilde;&sup3;&Igrave;&ETH;ò
//×&cent;&Ograve;&acirc;,&para;&Aacute;&Egrave;&iexcl;USARTx->SR&Auml;&Uuml;±&Uuml;&Atilde;&acirc;&Auml;&ordf;&Atilde;&ucirc;&AElig;&auml;&Atilde;&icirc;&micro;&Auml;&acute;í&Icirc;ó   
u8 USART_RX_BUF[USART_REC_LEN];     //&frac12;&Oacute;&Ecirc;&Otilde;&raquo;&ordm;&sup3;&aring;,×&icirc;&acute;óUSART_REC_LEN&cedil;&ouml;×&Ouml;&frac12;&Uacute;.
//&frac12;&Oacute;&Ecirc;&Otilde;×&acute;&Igrave;&not;
//bit15&pound;&not; &frac12;&Oacute;&Ecirc;&Otilde;&Iacute;ê&sup3;&Eacute;±ê&Ouml;&frac34;
//bit14&pound;&not; &frac12;&Oacute;&Ecirc;&Otilde;&micro;&frac12;0x0d
//bit13~0&pound;&not; &frac12;&Oacute;&Ecirc;&Otilde;&micro;&frac12;&micro;&Auml;&Oacute;&ETH;&ETH;§×&Ouml;&frac12;&Uacute;&Ecirc;&yacute;&Auml;&iquest;
u16 USART_RX_STA=0;       //&frac12;&Oacute;&Ecirc;&Otilde;×&acute;&Igrave;&not;±ê&frac14;&Ccedil;

//&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;IO &acute;&reg;&iquest;&Uacute;1
//bound:&sup2;¨&Igrave;&Oslash;&Acirc;&Ecirc;
void uart_init(u32 bound){
   //GPIO&para;&Euml;&iquest;&Uacute;&Eacute;è&Ouml;&Atilde;
  GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); //&Ecirc;&sup1;&Auml;&Uuml;GPIOA&Ecirc;±&Ouml;&Oacute;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//&Ecirc;&sup1;&Auml;&Uuml;USART1&Ecirc;±&Ouml;&Oacute;

//&acute;&reg;&iquest;&Uacute;1&para;&Ocirc;&Oacute;&brvbar;&Ograve;&yacute;&frac12;&Aring;&cedil;&acute;&Oacute;&Atilde;&Oacute;&sup3;&Eacute;&auml;
GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3); //GPIOA9&cedil;&acute;&Oacute;&Atilde;&Icirc;&ordf;USART1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3); //GPIOA10&cedil;&acute;&Oacute;&Atilde;&Icirc;&ordf;USART1

//USART1&para;&Euml;&iquest;&Uacute;&Aring;&auml;&Ouml;&Atilde;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10; //GPIOA9&Oacute;&euml;GPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//&cedil;&acute;&Oacute;&Atilde;&sup1;&brvbar;&Auml;&Uuml;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //&Euml;&Ugrave;&para;&Egrave;50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //&Iacute;&AElig;&Iacute;ì&cedil;&acute;&Oacute;&Atilde;&Ecirc;&auml;&sup3;&ouml;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //&Eacute;&Iuml;&Agrave;&shy;
GPIO_Init(GPIOB,&GPIO_InitStructure); //&sup3;&otilde;&Ecirc;&frac14;&raquo;&macrB11&pound;&notB10
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_5);//GPIO_Pin_10|GPIO_Pin_11|
   //USART1 &sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;&Eacute;è&Ouml;&Atilde;
USART_InitStructure.USART_BaudRate = bound;//&sup2;¨&Igrave;&Oslash;&Acirc;&Ecirc;&Eacute;è&Ouml;&Atilde;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×&Ouml;&sup3;¤&Icirc;&ordf;8&Icirc;&raquo;&Ecirc;&yacute;&frac34;&Yacute;&cedil;&ntilde;&Ecirc;&frac12;
USART_InitStructure.USART_StopBits = USART_StopBits_1;//&Ograve;&raquo;&cedil;&ouml;&Iacute;&pound;&Ouml;&sup1;&Icirc;&raquo;
USART_InitStructure.USART_Parity = USART_Parity_No;//&Icirc;&THORN;&AElig;&aelig;&Aring;&frac14;&ETH;&pound;&Ntilde;é&Icirc;&raquo;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//&Icirc;&THORN;&Oacute;&sup2;&frac14;&thorn;&Ecirc;&yacute;&frac34;&Yacute;&Aacute;÷&iquest;&Oslash;&Ouml;&AElig;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //&Ecirc;&Otilde;·&cent;&Auml;&pound;&Ecirc;&frac12;
  USART_Init(USART3, &USART_InitStructure); //&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;&acute;&reg;&iquest;&Uacute;1

  USART_Cmd(USART3, ENABLE);  //&Ecirc;&sup1;&Auml;&Uuml;&acute;&reg;&iquest;&Uacute;1

USART_ClearFlag(USART3, USART_FLAG_TC);

#if EN_USART3_RX
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//&iquest;&ordf;&AElig;&ocirc;&Iuml;à&sup1;&Oslash;&Ouml;&ETH;&para;&Iuml;

//Usart1 NVIC &Aring;&auml;&Ouml;&Atilde;
  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;//&acute;&reg;&iquest;&Uacute;1&Ouml;&ETH;&para;&Iuml;&Iacute;¨&micro;&Agrave;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//&Ccedil;&Agrave;&Otilde;&frac14;&Oacute;&Aring;&Iuml;&Egrave;&frac14;&para;3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;  //×&Oacute;&Oacute;&Aring;&Iuml;&Egrave;&frac14;&para;3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   //IRQ&Iacute;¨&micro;&Agrave;&Ecirc;&sup1;&Auml;&Uuml;
NVIC_Init(&NVIC_InitStructure); //&cedil;ù&frac34;&Yacute;&Ouml;&cedil;&para;¨&micro;&Auml;&sup2;&Icirc;&Ecirc;&yacute;&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;VIC&frac14;&Auml;&acute;&aelig;&AElig;÷&iexcl;&cent;

#endif

}


void USART3_IRQHandler(void)                 //&acute;&reg;&iquest;&Uacute;1&Ouml;&ETH;&para;&Iuml;·&thorn;&Icirc;&ntilde;&sup3;&Igrave;&ETH;ò
{
u8 Res;
#ifdef OS_TICKS_PER_SEC   //&Egrave;&ccedil;&sup1;&ucirc;&Ecirc;±&Ouml;&Oacute;&frac12;&Uacute;&Aring;&Auml;&Ecirc;&yacute;&para;¨&Ograve;&aring;&Aacute;&Euml;,&Euml;&micro;&Atilde;÷&Ograve;&ordf;&Ecirc;&sup1;&Oacute;&Atilde;ucosII&Aacute;&Euml;.
OSIntEnter();   
#endif
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)  //&frac12;&Oacute;&Ecirc;&Otilde;&Ouml;&ETH;&para;&Iuml;(&frac12;&Oacute;&Ecirc;&Otilde;&micro;&frac12;&micro;&Auml;&Ecirc;&yacute;&frac34;&Yacute;±&Oslash;&ETH;&euml;&Ecirc;&Ccedil;0x0d 0x0a&frac12;á&Icirc;&sup2;)
{
  Res =USART_ReceiveData(USART3);//(USART1->DR); //&para;&Aacute;&Egrave;&iexcl;&frac12;&Oacute;&Ecirc;&Otilde;&micro;&frac12;&micro;&Auml;&Ecirc;&yacute;&frac34;&Yacute;
  
  if((USART_RX_STA&0x8000)==0)//&frac12;&Oacute;&Ecirc;&Otilde;&Icirc;&acute;&Iacute;ê&sup3;&Eacute;
  {
   if(USART_RX_STA&0x4000)//&frac12;&Oacute;&Ecirc;&Otilde;&micro;&frac12;&Aacute;&Euml;0x0d
   {
    if(Res!=0x0a)USART_RX_STA=0;//&frac12;&Oacute;&Ecirc;&Otilde;&acute;í&Icirc;ó,&Ouml;&Oslash;&ETH;&Acirc;&iquest;&ordf;&Ecirc;&frac14;
    else USART_RX_STA|=0x8000; //&frac12;&Oacute;&Ecirc;&Otilde;&Iacute;ê&sup3;&Eacute;&Aacute;&Euml;
   }
   else //&raquo;&sup1;&Atilde;&raquo;&Ecirc;&Otilde;&micro;&frac12;0X0D
   {
    if(Res==0x0d)USART_RX_STA|=0x4000;
    else
    {
     USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
     USART_RX_STA++;
     if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//&frac12;&Oacute;&Ecirc;&Otilde;&Ecirc;&yacute;&frac34;&Yacute;&acute;í&Icirc;ó,&Ouml;&Oslash;&ETH;&Acirc;&iquest;&ordf;&Ecirc;&frac14;&frac12;&Oacute;&Ecirc;&Otilde;   
    }   
   }
  }      
  }
#ifdef OS_TICKS_PER_SEC   //&Egrave;&ccedil;&sup1;&ucirc;&Ecirc;±&Ouml;&Oacute;&frac12;&Uacute;&Aring;&Auml;&Ecirc;&yacute;&para;¨&Ograve;&aring;&Aacute;&Euml;,&Euml;&micro;&Atilde;÷&Ograve;&ordf;&Ecirc;&sup1;&Oacute;&Atilde;ucosII&Aacute;&Euml;.
OSIntExit();              
#endif
}
#endif






友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。