51 MCU Project (12) - Intelligent desk lamp design based on 51 MCU

   The functions of this design are as follows:

        First, the PCF8591 chip is used to realize AD DA conversion. AD collects information from the photoresistor. The stronger the light intensity, the smaller the voltage, and the smaller the value collected by AD. At the same time, the digital quantity collected by AD is used as the input quantity for DA output. The analog output terminal is connected with an LED to indicate the size of the output analog quantity. The larger the output analog quantity, the brighter the LED. So the working process of this part is as follows: when the light intensity is too weak, the value of AD output is larger, then the value of DA output is larger, and the LED is brighter. Automatic dimming is realized.
        In addition, the system also has an ultrasonic ranging module. If it is detected that the human body is very close to the desk lamp, it means that the child's learning posture is incorrect and may easily damage the eyes. At this time, a buzzer will alarm to prompt the child to adjust his sitting posture. When the sitting posture is adjusted correctly, the buzzer will no longer alarm.
        In addition, the system calculates how long the system has been working in real time. When the time exceeds 1 hour, a buzzer will sound to remind the child to take appropriate rest.
         All lighting information, distance information, and learning time information are printed out through the serial port and can be viewed with the serial port debugging assistant!
         This design is implemented using the Puzhong 51 development board!       (The code and project are placed at the end of the article)

        The actual picture after implementation is as follows:

        

The information prompted by the serial port assistant is as follows:

Implementation process:

PCF8591 is a commonly used multifunctional analog-to-digital converter (ADC) and digital-to-analog converter (DAC) integrated circuit. It has 4 single-ended analog input channels and one single-ended analog output channel, capable of converting analog signals to digital signals and vice versa.

PCF8591 is widely used in various electronic equipment, including sensor interface, data acquisition, voltage measurement and control and other fields. It operates over a wide voltage range, features low power consumption and high accuracy, and can meet the needs of many applications.

The PCF8591 communicates via the I2C bus and can be programmed to control and read data. Using appropriate software libraries and programming languages, you can easily interact with and integrate the PCF8591 into your projects.

In short, PCF8591 is a commonly used analog-to-digital converter integrated circuit that is suitable for many application scenarios and has the characteristics of high precision and low power consumption.

The process of using PCF8591 can be divided into the following steps:

1. Connect the hardware: Connect the PCF8591 to the microcontroller or other main control device. Usually using I2C bus connection, connect the SDA pin of PCF8591 to the SDA pin of the main control device, the SCL pin to the SCL pin of the main control device, and the ground pin and power pin at the same time.

2. Initialization settings: Initialize PCF8591 in the program. This typically involves selecting the input mode (single-ended or differential) and setting parameters such as gain and reference voltage for the analog input channels. The specific settings depend on your application needs.

3. Read analog input: Use the corresponding command or function to read the data of the analog input channel from PCF8591. Depending on the configuration of the PCF8591, you can choose to read data from a single channel or read data from multiple channels simultaneously. The data read is the digital value of the analog signal converted by ADC.

4. Control analog output: If necessary, you can use the digital-to-analog conversion function of PCF8591 to convert digital signals to analog signal output. By writing the appropriate value, the voltage value of the analog output channel can be controlled.

5. Process data: Properly process and parse the data read from PCF8591 according to your application requirements. For example, you can perform unit conversions, data filtering, calibration, and more to get the results you need.

It should be noted that the specific usage process may vary depending on different development platforms, programming languages ​​or software libraries. Therefore, it is recommended to refer to the relevant documentation and sample code for PCF8591 to understand how to use PCF8591 correctly in your specific environment.

Ultrasonic module:

HC-SR04 is a commonly used ultrasonic module used to measure distance. It contains an ultrasonic transmitter and an ultrasonic receiver, which can calculate distance by transmitting ultrasonic signals and receiving their echoes.

The process of using HC-SR04 is as follows:

1. Connect the hardware: Connect the HC-SR04 module to your controller (such as Arduino). The HC-SR04 module usually has 4 pins, including power pins (VCC and GND), trigger pins (Trig) and receiving pins (Echo). Connect the VCC pin to the 5V supply, the GND pin to ground, the Trig pin to the digital output pin on your controller, and the Echo pin to the digital input pin on your controller.

2. Send ultrasonic signal: Set the Trig pin to high level (usually 10 microseconds), then return it to low level. This will trigger the HC-SR04 module to send a short pulse of ultrasonic signal.

3. Receive ultrasonic echo: The HC-SR04 module will automatically receive the echo of the ultrasonic signal and convert it into a level signal on the Echo pin. You can determine the travel time of the ultrasonic signal by reading the level on the Echo pin.

4. Calculate the distance: By measuring the time difference (usually in microseconds) between the transmission and reception of the ultrasonic signal, the distance can be calculated. The formula is as follows: distance = (time difference * speed of sound) / 2, where the speed of sound is usually 340 meters/second.

It should be noted that the use of the HC-SR04 module may be affected by some factors, such as noise, changes in ultrasonic propagation speed, etc. When programming, you also need to select appropriate codes and library functions for triggering and reading operations based on the specific controller and development platform.

To sum up, the HC-SR04 module can be used to calculate distance by sending ultrasonic signals and measuring the time of their echoes. This is used in many projects for applications such as obstacle avoidance, distance measurement, etc.

Connection method between module and microcontroller:

/*

Wiring method:
Ultrasonic Trig-->P2.4 Echo--->P2.3
PCF8591 SCL-->P2.0 SDA-->P2.1
Note: VCC and GND of all modules need to be connected

*/
 

The main function code is as follows:

#include "reg52.h"
#include "LCD1602.h"
#include "delay.h"
#include <string.h>
#include <stdio.h>
#include <I2C.H>
#include "usart.h"
#include <intrins.h>

/*

接线方式:
超声波 Trig-->P2.4  Echo--->P2.3
PCF8591   SCL-->P2.0  SDA-->P2.1
注明:所有模块的VCC  和  GND都需要连接

*/

#define  PCF8591 0x90    //PCF8591 地址

int light=0;   //定义光照强度
int t=0;

sbit beep=P2^5;
sbit RX=P2^3;
sbit TX=P2^4;     //超声波模块端口定义
bit      flag =0;

char i=0;
char inf[]="light:000  distance:0000  t:000\r\n";

unsigned int  time=0;
unsigned long S=0;
unsigned int  distance=0;
unsigned int  timer=0;


//蜂鸣器报警的函数
void beep_my()
{
  int i=1000;
   while(i)
   {
     beep=~beep;
      i--;
      delay(100);
   }
}

 void  StartModule() 		         //启动模块
 {
	  TX=0;			                     //启动一次模块
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_();
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_();
	  TX=1;
     _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_();
	  _nop_(); 
	  _nop_(); 
	  _nop_(); 
	  _nop_();
     TX=0;
 }

void Int0Init()             //定时器初始化 
{  
   TMOD|=0x01;		   //设T0为方式1,GATE=1;
	TH0=0;
	TL0=0;          	
	ET0=1;             //允许T0中断	
	EA=1;			   //开启总中断
   
}

int Conut(void)         //计算距离
{  
	 time=TH0*256+TL0;
	 TH0=0;
	 TL0=0;
	
	 S=(time*1.7)/10;     //算出来是mm
    return S;
	
}


/*******************************************************************
DAC 变换, 转化函数               
*******************************************************************/
bit DACconversion(unsigned char sla,unsigned char c,  unsigned char Val)
{
   Start_I2c();              //启动总线
   SendByte(sla);            //发送器件地址
   if(ack==0)return(0);
   SendByte(c);              //发送控制字节
   if(ack==0)return(0);
   SendByte(Val);            //发送DAC的数值  
   if(ack==0)return(0);
   Stop_I2c();               //结束总线
   return(1);
}

/*******************************************************************
ADC发送字节[命令]数据函数               
*******************************************************************/
bit ISendByte(unsigned char sla,unsigned char c)
{
   Start_I2c();              //启动总线
   SendByte(sla);            //发送器件地址
   if(ack==0)return(0);
   SendByte(c);              //发送数据
   if(ack==0)return(0);
   Stop_I2c();               //结束总线
   return(1);
}

/*******************************************************************
ADC读字节数据函数               
*******************************************************************/
unsigned char IRcvByte(unsigned char sla)
{  unsigned char c;

   Start_I2c();          //启动总线
   SendByte(sla+1);      //发送器件地址
   if(ack==0)return(0);
   c=RcvByte();          //读取数据0

   Ack_I2c(1);           //发送非就答位
   Stop_I2c();           //结束总线
   return(c);
}

void main(void)
{  
 
   Int0Init() ;        //定时器初始化
   UsartInit();  //串口初始化
   while(1)
   {  
     
      
      ISendByte(PCF8591,0x41);
      light=IRcvByte(PCF8591);  //ADC0 模数转换1      光敏电阻
      DACconversion(PCF8591,0x40, light); //DAC	  数模转换
     
      inf[6]=light/100+0x30;
      inf[7]=(light/10)%10+0x30;
      inf[8]=light%10+0x30;
      StartModule();
      while(!RX);		//当RX为零时等待
	   TR0=1;			    //开启计数
	   while(RX);			//当RX为1计数并等待
	   TR0=0;				//关闭计数           
      distance= Conut();			//计算
      
      inf[20]=distance/1000+0x30;
      inf[21]=(distance/100)%10+0x30;
      inf[22]=(distance/10)%10+0x30;
      inf[23]=distance%10+0x30;
      
      if(distance<100)              //超声波测距用于检测人离台灯的距离  如果低于10CM 不利于眼睛  那么蜂鸣器报警  (这里的100可以调节)
         beep_my();

      
      t++;              //这里的t计算的是学习时间    单位为分钟
      inf[28]=t/100+0x30;
      inf[29]=(t/10)%10+0x30;
      inf[30]=t%10+0x30;
    
      if(t>60)              //t>60 说明学习时间已经超过1小时  那么蜂鸣器报警  (这里的100可以调节)
         beep_my();
      
        Uart1Sends(inf);  
        delay_nms(500);        //延迟500ms
      
   }
   
}

void time0() interrupt 1		//定时器0的中断服务函数
{   
   flag=1;							 //中断溢出标志
  	
}

The project files are as follows:

Intelligent desk lamp resources based on 51 microcontroller-CSDN library icon-default.png?t=N7T8https://download.csdn.net/download/guangali/88354310?spm=1001.2014.3001.5501

Guess you like

Origin blog.csdn.net/guangali/article/details/133072420