OpenEdv-开源电子网

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

串口通信问题

[复制链接]

3

主题

10

帖子

0

精华

新手上路

积分
42
金钱
42
注册时间
2012-8-6
在线时间
0 小时
发表于 2012-8-16 15:18:03 | 显示全部楼层 |阅读模式

这是一个串口通信程序,编译通过了,但在串口调试助手中毫无显示,求各位大神帮忙之指点一下!!


 #include "stm32f10x.h"
#include <stdio.h>

#define unsigned uint32_t vu32;
GPIO_InitTypeDef GPIO_InitSructure;
USART_InitTypeDef USART_InitStructure;

void Delay (vu32 ncount);
uint8_t GetKey(void);
void OutputString(uint8_t *buffp);
void PutChar(uint8_t c);
uint32_t  SerialKeyPressed(uint8_t *key);

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

void Delay (vu32 nCount)
{
 for(; nCount != 0;nCount--);
}
 
int main (void)
{
 uint8_t inputstr[128];
 /****************配置GPIO使能LED********************/
 GPIO_InitTypeDef GPIO_InitStructure;
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOA,&GPIO_InitStructure);
 GPIO_Init(GPIOB,&GPIO_InitStructure);/*GPIO配置完成*/
 /*****************关闭所有LED灯*******************/
 GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3);
 GPIO_SetBits(GPIOB,GPIO_Pin_2);

 /**********************USART配置************************/
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);/*使能USART时钟*/
 
 /*******************配置的具体参数**********************/
 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_Tx|USART_Mode_Rx;

 USART_Init(USART1,&USART_InitStructure); /*USART配置完成*/
 USART_Cmd(USART1,ENABLE);/*使能USART1*/
 

 printf("\n Yo!Yo!Check Now!\n");
 printf("\n This is Zhou Qu Speaking!\n");
 printf("\n STM32 is ready to recieve:\n") ;

 while (1)
 {
  GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3);
  GPIO_SetBits(GPIOB,GPIO_Pin_2);/*关闭所有LED指示灯*/

  OutputString(inputstr);
  printf("\nYour input is:\n%s\n",inputstr);
  GPIO_ResetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3);
  GPIO_ResetBits(GPIOB,GPIO_Pin_2);/* 打开所有LED指示灯*/
  Delay (0x2FFFF);
  GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3);
  GPIO_SetBits(GPIOB,GPIO_Pin_2);/*再次关闭所有LED指示灯*/
 }
}


PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {
  }

  return ch;
}

/***********************************子函数*****************************************/

void OutputString (uint8_t *buffp)
{
 uint8_t c = 0;
 uint16_t read_bytes = 0;
 do
 {
  c=GetKey();
  if(c=='\r'){break;}
  if(c=='\b')
  {
   if(read_bytes>0)
   {
    printf("\b \b");
    read_bytes--;
   }
   continue;
  }
  
  if(read_bytes >= 128)
  { 
   printf("data overflow!\n");
   continue;
  }
  if(c>=0x20 && c<=0x7E)
  {
   buffp[read_bytes++]=c;
   utChar(c);
  }
 }
 while(1);
 printf("\n\r");
 buffp[read_bytes] = '\0';
}
uint8_t  GetKey(void)
{
 uint8_t key=0;
 while(1)
 {
  if(SerialKeyPressed((uint8_t*)&key)) break;
 }
 return key;
}

uint32_t  SerialKeyPressed(uint8_t *key)
{
 if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)!=RESET)
 {
  *key = (uint8_t)USART1->DR;
  return 1;
 }
 else {
     return 0;
  }
}

void PutChar(uint8_t c)
{
 USART_SendData(USART1,c);
 while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
 {
 }
}

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

使用道具 举报

36

主题

1105

帖子

5

精华

论坛大神

Rank: 7Rank: 7Rank: 7

积分
2201
金钱
2201
注册时间
2012-2-8
在线时间
35 小时
发表于 2012-8-16 16:02:02 | 显示全部楼层
代码太长了,把LED代码去掉,OutputString 先扔一边,printf 也不要,先调初始化,和 utChar 函数,可以先软件仿真看看,再下载测试。
https://github.com/roxma
回复 支持 反对

使用道具 举报

3

主题

10

帖子

0

精华

新手上路

积分
42
金钱
42
注册时间
2012-8-6
在线时间
0 小时
 楼主| 发表于 2012-8-16 16:48:56 | 显示全部楼层
回复【2楼】Pony279:
---------------------------------
嗯,试试看!!
回复 支持 反对

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2025-7-21 05:34

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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