

c展开代码QQ 742971636
//AHT10----MSP430
//VCC----3.3V
//GND----GND
//SCL----P1.4
//SDA----P1.5
void AHT11_Read_Data( unsigned long *ct )       /* 接收湿度温度数据 */
{
 unsigned char byte_1th = 0;
 unsigned char byte_2th = 0;
 unsigned char byte_3th = 0;
 unsigned char byte_4th = 0;
 unsigned char byte_5th = 0;
 unsigned char byte_6th = 0;
 unsigned long retudata = 0;
 unsigned char cnt  = 0;
 AHT10_Mea();    /* 触发测量 */
 delay_ms( 80 ); /* 延时80毫秒 */
 cnt = 0;
 while ( ( (AHT10_Status() & 0x80) == 0x80) )
 {
  delay_ms( 3 );
  if ( cnt++ >= 100 )
  {
   break;
  }
 }
 I2C_Start();
 I2C_SendByte( AHT10AddRd );
 byte_1th = I2C_RecvByte();      /* 状态数据 */
 I2C_SendACK( 0 );               /* 应答 */
 byte_2th = I2C_RecvByte();      /* 湿度数据 */
 I2C_SendACK( 0 );
 byte_3th = I2C_RecvByte();      /* 湿度数据 */
 I2C_SendACK( 0 );
 byte_4th = I2C_RecvByte();      /* 高4位为湿度  低4位为温度 */
 I2C_SendACK( 0 );
 byte_5th = I2C_RecvByte();      /* 温度数据 */
 I2C_SendACK( 0 );
 byte_6th = I2C_RecvByte();      /* 温度数据 */
 I2C_SendACK( 1 );
 I2C_Stop();
 retudata = 0;            /* 原始湿度数据合成 */
 retudata = (byte_2th << 8) | byte_3th;
 retudata = ( (retudata << 8) | byte_4th) >> 4;
 retudata = retudata & 0x000fffff;
 ct[0]  = retudata;
 retudata = 0;            /* 原始温度数据合成 */
 retudata = ( (byte_4th % 16) << 8) | byte_5th;
 retudata = (retudata << 8) | byte_6th;
 retudata = retudata & 0x000fffff;
 ct[1]  = retudata;
}
void BeepAlarm()
{
 unsigned char beeptime=0;
 int cycl=500;
        P6DIR|=BIT2;
 while(cycl--)
 {
            P6OUT^=BIT2;
     for(beeptime=0;beeptime<20;beeptime++);
 }
 P6OUT|=BIT2;
}
int main( void )
{
 unsigned long CT_data[2];
 int  c1, t1;
        unsigned char xianshi[16];
        int dangwei=0;
 WDTCTL = WDTPW | WDTHOLD;       /* Stop watchdog timer */
 Clock_Init();                   /* 系统时钟设置 */
 Port_init();                    /* 显示器引脚初始化 */
 LCD_init();                     /* 显示器初始化 */
 LCD_Desk();                     /* 显示界面初始化 */
 AHT10_Init();
 /* _EINT();                                / * 开中断 * / */
 while ( 1 )
 {
  AHT11_Read_Data( CT_data );                              /* 读取温度和湿度 , 可间隔1.5S读一次 */
  c1 = CT_data[0] * 1000 / 1024 / 1024;              /* 计算得到湿度值(放大了10倍,如果c1=523,表示现在湿度为52.3%) */
  t1 = CT_data[1] * 200 * 10 / 1024 / 1024 - 500;    /* 计算得到温度值(放大了10倍,如果t1=245,表示现在温度为24.5℃) */
  delay_ms( 1500 );                                       /* 延时1.5S */
                xianshi[0]='R';
                xianshi[1]=':';
                xianshi[2]=c1%10000/1000+'0';
                xianshi[3]=c1%1000/100+'0';
                xianshi[4]=c1%100/10+'0';
                xianshi[5]='.';
                xianshi[6]=c1%10/1+'0';
                xianshi[7]='%';
                xianshi[8]=0;
                LCD_write_str(0,0,xianshi);
                xianshi[0]='T';
                xianshi[1]=':';
                xianshi[2]=t1%10000/1000+'0';
                xianshi[3]=t1%1000/100+'0';
                xianshi[4]=t1%100/10+'0';
                xianshi[5]='.';
                xianshi[6]=t1%10/1+'0';
                xianshi[7]='C';
                xianshi[8]=0;
                LCD_write_str(0,1,xianshi);
                c1/=10;//shidu
                t1/=10;//wendu
                if(t1>35 || c1>80 ||  t1<10  || c1<30  )
                {
                    dangwei=1;
                }else if(t1>30 || c1>70 ||  t1<15  || c1<40  )
                {
                    dangwei=2;
                }else
                {
                     dangwei=0;
                }
                if(dangwei)
                {
                   BeepAlarm();//有档位 蜂鸣器就响
                }
                //显示档位
                xianshi[0]='D';
                xianshi[1]=':';
                xianshi[2]='0'+dangwei;
                xianshi[3]=0;
                LCD_write_str(10,1,xianshi);
 }
}
csharp展开代码https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2


本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!