打印
[应用相关]

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

[复制链接]
1105|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
八层楼|  楼主 | 2018-12-14 10:28 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
一、关于STM32F4在通过串口通信的时候乱码问题
1.刚开始弄得时候,以为和stm32一样配置完串口之后就可以用了,结果后面发现串口打印出来的东西全是乱码。后面发现是STM32F4的固件库中使用的频率是25Mhz,而板子上的外部晶振是8Mhz的。因此需要去固件库的stm32f4xx.h中把HSE_VALUE改成8Mhz就可以了。


沙发
八层楼|  楼主 | 2018-12-14 10:28 | 只看该作者
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.\r\n ");
    }
}




#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 %d\r\n", file, line) */


  /* Infinite loop */
  while (1)
  {
  }
}
#endif


/**
  * @}
  */


/**
  * @}
  */




使用特权

评论回复
板凳
八层楼|  楼主 | 2018-12-14 10:29 | 只看该作者
#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;
}


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

83

主题

3887

帖子

2

粉丝