基于STC12C5A60S2单片机的DHT11+LCD12864代码
- DHT11.h
- DHT11.c
- LCD12864.h
- LCD12864.c
#ifndef __DHT11_H
#define __DHT11_H#include <STC12C5A60S2.H>
#include <intrins.h>#ifndef __UDEFINE_
#define __UDEFINE_
#define uchar unsigned char
#define uint unsigned int
#endif
sbit WS_data = P1^0;
void delay1s(void);
uchar WS_Start(void);
uchar WS_Read(void);
void get_temandhum(uchar* tem,uchar* hum);#endif
#include "NHT11.h"
#include "LCD12864.h"
uchar WS_Start()
{unsigned char a,b,c;uint flag = 255;WS_data = 0;for(c=193;c>0;c--)for(b=114;b>0;b--)for(a=1;a>0;a--);WS_data = 1;while(WS_data == 1 && flag > 10){flag--;} if(WS_data == 0){flag = 1;for(b=1;b>0;b--)for(a=218;a>0;a--);}else{flag = 0;}return flag;
}uchar WS_Read()
{uchar a,b;uchar num = 0x00;uchar i,temp;for(i = 0; i < 8;i++){b= 255;while(WS_data == 1 && b > 0){b--; } b= 255;while(WS_data == 0 && b > 0){b--;} for(a=109;a>0;a--); temp = WS_data;num = (num << 1)|temp; }return num;
}void get_temandhum(uchar* tem,uchar* hum)
{uchar ERR[] = "ReaERR";uchar RIT[] = "ReaCOR";uchar LTEM,HTEM,LHUM,HHUM,Check;Check = WS_Start();if(Check == 1){HHUM = WS_Read();LHUM = WS_Read();HTEM = WS_Read();LTEM = WS_Read();Check = WS_Read();if(Check == HHUM + LHUM + HTEM +LTEM){*tem = HTEM;*hum = HHUM;LCD_Pos(1,0);for(Check = 0; Check < 6;Check++){LCD_WriteDat(RIT[Check]); }}else{LCD_Pos(1,0);for(Check = 0; Check < 6;Check++){LCD_WriteDat(ERR[Check]); } } }else{}
}
#ifndef __LCD12864_H
#define __LCD12864_H#include <STC12C5A60S2.H>
#include <intrins.h>#ifndef __UDEFINE_
#define __UDEFINE_
#define uchar unsigned char
#define uint unsigned int
#endif
#define LCD_data P0
sbit EN = P3^4;
sbit RS = P3^5;
sbit RW = P3^6;
sbit PSB = P3^7;
void delay1ms(void);
void delay1s(void);
void LCD_Init(void);
void LCD_WriteCmd(uchar cmd);
void LCD_WriteDat(uchar dat);
void LCD_Pos(uchar x,uchar y);
void LCD_Test(void);#endif
#include "LCD12864.h"
void LCD_Test()
{uchar i,j;uchar TestStr[] = " 测试 ";LCD_Init();LCD_Pos(1,0);while(1){LCD_WriteCmd(0x0c);for(i = 0;i < 20;i+=2){LCD_Pos(1,0);for(j = i;j< 16+i;j++)LCD_WriteDat(TestStr[j]);delay1s();}}
}void LCD_Pos(uchar x,uchar y)
{uchar pos;if(1 == x){x = 0x80;}else if(2 == x){x = 0x90;}else if(3 == x){x = 0x88;}else if(4 == x){x = 0x98;}pos = x+y;LCD_WriteCmd(pos);
}void LCD_Init()
{PSB = 1; LCD_WriteCmd(0x01);delay1ms(); LCD_WriteCmd(0x30);delay1ms(); LCD_WriteCmd(0x0C);delay1ms();
}void delay1s(void)
{unsigned char a,b,c;for(c=217;c>0;c--)for(b=171;b>0;b--)for(a=73;a>0;a--);
}void delay1ms(void)
{unsigned char a,b;for(b=21;b>0;b--)for(a=130;a>0;a--);_nop_();
}void LCD_WriteCmd(uchar cmd)
{RW = 0;RS = 0;EN = 0;delay1ms();LCD_data = cmd;delay1ms();EN = 1;delay1ms();EN = 0;
}void LCD_WriteDat(uchar dat)
{RW = 0;RS = 1;EN = 0;delay1ms();LCD_data = dat;delay1ms();EN = 1;delay1ms();EN = 0;
}