|
串口讀不到數據
可以幫忙看一下嗎
單片機源程序如下:
- #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
復制代碼
|
|