STM32F030读取温湿度传感器SHT20的C程序源码(经.

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

*******************************************************************************@fileHTU20.x*@briefHTU20function*@cpuSTM32F051*@compilerKeiluVisionV5.01*@authorHanney*@copyright*@versionV1.0.1*@date18-Sept-2015*@modifydate20-Sept-2015*******************************************************************************@attention*///STM32F051模拟IIC//#includeboardAPI.h//#includestm32f0xx.h//#includeHTU20.h//#includeuart.h//#includedelay.h#includestm32f0xx.h#includestm32lib.h#defineHTU20_CLKRCC_AHBPeriph_GPIOB#defineHTU20_SDA_PORTGPIOB#defineHTU20_SCL_PORTGPIOB#defineHTU20_SDA_PINGPIO_Pin_11#defineHTU20_SCL_PINGPIO_Pin_10#defineRESOLUTION_11b0x81//RH11TH11#defineRESOLUTION_12b0x01//RH8TH12#defineRESOLUTION_13b0x80//RH10TH13#defineRESOLUTION_14b0x00//RH12TH14#defineOTP_disEN0x02//不能启动OPT加载#defineOTP_EN0x00//启动OPT加载#defineRESh16//湿度解析度#defineRESt16//温度解析度#defineMeasureDelay12//解析延时时间单位mstypedefunsignedcharBYTE;typedefunsignedintWORD;BYTEDelayms;GPIO_InitTypeDefGPIO_InitStructure;voiddelayms(BYTEms;/***@briefStartHTUGPIOportCLK*@retvalNone*/voidHTUInit(void{RCC_AHBPeriphClockCmd(HTU20_CLK,ENABLE;}/*================================================================IICaddress================================================================*/#defineSHT20ADDR0x80/*================================================================ACKandNACKdefination================================================================*/#defineACK0#defineNACK1/*================================================================HTU20DCommandCode================================================================*//*CommandCodeCommentTriggerTemperatureMeasurement0xe3HoldmasterTriggerHumidityMeasurement0xe5HoldmasterTriggerTemperatureMeasuremeng0xf3NoHoldmasterTriggerHumidityMeasurement0xf5NoHoldmasterWriteuserregister0xe6Readuserregister0xe7SoftReset0xfe*/#defineSOFT_RESET0xfe#defineREAD_REGISTER0xe7#defineWRITE_REGISTER0xe6#defineTRIGGER_TEMP0xf3#defineTRIGGER_HUMI0xf5/*================================================================HTU20operateinterface================================================================*/#defineHTU20_SCLGPIO_ReadInputDataBit(HTU20_SCL_PORT,HTU20_SCL_PIN#defineHTU20_SDAGPIO_ReadInputDataBit(HTU20_SDA_PORT,HTU20_SDA_PIN/***@briefSetSDApindircateasoutput*@retvalNone*/voidHTU20_SDA_OUT({GPIO_StructInit(&GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin=HTU20_SDA_PIN;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;GPIO_InitStructure.GPIO_OType=GPIO_OType_OD;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_Level_2;GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;GPIO_Init(HTU20_SDA_PORT,&GPIO_InitStructure;}/***@briefSetSDApindircateasintput*@retvalNone*/voidHTU20_SDA_IN({GPIO_StructInit(&GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin=HTU20_SDA_PIN;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_Level_2;GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;GPIO_Init(HTU20_SDA_PORT,&GPIO_InitStructure;}/***@briefSetSDApinashigh*@retvalNone*/voidHTU20_SDA_1({GPIO_SetBits(HTU20_SDA_PORT,HTU20_SDA_PIN;}/***@briefSetSDApinaslow*@retvalNone*/voidHTU20_SDA_0({GPIO_ResetBits(HTU20_SDA_PORT,HTU20_SDA_PIN;}/***@briefSetSCLpindircateasoutput*@retvalNone*/voidHTU20_SCL_OUT({GPIO_StructInit(&GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin=HTU20_SCL_PIN;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;GPIO_InitStructure.GPIO_OType=GPIO_OType_OD;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_Level_2;GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;GPIO_Init(HTU20_SCL_PORT,&GPIO_InitStructure;}/***@briefSetSCLpinashigh*@retvalNone*/voidHTU20_SCL_1({GPIO_SetBits(HTU20_SCL_PORT,HTU20_SCL_PIN;}/***@briefSetSCLpinaslow*@retvalNone*/voidHTU20_SCL_0({GPIO_ResetBits(HTU20_SCL_PORT,HTU20_SCL_PIN;}/*================================================================SimulationI2CFunction================================================================*//***@briefAshortdelayforiicsynchronization*@paramtus*@retvalNone*/voidI2CDelay(BYTEt{while(t--{__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;__nop(;}}/***@briefI2Cinit*@retvalNone*/voidI2CInit(void{HTUInit(;HTU20_SDA_OUT(;HTU20_SCL_OUT(;HTU20_SDA_1(;I2CDelay(1;HTU20_SCL_1(;I2CDelay(1;}/***@briefIICstartsignal*@commentSCL、SDA同为高,SDA跳变成低之后,SCL跳变成低*@retvalNone*/voidI2CStart(void{HTU20_SDA_OUT(;HTU20_SDA_1(;HTU20_SCL_1(;I2CDelay(5;//大于4.7usHTU20_SDA_0(;I2CDelay(5;//大于4usHTU20_SCL_0(;I2CDelay(5;}/***@briefIICStopSignal*@commetSCL、SDA同为低,SCL跳变成高之后,SDA跳变成高*@retvalNone*/voidI2CStop(void{HTU20_SDA_OUT(;HTU20_SDA_0(;HTU20_SCL_0(;I2CDelay(5;HTU20_SCL_1(;I2CDelay(5;HTU20_SDA_1(;I2CDelay(5;}/***@briefsimuIICwritebyte*@paramWrite_Byte:data*@retvalNone*/BYTEI2C_Write_Byte(BYTEWrite_Byte{BYTEi;HTU20_SDA_OUT(;for(i=0;i8;++i{if(Write_Byte&SHT20ADDR{HTU20_SDA_1(;}else{HTU20_SDA_0(;}I2CDelay(1;HTU20_SCL_1(;//输出SDA稳定后拉高SCL,从机检测到后采样I2CDelay(5;//保证足够长锁定时间,确保从机

1 / 21
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功