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

查看: 2121|回复: 10

帮看看这个串口通信问题

[复制链接]

3

主题

12

回帖

0

蝴蝶豆

新手上路

最后登录
1970-1-1
发表于 2012-9-7 15:38:48 | 显示全部楼层 |阅读模式
//用的是STM32F107VCT  25MHz的外部晶振

//固件库是V3.5的

//想实现串口通信(USART2),却怎么也发不出去数据,求看看是哪里出错了,我新手。。。谢谢啦!


#include "stm32f10x.h"





void RCC_Configuration(void);

void GPIO_Configuration(void);

void USART_Configuration(void);




int main()

{

        vu16 i=0,k=0;

        u8 Reset[4]={0x56,0x00,0x26,0x00};

        u8 ReceiveCmd[9];

        RCC_Configuration();

        GPIO_Configuration();

        USART_Configuration();


<span style="font-family: Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22px; text-align: left; ">        for(i=0;i
回复

使用道具 举报

2

主题

240

回帖

0

蝴蝶豆

中级会员

最后登录
2020-8-11
发表于 2012-9-7 20:57:18 | 显示全部楼层

RE:帮看看这个串口通信问题

哥们你这里定义的是
   u8 Reset[4]={0x56,0x00,0x26,0x00};
你这里发送的对吗
       for(i=0;i
回复 支持 反对

使用道具 举报

24

主题

591

回帖

0

蝴蝶豆

中级会员

最后登录
2020-12-2
发表于 2012-9-7 21:01:41 | 显示全部楼层

RE:帮看看这个串口通信问题

你看看这个:void USART2_Config(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_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
        USART_Init(USART2, &USART_InitStructure);
        USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
        USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
        USART_Cmd(USART2, ENABLE);
}
还有 Reset应该是 Reset【i】,你是这个意思吧?打印这个数组数据吧?
希望对你有用哦。
回复 支持 反对

使用道具 举报

0

主题

6

回帖

0

蝴蝶豆

新手上路

最后登录
1970-1-1
发表于 2012-9-8 01:02:10 | 显示全部楼层

回复:帮看看这个串口通信问题

学习
回复 支持 反对

使用道具 举报

3

主题

12

回帖

0

蝴蝶豆

新手上路

最后登录
1970-1-1
 楼主| 发表于 2012-9-8 07:56:39 | 显示全部楼层

回复:帮看看这个串口通信问题

<div style="padding:5px;border:1px dashed #ccc;">
回复第 2 楼 于2012-09-07 20:57:18发表:
哥们你这里定义的是
u8 Reset[4]={0x56,0x00,0x26,0x00};
你这里发送的对吗
for(i=0;i
回复 支持 反对

使用道具 举报

3

主题

12

回帖

0

蝴蝶豆

新手上路

最后登录
1970-1-1
 楼主| 发表于 2012-9-8 08:10:22 | 显示全部楼层

回复:帮看看这个串口通信问题

回复第 3 楼 于2012-09-07 21:01:41发表:
你看看这个:void USART2_Config(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_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
还有 Reset应该是 Reset【i】,你是这个意思吧?打印这个数组数据吧?
希望对你有用哦。
 
 
还是不行哦,发完数据,根本等不到那个标志位为空。。。
 
回复 支持 反对

使用道具 举报

3

主题

12

回帖

0

蝴蝶豆

新手上路

最后登录
1970-1-1
 楼主| 发表于 2012-9-8 08:12:21 | 显示全部楼层

回复:帮看看这个串口通信问题

<div style="padding:5px;border:1px dashed #ccc;">
回复第 2 楼 于2012-09-07 20:57:18发表:
哥们你这里定义的是
u8 Reset[4]={0x56,0x00,0x26,0x00};
你这里发送的对吗
for(i=0;i
回复 支持 反对

使用道具 举报

134

主题

4489

回帖

239

蝴蝶豆

版主

最后登录
2020-12-9
发表于 2012-9-8 15:18:46 | 显示全部楼层

RE:帮看看这个串口通信问题

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
请楼主确认你用的是哪个串口?如果确认是用的PD5和PD6,在GPIO配置的时候请加一句:
GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
回复 支持 反对

使用道具 举报

24

主题

591

回帖

0

蝴蝶豆

中级会员

最后登录
2020-12-2
发表于 2012-9-8 15:23:44 | 显示全部楼层

RE:帮看看这个串口通信问题

你直接用以下amin.c试试
#include "stm32f10x.h"
#include "platform_config.h"
#include "stm32f10x_usart.h"
#include "misc.h"
void USART2_Config(void);
void GPIO_Configuration(void);
void Delay(vu32 Time);

unsigned char TxBuf[10]  = " \r\n";
unsigned char TxBuf1[50] = " ******************************************\r\n";
unsigned char TxBuf2[50] = " *    STM32开发板!^_^  *\r\n";
unsigned char TxBuf3[50] = "\r\n Please input any word :\r\n ";
unsigned char TxBuf4[50]  = " *                                        *\r\n";

int main(void)
{
        int i, RX_status = 0;
        SystemInit();
        GPIO_Configuration();
        USART2_Config();
        /* ========USART打印欢迎信息============ */
        for( i = 0; TxBuf != '\0'; i++) {
                USART_SendData(USART2 , TxBuf);
                while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET);
                }
        for( i = 0; TxBuf1 != '\0'; i++) {
                USART_SendData(USART2 , TxBuf1);
                while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET);
                }
        for( i = 0; TxBuf4 != '\0'; i++) {
                USART_SendData(USART2 , TxBuf4);
                while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET);
                }
        for( i = 0; TxBuf2 != '\0'; i++) {
                USART_SendData(USART2 , TxBuf2);
                while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET);
                }
        for( i = 0; TxBuf4 != '\0'; i++) {
                USART_SendData(USART2 , TxBuf4);
                while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET);
                }
        for( i = 0; TxBuf1 != '\0'; i++) {
                USART_SendData(USART2 , TxBuf1);
                while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET);
                }
        for( i = 0; TxBuf3 != '\0'; i++) {
                USART_SendData(USART2 , TxBuf3);
                while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET);
                }
        while (1)
        {
                GPIO_SetBits(GPIOE, GPIO_Pin_1);
                RX_status = USART_GetFlagStatus(USART2, USART_FLAG_RXNE);
                if(RX_status == SET) {
                        USART_SendData(USART2 , USART_ReceiveData(USART2));
                        while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET);
                        GPIO_ResetBits(GPIOE, GPIO_Pin_1);
                        Delay(0xFFFFF);       
        }
        }
}

void USART2_Config(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_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
        USART_Init(USART2, &USART_InitStructure);
        USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
        USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
        USART_Cmd(USART2, ENABLE);
}

void GPIO_Configuration(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2,ENABLE);
        RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 |RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                         RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE, ENABLE);
           
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;                         //LED4
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOE, &GPIO_InitStructure);                                         
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;                
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
        GPIO_Init(GPIOA, &GPIO_InitStructure);                  
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;                
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   
        GPIO_Init(GPIOA, &GPIO_InitStructure);                       
}

void Delay(vu32 Time)
{
        for(; Time != 0; Time--);
}
#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
回复 支持 反对

使用道具 举报

3

主题

12

回帖

0

蝴蝶豆

新手上路

最后登录
1970-1-1
 楼主| 发表于 2012-9-8 19:14:23 | 显示全部楼层

回复:帮看看这个串口通信问题

回复第 8 楼 于2012-09-08 15:18:46发表:
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
请楼主确认你用的是哪个串口?如果确认是用的PD5和PD6,在GPIO配置的时候请加一句:
GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
 
 
确定是PD5  PD6 但是加了那句话还是不行。。
 
回复 支持 反对

使用道具 举报

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