|
串口读不到数据
可以帮忙看一下吗
单片机源程序如下:
- #include "ZPH01.h"
- #include "sys.h"
- void Zph01_Init(void){
-
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);
-
- GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
-
- USART_InitStructure.USART_BaudRate=9600;//设置:波特率
- USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;////设置:硬件流
- USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;////设置:模式
- USART_InitStructure.USART_Parity=USART_Parity_No;//设置:奇偶校验位
- USART_InitStructure.USART_StopBits=USART_StopBits_1;//设置:停止位
- USART_InitStructure.USART_WordLength=USART_WordLength_8b;////设置:字长
-
- USART_Init(USART2,&USART_InitStructure);//串口服务库函数
- USART_Cmd(USART2,ENABLE);//使能串口库函数
-
-
-
-
-
- }
- u8 Buf_Cnt;//数据大小
- unsigned char Buf[9];//用于存放接收到的数据
- unsigned char flag[5]={0xff,0x18,0x00,0x00,0x01};
- void Zph01_Rend_buf(void)
- {
- uint16_t value;
- u8 i;
- while(USART2->SR &(1<<5)){//判断Zph01是否把数据是否已经写入DR寄存器
- value = USART_ReceiveData(USART2);
- if(Buf_Cnt<9)
- {
- Buf[Buf_Cnt++] = value;
- }
- else{
- for(i=0;i<9;i++)
- printf("%c",Buf[i]);
- Buf_Cnt=0;
- }
-
- }
-
- }
- #ifndef _ZPH01_H
- #define _ZPH01_H
- #include "stm32f10x.h"
- void Zph01_Init(void);
- void Zph01_Rend_buf(void);//发送接收到的数据
- typedef struct
- {
- unsigned char PM_H; //PM2.5的整数部分
- unsigned char PM_L; //PM2.5的小数部分
- unsigned char Check_Sum; //校验和
- } DPH01_Data;
- #endif
复制代码
|
|