打印

stm32f103C8T6的UART2配置

[复制链接]
11513|10
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
mm7989680|  楼主 | 2013-1-6 17:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
stm32f103C8T6的UART2配置,现在配好后UART2有输出 但是输出不对。请高手指点啊
        USART2_SendData(0x80);时串口调试助手收到 00
                     送00 收到00,送01 收到03, 送0xa1收到0x83, 送0x10收到0x40
配置如下
void GPIO_Configuration(void)
        {
          GPIO_InitTypeDef GPIO_InitStructure;
          RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOB , ENABLE  );  //这里如果没有 RCC_APB2Periph_GPIOA 就没输出
//          RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE  );
           /* Configure USART2 Tx (PA.02) as alternate function push-pull */
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;                                 //        选中管脚2
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                 // 复用推挽输出
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;                 // 最高输出速率50MHz
          GPIO_Init(GPIOA, &GPIO_InitStructure);                                 // 选择A端口
            
          /* Configure USART2 Rx (PA.03) as input floating */
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;                          //选中管脚3
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;          //浮空输入
          GPIO_Init(GPIOA, &GPIO_InitStructure);                                  //选择A端口

          /*I2C I/O口配置 PB6,PB7 */
      GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;  
          GPIO_Init(GPIOB, &GPIO_InitStructure);
       
          GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
          GPIO_Init(GPIOB, &GPIO_InitStructure);

          /*按键输入 I/O口配置 PB14 */
          GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
          GPIO_Init(GPIOB, &GPIO_InitStructure);

           /* Configure LED (PB.8) as PP OUT */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;                          //选中管脚8
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;          //推拉输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;                 // 最高输出速率2MHz
  GPIO_Init(GPIOB, &GPIO_InitStructure);                                  //选择B端口

        }

*************************
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef  USART_ClockInitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE  );  //RCC_APB1Periph_TIM2 |

USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;                        // 时钟低电平活动
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;                                // 时钟低电平
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;                                // 时钟第二个边沿进行数据捕获
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;                // 最后一位数据的时钟脉冲不从SCLK输出
/* Configure the USART2 synchronous paramters */
USART_ClockInit(USART2, &USART_ClockInitStructure);                                        // 时钟参数初始化设置
                                                                                                                                         
USART_InitStructure.USART_BaudRate = 9600;                                                  // 波特率为:9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b;                          // 8位数据
USART_InitStructure.USART_StopBits = USART_StopBits_1;                                  // 在帧结尾传输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;                  // 发送使能+接收使能
/* Configure USART2 basic and asynchronous paramters */
USART_Init(USART2, &USART_InitStructure);
   
  /* Enable USART2 */
USART_ClearFlag(USART2, USART_IT_RXNE);                         //清中断,以免一启用中断后立即产生中断
USART_ITConfig(USART2,USART_IT_RXNE, ENABLE);                //使能USART2中断源
USART_Cmd(USART2, ENABLE);                                                        //USART2总开关:开启
}
沙发
uet_cache| | 2013-1-6 18:07 | 只看该作者
检查双方波特率是不是相同的。。

使用特权

评论回复
板凳
lirunze| | 2013-1-6 20:25 | 只看该作者
配置应该是没问题的啊

使用特权

评论回复
地板
13249207512| | 2013-1-6 20:28 | 只看该作者
复用时钟AFIO没有开哦

使用特权

评论回复
5
jomosiron| | 2013-1-6 21:39 | 只看该作者
应该是波特率问题吧

使用特权

评论回复
6
mm7989680|  楼主 | 2013-1-7 10:47 | 只看该作者
自己解决了,在stm32f10x.h中加一句 #define HSE_Value    ((uint32_t)6000000)因为我外部时钟6M
其中在system_stm32f10x.c中有#define SYSCLK_FREQ_72MHz  72000000

使用特权

评论回复
7
mm7989680|  楼主 | 2013-1-7 10:48 | 只看该作者
感谢各位的提醒啊,STM32才入手,呵呵

使用特权

评论回复
8
mm7989680|  楼主 | 2013-1-7 10:51 | 只看该作者
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);这句不加好像也是正确的
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB , ENABLE  );这里面RCC_APB2Periph_GPIOA不能少
这里的疑惑是既然UART2是挂在APB1上的那有RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 |RCC_APB1Periph_USART2, ENABLE  );不就行了么,为什么还要把APB2的GPIOA也打开啊?

使用特权

评论回复
9
Lena.li| | 2013-1-7 14:46 | 只看该作者
你好,我們公司是專業IC代燒,技持各種型號的IC燒錄,如有需求,還請聯系,謝謝!!
       優普士(深圳)有限公司          
           聯系人:李**
           聯系方式Tel:18898594141
           分機:0755-27707166#815
             QQ:913755874

使用特权

评论回复
10
windqj| | 2014-3-6 10:38 | 只看该作者
楼主,我有疑问。RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOB , ENABLE  );
这一句里面。usart2是挂在APB1的。而你却使用APB2的时钟开启函数,这个没问题么?还有,我之前看到别人的串口配置函数里面,都没有去配置USART_ClockInit(USART2, &USART_ClockInitStructure);这个时钟参数初始化,请问这个配置了有什么作业,不配置呢?

使用特权

评论回复
11
wujitagongli| | 2014-5-14 16:48 | 只看该作者
mm7989680 发表于 2013-1-7 10:47
自己解决了,在stm32f10x.h中加一句 #define HSE_Value    ((uint32_t)6000000)因为我外部时钟6M
其中在sys ...

高手,我也遇到串口的问题,我也是用的72兆时钟,但是用的usart1.
怎么发都不对,弄了半个多月,找不到问题在哪,请问你所说的在
在stm32f10x.h中加一句 #define HSE_Value    ((uint32_t)6000000)
是什么意思,
我貌似没有涉及到外部时钟6M的问题,我就是一个输入捕获,几个数字输出量。

使用特权

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

本版积分规则

112

主题

371

帖子

4

粉丝