4819|1

2

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

STM32F207串口问题 [复制链接]

选用的CPU是 STM32F207ZG,自己就写了个简单的串口1程序,想在串口调试助手上看到printf中的内容。在keil4里建立工程,并编写程序,编译没有问题,但是烧写到开发板里没有现象。不知道错在哪里,请大家帮忙看看!我用的是STM32F2xx_StdPeriph_Lib_V1.0.0库函数。谢谢了!
main.c:
#include "stm32f2xx.h"
#include "usart.h"
#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  #else
   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  #endif /* __GNUC__ */
int main(void)
{
  SystemInit();   /*配置系统时钟为120M*/
Usart_Config();  /*配置串口*/

   while (1)
    {
      printf("\r\ntest gprs\n");
  }
}
  PUTCHAR_PROTOTYPE
{
  /* 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_TC) == RESET)
  {}
  return ch;
}
usart.c:
#include "usart.h"
  void Usart_Config(void)
{
   GPIO_InitTypeDef GPIO_InitStructure;
   USART_InitTypeDef USART_InitStructure;
   /*   typedef struct
    {
    u32 USART_BaudRate;   波特率
    u16 USART_WordLength;  一个帧中传输或者接收到的数据位数
    u16 USART_StopBits;   停止位数目
    u16 USART_Parity;   奇偶模式
    u16 USART_HardwareFlowControl;   硬件流控制模式
    u16 USART_Mode;     使能或者失能发送和接收模式
    u16 USART_Clock;     USART时钟使能或者失能
    u16 USART_CPOL;   指定SLCK引脚上时钟输出的极性
    u16 USART_CPHA;   指定了SLCK引脚上时钟输出的相位
    u16 USART_LastBit;  控制是否在同步模式下,在SCLK引脚上输出的最后发送的那个数据字(MSB)对应的时钟脉冲                        
    } USART_InitTypeDef;
   */
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);    //配置GPIOA和USART2时钟
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
   
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);  

   /*配置串口2的TX*/
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //最高输出速率50MHz
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;    //AF复用
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;    //PP推挽式
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
   GPIO_Init(GPIOA, &GPIO_InitStructure);    // void GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)

   /*配置串口2的RX*/
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;    //OD Open-Drain 开漏
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
   GPIO_Init(GPIOA, &GPIO_InitStructure);    // void GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)

   /*配置串口2的模式*/
   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(USART1, &USART_InitStructure);   // void USART_Init(USART_TypeDef *USARTx,)
   USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
   USART_Cmd(USART1, ENABLE);  // void USART_Cmd(USART_TypeDef *USARTx, FunctionalState NewState)
}
此帖出自stm32/stm8论坛
点赞 关注
 

回复
举报

2

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
怎么没人回复?
此帖出自stm32/stm8论坛
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表