MicroController Pros Home Page My Account  Cart Contents  Checkout  

****Note: We no longer process and ship international orders. Only orders from USA and Canada are now accepted.****
You can place international orders for our products on E-Bay. Just copy the item's title description from our website and paste it into the search box on E-Bay and place your order there.

  Store » PCAM+ 8051 Tutorial 3 My Account  |  Cart Contents  |  Checkout   
Quick Find
 
Enter keywords to find the product you are looking for in the Quick Find field above

or use
Advanced Search
Categories
Accessory Boards->
8051->
ADI Blackfin
Arduino->
ARM->
Atmel AVR->
Cypress PSoC
Freescale->
FTDI->
Locktronics
Microchip PIC->
MIPS
Parallax->
Renesas
Silicon Labs
ST Microelectronics->
Texas Instruments->
Tibbo->
Books->
Displays->
E-Blocks->
EEPROM/EPROM/FLASH
Embedded Ethernet->
Embedded Software->
I/O Modules->
Parts & Components->
Pick & Place Tools
Programmable Logic (PLD)
Prototype PCBs->
Robotics
ROM/Flash Emulators
Test & Measurement->
Tutorial Software
Universal Programmers->
Wireless->
Information
Intro to Embedded Tools
Embedded News Digest
Useful Resources
Shipping & Returns
Warranty & Liability
Privacy Notice
Conditions of Use
Contact Us
PCAM+ 8051 Tutorial 3: Toggle LEDs

Back to PCAM+ page.


Back to Chapter 2

Toggle LEDs with Switch on 89S52 Microcontroller

This is another simple yet important tutorial for 89S52 or 8051 interfacing.

We are covering basic interface applications so that you will understand the concept rather than getting into the complex interfacing logic.

Let's discover how to turn LEDs On/Off using a switch, and learn the BIT value handling for the 89S52 microcontroller.

First, start the 8051 IDE and type the following code:

SETB P2.0
Loop:
    JB P1.0, Loop
    CPL P2.0
    Debounce:
        JNB P1.0, Debounce
    SJMP Loop

The code uses Bit 0 of PORT2 to connect to the LED. Therefore, use a jumper wire now to connect PORT2.0 to one of the LEDs.

The code is using P1.0 to read the switch status. Therefore, use another jumper wire to connect a switch (button) to P1.0 of the 89S52.

The SETB P2.0 code line will turn the LED OFF to start with.

Loop: is a section label.

The JB P1.0, Loop command checks the status of Port pin P1.0. The JB stands for JUMP if BIT is HIGH.Thus, if the PORT1.0 bit is HIGH, then it will JUMP to Loop.

When the switch is not pressed, the P1.0 will remain HIGH, thus the program restarts the loop. When the switch is pressed, P1.0 goes LOW. This action of pressing the switch means the JB command's condition (P1.0 being HIGH) is not met, thus it does not jump to Loop, so the program moves to the next line: CPL P2.0.

CPL is Complement. This line will Complement the PORT pin P2.0 (in other words, invert). Thus, the LED status will be inverted — from OFF to ON ... at first.

The Debounce section is like the Loop section, only note its command of JNB P1.0, Debounce. The JNB means JUMP NOT if BIT is HIGH. Thus, as long as the switch remains pressed, keeping P1.0 LOW, the code remains in the Debounce loop here.

When you release the switch, SJMP Loop returns the program to the Loop section. The LED remains ON because P2.0 was toggled by CPL earlier, and now the code is looping on JB P1.0, Loop again, waiting for another button press. When you press the switch button again, the CPL command again inverts the LED state, thus now turning it OFF.

This tutorial has explained Bit commands like JB, SETB, JNB & CPL. Thus you can now read input for the microcontroller.

Proceed to Chapter 4


Back to PCAM+ page.

Shopping Cart more
0 items
What's New? more
WiFi mikroBUS Module, SPWF01SA.11, 14x GPIO, 3.3V (WiFi 4 click)
WiFi mikroBUS Module, SPWF01SA.11, 14x GPIO, 3.3V (WiFi 4 click)
US$21.75
Bestsellers
01. 2x5 (10-pin) 0.05" pitch IDC Connector Flat Ribbon Cable, 20cm
02. 2x5 (10-pin) 0.05" pitch IDC Connector Flat Ribbon Cable, 12cm
03. 5-pin Press-Fit Header Strip, Breakaway Pins, 2.54mm Pitch
04. Parallel to USB Adapter: Connect USB printer to LPT port
05. 2x10 (20-pin) 0.1" pitch IDC Connector Flat Ribbon Cable, 15cm
06. 8-pin Press-Fit Header Strip, Breakaway Pins, 2.54mm Pitch
07. USB Universal In-Circuit Programmer PIC AVR ARM MSP 8051 EEPROM
08. 2x5 (10-pin) 0.1" pitch IDC Connector Flat Ribbon Cable, 15cm
09. ARM JTAG Debugger & Programmer, parallel port
10. Power Supply 3-12V DC, U.S. plug, 6 connection tips
Reviews more
Good board to evaluate the NXP LPC2129 although the micro on ..
4 of 5 Stars!
  Thursday 28 March, 2024   List of all our Products

Copyright © 2003-2017 MicroController Pros LLC
Powered by osCommerce