51 Single chip microcomputer learning notes "seven"--buzzer and relay

Source: Internet
Author: User

I. Buzzer 1. Buzzer Basics

The buzzer is divided into active buzzer and passive buzzer according to the driving mode, the active buzzer has an oscillator inside, and a low level will ring when the passive buzzer does not have an oscillator, so that it will ring to connect the pulse signal between the 500hz~4.5khz to drive. A passive buzzer is used in experiments.

2. Schematic diagram of the circuit

3. Experiment Notes

The experiment is based on the control buzzer in 4KHZ and 1KHZ frequency of the sound, the experimental wiring for JP8 P1.5.

4. Program source code
/************************************** > File Name: Buzzer experiment > Author:pengshp > Mail: [email protected] &G T date:2015 July 25 ***************************************/#include <reg51.h>#define UCHAR unsigned char#define UINT unsigned intSbit BUZZ = p1^5;unsigned CharT0rh=0;unsigned CharT0rl=0;voidOpenbuzz (unsigned intFREQU);voidStopbuzz ();voidMain () {unsigned intI EA =1;//Enable global interruptTmod =0x01;//Configure T0 operating mode 1     while(1) {Openbuzz (4000);//Start buzzer at 4KHZ frequency         for(i=0; i<40000;        i++); Stopbuzz ();//Stop buzzer         for(i=0; i<40000;        i++); Openbuzz ( +);//Start buzzer at 1KHZ frequency         for(i=0; i<40000;        i++); Stopbuzz (); for(i=0; i<40000;    i++); }}voidOpenbuzz (unsigned intfrequ) {unsigned intReload//Calculate the required timer reload valueReload =65536- (11059200/ A)/(frequ*2); T0RH = (unsigned Char) (Reload >>8); T0RL = (unsigned Char) Reload; TH0 =0xFF; TL0 =0xFE; ET0 =1;//Enable T0 interruptTR0 =1;//Start T0}voidStopbuzz () {ET0 =0; TR0 =0;}voidINTERRUPTTIMER0 () Interrupt1{TH0 = T0RH;    TL0 = T0RL; BUZZ = ~buzz;}
Two. Relay 1. Principle of Relay

Through the output of different levels, control the opening and closing of the relay, to achieve the control of the circuit, the non-power is closed to the normally open pin, when the power is closed to the normally closed pin. The main parameters are rated operating voltage, rated operating current, contact load, can control AC and DC power.

2. Schematic diagram of the circuit

3. Experiment Notes

The experiment through the key K1 control relay open and close, start the relay work, press the K1 relay stop work, and then press the relay again began to work, so repeat. The wiring is J2 connected P1.4,k1 P0.0.

4 Program Source code
/************************************** > File Name: Relay experiment > AUTHOR:PENGSHP > Mail: [email protected] &G T date:2015 July 25 ***************************************/#include <reg51.h>#define UCHAR unsigned char#define UINT unsigned intSbit relay=p1^4;//Relay bit declarationSbit k1=p0^0;//switch K1 bit declarationvoidDelay (UINT ms) {Uchar I; while(ms--) { for(i= -;i>0; i--); }}voidMain () {relay=0;//Relay workk1=1; while(1)    {if(! K1) {Delay ( -);if(k1==0)            { while(! K1);//wait for key to releaseRelay=~relay;//Relay reverse}        }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

51 Single chip microcomputer learning notes "seven"--buzzer and relay

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.