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

求助:帮忙看下我的串口程序

[复制链接]
二宽 提问时间:2015-5-14 12:42 /
#include "stm32f30x.h"
#define uint unsigned int
               
int main()
{
   
          GPIO_InitTypeDef GPIO_InitStructure;
          USART_InitTypeDef USART_InitStructure;
                RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//enable clk
          RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
                GPIO_DeInit(GPIOA);
          USART_DeInit(USART1);
       
          GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
       
          GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_9;//TX
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
                GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
       
          GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//LED
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
       
                USART_InitStructure.USART_BaudRate = 115200;//UART
                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_Rx | USART_Mode_Tx;
                USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
       

          GPIO_Init(GPIOA, &GPIO_InitStructure);
                USART_Init(USART1, &USART_InitStructure);
               
                USART_Cmd(USART1, ENABLE);//EN UART
               

                       
                USART_SendData(USART1, 0X55);
               
                while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==1)
                {
               
                GPIO_SetBits(GPIOA, GPIO_Pin_5);
                }
       
       
}
赞 收藏 评论19 发布时间:2015-5-14 12:42

举报

19个回答
二宽 回答时间:2015-5-15 15:26:43
结贴

#include "stm32f30x.h"
#define uint unsigned int

        uint i,j,k;

void Delay(i)
                {
                for(j=0;j<=i;j++)
                        ;
                }


int main()
{
   
          GPIO_InitTypeDef GPIO_InitStructure;
          USART_InitTypeDef USART_InitStructure;
                RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//enable clk
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
                GPIO_DeInit(GPIOA);
          USART_DeInit(USART1);
       
          GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
       
          GPIO_Init(GPIOA, &GPIO_InitStructure);
          GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
       
          GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_9;//TX
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
                GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
       
          GPIO_Init(GPIOA, &GPIO_InitStructure);
          GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
       
          GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//LED
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
               
                GPIO_Init(GPIOA, &GPIO_InitStructure);
       
                USART_InitStructure.USART_BaudRate = 115200;//UART
                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_Rx | USART_Mode_Tx;
                USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
          
                USART_Init(USART1, &USART_InitStructure);
               
                USART_Cmd(USART1, ENABLE);//EN UART
                USART_ClearFlag(USART1,USART_FLAG_TC);
                       
               
                while(1)
                {
                USART_SendData(USART1, 0X55);
                while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==1)
                {
               
                GPIO_SetBits(GPIOA, GPIO_Pin_5);
                Delay(500);
                       
                GPIO_ResetBits(GPIOA, GPIO_Pin_5);
                        Delay(500);
                }
               
        }
       
}
二宽 回答时间:2015-5-14 14:58:18
#include "stm32f30x.h"
#define uint unsigned int
               
int main()
{
   
          GPIO_InitTypeDef GPIO_InitStructure;
          USART_InitTypeDef USART_InitStructure;
                RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//enable clk
          RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
                GPIO_DeInit(GPIOA);
          USART_DeInit(USART1);
       
          GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
       
          GPIO_Init(GPIOA, &GPIO_InitStructure);
          GPIO_PinAFConfig(GPIOA, GPIO_Pin_10, GPIO_AF_7);
       
          GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_9;//TX
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
                GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
       
          GPIO_Init(GPIOA, &GPIO_InitStructure);
          GPIO_PinAFConfig(GPIOA, GPIO_Pin_9, GPIO_AF_7);
       
          GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//LED
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
               
                GPIO_Init(GPIOA, &GPIO_InitStructure);
       
                USART_InitStructure.USART_BaudRate = 9600;//UART
                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_Rx | USART_Mode_Tx;
                USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
          
                USART_Init(USART1, &USART_InitStructure);
               
                USART_Cmd(USART1, ENABLE);//EN UART
                USART_ClearFlag(USART1,USART_FLAG_TC);
                       
                USART_SendData(USART1, 0X0055);
               
                while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==1)
                {
               
                GPIO_SetBits(GPIOA, GPIO_Pin_5);
                       
                }
               
                GPIO_ResetBits(GPIOA, GPIO_Pin_5);
       
       
}
修改后还是不行,主芯片是STM32F334R8
二宽 回答时间:2015-5-14 14:59:41
小蚂蚁快溜跑 发表于 2015-5-14 14:08
void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
缺少端口复用的 ...

不行呀 ,  你有串口代码吗? 给我看一下呗 ,谢谢啦
二宽 回答时间:2015-5-14 12:43:29
串口助手收到的都是000000~
Winddddd 回答时间:2015-5-14 14:04:57
引脚配置不同功能,每一个引脚都要调用一次GPIO_Init(),你这样写相当于只配置了最后一个引脚PA5,还有要调用GPIO_PinAFConfig()配置复用功能
小蚂蚁快溜跑 回答时间:2015-5-14 14:08:27
void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
缺少端口复用的吧,你试试
lkl0305 回答时间:2015-5-14 14:19:15
端口复用吧?
废鱼 回答时间:2015-5-14 14:46:32
三楼说的正确,GPIO配置不对,A9 A10默认是串口不用映射,如果是B6 B7需要映射。
二宽 回答时间:2015-5-14 14:58:57

还是不行,你有代码?能给参考一下吗?谢谢了
二宽 回答时间:2015-5-14 14:59:13
Winddddd 发表于 2015-5-14 14:04
引脚配置不同功能,每一个引脚都要调用一次GPIO_Init(),你这样写相当于只配置了最后一个引脚PA5,还有要调 ...

还是不行,
二宽 回答时间:2015-5-14 15:35:01
我感觉是没有发出数据来,我的发送数据有问题吗?
Winddddd 回答时间:2015-5-14 15:53:45

可以看看我前段时间弄串口时写的笔记
http://blog.sina.com.cn/s/blog_d2edb6010102vewi.html
为什么是EEFOCUS小白 回答时间:2015-5-14 16:47:13
不懂帮顶
lkl0305 回答时间:2015-5-14 16:54:45
tjhbwjk 发表于 2015-5-14 14:58
还是不行,你有代码?能给参考一下吗?谢谢了

不知道你用的是哪个库,里面官网应该提供有实例,你可以参考下
二宽 回答时间:2015-5-15 15:25:17
lkl0305 发表于 2015-5-14 16:54
不知道你用的是哪个库,里面官网应该提供有实例,你可以参考下

我改好了  帅哥,谢谢了
12下一页

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版