STM32F407串口3一直不好使!!真心调不出毛病!

2019-07-20 23:41发布

#include "sys.h"
#include "usart.h"
//////////////////////////////////////////////////////////////////////////////////   
//Èç¹ûʹÓÃucos,Ôò°üÀ¨ÏÂÃæµÄÍ·Îļþ¼´¿É.
#if SYSTEM_SUPPORT_UCOS
#include "includes.h"     //ucos ʹÓà  
#endif
//////////////////////////////////////////////////////////////////////////////////  
//±¾³ÌÐòÖ»¹©Ñ§Ï°Ê¹Óã¬Î´¾­×÷ÕßÐí¿É£¬²»µÃÓÃÓÚÆäËüÈκÎÓÃ;
//ALIENTEK STM32F4̽Ë÷Õß¿ª·¢°å
//´®¿Ú1³õʼ»¯     
//ÕýµãÔ­×Ó@ALIENTEK
//¼¼ÊõÂÛ̳:www.openedv.com
//ÐÞ¸ÄÈÕÆÚ:2014/6/10
//°æ±¾£ºV1.5
//°æȨËùÓУ¬µÁ°æ±Ø¾¿¡£
//Copyright(C) ¹ãÖÝÊÐÐÇÒíµç×ӿƼ¼ÓÐÏÞ¹«Ë¾ 2009-2019
//All rights reserved
//********************************************************************************
//V1.3ÐÞ¸Ä˵Ã÷
//Ö§³ÖÊÊÓ¦²»Í¬ÆµÂÊϵĴ®¿Ú²¨ÌØÂÊÉèÖÃ.
//¼ÓÈëÁ˶ÔprintfµÄÖ§³Ö
//Ôö¼ÓÁË´®¿Ú½ÓÊÕÃüÁÄÜ.
//ÐÞÕýÁËprintfµÚÒ»¸ö×Ö·û¶ªÊ§µÄbug
//V1.4ÐÞ¸Ä˵Ã÷
//1,Ð޸Ĵ®¿Ú³õʼ»¯IOµÄbug
//2,ÐÞ¸ÄÁËUSART_RX_STA,ʹµÃ´®¿Ú×î´ó½ÓÊÕ×Ö½ÚÊýΪ2µÄ14´Î·½
//3,Ôö¼ÓÁËUSART_REC_LEN,ÓÃÓÚ¶¨Òå´®¿Ú×î´óÔÊÐí½ÓÊÕµÄ×Ö½ÚÊý(²»´óÓÚ2µÄ14´Î·½)
//4,ÐÞ¸ÄÁËEN_USART1_RXµÄʹÄÜ·½Ê½
//V1.5ÐÞ¸Ä˵Ã÷
//1,Ôö¼ÓÁ˶ÔUCOSIIµÄÖ§³Ö
//////////////////////////////////////////////////////////////////////////////////   

//////////////////////////////////////////////////////////////////
//¼ÓÈëÒÔÏ´úÂë,Ö§³Öprintfº¯Êý,¶ø²»ÐèҪѡÔñuse MicroLIB   
#if 1
#pragma import(__use_no_semihosting)            
//±ê×¼¿âÐèÒªµÄÖ§³Öº¯Êý                 
struct __FILE
{
int handle;
};
FILE __stdout;      
//¶¨Òå_sys_exit()ÒÔ±ÜÃâʹÓðëÖ÷»úģʽ   
_sys_exit(int x)
{
x = x;
}
//Öض¨Òåfputcº¯Êý
int fputc(int ch, FILE *f)
{  
while((USART1->SR&0X40)==0);//Ñ­»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï   
USART1->DR = (u8) ch;      
return ch;
}
#endif

#if EN_USART1_RX   //Èç¹ûʹÄÜÁ˽ÓÊÕ
//´®¿Ú1ÖжϷþÎñ³ÌÐò
//×¢Òâ,¶ÁÈ¡USARTx->SRÄܱÜÃâĪÃûÆäÃîµÄ´íÎó   
u8 USART_RX_BUF[USART_REC_LEN];     //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.
//½ÓÊÕ״̬
//bit15£¬ ½ÓÊÕÍê³É±êÖ¾
//bit14£¬ ½ÓÊÕµ½0x0d
//bit13~0£¬ ½ÓÊÕµ½µÄÓÐЧ×Ö½ÚÊýÄ¿
u16 USART_RX_STA=0;       //½ÓÊÕ״̬±ê¼Ç
//³õʼ»¯IO ´®¿Ú1
//bound:²¨ÌØÂÊ
void uart_init(u32 bound){
   //GPIO¶Ë¿ÚÉèÖÃ
  GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;



RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕͨÊä³öģʽ
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ­
  GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯
GPIO_SetBits(GPIOB,GPIO_Pin_5);//GPIOB5ÖÃ1

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //ʹÄÜGPIOAʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//ʹÄÜUSART1ʱÖÓ


//´®¿Ú1¶ÔÓ¦Òý½Å¸´ÓÃÓ³Éä
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9¸´ÓÃΪUSART1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10¸´ÓÃΪUSART1

//USART1¶Ë¿ÚÅäÖÃ
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9ÓëGPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //ËÙ¶È50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //ÉÏÀ­
GPIO_Init(GPIOA,&GPIO_InitStructure); //³õʼ»&macrA9£&notA10



   //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);

#if EN_USART1_RX
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿ªÆôÏà¹ØÖжÏ
//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¼Ä´æÆ÷¡¢
#endif

}

void USART1_IRQHandler(void)                 //´®¿Ú1ÖжϷþÎñ³ÌÐò
{
u8 Res;
#ifdef OS_TICKS_PER_SEC   //Èç¹ûʱÖÓ½ÚÅÄÊý¶¨ÒåÁË,˵Ã÷ҪʹÓÃucosIIÁË.
OSIntEnter();   
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //½ÓÊÕÖжÏ(½ÓÊÕµ½µÄÊý¾Ý±ØÐëÊÇ0x0d 0x0a½áβ)
{
  Res =USART_ReceiveData(USART1);//(USART1->DR); //¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
  
  if((USART_RX_STA&0x8000)==0)//½ÓÊÕδÍê³É
  {
   if(USART_RX_STA&0x4000)//½ÓÊÕµ½ÁË0x0d
   {
    if(Res!=0x0a)USART_RX_STA=0;//½ÓÊÕ´íÎó,ÖØпªÊ¼
    else USART_RX_STA|=0x8000; //½ÓÊÕÍê³ÉÁË
   }
   else //»¹Ã»ÊÕµ½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;//½ÓÊÕÊý¾Ý´íÎó,ÖØпªÊ¼½ÓÊÕ   
    }   
   }
  }      
  }
#ifdef OS_TICKS_PER_SEC   //Èç¹ûʱÖÓ½ÚÅÄÊý¶¨ÒåÁË,˵Ã÷ҪʹÓÃucosIIÁË.
OSIntExit();              
#endif
}
#endif


#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);   
  }
}
}
这是原子哥的代码我改的怎么一直不好使55555555
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
Sun_Fly
1楼-- · 2019-07-21 00:25
需要重映射一下,我以前也遇到过这问题
Sun_Fly
2楼-- · 2019-07-21 03:27
串口3需要重映射,其他用不用需要看手册了

一周热门 更多>