- #include "am2303.h"
- #include "delay.h"
- #include "usart1.h"
- /*
- 開始信號:SCL 為高電平時,SDA 由高電平向低電平跳變,開始傳送數據
- */
- #define SDA GPIO_Pin_7 //SDA 數據
- #define AM_PORT GPIOB
- #define AM_SDA_HIGH GPIO_SetBits(AM_PORT, SDA) //數據高
- #define AM_SDA_LOW GPIO_ResetBits(AM_PORT, SDA) //數據低
- #define AM_SDA_READ GPIO_ReadInputDataBit(AM_PORT, SDA) //數據讀
- /*********************************************************************************
- 功 能:AM2303_Configuration配置
- 入口參數:無
- 返 回 值:無
- *********************************************************************************/
- void AM2303_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB ,ENABLE);
- GPIO_InitStructure.GPIO_Pin = SDA;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //開漏輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_Init(AM_PORT, &GPIO_InitStructure);
- AM_SDA_HIGH; //初始化IO
- }
- /*********************************************************************************
- 功 能:AM2303_SDA_DirSet 輸入輸出選擇
- 入口參數:io_mode
- 返 回 值:無
- **********************************************************************************/
- static void AM2303_SDA_DirSet(SDA_GPIO_MODE io_mode) //SDA引腳輸入輸出設置
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB ,ENABLE);
- if(io_mode==SDA_OUTPUT)
- {
- GPIO_InitStructure.GPIO_Pin = SDA;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_Init(AM_PORT, &GPIO_InitStructure);
- }
- if(io_mode==SDA_INPUT)
- {
- GPIO_InitStructure.GPIO_Pin = SDA;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(AM_PORT, &GPIO_InitStructure);
- }
- }
- uint16_t FLAG=0;
- /********************************************************************************
- 功 能:AM2303_Read_Byte
- 入口參數:無
- 返 回 值:無
- *********************************************************************************/
- static uint8_t AM2303_Read_Byte(void)
- {
- uint8_t i;
- uint8_t temp=0;
- uint8_t data=0;
-
- for(i=0;i<8;i++)
- {
- FLAG=2;
- while((!(AM_SDA_READ))&&FLAG++); //檢測上次低電平是否結束
- if(FLAG==1)break; //超時則跳出循環
- Delayus(30);
- temp=0;
- if(AM_SDA_READ) //判斷數據位是 0 還是 1 // 如果高電平高過預定 0 高電平值則數據位為 1
- temp=1;
- FLAG=2;
- while((AM_SDA_READ)&&FLAG++); //檢測高電平是否結束
- if(FLAG==1)break; //超時則跳出循環
- data<<=1;
- data|=temp;
- }
- return data;
- }
- /********************************************************************************
- 功 能:AM2303_Read
- 入口參數:無
- 返 回 值:無
- *********************************************************************************/
- void AM2303_Read(void)
- {
- //uint8_t i;
- float humi=0.0;
- float temper=0.0;
- uint8_t sensor_data[5]={0};
- uint8_t sensor_chck=0;
-
- AM2303_SDA_DirSet(SDA_OUTPUT); //設置SDA口為輸出口
- //主機拉低時鐘10ms
- AM_SDA_LOW;
- Delayms(10);
- //主機釋放時鐘
- AM_SDA_HIGH;
- Delayus(30);
-
- AM2303_SDA_DirSet(SDA_INPUT); //設置SDA口為輸入口
- //判斷響應信號
- if(AM_SDA_READ)
- {
- USART1_Printf("AM2303_ack_error\r\n"); //響應錯誤
- FLAG=1;
- }
- while(AM_SDA_READ ==0) //響應主機
- {
- //判斷從機是否發出 80us 的低電平響應信號是否結束
- FLAG=2;
- while((!(AM_SDA_READ))&&FLAG++);
- if(FLAG==1)break;
-
- //判斷從機是否發出 80us 的高電平響應信號是否結束
- FLAG=2;
- while((AM_SDA_READ)&&FLAG++);
- if(FLAG==1)break;
-
- //數據接收狀態
- sensor_data[0]=AM2303_Read_Byte();
- if(FLAG==1)break;
- sensor_data[1]=AM2303_Read_Byte();
- if(FLAG==1)break;
- sensor_data[2]=AM2303_Read_Byte();
- if(FLAG==1)break;
- sensor_data[3]=AM2303_Read_Byte();
- if(FLAG==1)break;
- sensor_data[4]=AM2303_Read_Byte();
- if(FLAG==1)break;
- break;
- }
- //數據打印
- // if(debug==0)
- // {
- // for(i=0;i<5;i++)
- // USART1_Printf("sensor_data[%d]=%d\r\n",i,sensor_data[i]);
- // }
- //進入校準狀態
- sensor_chck=sensor_data[0]+sensor_data[1]+sensor_data[2]+sensor_data[3];
- if(sensor_data[4]==sensor_chck)
- {
- humi= (sensor_data[0]*256+sensor_data[1])/10.0; //濕度數據
- temper= (sensor_data[2]*256+sensor_data[3])/10.0; //溫度數據
- USART1_Printf("%.2f,%2.0f%%\n",temper,humi);
- // USART1_Printf("temper= %2f\r\n",temper);
- }
- else
- {
- USART1_Printf("AM2303_chck_error\r\n"); //校準錯誤
- }
- }
復制代碼- #include "delay.h"
- #include "usart1.h"
- #include "command.h"
- #include "am2303.h"
- //===============================================
- //配置NVIC,中斷使用方式
- //===============================================
- void NVIC_Configuration(void)
- {
- /* Configure the NVIC Preemption Priority Bits */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中斷優先極設置0、1、2、3、4
- #ifdef VECT_TAB_RAM
- /* Set the Vector Table base location at 0x20000000 */
- NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
- #else /* VECT_TAB_FLASH */
- /* Set the Vector Table base location at 0x08000000 */
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
- #endif
- }
- //===============================================
- //初始化所有外設
- //===============================================
- void Init_All_Configuration(void)
- {
- SystemInit();
- //-------------------------------------
- Delay_Configuration(72); //SYSCLK系統時鐘是72M
- USART1_Configuration(115200); //串口1配置
- AM2303_Configuration();
- }
- //==========================================
- int main(void)
- {
- NVIC_Configuration();
- //----------------------
- Init_All_Configuration();
-
- USART1_Printf("FLEX TEST Init OK!\r\n>>");
- //==========================================
- while(1)
- {
- USART1_Check(USART1_Receive_Buffer); //串口處理
- }
- //==========================================
- }
復制代碼 全部資料51hei下載地址:
AM2302 溫濕度板程序stm32.7z
(178.92 KB, 下載次數: 20)
2021-6-24 18:06 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|