#include"reg52.h"#include"intrins.h"#definedatabufferP0 //注意不带分号
sbit RS = P1^1;
sbit RW = P1^2;
sbit EN = P1^3;voidDelay15ms()//@11.0592MHz{unsignedchar i, j;
i =27;
j =226;do{while(--j);}while(--i);}voidDelay5ms()//@11.0592MHz{unsignedchar i, j;
i =9;
j =244;do{while(--j);}while(--i);}voidCheck_Busy(){char tmp =0x80;
databuffer =0x80;while(tmp &0x80){
RS =0;
RW =1;
EN =0;_nop_();
EN =1;_nop_();_nop_();
tmp = databuffer;_nop_();
EN =0;//状态2前面添加这两条语句即可。_nop_();//_nop_();}}voidWrite_Cmd_Func(char Position){Check_Busy();
RS =0;//写指令即,数据a要显示的地址
RW =0;
EN =0;_nop_();
databuffer = Position;//数据传输点。在写操作下,处于EN = 1前面。_nop_();
EN =1;//状态2前面添加这两条语句即可。_nop_();_nop_();
EN =0;_nop_();}voidWrite_Data_Func(char ShowData){Check_Busy();
RS =1;//写数据a
RW =0;
EN =0;_nop_();
databuffer = ShowData;//数据传输点。在写操作下,处于EN = 1前面。_nop_();
EN =1;_nop_();_nop_();
EN =0;_nop_();}voidLCD_1602_Init(){Delay15ms();Write_Cmd_Func(0x38);Delay5ms();Write_Cmd_Func(0x38);Write_Cmd_Func(0x08);Write_Cmd_Func(0x01);Write_Cmd_Func(0x06);Write_Cmd_Func(0x0c);//0x0c}voidmain(){char Position =0x80+0x04;char ShowData ='A';LCD_1602_Init();Write_Cmd_Func(Position);Write_Data_Func(ShowData);}
LCD模块显示一个字符串
#include"reg52.h"#include"intrins.h"#definedatabufferP0//注意不带分号
sbit RS = P1^1;
sbit RW = P1^2;
sbit EN = P1^3;voidDelay15ms()//@11.0592MHz{unsignedchar i, j;
i =27;
j =226;do{while(--j);}while(--i);}voidDelay5ms()//@11.0592MHz{unsignedchar i, j;
i =9;
j =244;do{while(--j);}while(--i);}voidCheck_Busy(){char tmp =0x80;
databuffer =0x80;while(tmp &0x80){
RS =0;
RW =1;
EN =0;_nop_();
EN =1;_nop_();_nop_();
tmp = databuffer;_nop_();
EN =0;//状态2前面添加这两条语句即可。_nop_();//_nop_();}}voidWrite_Cmd_Func(char Position){Check_Busy();
RS =0;//写指令即,数据a要显示的地址
RW =0;
EN =0;_nop_();
databuffer = Position;//数据传输点。在写操作下,处于EN = 1前面。_nop_();
EN =1;//状态2前面添加这两条语句即可。_nop_();_nop_();
EN =0;_nop_();}voidWrite_Data_Func(char ShowData){Check_Busy();
RS =1;//写数据a
RW =0;
EN =0;_nop_();
databuffer = ShowData;//数据传输点。在写操作下,处于EN = 1前面。_nop_();
EN =1;_nop_();_nop_();
EN =0;_nop_();}voidLCD_1602_Init(){Delay15ms();Write_Cmd_Func(0x38);Delay5ms();Write_Cmd_Func(0x38);Write_Cmd_Func(0x08);Write_Cmd_Func(0x01);Write_Cmd_Func(0x06);Write_Cmd_Func(0x0c);//0x0c}voidLCD1602_ShowData_Line(int row,int col,char* string){switch(row){case1:Write_Cmd_Func(0x80+col);//确定第一行的首字母位置,其他依次写入即可,即多次调用Write_Data_Func()函数即可。while(*string){Write_Data_Func(*string);
string++;}break;case2:Write_Cmd_Func(0x80+0x40+col);while(*string){Write_Data_Func(*string);
string++;}break;}}voidmain(){LCD_1602_Init();LCD1602_ShowData_Line(1,5,"Hello World!");}
DHT11模块
1.检测DHT11是否存在的方法
方法一编程实现(不用看)
#include"reg52.h"#include"intrins.h"
sbit LED1 = P3^6;
sbit DHT = P1^0;voidDelay2000ms()//@11.0592MHz{unsignedchar i, j, k;_nop_();_nop_();
i =85;
j =12;
k =155;do{do{while(--k);}while(--j);}while(--i);}voidDelay30ms()//@11.0592MHz{unsignedchar i, j, k;_nop_();_nop_();
i =2;
j =67;
k =183;do{do{while(--k);}while(--j);}while(--i);}voidDelay40us()//@11.0592MHz{unsignedchar i;
i =108;while(--i);}voidCheck_DHT11(){//a点DHT = 1;//ab区间 延时,但未说明具体延时时间//b点DHT = 0;//bc区间 延时至少18ms,可取30ms//c点DHT = 1;//Dp模块检测点,距离c点40us或者50us或者60us处//若Dp电平为低电平。说明模块存在,并将LED1灯点亮
DHT =1;
DHT =0;Delay30ms();
DHT =1;//c点Delay40us();//找到Dp点if(DHT ==0){
LED1 =0;}}voidmain(){// 1.模块上电稳定// 2.LED初始保持熄灭状态// 3.检测模块是否存在
LED1 =1;Delay2000ms();Check_DHT11();while(1);}
方法二编程实现(推荐)
2.DHT11模块测温湿度的代码
#include"reg52.h"#include"intrins.h"
sbit DHT = P1^0;char flag ;char tmp;char datas[6];voidDelay2000ms()//@11.0592MHz{unsignedchar i, j, k;_nop_();
i =15;
j =2;
k =235;do{do{while(--k);}while(--j);}while(--i);}voidDelay1000ms()//@11.0592MHz{unsignedchar i, j, k;_nop_();
i =8;
j =1;
k =243;do{do{while(--k);}while(--j);}while(--i);}voidDelay30ms()//@11.0592MHz{unsignedchar i, j;
i =54;
j =199;do{while(--j);}while(--i);}voidDelay40us()//@11.0592MHz{unsignedchar i;_nop_();
i =15;while(--i);}voidDHT11_Start(){
DHT =1;
DHT =0;Delay30ms();
DHT =1;while(DHT);//找到Dp点,模块存在while(!DHT);//e点while(DHT);//找到f点}voidRead_Data_FromDHT11(){int i ;int j ;DHT11_Start();//每测量一次温湿度需调用一次该函数for(i =1;i<=5;i++){for(j =1;j<=8;j++){while(!DHT);Delay40us();//单片机破译 01区分点,// 即由高电平持续时间不同代表逻辑01if(DHT ==1){
flag =1;while(DHT);}else{
flag =0;}
tmp = tmp<<1;
tmp = tmp|flag;}
datas[i]= tmp;}}voidmain(){//1.DHT11模块上电稳定//2.DHT11模块初始化//3.不断获取 单片机获取DHT11模块传来的温湿度数据//与串口结合,将获取的数据上传到PC串口助手Delay2000ms();while(1){Delay1000ms();Read_Data_FromDHT11();}}
3.单片机通过串口将温湿度上传到上位机
串口相关的编程实现
#include"reg52.h"#include"intrins.h"
sfr AUXR =0x8E;voidUartInit(void)//9600bps@11.0592MHz{
AUXR =0x01;
SCON =0x50;//配置串口工作方式1,REN使能接收
TMOD &=0x0F;
TMOD |=0x20;//定时器1工作方式位8位自动重装
TH1 =0xFD;
TL1 =0xFD;//9600波特率的初值
TR1 =1;//启动定时器
EA =1;//开启总中断
ES =1;//开启串口中断}voidDelay1000ms()//@11.0592MHz{unsignedchar i, j, k;_nop_();
i =8;
j =1;
k =243;do{do{while(--k);}while(--j);}while(--i);}voidsendByte(char data_msg){
SBUF = data_msg;while(!TI);
TI =0;}voidsendString(char* str){while(*str !='\0'){sendByte(*str);
str++;}}voidmain(){UartInit();while(1){Delay1000ms();//往发送缓冲区写入数据,就完成数据的发送sendString("Hello world!\r\n");}}
串口上传DHT11模块温湿度的数据编程实现
#include"reg52.h"#include"intrins.h"
sfr AUXR =0x8E;
sbit DHT = P1^0;char flag ;char tmp;char datas[6];voidUartInit(void)//9600bps@11.0592MHz{
AUXR =0x01;
SCON =0x50;//配置串口工作方式1,REN使能接收
TMOD &=0x0F;
TMOD |=0x20;//定时器1工作方式位8位自动重装
TH1 =0xFD;
TL1 =0xFD;//9600波特率的初值
TR1 =1;//启动定时器
EA =1;//开启总中断
ES =1;//开启串口中断}voidDelay1000ms()//@11.0592MHz{unsignedchar i, j, k;_nop_();
i =8;
j =1;
k =243;do{do{while(--k);}while(--j);}while(--i);}voidDelay2000ms()//@11.0592MHz{unsignedchar i, j, k;_nop_();
i =15;
j =2;
k =235;do{do{while(--k);}while(--j);}while(--i);}voidDelay30ms()//@11.0592MHz{unsignedchar i, j;
i =54;
j =199;do{while(--j);}while(--i);}voidDelay40us()//@11.0592MHz{unsignedchar i;_nop_();
i =15;while(--i);}voidsendByte(char data_msg){
SBUF = data_msg;while(!TI);
TI =0;}voidsendString(char* str){while(*str !='\0'){sendByte(*str);
str++;}}voidDHT11_Start(){
DHT =1;
DHT =0;Delay30ms();
DHT =1;while(DHT);//找到Dp点,模块存在while(!DHT);//e点while(DHT);//找到f点}voidRead_Data_FromDHT11(){int i ;int j ;DHT11_Start();//每测量一次温湿度需调用一次该函数for(i =1;i<=5;i++){for(j =1;j<=8;j++){while(!DHT);Delay40us();//单片机破译 01区分点,// 即由高电平持续时间不同代表逻辑01if(DHT ==1){
flag =1;while(DHT);}else{
flag =0;}
tmp = tmp<<1;
tmp = tmp|flag;}
datas[i]= tmp;}}voidmain(){UartInit();Delay2000ms();while(1){Delay1000ms();Read_Data_FromDHT11();//往发送缓冲区写入数据,就完成数据的发送sendString("H:");sendByte(datas[1]/10+0x30);sendByte(datas[1]%10+0x30);sendString(".");sendByte(datas[2]/10+0x30);sendByte(datas[2]%10+0x30);sendString(" && ");sendString("T:");sendByte(datas[3]/10+0x30);sendByte(datas[3]%10+0x30);sendString(".");sendByte(datas[4]/10+0x30);sendByte(datas[4]%10+0x30);sendString("\r\n");}}
LCD1602相关的编程实现
4.温湿度小系统
未分文件编程
#include"reg52.h"#include"intrins.h"#definedatabufferP0
sfr AUXR =0x8E;
sbit DHT = P1^0;
sbit RS = P1^1;
sbit RW = P1^2;
sbit EN = P1^3;char datas[6];char huma[9];char temp[9];char flag ;char tmp;voidUartInit(void)//9600bps@11.0592MHz{
AUXR =0x01;
SCON =0x50;//配置串口工作方式1,REN使能接收
TMOD &=0x0F;
TMOD |=0x20;//定时器1工作方式位8位自动重装
TH1 =0xFD;
TL1 =0xFD;//9600波特率的初值
TR1 =1;//启动定时器
EA =1;//开启总中断
ES =1;//开启串口中断}voidDelay1000ms()//@11.0592MHz{unsignedchar i, j, k;_nop_();
i =8;
j =1;
k =243;do{do{while(--k);}while(--j);}while(--i);}voidDelay2000ms()//@11.0592MHz{unsignedchar i, j, k;_nop_();
i =15;
j =2;
k =235;do{do{while(--k);}while(--j);}while(--i);}voidDelay30ms()//@11.0592MHz{unsignedchar i, j;
i =54;
j =199;do{while(--j);}while(--i);}voidDelay40us()//@11.0592MHz{unsignedchar i;_nop_();
i =15;while(--i);}voidDelay15ms()//@11.0592MHz{unsignedchar i, j;
i =27;
j =226;do{while(--j);}while(--i);}voidDelay5ms()//@11.0592MHz{unsignedchar i, j;
i =9;
j =244;do{while(--j);}while(--i);}voidsendByte(char data_msg){
SBUF = data_msg;while(!TI);
TI =0;}voidsendString(char* str){while(*str !='\0'){sendByte(*str);
str++;}}voidDHT11_Start(){
DHT =1;
DHT =0;Delay30ms();
DHT =1;while(DHT);//找到Dp点,模块存在while(!DHT);//e点while(DHT);//找到f点}voidRead_Data_FromDHT11(){int i ;int j ;DHT11_Start();//每测量一次温湿度需调用一次该函数for(i =1;i<=5;i++){for(j =1;j<=8;j++){while(!DHT);Delay40us();//单片机破译 01区分点,// 即由高电平持续时间不同代表逻辑01if(DHT ==1){
flag =1;while(DHT);}else{
flag =0;}
tmp = tmp<<1;
tmp = tmp|flag;}
datas[i]= tmp;}}voidCheck_Busy(){char tmp =0x80;
databuffer =0x80;while(tmp &0x80){
RS =0;
RW =1;
EN =0;_nop_();
EN =1;_nop_();_nop_();
tmp = databuffer;_nop_();
EN =0;//状态2前面添加这两条语句即可。_nop_();//_nop_();}}voidWrite_Cmd_Func(char Position){Check_Busy();
RS =0;//写指令即,数据a要显示的地址
RW =0;
EN =0;_nop_();
databuffer = Position;//数据传输点。在写操作下,处于EN = 1前面。_nop_();
EN =1;//状态2前面添加这两条语句即可。_nop_();_nop_();
EN =0;_nop_();}voidWrite_Data_Func(char ShowData){Check_Busy();
RS =1;//写数据a
RW =0;
EN =0;_nop_();
databuffer = ShowData;//数据传输点。在写操作下,处于EN = 1前面。_nop_();
EN =1;_nop_();_nop_();
EN =0;_nop_();}voidLCD_1602_Init(){Delay15ms();Write_Cmd_Func(0x38);Delay5ms();Write_Cmd_Func(0x38);Write_Cmd_Func(0x08);Write_Cmd_Func(0x01);Write_Cmd_Func(0x06);Write_Cmd_Func(0x0c);//0x0c}voidLCD1602_ShowData_Line(int row,int col,char* string){switch(row){case1:Write_Cmd_Func(0x80+col);//确定第一行的首字母位置,其他依次写入即可,即多次调用Write_Data_Func()函数即可。while(*string){Write_Data_Func(*string);
string++;}break;case2:Write_Cmd_Func(0x80+0x40+col);while(*string){Write_Data_Func(*string);
string++;}break;}}voidData_Build(){//T:34.55°
huma[0]='H';
huma[1]=':';
huma[2]= datas[1]/10+0x30;
huma[3]= datas[1]%10+0x30;
huma[4]='.';
huma[5]= datas[2]/10+0x30;
huma[6]= datas[2]%10+0x30;
huma[7]='C';
huma[8]='\0';
temp[0]='T';
temp[1]=':';
temp[2]= datas[3]/10+0x30;
temp[3]= datas[3]%10+0x30;
temp[4]='.';
temp[5]= datas[4]/10+0x30;
temp[6]= datas[4]%10+0x30;
temp[7]='C';
temp[8]='\0';}voidmain(){UartInit();LCD_1602_Init();Delay2000ms();while(1){Read_Data_FromDHT11();Data_Build();//往发送缓冲区写入数据,就完成数据的发送// sendByte(datas[1]/10+0x30);Delay1000ms();Delay1000ms();sendString(huma);sendString(" ");sendString(temp);sendString("\r\n");LCD1602_ShowData_Line(1,4,huma);LCD1602_ShowData_Line(2,4,temp);}}