- //做了一个DHT11温湿度传感器的1602的显示,用8051做的,换成60s2的片子以后不行了...
- //我想应该是机械周期的事吧,应该怎么改呢,大家有遇到的帮帮我、、、这是简单的显示程序:
- //STC 延时比ATMEL 快了6~8倍,改一下DELAY的值就行了
- //这个你得该延时,因为8051是12T的时钟周期而60S2是1T的
- //这是一个延时的问题,把延时改一下就好了
- #include <STC12C5A60S2.h>
- #define uchar unsigned char
- #define uint unsigned int
- /*引脚定义*/
- uchar code table[]="0123456789";
- sbit en=P2^1;
- sbit rs=P2^0;
- sbit io=P1^1;
- /*定义变量*/
- uchar data_byte=0, count, SHISHI, SHIGE, WENSHI, WENGE;
- uint TH_data, TL_data, RH_data, RL_data, CK_data;
- uint TH_temp, TL_temp, RH_temp, RL_temp, CK_temp;
- uchar num;
- /*延时程序*/
- void delayms(unsigned int xms)
- {
- uint i,j;
- for(i=xms;i>0;i--)
- for(j=110;j>0;j--);
- }
- /*10微妙延时程序*/
- void delay1()
- {
- unsigned char i;
- for(i=0; i<3; i++);
- }
- /*写命令*/
- void command(uchar com)
- {
- rs=0;
- P0=com;
- en=1;
- delayms(5);
- en=0;
- delayms(5);
- }
- /*写数据*/
- void information(uchar date)
- {
- rs=1;
- P0=date;
- en=1;
- delayms(5);
- en=0;
- delayms(5);
- }
- /*初始化*/
- void init()
- {
- en=0;
- command(0x38);//设置16x2显示,5x7点阵,8位数据接口
- command(0x0c);//设置开显示,不显示光标
- command(0x06);//写一个字符后地址指针加一
- command(0x01);//显示清零,数据指针清零
- }
- /**********************DHT11模块**********************/
- uchar receive_byte()//接收一个字节
- {
- uchar i,temp;
- for(i=0;i<8; i++)
- {
- count = 2;
- while((!io) && count++)//等待50us低电平结束并防止死循环
- temp = 0;
- delay1();
- delay1();
- delay1();
- if (io==1)
- {
- temp = 1;
- count = 2;
- while(io && count++);
- }
- else
- temp = 0;
- data_byte<<=1;
- data_byte|=temp;
- }
- return (data_byte);
- }
- /*DHT11开始函数*/
- void read_io()//开始信号读数据并校验
- {
- io = 0;
- delayms(18);//主机拉低18ms
- io=1;//DATA总线由上拉电阻拉高 主机延时20us
- delay1();
- delay1();
- delay1();
- delay1();
- io = 1;//主机设置为输入高电平判断从机响应信号
- if(!io)
- {
- count = 2;
- while((!io)&&count++);//判断DHT11发出80us低电平响应信号是否结束
- count = 2;
- while(io && count++);//判断DHT11拉高总线80us高电平是否结束
- RH_temp = receive_byte();
- RL_temp = receive_byte();
- TH_temp = receive_byte();
- TL_temp = receive_byte();
- CK_temp = receive_byte();
- io = 1;
- num = (RH_temp+RL_temp+TH_temp+TL_temp);//数据校验
- if(num==CK_temp)
- {
- RH_data=RH_temp;
- RL_data=RL_temp;
- TH_data=TH_temp;
- TL_data=TL_temp;
- CK_data=CK_temp;
- }
- }
- //湿度整数部分
- SHISHI = RH_data/10;
- SHIGE = RH_data%10;
- //温度整数部分
- WENSHI =(char) TH_data/10;
- WENGE = (char) TH_data%10;
- }
- /*主函数*/
- void main()
- {
- P4SW=0x70;
- init();
- while(1)
- {
- read_io();
- command(0x80);
- information(table[WENSHI]);
- command(0x81);
- information(table[WENGE]);
- }
- }
复制代码 |