Wednesday, June 30, 2010

Session 2: 8051 Assemble Language

• Register
• MOV
• Power Up
• Stack
• PSW
• Register banks

Register Banks

1. General purpose, 8 bit
             o A: (Accumulator), B
             o R0,R1, R2, R3, R4, R5, R6, R7
2. 16-bit, specifically used as pointers
           o DPTR: (data pointer) : as 8-bit DPH, DPL (data pointer hi/low)
           o PC: (Program Counter)

MOV Intruction

1. Syntac:
           Mov dest, src
                      o Think assignment statement: dest:=src;
2. Dest src are called Operands
            o Dest can be A, B, R0...R7, DPH, DPL
            o Src can be A, B, R0...R7, or an immediate
3. Immediate = constant value, e.g as #12

Assembler idiosyncrasy with immediates

1. Default base: decimal
             o #12 (assumed to be decimal)
             o Can be hex: #12H (hex 12, = dec 18)
2. However the char after # must be '0'.. '9'
             o #FFH is invalid (since 'F' is not in '0'..'9'
             o Solution: #0FFH (add a useless '0')

Immidiates vs direct (addressing mode)

1. Mov A, #17H
              o A= 0x17; #17H is literal
2. Mov a, 17
              o A= * ((char *)17) 17 is an address
3. Big difference
              o R0,...R7 => Register mode
              o #17 => Immediate mode;
              o 17 => direct mode (address -- where?)

Add instruction

1. ADD A, source
              o Note: A is hardwired cannot use anything else
              o Meaning: A:= A + source;
2. Source itself can be any R-Value operad
             o Immediate, direct, register
How to write R2:= 0x25 + 0x35
             1. Key: need to involve the accumulator A
             2. MOV A, 25H ;; A: = 0x25;
                 ADD A, 34H ;; A: = A + 0x34;
                 MOV R2, A ;; R2:= A;
             3. Would be incorrect (in 8051) to try
                 MOV R2, 25H ;; this part is ok
                 ADD R2, 34H ;; must use A, not R2
            4. Since the sum is constant, could do
                MOV R2, 59H ;; eval. by assembler

Assembly Language

1. Directives
             o Commands to the assembler
                e.g., starting address, allocate memory,...
2. (assembly) instructions
              o Correspond to machine instructions
3. Labels
              o Symbolic names that mark addresses
4. Comments

Example Program (2-1)

ORG 0H :start address
MOV R5, #25H
MOV R7, #34H
MOV A, #0
ADD A, R5
ADD A, R7
ADD A, #12H
HERE: SJMP HERE
END ; some may be. END

Mnemonic

• A symbol that helps you remember
• example: phone number
             o 2 = A, B, C 3 = D, E, F 4 = G, H, I
             o doesn't need to be exact; can approximate
• Opcodes and directives are mnemonics
             o MOV ~~ "move"
             o JMP ~~ "jump" etc

Try this out

• Save assembly program as plain text file Name file.a51
• asem file.a51 ;;type this as a command
  => if correct, says no erross
• Output files -- simple case, sigle file
          o .hex file: hex-formatted binary file
          o .lst file: "listing" (before/after views)

.lst file by assmebler

Addr       Machine code      Assembly Source
0000          7D 25                 Mov R5, #25H
0002          7D 34                 Mov R7, #34H
0004          7F 00                  Mov A, #0
0006          2D                      ADD A, R5
0007          2F                      ADD A, R7
0008          24 12                 ADD A, #12H
000A         80 FE                 HERE: SJMP HERE

More assembler features

• Literal: in other bases
            o Binary: add B after: 0000 1010B
• DB: define byte (or string, whatever data)
            o store the data in program memory
            o label is optional, not required!
• EQU: constant declaration (macro)
            o inlined when used. requires a macro name

Example of DB vs EQU

• DATA1: DB "Hello world"
  DATA2: DB 25
  => both accupy space in (code) memory
• COUNT EQU 25 ;;occupies no space!
  MOV R3, #COUNT
  => macro expansion into MOV R3, #25

Adders -- from logic gates

• Half adder
  - Sum = same as an Xor
  - Carry = same as And
• Full adder
  -Take Carry from prev bit
  -Consists of two half adders
• Then connect them in a chain
• Forunately, VHDL can do bitvector add

PSW: program status word

• 8 bit register containing flags
          o indicating status of the processor
CY (C bit)     PSW.7        Carry flag
AC                PSW.6        Auxiliary carry. for BCD arithmetic
F0, -              PSW.5        (user)
RS1               PSW.4        Register bank select
RS0               PSW.3        Register bank select
OV                PSW.2        Overflow
P                   PSW.0        Parity: even or odd # of i's in A

CY flag (C-bit)
• Carry out of the highest-order adder
            o Does not mean overflow!!
   Example: 0x01 + 0xFF = 0x00 with C= 1
   (corresponds to 1 + (-1) = 0.)
           o Carry or borrow, both have C=1
• Carry bit can be set, cleared, moved
          o CLR C ;; clear the C bit, means := 0
             SETB C ;; set the C bit, means := 1

OV-flag (overflow)


• Overflow: too big/too small to represent
• ADD:
       o Both operands same sign, sum different
          e.g., both +, sum-; or both -, sum +
       o Cannot overflow when operands mix + -
          Because the sum is beween the two
• SUB:
        o Opposite condition of ADD

Outcome of a signed A + B

• A and B have different signs
        o Can't overflow. Because either
           A≤ Sum ≤ B or B ≤ sum ≤ A
        o V (overflow bit) is always '0' in this case.
• A and B have same signs
        o Sum has same sign as A, B => No overflow
        o Sum has a different sign => Overflow! (V='1')

Example of Overflow in signed addition

• Both A, B positive, Sum negative






• The status bits are
                 o V = 1 (overflow), C = 0 (no carry out),
                 o N = 1 (negative), Z = 0 (not zero)

Example of overflow in signed addition (cont'd)

• Both A, B negative, Sum positive







• The status bits are
          o -V = 1 (overflow), C=1 (carry)
          o N = 0 (not negative), z = 0 (not zero)
• Sum does not include the carry bit!!

Examples of NOT Overflow in signed addition

• A, B have different signs







• Overflow is not the same as arithmetic carry
             o Truncating at the top is a feature, not a bug!
             o C = 1 (ignored), V= 0, N = 0, Z = 0

Examples of unsigned addition

• MSB of A, B both = 0, MSB of Sum = 1







• the status bits are
          o V = 0 (overflow), C = 0 (no carry out0,
          o N = 0 (not negative), Z = 0 (not zero)


 Examples of Overflow in unsigned additon (Cont'd)

• MSB of both A, B= 1, MSB of Sum = 0






• The status bits are
            o V = 1 (overflow), C = 1 (carry)
            o N = 0 (not negative), Z = 0 (not zero)
• Sum does not include the carry bit

Signed Subtract


• A - B = A + (-B)
           o Take 2's complement of B (flip all bits, add 1)
           o VHDL also provides the -operator
• N, Z, C, V bits
           o C = "borrow", 1 when sign bit:
           o if A, B same sign => V='0'
           o if A, B diff signs and Res diff sign from A => V='1'

Examples of signed subtraction

• Positive A, Positive B, Negative Result

 







• The status bits are

            o V = 0 (overflow), C = 1 (borrow).
            o N = 1 (negative), Z = 0 (not zero).

Examples of overflow in signed subtraction

• Positive A, Negative B, Negative Result



 






• The status bits are
           o V = 1 (overflow), C = 1 (borrow).
           o N = 1 (negative), Z = 0 (not zero).

 Unsigned Subtract
 
• A - B = A + (-B)
         o Take 2's complement of B, add as before
         o VHDL also provides the -operator
• N, Z, C, V bits
         o N always '0' (unsigned).
         o C = "borrow". Same as signed version
            '1' when the MSB = table here
            A      0   0   0   1
            B      0   1   1   1
            Res   1   1   0   1
         o V unsigned = C unsigned

Examples of overflow in unsigned subtraction
 
• Both A, B Positive, Res negative


 



• C = 1 (ignored), V = 1, N = 0, Z = 0
         A        0   0   0   1
         B        0   1   1   1
         Res     1   1   0   1

Register banks and Scratchpad memory

• Four register banks [ 00-lFH]
          o 8 registers per bank (8 bytes/bank)
          o bank 1 is stack
• Bit-addressable (16 bytes) [20-2FH]
• Scratchpad (Byte-addressable) [30-7FH]
• Total 128 bytes [00-7FH]

Register bank selection


• Register can be from 1 of 4 banks
            o Depending on PSW.4, PSW.3
• Example
           o MOV R0, #12H
             Where is R0?



 




• Answer: depends on which bank!

            o Could be address 00H, 08H, 10H, or 18H

Instruction for setting banks

• "Set a bit" (means assign the bit to 1)
            o SETB PSW.4
• "Set a bit" (means assign the bit to 0)
            o CLR PSW.3
• So, together, PSW.4 = 0, PSW.3 = 1
   => selects bank a

Two ways of accessing the same registers

• Register mode:
           o Use R0, R1 ...name
           o Current bank
           o Need bank switching
• Direct mode:
           o Use the RAM address of the register!
           o 00 for R0 of bank 1, 12 for R2 of bank 3

Examples

• Register addressing:
           SET B PSW.4
           CLR PSW.3
           MOV R3, #99H
• Direct addressing:
           MOV 13, #99H









Stack
• Fundamental data structure in programming
           o Return address of a call
           o space for local (auto) variables
• Implicitly addressed
           o PUSH (add element to stack)
           o POP (remove element from stack)
• Need: stack pointer (SP) -- register in 8051

8051 architecture-supported stack

• 8-bit Stack Pointer SP
           o Initialized to 07H on power up
              => points to just before R0 of bank1
• Grows from lower to higher address
           o PUSH: pre-increment: stack [++SP]=d;
           o POP: post-decrement: d=stack [SP--];
• Opposite from stacks on other CPUs!!

Synax of PUSH/Pop instructions

• PUSH 6
           o This doesn't mean PUSH #6
           o It means PUsh content of R6 to stack!
• POP 2
           o Means pop the top element of stack to R2
           o Reason: think "RAM Address"

Range of value for SP

• [07 - 1FH] (register bank 1, 2, 3)
           o On power-up SP = 07H
           o Should not go lower;
              Could go up to 1FH (R7 of bank3)
• [30 - 7FH] (scratchpad memory)
• NOT [20 - 2FH] -- bit addressable area

Other use of stack : call /Return

• Call instruction
           o pushes return address onto stack then go to target
• Return instruction
           o pops return address then go to target
• Interrupt: hardware pushes return address
• Return-from-interrupt instruction

Saturday, June 26, 2010

Session 1: 8051

1. Microprocessors VS MCU
2. Advantages of MCUs
3. Embedded systems
4. Choose a microcontroller
5. Speed, packaging, memory & cost per unit
6. Various members of 8051 family
7. Various manufacturers of 8051

CPU vs MCU vs Embedded microprocessor
>> Older
1. 6811 (formerly motorola, now Freescale)
2. 8051 (intel), Z8 (Zilog), PIC 16(Microchip)
>>Newer
1. ATMega (Atmel)
2. Hitachi H8
>> 8-bit means register (native data size) 8 bits

Why 8051 is still popular aftar 25 years
1. Intel allows others to make compatible MCUs
             >> Atmel, Philips/Signetcs, Siemens, Dallas Semiconductor
2. New and improved
             >> Silicon Labs (100MHZ low power, hi-perf)
3. Free designs available
             >> Integrated RF (e.g, Nordic nFR24E1, Chipcon cc2430, RadioPulse RG2400)

8051 Family









Question: How to program?
1. Make ROM (cheap if large quantity)
            >> Problem: You cannot change the program!
2. Make writable ROM
            >> PROM: (P=Programmable) Write-Once
            >> EEPROM: Electrically erasable Prom
            >> UV-EPROM : Erasable w/Ultraviolet (UV) light
            >> Flash: atype of EEPROM
            >> NV-RAM: Non Volatile RAM

EEPROM Programmer and UV-Eraser

 



























Flash memory
1. NAND-flash
            >> Cheaper, page access, erase whole page before writing
            >> Good for data (e.g, digital camera)
 2. NOR-flash
            >> Word addressable, more expensive
            >> Good for MCU program (firmware)
 3. Limited number of rewrite cycles (10,000)

Package types
1. DIP (dual-inline package) used in breadboards









2. PLCC (Plastic leadless chip carrier) Removable from socket

 
 
 
 
 
 
3. BGA (ball-grid array) Connect on bottom 
 






4. SMT (Surface mount) Small or no leads


 
 






Choosing a Microcontroller
  1. Computing needs
  2. Speed, packaging, power consumption, RAM, ROM, I/O pins, timers, radio, cost
  3. Voltage: TTL, CMOS, NMOS
  4. Software development tools
  5. Assembler, debugger, C compiler, emulator, technical support
  6. Availability & source

ISP Flash Microcontroller Programmer

Introduction

 This ISP Programmer can be used either for in-system programming or as a stand-alone spi programmer for Atmel ISP programmable devices. The programming interface is compatible to STK200 ISP programmer hardware so the users of STK200 can also use the software which can program both the 8051 and AVR series devices.

Hardware
Figure 1 shows the circuit diagram of the in-system programmer interface, the power to the interface is provided by the target system. The 74HCT541 ic isolate and buffer the parallel port signals. It is necessary to use the HCT type ic in order to make sure the programmer should also work with 3V type parallel port.

                Figure 1: Circuit Diagram of the ISP Programmer Interface

Figure 2 shows the circuit diagram of the stand-alone spi programmer, the power to the interface is provided by the PC USB port which can supply a max of 100mA current. Get a cheap USB cable, cut the cable other end connector and attach a crimp shell connector to this end, red wire is 5V and black is 0V.

The printer port buffer interface is same as shown in figure 1. For the u-controller a 40 pin ZIF socket can be used.

This programmer circuit can be use to program the 89S series devices and the AVR series devices which are pin compatible to 8051, like 90S8515. For other AVR series devices the user can make an adapter board for 20, 28 and 40 pin devices. The pin numbers shown in brackets correspond to PC parallel port connector.

                    Figure 2: Circuit Diagram of the SPI Programmer

Software
The ISP-30a.zip file contains the main program and the i/o port driver. Place all files in the same folder.
The main screen view of the program is shown in figure 3.

Also make sure do not program the RSTDISBL fuse in ATmega8, ATtiny26 and ATtiny2313 otherwise further spi programming is disable and you will need a parallel programmer to enable the spi programming. For the fuses setting consult the datasheet of the respective device.

For the auto hardware detection it is necessary to short pin 2 and 12 of DB25 connector, otherwise the software uses the default parallel port i.e. LPT1.

Following are the main features of this software,

Read and write the Intel Hex file
Read signature, lock and fuse bits
Clear and Fill memory buffer
Verify with memory buffer
Reload current Hex file
Display buffer checksum
Program selected lock bits & fuses
Auto detection of hardware


Note:
The memory buffer contains both the code data and the eeprom data for the devices which have eeprom memory. The eeprom memory address in buffer is started after the code memory, so it is necessary the hex file should contains the eeprom start address after the end of code memory last address i.e. for 90S2313 the start address for eeprom memory is 0x800.

The software does not provide the erase command because this function is performed automatically during device programming. If you are required to erase the controller, first use the clear buffer command then program the controller, this will erase the controller and also set the AVR device fuses to default setting.


Download
ISP-Flash Programmer Software ISP-30a.zip

                  Figure 3: Main screen of the program ISP-Pgm Ver 3.0a

Wednesday, June 23, 2010

Graphic LCD

Done working on Graphic LCD (GLCD) interfacing with 89c51. Here is exapmle asm file and hex code file.
And following are the connection pattern for GLCD and 89c51.


Friday, June 18, 2010

My 89C51 Development board

This is my 89c51 development board with the help of this board life become so easy, using this board its so much easy to made projects, and also using above tools testing of any program is much easier then before.

Basic LCD Project


This is my another project in which i interface 16x2 LCD with 89c51 microcontroller.  

Binary To Decimal Converter


This is my first 89c51 microcontroller project.
In this project i uses some TTL ic's to work done in which 74LS47 is use to convert the decimal data into 7-Segment signal, and 74LS07 is use as a Buffer to provide some current to LED's and drive 89c51 input.

Click for Download

LED Test Example



















In this example i m trying to describe the basic components which have been attached to 89c51 microcontroller to make it run in normal mode.
And also in this example i Blink LED.
Following is the sample program to blink LED.

ORG 0H
AGAIN:
CLR P2.0                 ;SEND LOW SIGNAL TO PORT 2 BIT NO. 0
CALL DELAY         ;CALL DELAY SUBROUTINE
SETB P2.0               ;SEND HIGH SIGNAL TO PORT 2 BIT NO. 0
CALL DELAY         ;CALL DELAY SUBROUTINE
SJMP AGAIN         ;JUMP TO LABEL AGAIN

DELAY:
MOV R1,#255        ;MOVE 255 TO REGISTER R1
HERE:
MOV R2,#50          ;MOVE 50 TO REGISTER R1
DJNZ R2,$             ;DECREMENT IN R2 UNTILL IT BECOME ZERO
DJNZ R1,HERE     ;DECREMENT IN R1 UNTILL IT BECOME ZERO
RET                        ;RETURN TO LAST POSITION
END

About 89c51 Microcontroller


8051 Microcontroller is a programable device which is used for controlling purpose.
Basically 8051 controller is Mask porogramble means it will programed at the time of manufacturing and will not programed again, there is a derivative of 8051 microcontroller, 89c51 microcontroller which is reprogramable upto 10000 times, here is small discription abot the 89c51 microcontroller.
It have 4 ports which are used as input or output according to your need.
These prots are also programmed as bit wise pattern, means you can use each bit of microcontroller saperatelly as input or output.
This 89c51 is 8-bit device mean each port have 8-bits for I/O, total of 32-bits in the controller.
This device also have Timer, Serial Port interface and Interrupt controlling you can use these according to your need.
This device have 4K of ROM space to store the program,and 256Bytes of RAM space, this device have ability to interface with TTL based devices and also with the external MEMORY to increase its data space, but when your are using external MEMORY the 2 ports of 89c51 microcontroller are used for this purpose.