[STM32F1] STM32中断法USART接收

[复制链接]
1250|3
 楼主| tbnet 发表于 2015-10-7 17:31 | 显示全部楼层 |阅读模式
由上位机(PC)通过串口调试助手发送数据,下位机接收到数据后,将接收到的数据发送到上位机,在串口调试助手中显示出来,程序运行不成功,查不到原因???
  1. <p>/*******************************************************************************
  2. * 文件名称    : 19.3.1 中断法USART接收(库函数版本V3.5.0)
  3. * 文件描述    : 由上位机(PC)通过串口调试助手发送数据,下位机接收到数据后,将接收到的数据发送到上位机,在串口调试助手中显示出来
  4. * 编写人      : 蒋厚兵
  5. * 课程            :  
  6. * 教材        :                    
  7. *******************************************************************************/
  8. #include"stm32f10x.h"
  9. #include<stdio.h>
  10. #include "rcc.h"
  11. #include "NVIC.h"
  12. #include"usart.h"



  13. int main()
  14. {
  15.         /******************1.初始化*************************************************/
  16.         RCC_PLL3_Configuration();   
  17.         NVIC_Configuration();
  18.         Usart_GPIO();       
  19.         Usart_Init(9600);                   //配置USART1       
  20.        
  21.         GPIO_Write(GPIOA, 0xffff);                           //
  22.         GPIO_Write(GPIOB, 0xffff);                           //        

  23.         while(1);       
  24.        
  25. } </p><p>
  26. </p><p><div class="blockcode"><blockquote><p>/******************************************************************************************************
  27. 串口发送接收

  28. *******************************************************************************************************/

  29. #include"stm32f10x.h"
  30. #include"usart.h"
  31. #include<stdio.h>


  32. void Usart_GPIO()
  33. {
  34.         GPIO_InitTypeDef GPIO_InitStructure;
  35.         /*        结构体类型                           变量*/       

  36.         //开启相应的外设时钟
  37.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE );                //使能APB2外设的GPIOA和复用功能的时钟       
  38.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);                 //使能APB2外设的USART1的时钟                  
  39.         GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);                //关闭调试端口
  40.         USART_DeInit(USART1);  //复位串口1
  41.         //设置引脚
  42.         //USART1_TX发射引脚   PA.9
  43.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  44.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  45.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;        //复用推挽输出
  46.         GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9       

  47.         //USART1_RX        接受引脚  PA.10
  48.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  49.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  50.         GPIO_Init(GPIOA, &GPIO_InitStructure);  //初始化PA10               
  51. }

  52. void Usart_Init(u32 BaudRate)
  53. {
  54.         USART_InitTypeDef        USART_InitStructure;       
  55.         //结构体                        变量(共10个成员)
  56.         NVIC_InitTypeDef NVIC_InitStructure;

  57. //        USART_ClockInitTypeDef USART_ClockInitStruct;

  58.         //配置Usart       
  59.         USART_InitStructure.USART_BaudRate = BaudRate;                                   //波特率
  60.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;                //字长8位
  61.         USART_InitStructure.USART_StopBits = USART_StopBits_1;                        //停止位的位数1位
  62.         USART_InitStructure.USART_Parity = USART_Parity_No;                        //校验:不需要校验
  63.         USART_InitStructure.USART_HardwareFlowControl =        USART_HardwareFlowControl_None;                //硬件流控制失能                                                                                                                                                         
  64.         USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;                          //发送使能,接收使能
  65.         USART_Init(USART1, &USART_InitStructure);                // 初始化USART串口

  66. //         USART_ClockInitStruct.USART_Clock=USART_Clock_Disable;      //??????
  67. //         USART_ClockInitStruct.USART_CPOL=USART_CPOL_Low;        //???????
  68. //         USART_ClockInitStruct.USART_CPHA=USART_CPHA_2Edge;    //??CPHA?????2?????????
  69. //         USART_ClockInitStruct.USART_LastBit=USART_LastBit_Disable;  // ??????,??????????????SCLK??
  70. //         USART_ClockInit(USART1, &USART_ClockInitStruct);      //??USART????????               

  71.         //Usart1 NVIC 配置
  72.         NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  73.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
  74.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;                //子优先级3
  75.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQ通道使能
  76.         NVIC_Init(&NVIC_InitStructure);        //根据指定的参数初始化VIC寄存器       


  77.         USART_ClearFlag(USART1,USART_IT_RXNE);
  78.         USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启接收中断
  79.         USART_Cmd(USART1, ENABLE);                //UE位使能,打开USART1外设
  80. }

  81. /*******************************************************************************
  82. * Function Name  : fputc
  83. * Description    : 重定向这个C库(stdio)printf函数  文件流——》串口USART1
  84. * Input          : ch,*f
  85. * Output         : None
  86. * Return         : None
  87. *******************************************************************************/
  88. int fputc(int ch,FILE *f)
  89. {
  90.        
  91.         //等待发送完毕
  92.         while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET) ;

  93.         //ch送给USART1
  94.         USART_SendData(USART1, ch);        //
  95.        
  96.         //返回ch
  97.         return(ch);
  98. }

  99. //串口1中断服务程序
  100. void USART1_IRQHandler(void)                       
  101. {
  102.         u8 Res;
  103.         if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //接收中断
  104.         {                                                                                                                       
  105.                 Res =USART_ReceiveData(USART1);//(USART1->DR);        //读取接收到的数据
  106.                 USART_SendData(USART1, Res);          //将接收到的数据发送出去               
  107.         }                     
  108. }

  109. </p><p>
  110. </p><p><div class="blockcode"><blockquote>#ifndef __USART_H
  111. #define __USART_H                           
  112. #include "stm32f10x.h"
  113. #include<stdio.h>

  114. void Usart_GPIO(void);
  115. void Usart_Init(u32 BaudRate);
  116. extern int fputc(int ch,FILE *f);


  117. #endif






mmuuss586 发表于 2015-10-7 19:10 | 显示全部楼层
查下硬件;
另外单独测试下发送是否可以,先判断串口初始化是否有问题;
迪卡 发表于 2015-10-8 19:55 | 显示全部楼层
程序编译有问题吗,是下不进去还是运行结果不对
309030 发表于 2015-10-8 20:41 | 显示全部楼层
程序运行不成功,查不到原因???

程序不成功,是什么问题,乱码,显示不出来,还是什么
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

3

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部