你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

查看: 2233|回复: 10

帮忙看看这个串口程序怎么不对

[复制链接]

2

主题

13

回帖

0

蝴蝶豆

新手上路

最后登录
2018-2-1
发表于 2012-6-3 20:46:44 | 显示全部楼层 |阅读模式
#include "stm32f10x.h"
void USART1_Init(void)
{
USART_InitTypeDef USART_InitStructure;
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_Mode=USART_Mode_Tx|USART_Mode_Rx;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
void USART1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure; 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void Delay(vu32 nCount)
{
for(;nCount!=0;nCount--);
}
int main(void)
{
USART1_Init();
USART1_GPIO_Config();
while(1)
{
USART_SendData(USART1,‘a’);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
Delay(0xfff);
}
}
 
回复

使用道具 举报

2

主题

13

回帖

0

蝴蝶豆

新手上路

最后登录
2018-2-1
 楼主| 发表于 2012-6-3 20:57:47 | 显示全部楼层

RE:帮忙看看这个串口程序怎么不对

...
回复 支持 反对

使用道具 举报

5

主题

16

回帖

0

蝴蝶豆

新手上路

最后登录
1970-1-1
发表于 2012-6-4 09:51:07 | 显示全部楼层

RE:帮忙看看这个串口程序怎么不对

串口的rcc没有enbale
回复 支持 反对

使用道具 举报

1

主题

5

回帖

0

蝴蝶豆

新手上路

最后登录
2020-7-24
发表于 2012-6-4 11:11:03 | 显示全部楼层

RE:帮忙看看这个串口程序怎么不对

同意楼上,串口时钟没开
回复 支持 反对

使用道具 举报

2

主题

13

回帖

0

蝴蝶豆

新手上路

最后登录
2018-2-1
 楼主| 发表于 2012-6-4 19:43:24 | 显示全部楼层

RE:帮忙看看这个串口程序怎么不对

你的意思是RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
这一句吗
回复 支持 反对

使用道具 举报

2

主题

13

回帖

0

蝴蝶豆

新手上路

最后登录
2018-2-1
 楼主| 发表于 2012-6-4 19:44:35 | 显示全部楼层

RE:帮忙看看这个串口程序怎么不对

有一句RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
这个也一样的吧
回复 支持 反对

使用道具 举报

24

主题

591

回帖

0

蝴蝶豆

中级会员

最后登录
2020-12-2
发表于 2012-6-4 20:24:40 | 显示全部楼层

RE:帮忙看看这个串口程序怎么不对

好像你的时钟没有打开哦!
回复 支持 反对

使用道具 举报

24

主题

591

回帖

0

蝴蝶豆

中级会员

最后登录
2020-12-2
发表于 2012-6-4 20:28:43 | 显示全部楼层

RE:帮忙看看这个串口程序怎么不对

看看这个一定可以//======================================================
void USART1_Configuration(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
       
        /* Enable USART1 clock */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
        //USART1
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;                 //USART1 TX
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;    //复用推挽输出
        GPIO_Init(GPIOA, &GPIO_InitStructure);                    //A端口
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;                 //USART1 RX
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   //复用开漏输入
        GPIO_Init(GPIOA, &GPIO_InitStructure);                         //A端口
          
    /* USART1 configuration ------------------------------------------------------*/
        /* USART and USART2 configured as follow:
              - BaudRate = 115200 baud  
              - Word Length = 8 Bits
              - One Stop Bit
              - No parity
              - Hardware flow control disabled (RTS and CTS signals)
              - Receive and transmit enabled
        */
        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;
       
        /* Configure USART1 */
        USART_Init(USART1, &USART_InitStructure);
        /* Enable USART1 Receive and Transmit interrupts */
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
        USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
       
        /* Enable the USART1 */
        USART_Cmd(USART1, ENABLE);
}:
回复 支持 反对

使用道具 举报

2

主题

13

回帖

0

蝴蝶豆

新手上路

最后登录
2018-2-1
 楼主| 发表于 2012-6-5 20:05:30 | 显示全部楼层

回复:帮忙看看这个串口程序怎么不对

我感觉都一样呀,怎么我的不行呀
回复 支持 反对

使用道具 举报

2

主题

13

回帖

0

蝴蝶豆

新手上路

最后登录
2018-2-1
 楼主| 发表于 2012-6-5 20:09:57 | 显示全部楼层

RE:帮忙看看这个串口程序怎么不对

哈哈,终于找到问题了
结果是这个串口的GPIO定义必须要在USART初始化前面
回复 支持 反对

使用道具 举报

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版