F103求助USART3无法工作,MDK仿真能够输出,但板子不能打印

2019-07-14 16:00发布

代码如下:那位大神指点一下啊~


#include "STM32f10x.h"
#include "stdio.h"

void delay(__IO u32 nCount);

int main(void)
{
        /* USART1 config 115200 8-N-1 */
USART3_Config();

printf("   STM32f103 USART1 test");
        
//USART3_Config();

        while(1)
        {
printf(" TM32f103 USART3 test ");
        
//        for (;;) {
//}
}


        
        }


        
void delay(__IO u32 nCount)
{
        for (; nCount != 0; nCount--);
}




void USART3_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        
        /* config USART3 clock */
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 | RCC_APB2Periph_GPIOB, ENABLE);
        

        
        /* USART3 GPIO config */
        /* Configure USART3 Tx (PB.10) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);   
        /* Configure USART3 Rx (PB.11) as input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        
/* USART3 mode config */
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(USART3, &USART_InitStructure);
        

        
        /* ʹÄÜ´®¿Ú3½ÓÊÕÖÐ¶Ï */
        USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
        
        USART_Cmd(USART3, ENABLE);
}



//ÅäÖÃUSART3½ÓÊÕÖжÏ
void NVIC_Configuration(void)
{
        NVIC_InitTypeDef NVIC_InitStructure;
        /* Configure the NVIC Preemption Priority Bits */  
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
        
        /* Enable the USARTy Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;         
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
}




void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
{
  uint32_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;

  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd));
  assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority));  
  assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority));

  if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
  {
    /* Compute the Corresponding IRQ Priority --------------------------------*/   
    tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08;
    tmppre = (0x4 - tmppriority);
    tmpsub = tmpsub >> tmppriority;

    tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
    tmppriority |=  NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub;
    tmppriority = tmppriority << 0x04;

    NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;

    /* Enable the Selected IRQ Channels --------------------------------------*/
    NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
      (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  }
  else
  {
    /* Disable the Selected IRQ Channels -------------------------------------*/
    NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
      (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  }
}




#define AIRCR_VECTKEY_MASK    ((uint32_t)0x05FA0000)


void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
{
  /* Check the parameters */
  assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup));

  /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */
  SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;
}


/// Öض¨Ïòc¿âº¯Êýprintfµ½USART3
int fputc(int ch, FILE *f)
{
                /* ·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ýµ½USART3 */
                USART_SendData(USART3, (uint8_t) ch);
               
                /* µÈ´ý·¢ËÍÍê±Ï */
                while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);               
        
                return (ch);
}

/// Öض¨Ïòc¿âº¯Êýscanfµ½USART3
int fgetc(FILE *f)
{
                /* µÈ´ý´®¿Ú1ÊäÈëÊý¾Ý */
                while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);

                return (int)USART_ReceiveData(USART3);
}
/*********************************************END OF FILE**********************/
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
18条回答
60user114
1楼-- · 2019-07-14 22:32
:'(:'(:'(:'(:'(大神呢
csw_ying
2楼-- · 2019-07-15 00:50
你把那些乱码处理下,没法看
dingyang598
3楼-- · 2019-07-15 04:13
 精彩回答 2  元偷偷看……
mosen137
4楼-- · 2019-07-15 05:41
你这个代码  哎  :(:(:(
60user114
5楼-- · 2019-07-15 11:15
dingyang598 发表于 2018-11-15 18:53
没有把复用功能映射到IO口吧,有一个GPIO_PinAFConfig函数就是这个功能,可以试试

我用的是LQFP48脚封装的,不需要开吧!等会我开了试试。
60user114
6楼-- · 2019-07-15 14:32
重新整理了一下。

#include "stm32f10x.h"
#include "stdio.h"
#include "misc.h"
#include "led.h"

void delay(__IO u32 nCount);
void GPIO_Config(void);
void USART3_Config(void);


int main(void)
{
       
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 | RCC_APB2Periph_GPIOB,ENABLE);
        GPIO_Config();
        USART3_Config();
       
        while(1)
        {
               
                printf("adsfsfsfsfs");
               
        }
       
//        return (1);
       
        }


void delay(__IO u32 nCount)

{
        for (; nCount != 0; nCount--);
}


void GPIO_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART3 Tx (PB.10) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);   
        /* Configure USART3 Rx (PB.11) as input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void USART3_Config(void)
{
        USART_InitTypeDef USART_InitStructure;
        //* USART3 mode config */
USART_InitStructure.USART_BaudRate =9600;
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(USART3, &USART_InitStructure);
       
        USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
       
        USART_Cmd(USART3, ENABLE);

}


int fputc(int ch, FILE *f)
{
               
                USART_SendData(USART3, (uint8_t) ch);
               
               
                while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);               
       
                return (ch);
}


int fgetc(FILE *f)
{
               
                while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);

                return (int)USART_ReceiveData(USART3);
}

一周热门 更多>