OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 19026|回复: 4

各位大侠帮我看看这个stm32 跟电脑串口通讯的程序 软件仿真可以 硬件却不行了,发到串口调试助手上总是乱码。

[复制链接]

3

主题

9

帖子

0

精华

新手上路

积分
41
金钱
41
注册时间
2013-8-18
在线时间
0 小时
发表于 2013-8-18 10:05:01 | 显示全部楼层 |阅读模式

   各位大侠帮我看看这个stm32 跟电脑串口通讯的程序 软件仿真可以 硬件却不行了
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"   //包含了所有的头文件 它是唯一一个用户需要包括在自己应用中的文件,起到应用和库之间界面的作用。
#include <stdio.h>

/*****************************函数声明*************************************/
void Delay_Ms(u16 time);
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(u32);


extern void ADC_Configuration(void);
extern u16 Test_ConversionResult(void);
//extern void SMG_Init(void);
//extern void NumbTube_Display(u32 data,u8 radix_point);


/*******************************************************************************
* Function Name  : fputc
* Description    : 重定向这个C库中的stdio库,使文件流——》串口USART1
* Input          : ch FILE *f
* Output         : None
* Return         : None
*******************************************************************************/
int fputc(int ch,FILE *f)
{
    //ch 发送到USART1
  USART_SendData(USART1,(u16)ch);
    //等待发送完毕
  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET );
    //返回ch
 return ch;
}


/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{
 #ifdef DEBUG 
   debug();
 #endif

        u32 ConversionValue;
   RCC_Configuration();    //使能外设时钟
 GPIO_Configuration();
 NVIC_Configuration();
 USART_Configuration(15200);
 ADC_Configuration();
  while (1)
   {
  printf("\r\n 'B' \r\n");
  printf("\n\twww.A435.com");
  //ConversionValue = (u32)Test_ConversionResult();

          //printf("\n\ti value is %d",ConversionValue);
  //printf("\n\ti value is %o",i);
  //printf("\n\tThe value of i is %d,%d",i+i,i*i);
  //printf("\n\t-----------------------");

  //ConversionValue = ConversionValue * 1000;
  
  //NumbTube_Display(ConversionValue,4);// 数码管显示ADC转换的值
    //输出到串口,并用串口助手显示

      
   }
}

/*******************************************************************************
* Function Name  : Delay_Ms
* Description    : delay 1 ms.
* Input          : time (ms)
* Output         : None
* Return         : None
*******************************************************************************/
void Delay_Ms(u16 time)  //延时函数
{
 u16 i,j;
 for(i=0;i<time;i++)
    for(j=1000;j>0;j--);
}

/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{
 //=============================== 使用内部RC晶振 ===================================
    /*  
 RCC_HSICmd(ENABLE);//使能内部高速晶振 ;
  RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);//选择内部高速时钟作为系统时钟SYSCLOCK=8MHZ 
 RCC_HCLKConfig(RCC_SYSCLK_Div1);//选择HCLK时钟源为系统时钟SYYSCLOCK
   RCC_PCLK1Config(RCC_HCLK_Div4);//APB1时钟为2M
   RCC_PCLK2Config(RCC_HCLK_Div4);//APB2时钟为2M
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);//使能APB2外设GPIOB时钟
 */

 //==========================使用外部RC晶振========================================
   RCC_DeInit();    //初始化为缺省状态
   RCC_HSEConfig(RCC_HSE_ON);  //高速时钟使能
   while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);  //等待高速时钟使能就绪

    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //Enable Prefetch Buffer
    FLASH_SetLatency(FLASH_Latency_2);        // Flash 2 wait state
    RCC_HCLKConfig(RCC_SYSCLK_Div1);       // HCLK = SYSCLK
    RCC_PCLK2Config(RCC_HCLK_Div1);       // PCLK2 = HCLK
    RCC_PCLK1Config(RCC_HCLK_Div2);         // PCLK1 = HCLK/2
 /* Configure ADCCLK such as ADCCLK = PCLK2/2 */
     RCC_ADCCLKConfig(RCC_PCLK2_Div2);

 RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); // PLLCLK = 8MHz * 9 = 72 MHz 
    RCC_PLLCmd(ENABLE);            // Enable PLL
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);  // Wait till PLL is ready

    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);      // Select PLL as system clock source
    while(RCC_GetSYSCLKSource() != 0x08);     // Wait till PLL is used as system clock source


 //====================================================================================
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);   //开启ADC1的时钟
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);  // 使能APB2外设GPIOC时钟
 /*Enable USART1 and GPIOA clock*/
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : 初始化GPIO外设
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{

   GPIO_InitTypeDef GPIO_InitStructure; // 声明一个结构体变量
    //==========为结构体成员填入相应的值============
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;   //PC5 以下4行作用是把PC5引脚配置成模拟输入的模式
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
 GPIO_Init(GPIOC,&GPIO_InitStructure);

  /* Configure USART1 Tx (PA.09) as alternate function push-pull */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_Init(GPIOA, &GPIO_InitStructure);
    
   /* Configure USART1 Rx (PA.10) as input floating */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
   GPIO_Init(GPIOA, &GPIO_InitStructure);


//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
//   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //管脚频率为50MHZ
//   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //模式为推挽输出
//   GPIO_Init(GPIOA, &GPIO_InitStructure);      //初始化GPIOB寄存器
}


 void USART_Configuration(u32 Baud)
{
    USART_InitTypeDef USART_InitStructure;
    USART_ClockInitTypeDef  USART_ClockInitStructure;
 
    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;
    /* Configure the USART1 synchronous paramters */
    USART_ClockInit(USART1, &USART_ClockInitStructure);

 USART_InitStructure.USART_BaudRate = Baud;
 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_Tx | USART_Mode_Rx;
// USART_InitStructure.USART_Clock = USART_Clock_Disable;
// USART_InitStructure.USART_CPOL = USART_CPOL_High;
// USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;
// USART_InitStructure.USART_LastBit = USART_LastBit_Enable;
 USART_Init(USART1, &USART_InitStructure);
 USART_Cmd(USART1, ENABLE);
}

 

void NVIC_Configuration(void)
{
  
#ifdef  VECT_TAB_RAM 
   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);  
#endif
}

 用simulator在串口显示的数据都正常,但是用st-link debugger与电脑连接,用串口助手看的话,一个是发现传上来的数据不对,二是无限发送,没有办法停下来。
我看网上说 勾选 use MicroLib和use cross-mode optimization ,就可以了。但是我钩了还是不行。波特率也没问题。请哪位大侠帮忙解决一下。非常感谢!!

 

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2013-8-18 10:09:38 | 显示全部楼层
参考下我们战舰板的串口例程。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复 支持 反对

使用道具 举报

3

主题

58

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
425
金钱
425
注册时间
2013-8-9
在线时间
65 小时
发表于 2013-8-19 08:33:04 | 显示全部楼层
建议使用原子哥开发的串口通讯软件!
坚持自己的目标,就一定能够实现!
回复 支持 反对

使用道具 举报

29

主题

311

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1530
金钱
1530
注册时间
2012-9-4
在线时间
262 小时
发表于 2013-8-19 16:27:40 | 显示全部楼层
出现乱码很可能是串口助手上面的波特率没设置对。。。。仔细检查检查。。。。
STM32---STM32---STM32---STM32---STM32---STM32---STM32---STM32---STM32
回复 支持 反对

使用道具 举报

3

主题

9

帖子

0

精华

新手上路

积分
41
金钱
41
注册时间
2013-8-18
在线时间
0 小时
 楼主| 发表于 2013-8-24 20:15:23 | 显示全部楼层
回复【4楼】aben:
---------------------------------
恩,是的波特率改了一下又改回来,就好了,就是波特率的问题,非常感谢了!!
回复 支持 反对

使用道具 举报

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

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-7-14 14:19

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表