51 MCU learning notes [7] -- buzzer and relay, MCU learning notes
1. BUZZER 1. BUZZER Basics
The buzzer is divided into the active buzzer and the passive buzzer Based on the driving method. The active buzzer has an internal oscillator, which will sound when connected to a low level. The Passive buzzer does not have an oscillator, so it must be connected to a Hz ~ A pulse signal between kHz is triggered. Passive buzzer is used in many experiments.
2. circuit schematic
3. Experiment description
In this experiment, sound is produced at a frequency of 4 kHz and 1 kHz respectively, and the connection wiring is JP8 connected to P1.5.
4. program source code
/***********************************> File Name: buzzer experiment> Author: pengshp> Mail: pengshp3@outlook.com> Date: july 25, 2015 *************************************** /# include <reg51.h> # define uchar unsigned char # define uint unsigned intsbit BUZZ = P1 ^ 5; unsigned char T0RH = 0; unsigned char T0RL = 0; void OpenBuzz (unsigned int frequ); void StopBuzz (); void main () {unsigned int I; EA = 1; // enable global interruption TMOD = 0 X01; // configure T0 working mode 1 while (1) {OpenBuzz (4000); // start the buzzer at a 4 KHz frequency for (I = 0; I <40000; I ++); StopBuzz (); // stop the buzzer for (I = 0; I <40000; I ++); OpenBuzz (1000 ); // start the buzzer at 1 kHz frequency (I = 0; I <40000; I ++); StopBuzz (); for (I = 0; I <40000; I ++) ;}} void OpenBuzz (unsigned int frequ) {unsigned int reload; // calculate the required timer reload value reload = 65536-(11059200/12)/(frequ * 2 ); t0RH = (unsigned char) (reload> 8); T0RL = (unsigned char) reload; TH0 = 0xFF; TL0 = 0xFE; ET0 = 1; // enable T0 to interrupt TR0 = 1; // start T0} void StopBuzz () {ET0 = 0; TR0 = 0 ;} void InterruptTimer0 () interrupt 1 {TH0 = T0RH; TL0 = T0RL; BUZZ = ~ BUZZ ;}
Ii. Relay 1. relay principle
Through the output of different levels, control the opening and closing of the relays to achieve control of the circuit. If the circuit is not powered on, it is closed to the normally opened pin, and the circuit is closed to the normally closed pin when powered on. The main parameters include rated operating voltage, rated operating current, and contact load, which can control AC and DC.
2. circuit schematic
3. Experiment description
In this experiment, the keys K1 control relay are enabled and combined to start the relay work. Press K1 relay to stop the work, and then press the relay to start work again. Connect J2 to P1.4 and K1 to P0.0.
4 program source code
/***********************************> File Name: relay lab> Author: pengshp> Mail: pengshp3@outlook.com> Date: july 25, 2015 *************************************** /# include <reg51.h> # define uchar unsigned char # define uint unsigned intsbit RELAY = P1 ^ 4; // relay position declaration sbit K1 = P0 ^ 0; // switch K1 bit declaration void Delay (uint ms) {uchar I; while (ms --) {for (I = 120; i> 0; I --) ;}} void main () {RELAY = 0; // RELAY operation K1 = 1; while (1) {if (! K1) {Delay (50); if (K1 = 0) {while (! K1); // wait for the key to release RELAY = ~ RELAY; // reverse RELAY }}}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.