51 single chip computer learning two led water lamp and button control

Publisher:温馨小筑Latest update time:2022-07-29 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

MCU chip model STC89C516


need


Required knowledge


1. About the delay function


void Delay100ms() //100s delay function with crystal frequency 12mhz

{

unsigned char i, j;


i = 195;

j = 138;

do

{

while (--j);

} while (--i);

}


It can delay the corresponding time of the previous statement, and use the auxiliary program to generate


2. About shifting in bit operations in C51 language

The symbols are ">>", "<<"


a<=0, shift the integer a to the left by m bits in binary format, and after the high bits are shifted out, fill the low bits with 0.

a>>m, a and m must be integer expressions, and m>=0. , shift the integer a to the right by m bits, shift the lower bits out, and fill the higher bits with 0

like

a=0x45=01010100B,

b=0x3b=00111011B,

Then a<<2=01010000B=0x50, b>>2=00001110B=0x0e.


3. About eliminating vibration of buttons

The switch used for common buttons is a mechanical elastic switch. When the mechanical contact is opened and closed, due to the elastic effect of the mechanical point, the button switch will not be immediately and stably connected when closed, nor will it be disconnected all at once when disconnected. Therefore, there will be a series of jitters at the moment of closing and disconnecting. The jitter time is generally a few tenths of a second to several seconds.

Key jitter can cause the key to be misread multiple times. In order to ensure that the CPU processes a key closure only once, debounce is necessary.

There are two ways to debounce a button, one is hardware debounce and the other is software debounce. In order to make the circuit simpler, software debounce is usually used. Generally speaking, a simple button debounce is to read the state of the button first. If the button is pressed, delay 10ms, and read the state of the button again. If the button is still pressed, it means that the button has been pressed.

Commonly used software de-jitter methods for microcontrollers:

1. First set the IO port to a high level (since the development board IO has a pull-up resistor, the default IO is a high level).

2. Read the IO port level to confirm whether any button is pressed.

3. If the IO level is low, delay for several milliseconds.

4. Read the IO level again. If it is still low, it means the corresponding button is pressed.

5. Execute the program of the corresponding key


Circuit Connection

insert image description here

Flowing lights

insert image description here

Button control


software design

1. Flowing lights

The D1-D8 indicators light up one by one in a cycle.

According to the implementation principle of the running light, that is, the IO port outputs low levels one by one from low to high or from high to low, we can combine the shift operation and the loop.

First, led=~0x01. Since the LED is lit at a low level, the result after inverting 0X01 is 0XFE, and the corresponding binary number is 1111 1110, that is, the lowest bit is 0. Therefore, the D1 indicator light will light up at the beginning, and then it will enter the while loop after a delay. Since 8 LEDs need to be lit up from D1->D8 in a cycle, the for loop statement can be used to loop 8 times.

Each time the cycle is repeated, the lighted lamp moves one position to the right, that is, the low level output by port P0 needs to be shifted one position to the left. Therefore, the statement P0=~(0x01<

#include "reg52.h" 

#include  

#define led P0 


void Delay100ms() //@12.000MHz

{

unsigned char i, j;


i = 195;

j = 138;

do

{

while (--j);

} while (--i);

}


void main ()

{

unsigned char b;

led = ~0x01;

Delay100ms();

while (1)

{

for(b=0;b<8;b++)

{

led = ~(0x01< Delay100ms();

}

}

}


2. Button control


Define the control pins of key K1 and LED1, set the LED to level and turn off the D1 indicator. Then enter the while loop, execute the keycheck() function in the loop body, that is, determine whether the key K1 is pressed. If it is pressed, delay 10ms (delay function) to eliminate the jitter, and then determine whether the key K1 is pressed again. If K1 is indeed pressed, execute the LED state flip control, and finally determine whether the key is released.


#include "reg52.h" 

#include


sbit k1=P1^0;

sbit led=P0^0;


typedef unsigned int u16;

typedef unsigned char u8;


void delay() //@12.000MHz Use delay function to eliminate key jitter

{

unsigned char i, j;


i = 20;

j = 113;

do

{

while (--j);

} while (--i);

}


void keycheck()

{

if (k1==0)

{

delay(); //Jitter elimination

if (k1==0)

{

led=~led;

}

while (!k1);

}

}


void main ()

{

led=1;

while (1)

{

keycheck();

}

}


Effect

insert image description here

Reference: Baidu Encyclopedia, Jinzhong 51 MCU Development Strategy


Reference address:51 single chip computer learning two led water lamp and button control

Previous article:【51 MCU】Ordinary I/O port simulates SPI port C language program
Next article:51 single chip microcomputer learning four serial port communication

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号