1873 views|6 replies

1100

Posts

3

Resources
The OP
 

"Playing with the board" + playing with the first development board in my life again [Copy link]

 
This post was last edited by gaoyang9992006 on 2020-2-16 12:59

Speaking of this board, it was ten years ago. Ten years ago, I just went to Harbin to study and bought a 51 development board in the Electronic World. From then on, I started my journey of learning single-chip microcomputers. For several years after graduation, it has been lying in a box in my house. Recently, due to the virus, I can only stay at home. Recently, the weather has changed suddenly, so I want to make a thermometer and hygrometer to measure the temperature and humidity in the bedroom, so as to know whether to turn on or off the air conditioner.

I found the development board and a 1602 LCD. After testing, I found that only the right half of the 1602 could be displayed. I guess it was seriously aged. I also found an AM2301 temperature and humidity sensor. It was perfect and could work.

This is my development board. I spent 12 yuan on it. It only has the minimum system. I thought I got it cheap. . .

This is an ISP programmer, used to burn AT89S52 microcontrollers. I spent 45 yuan at the time, which was actually expensive. The boss also asked me if I had inquired about the price at other stores, and I said no.

Then I was given this price.

I bought this later on a second-hand market, I forgot how much it cost.

This is the natural temperature and humidity indoors. It is still relatively dry. In the north, it rained yesterday and it is sunny today. It is relatively cold. It was even frozen outside in the morning.

Cover it with your hands to see if there is any change

The change is obvious. Because of the outer shell, the temperature rises slowly, making it easier to measure the ambient temperature and not affected too much by the local temperature.


Next is to share the source code and project. It is very useful for reference. Because I wrote it in separate header files, you can use it directly and it is very easy to port it to other 51 projects.

LCD1602_AM2301.zip (20.57 KB, downloads: 7)



This content was originally created by EEWORLD forum user gaoyang9992006 . If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source

Latest reply

Awesome, I think he has strong hands-on ability   Details Published on 2020-4-10 23:04
 
 

1100

Posts

3

Resources
2
 

LCD1602.C is the main file, and the main function is also in it. It is convenient for LCD1602 testing, and can also be directly cut to another file.

#include"LCD1602.h"
#include"AM2301.h"
void delay(unsigned int j)
 {
	 for(; j>0; j--);
 }

void LCD1602CheckBusy(void)
{
	unsigned char tem;
	LCD1602_RS_Low();
	LCD1602_RW_High();
	LCD1602_EN_Low(); 
	while(1)	
	{
		LCD1602_DATA_BUS=0xFF;
		LCD1602_EN_High();
//		tem=LCD1602_DATA_BUS;
		LCD1602_READ_DATA(tem,LCD1602_DATA_BUS);
		LCD1602_EN_Low();
		if((tem&0x80)==0) break;		
	}
}
 
 void LCD1602WriteCom(unsigned char com)
{
	LCD1602CheckBusy();
	LCD1602_RS_Low();
	LCD1602_RW_Low();
	LCD1602_EN_Low();
  //	LCD1602_DATA_BUS=com;
	LCD1602_READ_DATA(LCD1602_DATA_BUS,com);
	delay(2);
	LCD1602_EN_High();
	delay(2);
	LCD1602_EN_Low();
}

void LCD1602WriteData(unsigned char dat)
{
	LCD1602CheckBusy();
	LCD1602_RS_High();
	LCD1602_RW_Low();
	LCD1602_EN_Low();	
//	LCD1602_DATA_BUS=dat;
	LCD1602_READ_DATA(LCD1602_DATA_BUS,dat);
	delay(2);
	LCD1602_EN_High();
	delay(2);
	LCD1602_EN_Low();
}

void LCD1602Init(void)
{
	LCD1602_EN_Low();
	LCD1602WriteCom(0x38);
	LCD1602WriteCom(0x0c);
	LCD1602WriteCom(0x06);
	LCD1602WriteCom(0x01);
	LCD1602WriteCom(0x80);
}
/*
void LCD1602DispString(unsigned char addr,unsigned char *str)
{
	unsigned char i=0;
	LCD1602WriteCom(addr);
	while(*str>0)
	{
		LCD1602WriteData(*str++);
		delay(10);
	}
}
*/
/*写1位数字到指定的位置。参数:位置,1位字符*/
void LCD1602Disp1(unsigned char addr,unsigned char num)
{
	LCD1602WriteCom(addr);
	LCD1602WriteData(num);
	delay(2);//延时13+11*2 us,35us.
}

void main(void)
{
	unsigned int shidu,wendu;

	LCD1602Init();


	{

//		LCD1602DispString(0x80,"ABCDEFGHIJKLMNO"); 
//		LCD1602DispString(0xC0,"123456789012345");
//		LCD1602Disp1(0x8F,'0'+3);
//		LCD1602Disp1(0xCF,':');		
	}		
	while(1)
	{
		Read_Sensor();		  // 读取传感器数据
		shidu=Sensor_Data[0];
		shidu<<=8;
		shidu=shidu|Sensor_Data[1];
		wendu=Sensor_Data[2];
		wendu<<=8;
		wendu=wendu|Sensor_Data[3];
		LCD1602Disp1(0x8B,'0'+shidu/100);	
		LCD1602Disp1(0x8C,'0'+shidu%100/10);
		LCD1602Disp1(0x8D,'.');		
		LCD1602Disp1(0x8E,'0'+shidu%100%10);
		LCD1602Disp1(0x8F,'%');
		if(wendu&0x8000)
		{
			LCD1602Disp1(0xCA,'-');
			wendu=wendu&0x7FFF;
		}
		else
		LCD1602Disp1(0xCA,'+'); 
		
		LCD1602Disp1(0xCB,'0'+wendu/100);	
		LCD1602Disp1(0xCC,'0'+wendu%100/10);
		LCD1602Disp1(0xCD,'.');			
		LCD1602Disp1(0xCE,'0'+wendu%100%10);
		LCD1602Disp1(0xCF,'C');			
	}

}

 
 
 

1100

Posts

3

Resources
3
 

This is the LCD1602.h file, which defines the pin assignments. By modifying this file, you can easily migrate to other pins in the 51 project.

#include <AT89X52.h>
#include<intrins.h>  //包含_nop_()函数定义的头文件
#define LCD1602_DATA_BUS P0

sbit LCD1602_RS=P2^0;
sbit LCD1602_RW=P2^1;
sbit LCD1602_EN=P2^2;
#define LCD1602_RS_High() LCD1602_RS=1
#define LCD1602_RS_Low()  LCD1602_RS=0
#define LCD1602_RW_High() LCD1602_RW=1
#define LCD1602_RW_Low()  LCD1602_RW=0
#define LCD1602_EN_High() LCD1602_EN=1
#define LCD1602_EN_Low()  LCD1602_EN=0
/*从b里读数据写到a.		例如:tem=LCD1602_DATA_BUS;可以写作LCD1602_READ_DATA(tem,LCD1602_DATA_BUS);*/
#define LCD1602_READ_DATA(a_addr,b_addr) a_addr=b_addr


/*延时函数,延时时间=13+11*j,例如当j=17时,延时为200us*/
void delay(unsigned int j);
/*写指令函数。参数:指令*/
void LCD1602WriteCom(unsigned char com);
/*写数据函数。参数:写入数据*/
void LCD1602WriteData(unsigned char dat);
/*初始化函数。参数:无*/
void LCD1602Init(void);
/*检查忙指令。参数:无*/
void LCD1602CheckBusy(void);
/*写字符串。参数:地址(第一行0x80~0x8F,第二行0xC0~0xCF),字符串*/
void LCD1602DispString(unsigned char addr,unsigned char *str);
/*写1位数字到指定的位置。参数:位置,1位字符*/
void LCD1602Disp1(unsigned char addr,unsigned char num);

 
 
 

1100

Posts

3

Resources
4
 

This is AM2301.h

The ports and arrays for storing temperature and humidity are defined. Just include this file and call the read function, then read the conversion results from the array for display.

//AM2301
#include <AT89X52.h>
#include<intrins.h>  //包含_nop_()函数定义的头文件
//读传感器 端口位定义,可修改
sbit AM2301_PIN = P2^3;

// 变量定义
unsigned char Sensor_Data[5]={0x00,0x00,0x00,0x00,0x00};
unsigned char Sensor_Check;		  //校验和

unsigned char Sensor_AnswerFlag;  //收到起始标志位
unsigned char Sensor_ErrorFlag;   //读取传感器错误标志
unsigned int  Sys_CNT;

/********************************************\
|* 功能: 延时	晶振为12M时				    *|
|*  t = 1 为 20us  然后成倍增加10us左右		*|
\********************************************/
void Delay_N10us(unsigned char t)
{
    while(t--)
   {
   	 _nop_();
   }
}
/********************************************\
|* 功能: 延时	晶振为12M时					*|
|* 延时大约 1ms			    				*|
\********************************************/ 
void Delay_N1ms(unsigned int t)
{
  unsigned int i;
  unsigned int j;
  for(j=t;j>0;j--)
     for(i=124;i>0;i--);  //延时大约 1ms
}

/********************************************\
|* 功能: 读传感器发送的单个字节	        *|
\********************************************/
unsigned char Read_SensorData(void)
  {
	unsigned char i,cnt;
	unsigned char buffer,tmp;
	buffer = 0;
	for(i=0;i<8;i++)
	{
		cnt=0;
		while(!AM2301_PIN)	//检测上次低电平是否结束
		{
		  if(++cnt >= 300)
		   {
			  break;
		   }
		}
		//延时Min=26us Max50us 跳过数据"0" 的高电平
		Delay_N10us(2);	 //延时30us   
		
		//判断传感器发送数据位
		tmp =0;
		if(AM2301_PIN)	 
		{
		  tmp = 1;
		}  
		cnt =0;
		while(AM2301_PIN)		//等待高电平 结束
		{
		   	if(++cnt >= 200)
			{
			  break;
			}
		}
		buffer <<=1;
		buffer |= tmp;	
	}
	return buffer;
  }

/********************************************\
|* 功能: 读传感器              	        *|
\********************************************/
unsigned char Read_Sensor(void)
  {
		unsigned char i;
		//主机拉低(Min=800US Max=20Ms) 
    AM2301_PIN = 0;
		Delay_N1ms(2);  //延时2Ms
		//释放总线 延时(Min=30us Max=50us)
		AM2301_PIN = 1; 	
		Delay_N10us(1);//延时30us
		//主机设为输入 判断传感器响应信号 
		AM2301_PIN = 1; 
   	Sensor_AnswerFlag = 0;  // 传感器响应标志	 
		//判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行	  
		if(AM2301_PIN ==0)
		{
			 Sensor_AnswerFlag = 1;//收到起始信号
			 Sys_CNT = 0;
			 //判断从机是否发出 80us 的低电平响应信号是否结束	 
			 while((!AM2301_PIN))
			 {
					 if(++Sys_CNT>300) //防止进入死循环
					{
					 Sensor_ErrorFlag = 1;
					 return 0;
					} 
				}
				Sys_CNT = 0;
				//判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
				while((AM2301_PIN))
				{
					 if(++Sys_CNT>300) //防止进入死循环
				 {
					 Sensor_ErrorFlag = 1;
					 return 0;
				 } 
				} 		 
				// 数据接收	传感器共发送40位数据 
				// 即5个字节 高位先送  5个字节分别为湿度高位 湿度低位 温度高位 温度低位 校验和
				// 校验和为:湿度高位+湿度低位+温度高位+温度低位
				for(i=0;i<5;i++)
				{
					Sensor_Data[i] = Read_SensorData();
				}
			}
			else
			{
				Sensor_AnswerFlag = 0;	  // 未收到传感器响应	
			}
	  return 1;
  } 

 
 
 

1100

Posts

3

Resources
5
 

I'll look through it again. If I find a suitable display or wireless module, I'll make an internet-connected device so I can check the measurement results on a mobile phone app.

 
 
 

3184

Posts

0

Resources
6
 

sharp

Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 
 
 

4817

Posts

4

Resources
7
 

Awesome, I think he has strong hands-on ability

 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list