Fifth, single chip learning-experiment of relay and buzzer

The purpose of the experiment: understand and master the working principle of the relay and buzzer drive circuit;
      understand and master the driving method of driving high-current devices with single-chip I / O;
experimental module: core board + water lamp and independent button module + relay module + bee Ming module;
experiment: control relay and a buzzer key operation, and instructing the corresponding led lights, i.e., the first
        keys being pressed, the first led lamp lighting, buzzer to respond; second button press Down, the second led
        light is on, and the relay pulls in; the third button is pressed, the first and second running lights are on, the relay pulls
     in, and the buzzer responds; the fourth button is pressed, and recovery In the initial state, all led lights are off, the relay is
        off, and the buzzer does not respond.

Module connection diagram:

 

Circuit schematic diagram:

Circuit driving principle: (1) The buzzer sounding principle is that the current passes through the electromagnetic coil to make the electromagnetic coil generate a magnetic field to drive the vibrating membrane to sound. Relying only on the single-chip I / O is not enough to drive the buzzer to work; the positive electrode of the buzzer Connected to the C pole of the triode, the negative electrode of the buzzer is connected to the ground, the base B of the triode is controlled by the P1.2 pin of the microcontroller after the current limiting resistor, when the P1.2 output high level, the triode T1 is cut off There is no current flowing through the coil, and the buzzer does not sound; when P1.42 outputs a low level , the transistor is turned on, so that the current of the buzzer forms a loop and emits a sound. Therefore, we can control the level of pin P1.2 through the program to make the buzzer sound or turn off. (2) The relay drive circuit is as above, mainly through the PNP type transistor S8550 to realize the pull-in and disconnection of the relay through the I / O port of the single-chip microcomputer; the transistor drive relay mainly uses the amplification characteristics and switching characteristics of the triode; when working with the single-chip microcomputer When the connected I / 0 port outputs a low level, the transistor is turned on. At this time, the resistance between the E pole (emitter) and the C pole (collector) of the transistor is very small. At this time, the circuit is equivalent to VCC passing the relay and then passing Connected to the ground through the transistor to form a complete circuit, the relay pulls in; on the contrary, when the I / 0 port connected to the microcontroller outputs a high level, the transistor is cut off, and then the E pole (emitter) and C pole (collector) of the transistor The resistance is very large, the circuit cannot form a loop, and the relay does not pull in. Therefore, we can control the pull-in and turn-off of the relay through program control of the level of the I / O port connected to the single-chip microcomputer.

 

Engineering code 1: Function description: Control the buzzer to realize the music "Happy Birthday"
hardware connection: Connect J10_0 and J18_bz with a Dupont wire

#include <reg51.h> // Include header file
sbit BZ = P0 ^ 0; // Define buzzer control pin       
# define uchar unsigned char    
#define uint unsigned int
 
 uchar code SOUNDLONG [25] = // "Happy birthday "Beat
 {
  15,5,20,20,20,40,
  15,5,20,20,20,40,
  15,5,20,20,20,20,20,
  15,5,20,20,20 , 40
 };
uchar code SOUNDTONE [25] = // "Happy Birthday" tone
{
 212,212,190,212,159,169,
 212,212,190,212,142,159,
 212,212,106,126,159,169,190,
 119,119,126,159,142,159     
};
// ******************** ************************************************** ****************************
// Delay function
// **************************************************** **************************************************
delay (uint time) // int type data is 16 bits, so the maximum value is 65535           
 (
  uint i, j; // define variables i, j, for loop statement
  for (i = 0; i <time; i ++) / / for loop, loop 50 * time times
     for (j = 0; j <50; j ++); // for loop, loop 50 times
 )
// ***************** ************************************************** *******************************
// Play tone
// ************* ************************************************** ***********************************
void Play_music ()                         
{
  uint Tone, Long; // define tone variable
  uint i, j, k; // define
     for (i = 0; i <25; i ++) // 25 tones of "Happy Birthday"
      {
          Tone = SOUNDTONE [i]; // Read tones
          Long = 30 * SOUNDLONG [i]; // Read beats
          for (j = 0; j <Long; j ++) // Output different beats
           {
            BZ = 0; // Turn off the buzzer
            for (k = 0; k <Tone; k ++); // The difference of Tone to distinguish 7 kinds of tones
            BZ = 1; // Turn on the buzzer
            for (k = 0; k <Tone; k ++); // The difference of Tone to distinguish 7 tones
        }
      }
}
// ************ ************************************************** ************************************
// Main function
// ******** ************************************************** ****************************************
void main ()
{
 while (1)
  {
   Play_music (); // Play music
   BZ = 0; // Turn off the buzzer
   delay (10000); // Turn off the buzzer time
  }
}

 

 

Engineering code 2:
  Function: on-off control relay, also issued a "popping" sound
  hardware connection: with a Dupont line connected to J11_7 J18_K2

********************** ************************************************** *************************** /
#include "reg51.h" // Include header file
sbit K2 = P2 ^ 7; // Define relay control IO
#define uchar unsigned char
#define uint  unsigned int
//**************************************************************************************************
//延时函数
//**************************************************************************************************
delay(uint time)              //int型数据为16位,所以最大值为65535           
 {
  uint  i,j;                  //定义变量i,j,用于循环语句
  for(i=0;i<time;i++)         //for循环,循环50*time次
     for(j=0;j<50;j++);       //for循环,循环50次
 }
//**************************************************************************************************
//主函数
//**************************************************************************************************
void main()                   //主函数
{
     while(1)                 //进入while死循环
      {
        K2=0;                 //断开继电器
        delay(5000);          //延时
        K2=1;                 //导通继电器
        delay(5000);          //延时
      }        
}

 

Guess you like

Origin www.cnblogs.com/breads/p/12713017.html