51 Study Day 2 - 2. Flowing Light Design and Buzzer Sound

Publisher:哈哈哈33Latest update time:2022-05-09 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

2. Flowing light design and buzzer sound

1. Simple delay program:

Use the while() loop to achieve a delay effect

#include


void main()


{


   sbit D1=p1^0; //declare a variable, set p0 equal to D1


   int=a;


   while(1) //This is an infinite loop


  {


      a=500; // indicates time delay


     D1=0; // indicates low level, i.e. the light is off


     while (a--); // means a is decremented to 0 and the following program is executed


     a=500;


    D1=1;


     while(a--);


   }


}


2. Subroutine call:

Design a delay function first, then call it directly!

#include


#define uint unsigned char //This is a macro definition. In the future, you can directly input uahr using char without declaring it again!


#defne uint unsigned int


sbit D1=P1^0;


void delay(); //The main function calls the sub-function, declare the function first, a function without parameters!


void main()


{


  D1=0;


  delay();


  D1=1;


  delay();


}


void delay()//This is a delay function to control x, y can control the delay time! No parameters!


{


uint x,y;


for(x=100;x>0;x--)


           for(y=200;y>0;y--)


}


Sub-function with parameters: more convenient to adjust.

#include


#define uint unsigned char //This is a macro definition. In the future, you can directly input uahr using char without declaring it again!


#defne uint unsigned int


sbit D1=P1^0;


void delay(uint z);//The main function calls the sub-function, declare the function first, the function with parameters!


void main()


{


D1=0;


delay(600);


D1=1;


delay(600);


}


void delay(uint z) //This is a delay function to control x, y can control the delay time! With parameters!


{


uint x,y;


for(x=100;x>0;x--)


        for(y=z;y>0;y--)


}


3. Flowing water lamp design

A running light has 8 pieces, corresponding to 8 bits, and the binary representation is: 1111 1110 ~1111 1101~1111 1011~~~ This forms a running light (0 is bright)

At this time, you need to call the left shift function: to implement the above pipeline

#include


#include "instrins.h//Left shift function header file


#define uchar unsigned char //This is a macro definition. In the future, you can directly input uchahr using char without declaring it again!


#defne uint unsigned int


 uchar=temp; //define an eight-bit variable


void delay(uint z);//The main function calls the sub-function, declare the function first, the function with parameters!


void main()


{


    temp=0xfe; //0xfe means: 1111 1110 defines P1 port


    P1=temp;


    while(1)


    {


    temp=_crol_(temp,1);//Call left shift function


    delay(1000);


    P1=temp;


    }


}


void delay(uint z) //This is a delay function to control x, y can control the delay time! With parameters!


{


uint x,y;


for(x=z;x>0;x--)


        for(y=z;y>0;y--)


}


4. Buzzer design

PNP transistor: When b is low level, the ec circuit forms a voltage drop, and the buzzer sounds. When b is high level, there is no voltage drop and the buzzer does not sound. (b is controlled by the microcontroller)



Method: Just control the buzzer io port directly!


#include


#include "instrins.h//Left shift function header file


#define uchar unsigned char //This is a macro definition. In the future, you can directly input uchahr using char without declaring it again!


#defne uint unsigned int


 uchar=temp; //define an eight-bit variable


sbit feng = P2^3; //The third bit of P2 port is the buzzer port


void main()


{


    temp=0xfe; //0xfe means: 1111 1110 defines P1 port


    P1=temp;


    feng=0;//At this time, the buzzer keeps ringing and the running light starts moving!


   while(1)


      {


      temp=_crol_(temp,1);//Call left shift function


      delay(60);


      P1=temp;


       }


}


void delay(uint z) //This is a delay function to control x, y can control the delay time! With parameters!


{


uint x,y;


for(x=100;x>0;x--)


        for(y=z;y>0;y--)


}


加油!!!你一定行!

Reference address:51 Study Day 2 - 2. Flowing Light Design and Buzzer Sound

Previous article:51 MCU interrupt mainly about timing -- timing -- calculating the initial value of the timer -- introduction
Next article:51 MCU Learning——8.3--Serial Communication

Recommended ReadingLatest update time:2024-11-15 16:03

51 single chip microcomputer learning - timer
1. About timer The timer consists of two registers: the upper 8 bits and the lower 8 bits. TMOD is the timer working mode register, which determines the working mode of the timer; TCON is the control register of the timer, which is used to control the start and stop of the timer and set the overflow flag. 2. Control
[Microcontroller]
What is the difference between sbit and bit in C51 programming
First of all, bit and sbit are both variable types extended by C51. sbit is generally used to define bit variables of special function registers to facilitate operations on a certain bit of the register. For example: sbit TXD=P3^0; This defines TXD as a bit variable, and this sbit has a certain address. Bit is simil
[Microcontroller]
51 control MT8880 dual audio transceiver assembly program
        ORG 0000H                         AJMP MAIN                                 :                                 : ;******************** ;* 8880 Initialization * ;******************** MAIN: MOV A, #90H                         MOV P0, A ; write 8880CRA                         MOV P0, A ; write 8880CRA             
[Microcontroller]
51 MCU C language operator table
51 MCU C language operator table: Note: In logical operations, any value that equals "0" is true, and any value that equals "0" is false.
[Microcontroller]
51 MCU C language operator table
51 MCU Tutorial Lecture 5_C51 Program
The C51 language program is composed of a main function and several functions. The main function is the starting point of the program. When the microcontroller runs the program, it will first execute the first statement in the main function until all the statements in the main function are executed. The program of the
[Microcontroller]
Interaction with host computer via serial communication of C51 MCU
Overview: The host computer (PC) sends instructions to the MCU, and the MCU serial port receives the instructions and sends a response according to the list.   Global variable declaration #include E51Pro.h BYTE ComBuf ; //Serial communication data buffer, used for both sending and receiving UINT nAddress; // R
[Microcontroller]
51 MCU Tutorial (IV): Independent-Matrix Keyboard Application and Design
Introduction: The previous lecture introduced and applied the serial port communication of the microcontroller, and gave examples. From this lecture, the peripheral circuits of the microcontroller will be introduced. This lecture introduces the circuit of the microcontroller external keyboard. Through this lecture, re
[Microcontroller]
51 MCU Tutorial (IV): Independent-Matrix Keyboard Application and Design
Design of Intelligent Voltage-stabilized Power Supply Based on AT89C51
This paper introduces a design of intelligent voltage-stabilized power supply based on AT89C51. Its core technology is to change the output of the voltage-stabilized module by controlling the digital-to-analog conversion through AT89C51. The system consists of the rectifier and filter preliminary voltage-stabilized ci
[Analog Electronics]
Design of Intelligent Voltage-stabilized Power Supply Based on AT89C51
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号