竭诚为您提供优质文档/双击可除
stm32串口收发实验报告
  篇一:实验三串口通信实验
  实验三串口通信实验
  【实验目的】
  1、掌握ARmcortex-m3的串行口工作原理
  2、掌握ARmcortex-m3的uART寄存器配置方法
  3、学习编程实现ARmcortex-m3的uART通信
  【实验仪器】
  1、硬件:sTm32F103cb开发板,uLInK(或JLInK)仿真器套件,pc机
  2、软件:RealViewmDK开发套件,windowsxp。
  【实验电路及连线】
  串口的发送端u1_Tx,接收端u1_Rx,分别连接gpIo的pA9和pA10。
  【实验内容】
  1、熟悉RealViewmDK开发套件的使用
  2、建立一个简单的usART工程。
  3、编写程序,实现sTm32单片机通过串口1发送一个字符串“sTm32F103cbusART1TesT!”,并在pc机的超级终端上显示。要求用printf函数输出。
  【实验步骤、分析及结果(在下面写出你的代码)】
  结果:
  代码:
  #include"stm32f10x_lib.h"
  #include"stdio.h"
  voidgpIo_configuration()
  {
  gpIo_InitTypeDefgpIo_Initstructure;
  Rcc_Apb2periphclockcmd(Rcc_Apb2periph_usART1,enAbLe);
  Rcc_Apb2periphclockcmd(Rcc_Apb2periph_gpIoA,enAbLe);
  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.gpIo_pin=gpIo_pin_10;
  gpIo_Initstructure.gpIo_mode=gpIo_mode_In_FLoATIng;
  gpIo_Init(gpIoA,
  }
  intfputc(intch,FILe*f)
  {
  usART_sendData(usART1,(u8)ch);
  while(usART_getFlagstatus(usART1,usART_FLAg_Txe)==ReseT);
  returnch;
  }
  voidusART_configuration(void)
  {
  usART_InitTypeDefusART_Initstructure;
  //Rcc_Apb2periphclockcmd(Rcc_Apb2periph_usART1,enAbLe);///////放到gpIo_configuration
  usART_Initstructure.usART_baudRate=115200;
  usART_Initstructure.usART_wordLength=usART_wordLength_8b;//8位传输;usART_Initstructure.usART_stopbits=usART_stopbits_1;//1个停止位usART_Initstructure.usART_parity=usART_parity_no;
  usART_Initstructure.usART_hardwareFlowcontrol=usART_hardwareFlowcontrol_none;//禁止硬件流控制,禁止RTs和cTs信号
  usART_Initstructure.usART_mode=usART_mode_Rx|usART_mode_Tx;//全双工usART_Init(usART1,//初始化uARTx
  usART_cmd(usART1,enAbLe);//使能串口一
  }
  main()
  {
  //u32i;
  //chara[]="sTm32F103cbusART1TesT!";
  gpIo_configuration();
  usART_configuration();
  /*可以在发送之前清除标志位Tc*/
  printf("sTm32F103cbusART1TesT!");
  while(1);
  }
  教师评语:
  实验成绩:
  教师:年月日
  篇二:stm32串口间通信实验
  sTm32串口间通信
  该工程主要实现    voiduart_init(u32bound)
  {
  //gpIo端口设置
  gpIo_InitTypeDefgpIo_Initstructure;
  usART_InitTypeDefusART_Initstructure;//nVIc_InitTypeDefnVIc_Initstructure;
  Rcc_Apb2periphclockcmd(Rcc_Apb2periph_usART1|Rcc_Apb2periph_gpIoA|Rcc_Apb2periph_AFIo,enAbLe);
  //usART1_TxpA.9
  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,
  //usART1_RxpA.10
  gpIo_Initstructure.gpIo_pin=gpIo_pin_10;
  gpIo_Initstructure.gpIo_mode=gpIo_mode_In_FLoATIng;
  gpIo_Init(gpIoA,
  //usart1nVIc配置
  //nVIc_Initstructure.nVIc_IRQchannel=usART1_IRQn;
  //nVIc_Initstructure.nVIc_IRQchannelpreemptionpriority=3;
  //nVIc_Initstructure.nVIc_IRQchannelsubpriority=3;//nVIc_Initstructure.nVIc_IRQchannelcmd=enAbLe;////IRQ通道使能//nVIc_Init(
  nVIc寄存器usART1
  //usART初始化设置//根据nVIc_Initstruct中指定的参数初始化外设
  usART_Initstructure.usART_baudRate=bound;//一般设置为9600;
  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(usART1,
  usART_ITconfig(usART1,usART_IT_Txe,enAbLe);//开启中断
  usART_cmd(usART1,enAbLe);//使能串口
  }
  voidput_string(u8*p)
  {
  while(*p)
  {
  usART_sendData(usART1,*p++);
  while(usART_getFlagstatus(usART1,usART_FLAg_Txe)==ReseT){usART_clearITpendingbit(usART1,usART_IT_Txe);
  }
  }
  //这些函数很有用所以附上来了
  voiduart_putchar(u8ch)
  {
  usART_sendData(usART1,(u8)ch);
  while(usART_getFlagstatus(usART1,usART_FLAg_Txe)==ReseT){}usART_clearITpendin
gbit(usART1,usART_IT_Txe);
  //returnch;
  }
  voiduart_putstring(u8*buf,u8len)
  {
模拟串口使用printf函数  u8i;
  for(i=0;i  {
  uart_putchar(*buf++);
  }
  }}
  发送main函数:
  #include"led.h"
  #include"delay.h"
  #include"sys.h"
  #include"key.h"
  #include"usart.h"
  //串口实验
  intmain(void)
  {
  u8i=0;systemInit();//系统时钟等初始化delay_init(72);//延时初始化nVIc_configuration();//设置nVIc中断分组2:2位抢占优先级,2位响应优先级uart_init(9600);//串口初始化为9600LeD_Init();//LeD端口初始化while(1){//if(flag==1)put_string((u8*)(usART_Tx_buF+i));
  }if(i>12)i=0;LeD0=!LeD0;delay_ms(250);