C51——分文件实现代码优化(温湿度代码)

分文件实现代码优化,
把几个模块使用分别建立成分文件(.c文件)
主函数调用分函数中的函数封装为.h形式,.h在主文件中用头文件形式展现
分文件例如:lcd1602.c/dht11.c/uart.c/dalay.c
被主函数调用的函数封装.h文件:例如lcd1602.h/dht11.h/uart.h/dalay.h
如果要实现两个文件中同时使用,也可以封装为.h形式,在两个文件中头文件形式使用
例如config.h头文件的建立。
注意*封装的.h文件中函数头要与.c文件头一致,否则会警告。
extern char datas[5];//声明为外部变量,使用时可以不进行传参
分文件见D:\代码\52单片机\7、DHT11温湿度传感器\5、分文件实现优化代码

以下为main.c文件,可以看到包含分文件的.h头文件

#include "reg52.h"
#include "intrins.h"
#include "dalay.h"
#include "uart.h"
#include "lcd1602.h"
#include "dht11.h"
#include "config.h"

extern char datas[5];//声明为外部变量,使用时可以不进行传参
char temp[10];
char hum[11];

void build_data()
{
  hum[0]='H';
 hum[1]='u';
 hum[2]='m';
 hum[3]=':';
 hum[4]=datas[0]/10+0x30;
 hum[5]=datas[0]%10+0x30;
 hum[6]='.';
 hum[7]=datas[1]/10+0x30;
 hum[8]=datas[1]%10+0x30;
 hum[9]='%';
 hum[10]='\0';

  temp[0]='T';
 temp[1]='e';
 temp[2]='m';
 temp[3]=':';
 temp[4]=datas[2]/10+0x30;
 temp[5]=datas[2]%10+0x30;
 temp[6]='.';
 temp[7]=datas[3]/10+0x30;
 temp[8]=datas[3]%10+0x30;
 temp[9]='\0';
}
void main()
{
  led1=1;
 UartInit();
 init_lcd1602();
 Delay1000ms();
  start_DHT11();
  while(1){
    Delay1000ms();
  read_DHT11_data();//本需要传参datas,有extern可以不传参
  if(datas[0]>35){
       FS=0;
    }
  if(datas[0]<35){
      FS=1;
    }
  build_data();
  show_line_lcd1602(2,2,temp);
  show_line_lcd1602(1,2,hum);
  sendstring("yang zerui nx\r\n");
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值