由上位机(PC)通过串口调试助手发送数据,下位机接收到数据后,将接收到的数据发送到上位机,在串口调试助手中显示出来,程序运行不成功,查不到原因???- <p>/*******************************************************************************
- * 文件名称 : 19.3.1 中断法USART接收(库函数版本V3.5.0)
- * 文件描述 : 由上位机(PC)通过串口调试助手发送数据,下位机接收到数据后,将接收到的数据发送到上位机,在串口调试助手中显示出来
- * 编写人 : 蒋厚兵
- * 课程 :
- * 教材 :
- *******************************************************************************/
- #include"stm32f10x.h"
- #include<stdio.h>
- #include "rcc.h"
- #include "NVIC.h"
- #include"usart.h"
-
- int main()
- {
- /******************1.初始化*************************************************/
- RCC_PLL3_Configuration();
- NVIC_Configuration();
- Usart_GPIO();
- Usart_Init(9600); //配置USART1
-
- GPIO_Write(GPIOA, 0xffff); //
- GPIO_Write(GPIOB, 0xffff); //
- while(1);
-
- } </p><p>
- </p><p><div class="blockcode"><blockquote><p>/******************************************************************************************************
- 串口发送接收
- *******************************************************************************************************/
- #include"stm32f10x.h"
- #include"usart.h"
- #include<stdio.h>
- void Usart_GPIO()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* 结构体类型 变量*/
- //开启相应的外设时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE ); //使能APB2外设的GPIOA和复用功能的时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能APB2外设的USART1的时钟
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE); //关闭调试端口
- USART_DeInit(USART1); //复位串口1
- //设置引脚
- //USART1_TX发射引脚 PA.9
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9
- //USART1_RX 接受引脚 PA.10
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
- GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA10
- }
- void Usart_Init(u32 BaudRate)
- {
- USART_InitTypeDef USART_InitStructure;
- //结构体 变量(共10个成员)
- NVIC_InitTypeDef NVIC_InitStructure;
- // USART_ClockInitTypeDef USART_ClockInitStruct;
- //配置Usart
- USART_InitStructure.USART_BaudRate = BaudRate; //波特率
- 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; //硬件流控制失能
- USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //发送使能,接收使能
- USART_Init(USART1, &USART_InitStructure); // 初始化USART串口
- // USART_ClockInitStruct.USART_Clock=USART_Clock_Disable; //??????
- // USART_ClockInitStruct.USART_CPOL=USART_CPOL_Low; //???????
- // USART_ClockInitStruct.USART_CPHA=USART_CPHA_2Edge; //??CPHA?????2?????????
- // USART_ClockInitStruct.USART_LastBit=USART_LastBit_Disable; // ??????,??????????????SCLK??
- // USART_ClockInit(USART1, &USART_ClockInitStruct); //??USART????????
- //Usart1 NVIC 配置
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
- USART_ClearFlag(USART1,USART_IT_RXNE);
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启接收中断
- USART_Cmd(USART1, ENABLE); //UE位使能,打开USART1外设
- }
- /*******************************************************************************
- * Function Name : fputc
- * Description : 重定向这个C库(stdio)printf函数 文件流——》串口USART1
- * Input : ch,*f
- * Output : None
- * Return : None
- *******************************************************************************/
- int fputc(int ch,FILE *f)
- {
-
- //等待发送完毕
- while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET) ;
- //ch送给USART1
- USART_SendData(USART1, ch); //
-
- //返回ch
- return(ch);
- }
- //串口1中断服务程序
- void USART1_IRQHandler(void)
- {
- u8 Res;
- if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断
- {
- Res =USART_ReceiveData(USART1);//(USART1->DR); //读取接收到的数据
- USART_SendData(USART1, Res); //将接收到的数据发送出去
- }
- }
- </p><p>
- </p><p><div class="blockcode"><blockquote>#ifndef __USART_H
- #define __USART_H
- #include "stm32f10x.h"
- #include<stdio.h>
- void Usart_GPIO(void);
- void Usart_Init(u32 BaudRate);
- extern int fputc(int ch,FILE *f);
- #endif
|