找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3110|回复: 0
打印 上一主题 下一主题
收起左侧

51单片机设计Hs1101湿度计源程序

[复制链接]
跳转到指定楼层
楼主
ID:297498 发表于 2018-3-26 20:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<reg52.h>
#include <absacc.h>
#define uint unsigned int
#define uchar unsigned char
sbit rs=P2^0;
sbit rw=P2^1;
sbit en=P2^2;
unsigned char  shuju1[9]={0};         //湿度
uchar code table2[16]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x53,0x48,0x49,0x25,0x2e,0x20};        //湿度调用

/**************1602d调用函数*******************/

void write_com(uchar com);
void write_shuju(uchar shuju);
void init();
void delay(uchar z);

/****************hs1101调用函数*************************/
void readfrequency(void);           //  读频率函数//
bit minrange=0;                     //湿度下限标志位//
bit maxrange=0;                     //湿度上限标志位//
bit T0Over=0;                       //定时器结束标记

unsigned char w,x,y;

/******************主函数******************/
void main()                  
{
    init();
    while(1)   
    {
        readfrequency();
    }
}


/*****************1602延时程序*****************/

void delay(uchar z)
{
    uint a,b;
    for(a=0;a<z;a++)
        for(b=0;b<110;b++);
}

/*******************1602初始化**************/

void init()
{
    en=0;
    write_com(0x38);        //设置为8位并行,显示2行,5*7点阵显示
    write_com(0x0c);        //设置显示开 无光标 光标不闪烁
    write_com(0x01);          //清屏指令
    write_com(0x80);
}

/***************1602写指令*********************/

void write_com(uchar com)
{
    rs=0;
    rw=0;
    P1=com;
    delay(5);
    en=1;
    en=0;
}

/**************1602写数据**********************/

void write_shuju(uchar shuju)
{
    rs=1;
    rw=0;
    P1=shuju;
    delay(5);
    en=1;
    en=0;
}



/****************中断*****************************/

void timer0() interrupt 1
{
    TR1=0;
    TR0=0;
    TF0=0;
    ET0=0;
    T0Over=1;
}

/*************湿度传感器频率采集及转换模块***************/

void readfrequency()
{
    unsigned int ftequency,RH;
    TMOD=0x51;
    TH0=0x4C;
    TL0=0X00;
    TH1=0;
    TL1=0;
    TR1=1;
    TR0=1;
    ET0=1;
    ET1=0;
    EA=1;
    T0Over=0;
    while(!T0Over)
    write_com(0x80+0x40);
    write_shuju(table2[shuju1[0]]);
    write_com(0x80+0x41);
    write_shuju(table2[shuju1[1]]);
    write_com(0x80+0x42);
    write_shuju(table2[shuju1[2]]);
    write_com(0x80+0x43);
    write_shuju(table2[shuju1[3]]);
    write_com(0x80+0x44);
    write_shuju(table2[shuju1[4]]);
    write_com(0x80+0x45);
    write_shuju(table2[shuju1[5]]);
    write_com(0x80+0x46);
    write_shuju(table2[shuju1[6]]);
    write_shuju(table2[shuju1[7]]);
    write_shuju(table2[shuju1[8]]);
    ftequency=(TH1*256+TL1)*20;
    minrange=0;
    maxrange=0;
    if(ftequency)
    {
        if(ftequency<6033)
            maxrange=1;
        if(ftequency>7351)
            minrange=1;
        if(6330<ftequency<=7351)
            RH=5880-0.8*ftequency;
        if(6033<=ftequency<=6330)
            RH=5860-0.8*ftequency;
        shuju1[0]=10;
        shuju1[1]=11;
        shuju1[2]=12;
        shuju1[3]=15;
        shuju1[4]=RH/100;
        shuju1[5]=(RH%100)/10;
        shuju1[6]=14;
        shuju1[7]=(RH%10);
        shuju1[8]=13;
    }
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表