×
单片机 > 单片机程序设计 > 详情

关于STM32F407ZGT6的一些知识小结及串口1程序

发布时间:2020-09-02 发布时间:
|

一、关于STM32F4在通过串口通信的时候乱码问题 

1.刚开始弄得时候,以为和stm32一样配置完串口之后就可以用了,结果后面发现串口打印出来的东西全是乱码。后面发现是STM32F4的固件库中使用的频率是25Mhz,而板子上的外部晶振是8Mhz的。因此需要去固件库的stm32f4xx.h中把HSE_VALUE改成8Mhz就可以了。


2.还有一种问题是,如果使用的是电平转换的问题。要直接从芯片的引脚进行通信的话,可以直接用一个TTL下载器直接相连,如CH340;如果板子上带有MAX3232芯片的电平转换,要通过DB9插口线转RS232电平为TTL电平然后和电脑相连。


#include "stm32f4xx.h"

#include "stdio.h"

#include "uart.h"

void nvic_config(void)

{

    NVIC_InitTypeDef NVIC_InitStructure;

    /* NVIC configuration */

    /* Configure the Priority Group to 2 bits */

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);


    /* Enable the USARTx Interrupt */

    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

}

/**

  * @brief   Main program

  * @param  None

  * @retval None

  */

int main(void)

{

    nvic_config();

    uart_init();


    while(1)

    {

        printf("hello!welcome to F4..1H.rn ");

    }

}



#ifdef  USE_FULL_ASSERT


/**

  * @brief  Reports the name of the source file and the source line number

  *         where the assert_param error has occurred.

  * @param  file: pointer to the source file name

  * @param  line: assert_param error line source number

  * @retval None

  */

void assert_failed(uint8_t* file, uint32_t line)

  /* User can add his own implementation to report the file name and line number,

     ex: printf("Wrong parameters value: file %s on line %drn", file, line) */


  /* Infinite loop */

  while (1)

  {

  }

}

#endif


/**

  * @}

  */ 


/**

  * @}

  */ 


/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

#include "uart.h"

#include "stdio.h"


void uart_init(void)

{

    USART_InitTypeDef USART_InitStructure;

    GPIO_InitTypeDef GPIO_InitStructure;


    /* 开启GPIO_B的时钟 */

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

    /* 开启串口1的时钟 */

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);


    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;


    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6;

    GPIO_Init(GPIOB, &GPIO_InitStructure);


    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;

    GPIO_Init(GPIOB, &GPIO_InitStructure);



    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);



    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_Tx | USART_Mode_Rx;


    USART_Init(USART1, &USART_InitStructure);


    /* 使能串口1 */

    USART_Cmd(USART1, ENABLE);

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

}


int fputc(int ch, FILE *f)

{

    /* Place your implementation of fputc here */

    /* e.g. write a character to the USART */

    USART_SendData(USART1, (uint8_t) ch);


    /* Loop until the end of transmission */

    while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)

    {}


    return ch;

}




『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
51单片机中断源的扩展方法