You are on page 1of 6

Experiment No.

1 Interfacing LEDs Using TM-1


Objectives:
1. This experiment aims to familiarize the students with the Programmer and the Training Module TM-1, especially its ports functions. 2. After the sample program, the students are expected to make a simple program that will trigger the LEDs connected to its port with different pattern.

Discussion:
This IT104L Laboratory Course will focus on 8051-family microcontroller experiments. In particular, the Laboratory Course will use the Atmel AT89C2051/AT89C4051 microcontroller, AT89CX51 Programmer, and Training Modules TM-1 and TM-2. Atmel AT89CX051 Microcontroller The AT89C2051 / AT89C4051 is a low-voltage, high-performance CMOS 8-bit microcontroller with 2K / 4K bytes of Flash programmable and erasable read-only memory (PEROM). The device is manufactured using Atmels high-density non-volatile memory technology and is compatible with the industry-standard MCS-51 instruction set. By combining a versatile 8-bit CPU with Flash on a monolithic chip, the Atmel AT89C2051 / AT89C4051 is a powerful microcontroller which provides a highly-flexible and cost-effective solution to many embedded control applications. The AT89C2051 / AT89C4051 provides the following standard features: 2K / 4K bytes of Flash, 128 bytes of RAM, 15 I/O lines, two 16-bit timers/counters, a five-vector, two-level interrupt architecture, a full duplex serial port, a precision analog comparator, on-chip oscillator and clock circuitry. In addition, the AT89C2051 / AT89C4051 is designed with static logic for operation down to zero frequency and supports two software-selectable power saving modes. The idle mode stops the CPU while allowing the RAM, timers/counters, serial port and interrupt system to continue functioning. The power-down mode saves the RAM contents but freezes the oscillator disabling all other chip functions until the next hardware reset. Atmel AT89CX051 Programmer The Atmel AT89CX051 Programmer includes the programmer unit and its software. This software can program either the Atmel AT89C2051 or AT89C4051 microcontroller. It has a comprehensive set of features that allows you to view,

program, erase and secure the data of an Atmel microcontroller. Equipped with a computer-interface, the software allows you to: 1. Load or open hex files containing the code you want to program to the device. 2. Read the hex code from the device and store it in the program buffer for editing or saving. 3. View and edit the hex codes in the program buffer. 4. Save any changes made in the program buffer to a hex file for use with other devices. 5. Check the device ID. 6. Erase all the data in the device. 7. Blank check the device. 8. Program the hex codes found in the program buffer to the device. 9. Verify the programmed hex code in the device. 10. Protect the device from accidentally reprogramming by locking the device so that the code cannot be read from it. Training Module TM-1 The Training Module 1 (TM-1) is a helping tool in which you can explore the many features of a microcontroller. This module uses Atmel AT89C2051 and AT89C4051 microcontroller. This training module has the following applications: 1. 3-External Switches 2. 7-Segment Display 3. Buzzer 4. LEDs 5. Relay 6. RS232 Serial Communication The TM-1 module is equipped with its own power supply, switch and crystal. These components allow the TM-1 to be run in a stand-alone mode using programmed Atmel 89C2051 or Atmel 89C4051 and a power supply. TM-1s Port 1 Port P1 of TM-1 pins are all connected to LEDs that reflect the status of each pin of the port. In addition to LEDs, port P1 is also connected to 7-segment LED display. However, these two displays cannot operate at the same time. To select the operation of any display, either LED display or 7-segment display, SEL2 jumper must be properly set. This experiment illustrates basic software techniques to control this port, that is, to set or reset the ports pin. In actual applications these output pins may be connected to lighting devices, mechanical actuators, and other components.

The LEDs are connected to port P1. A logic high output on the port turns the corresponding LED OFF while a logic low signal turns ON the corresponding LED. LEDs Connection to Port P1 LED LED1 LED2 LED3 LED4 LED5 LED6 LED7 LED8 AT89C2051 MCU Port P1 Pin Port 12 P1.0 13 P1.1 14 P1.2 15 P1.3 16 P1.4 17 P1.5 18 P1.6 19 P1.7 Description Output Output Output Output Output Output Output Output

Sample Program
The easiest approach to explain the program is through a simple software flowchart as shown below. Program Flowchart

The initialization is required to set up the input and output ports as well as the register and stack pointer. CLR EA MOV SP, #30H

Switching on the LED simply requires the bit P1.0 of port P1 be switched to a low logic level (0) in the TM-1. This is accomplished by using the move (MOV) instruction to feed the binary 11111110 to the output port. MOV P1, #LED_ON A delay subroutine is then called to allow LED to be lit for a certain period. In other words, the delay routine determined how long the LED is lit. The delay example uses a common approach of loading a counter with a specific value (0H to 0FFH) and decrementing the counter until it becomes zero. The move (MOV) instruction is used to place the constant to the general purpose registers R0 and R1. To increase the delay period, multiple loops are used. The most convenient way of determining whether the count has reached zero is to use the decrement and jump if not zero (DJNZ) instruction. This DJNZ instruction decrements the specified counter (R0 or R1). If the counter is zero, the control is passed to the address specified (Loop1 or Loop2). When the counter reaches zero, the control is passed to the next instruction. The delay routine is returned back to the original loop through the return (RET) instruction. Switching OFF the LED simply requires that bit P1.0 of port P1 be set. The delay is called again to allow the LED to be OFF for the same period of time. MOV P1, #LED_OFF The switching ON and OFF of LED1 is repeatedly done until TM-1 is switch OFF.

Source Code
;Project: ;File: ;Activity1_0: ;Module: ;MCU: ;Date: ;Declaration LED1_ON LED1_OFF ;Program setup ORG 0000H LJMP Start ORG 0030H EQU EQU 11111110B 11111111B ;LED1_ON is assign the value 11111110B ; LED1_OFF is assign the value 11111111B Expt1_0 Expt1_0.asm Interfacing LED Using TM-1 Switching LED1 ON and OFF TM-1 Atmel AT89C2051 mm/dd/yyyy

;Initialization Start: ;Main Loop Main:

CLR EA MOV SP, #30H MOV P1, #LED1_ON CALL Delay MOV P1, #LED1_OFF CALL Delay SJMP Main MOV R0, #0FFH MOV R1, #0FFH DJNZ R1, Loop2 DJNZ R0, Loop1 RET END

;interrupts are disabled ;stack pointer is initialized to 30H ;LED1 is switched ON ;Delay routine is called to keep LED1 lit during the duration ;LED1 is switched OFF ;Delay routine is called to keep LED1 lit during the duration

;Delay Routine Delay: Loop1: Loop2:

;initialize count down timer ;initialize count down timer ;decrement and loop back until R1 is zero ;decrement and loop back until R0 is zero ;return to Main Loop

;Program end

Operating Procedure:
1. Create the project for the sample program. Save the project as Expt1_0 in the Experiment1 folder. Choose the target MCU as Atmel AT89C2051. 2. Write, edit and assemble the above assembly program. Save the file as Expt1_0.asm. 3. Burn the HEX file named Expt1_0.hex into the AT89C2051 MCU using the Programmer, which requires 12V DC power supply. Insert the MCU in the ZIF socket with the pins 10 and 11 of the MCU connected to the bottom-most pins 12 and 13 of the ZIF socket. 4. Transfer the programmed IC to the TM-1. Be sure to power OFF the module first. Make sure the IC is in the correct position. The notch of the IC must match the notch of the IC socket. 5. Move the SEL2 jumper to LED. Plug-in the 6V DC into the DC jack and turn ON the power. 6. LED1 will switch ON and OFF alternately. The process of switching ON and OFF will be repeated until TM-1 is turn OFF.

Activity 1-1: Flashing the LEDs


Use the TM-1. Write a routine to flash (ON and OFF) all LEDs connected to port P1. These are LEDs 1 to 8. Try different delay periods to vary the ON and OFF duration of the LEDs as well as the rate of flashing. 1. 2. o o o o o o o o

Activity 1-2: Incremental Lighting Pattern


Use TM-1. Write a routine to display the following incremental lighting pattern. 1. o o o o o o o 2. o o o o o o 3. o o o o o 4. o o o o 5. o o o 6. o o 7. o 8.

You might also like