[MCU] Keil+Proteus two buttons to realize the switch light

Publisher:XiangsiLatest update time:2021-11-26 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Use Keil and Proteus to light up the LED light. (Without a development board, you can only rely on simulation software. After you have a single-chip microcomputer board, you can do a physical demonstration.)

Function 1: Protues drawing

Function 2: Keil generates .hex file


Enter the text:

1. Proteus part:

We first need to draw our diagram in Proteus. How to draw? The steps are as follows:

1) First, we need to create a new project. If you don't know how to create a project using Proteus, please go to: Create MCU projects using Proteus and Keil

2) Find the required components in Proteus:

insert image description here

Special note: In the third step here, in this example, you only need to search for: AT89C51, CAP, CAP-ELEC, CRYSTAL, LED-BIBY, RES, SWITCH (other components are not used in this example)


3) Start drawing (just like this picture, the parameters of some devices can be based on this picture):

insert image description here

Special note: When drawing here, the VDD and ground symbols are:

insert image description here

4) The Proteus drawing is completed, now enter the Keil part.


2. Keil part:

We first need to create a new Keil project. How to create it? The steps are as follows:

1) If you don't know how to create a project using Keil5, please go to:

Proteus and Keil create microcontroller projects respectively

2) After creating our project, we start writing code:


/*****************************Experimental routine**********************

Function: To realize convenient control of room lighting, one lighting lamp is controlled by a (closed) switch at the door and the bedside.

The closing of the switch does not directly determine whether the light is on or off, but the change of the switch state is the key to determining whether the light is on or off.

No matter what the current state is, as long as one of the switch states changes, the light will switch between the two states of on or off.

In this experiment, the first LED connected to the P0 port is used to simulate a lighting lamp, and S2 and S3 (P1.0 and P1.1) are used to simulate two switches.

Crystal oscillator: 11.0592MHZ Test whether the switch has changed, main function: control the light when a change is detected, and then record the current state

**********************************************************************/


#include

sbit light=P0^0; //Note the representation method of a certain bit, a certain bit of the special function register is represented by Sbit

sbit s1=P1^0;

sbit s2=P1^2;


bit change,s1sta,s2sta; //s1sta,s2sta are the states of switches s1 and s2 at the previous moment, which are used to compare with the current states of s1 and s2.

//Then determine whether the status of the two switches has changed.

bit keyscan()

{

if((s1^s1sta)||(s2^s2sta)) //^ is XOR, if the two are different, the result is 1. That is, if the state of any of the two switches changes, this condition is true.

change=1;

return change;

}


void main()

{

s1sta=s1;

s2sta=s2;

while(1)

{

keyscan(); //Scan whether the key status has changed, that is, whether someone has changed the key status.

if(change)

{

change=0; //clear

light=~light; //Once the switch status is changed, the state of the light will be reversed.

s1sta=s1; //At the same time, send the current status of s1 and s2 to s1sta and s2sta to prepare for subsequent comparisons.

s2sta=s2;

}

}

}


3) After the code is completed, we need to compile and generate the required .hex file (this file needs to be burned into Proteus)


first step:

insert image description here

Step 2:

insert image description here

third step:

insert image description here

Special note: I use Keil5, and the default generated .hex file is saved in:

insert image description here

4) At this point, the required .hex file is generated, and we re-enter Proteus:

insert image description here

5) At this point, the .hex file has been burned into Proteus. Click the lower left corner of Proteus to start the simulation:

insert image description here

6) Click the corresponding button in the figure to observe the effect:


Finished, sprinkle flowers...


This is just a simple Keil+Proteus effect experiment, continue to update more experimental projects...

Keywords:MCU Reference address:[MCU] Keil+Proteus two buttons to realize the switch light

Previous article:[MCU] Keil+Proteus running light (modular programming)
Next article:[MCU] Proteus and Keil create MCU projects respectively

Latest Microcontroller Articles
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号