






































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A comprehensive introduction to microcontroller programming, focusing on assembly language, debugging techniques, and essential concepts like parallel and serial communication. It includes practical examples, flowcharts, and explanations of key registers and functions. Particularly valuable for students learning about microcontrollers and their applications.
Typology: Lab Reports
1 / 46
This page cannot be seen from the preview
Don't miss anything!
Assembly language is of higher level than machine language and hence easier to use.
An assembly language code consists of
a) Program statement lines b) Comment lines
A program statement is a line that contains 4 fields in the following format:
[
where [ ] indicates an optional field that may not be always required. The fields are separated by a tab or space. (Tab is recommended, since it ensures an orderly appearance to your code. For the same reason, when a field is not used, the tab or blank should still to be used, such that the fields of the same type stay aligned in same columns.) When writing
The
The
Table 1 Assembler directives
Name of Assembler directive what it does Alias for END end program DB define bytes FCB DW define words FDB DS define storage RMB EQU equate FCB form constant byte FCC form constant characters FDB form double bytes ORG (^) set origin RMB (^) reserve memory bytes #INCLUDE include source file $INCLUDE include source file #INCLUDE
The
The constants used in the
Table 2 Assembler symbols for constants
Symbol Meaning Example
$
%
@
‘
The expressions used in the
Table 3 Assembler symbols for expressions
Symbol Meaning Example
! binary OR %11111111!%
∗ multiplication^3 ∗$2A / division $7E/
Important conventions used in the
Table 4 Other important conventions
Symbol Meaning Example
; (^) start of comment line and of comment inside a program statement LDAA^ #$FF^ ;^ Load accA
, Y index X mode (IND,Y) LDAA TFLG2,Y
(Section 6 and Section A of M68HC11 Reference Manual)
The 6811 microcontroller has 145 different commands. These commands can be grouped into several categories. The categories and the commands in those categories are listed below:
a) Addition: ABA, ABX, ABY, ADCA, ADCB, ADDA, ADDB, ADDD, INC, INCA, INCB, INS, INX, INY b) Subtraction: SBA, SBCA, SBCB, SUBA, SUBB, SUBD, DEC, DECA, DECB, DES, DEX, DEY c) Multiplication: MUL d) Division: FDIV, IDIV
a) Standard logical operations: ANDA, ANDB, EORA, EORB, ORAA, ORAB, COM (Boolean inverse), COMA, COMB b) Operations that shift the location of the bits in the register: ASL, ASLA, ASLB, ASLD, ASR, ASRA, ASRB, LSL, LSLA, LSLB, LSLD, LSR, LSRA, LSRB, LSRD, ROL, ROLA, ROLB, ROR, RORA, RORB c) Operations that compare two numbers: BITA, BITB, CBA, CMPA, CMPB, CPD, CPX, CPY
Branching commands: BCC, BCS, BEQ, BGE, BGT, BHI, BHS, BLE, BLO, BLS, BLT, BMI, BNE, BPL, BRA, BRCLR, BRN, BRSET, BSR, BVC, BVS, JMP, JSR, RTS, RTI, WAI
Memory/Register Functions
a) Move data into / out of memory: LDAA, LDAB, LDD, LDS, LDX, LDY, STAA, STAB, STD, STS, STX, STY b) Change the values in memory/registers: BCLR, BSET, CLC, CLI, CLR, CLRA, CLRB, CLV, COM, COMA, COMB, NEG, NEGA, NEGB, SEC, SEI, SEV c) Transfer data from one register to another: TAB, TAP, TBA, TPA, TSX, TSY, TXS, TYS, XGDX, XGDY
Stack Pointer Functions: PSHA, PSHB, PSHX, PSHY, PULA, PULB, PULX, PULY
Misc.: NOP, SWI
Note: Boolean inversion commands: COM, COMA, COMB
This simple program is an example of addition. It performs the operation:
VAR0 + VAR1 ‡ SUM
In addition, the program checks if an overflow happened during the addition process, and sets the flag OVERFL accordingly.
Initialize variables: VAR0 ‡ $ VAR1 ‡ $ SUM ‡^ $ OVERFL ‡ $
Load $00 into accB as the initial (optimistic) guess for the overflow status Load first variable into accA Add second variable to accA (result stay in ac cA)
Since overflow bit was not clear, Invert accB
Brach if overflow bit is clear
Store result of addition from accA into SUM Store current value of overflow flag from accB into OVERFL
SW I
Y
N
FLOW CHART
LABEL
14: * Include definition of variables for MC68HC 1: * Define variables used by MC68HC11 microcontroller 2: 3: 0000 DATA EQU $0000 ;start of data 4: c000 PROGRAM EQU $C000 ;start of program 5: fffe RESET EQU $FFFE ;reset vector 6: 1000 REGBAS EQU $1000 ;register base 7: 8: 0000 PORTA EQU $ 9: 0002 PIOC EQU $ 10: 0003 PORTC EQU $ 11: 0004 PORTB EQU $ 12: 0005 PORTCL EQU $ 13: 0007 DDRC EQU $ 14: 0008 PORTD EQU $ 15: 0009 DDRD EQU $ 16: 000a PORTE EQU $0A 17: 000b CFORC EQU $0B 18: 000c OC1M EQU $0C 19: 000d OC1D EQU $0D 20: 000e TCNT EQU $0E 21: 0010 TIC1 EQU $ 22: 0012 TIC2 EQU $ 23: 0014 TIC3 EQU $ 24: 0016 TOC1 EQU $ 25: 0018 TOC2 EQU $ 26: 001a TOC3 EQU $1A 27: 001c TOC4 EQU $1C 28: 001e TOC5 EQU $1E 29: 0020 TCTL1 EQU $ 30: 0021 TCTL2 EQU $ 31: 0022 TMSK1 EQU $ 32: 0023 TFLG1 EQU $ 33: 0024 TMSK2 EQU $ 34: 0025 TFLG2 EQU $ 35: 0026 PACTL EQU $ 36: 0027 PACNT EQU $ 37: 0028 SPCR EQU $ 38: 0029 SPSR EQU $ 39: 002a SPDR EQU $2A 40: 002b BAUD EQU $2B 41: 002c SCCR1 EQU $2C 42: 002d SCCR2 EQU $2D 43: 002e SCSR EQU $2E 44: 002f SCDR EQU $2F 45: 0030 ADCTL EQU $ 46: 0031 ADR1 EQU $ 47: 0032 ADR2 EQU $ 48: 0033 ADR3 EQU $ 49: 0034 ADR4 EQU $ 50: 0039 OPTION EQU $ 51: 003a COPRST EQU $3A 52: 003b PPROG EQU $3B 53: 003c HPRIO EQU $3C 54: 003d INIT EQU $3D 55: 003e TEST1 EQU $3E 56: 003f CONFIG EQU $3F
list# address object label opcode operand comments or directive
57: *1234567890123456789012 3456789012345678901234567890123456789 15: #INCLUDE 'A:\VAR_DEF.ASM' 16: 17: * Define program variables 18: ORG DATA 19: VAR0 RMB 1 ;reserve 1 byte for VAR 20: VAR1 RMB 1 ;reserve 1 byte for VAR 21: SUM RMB 1 ;reserve 1 byte for sum 22: OVERFL RMB 1 ;reserve 1 byte for overflow flag 23: 24: * Start main program 25: ORG PROGRAM 26: c000 c6 00 LDAB #00 ;assume no overflow (optimistic!) 27: c002 96 00 LDAA VAR0 ;load VAR1 in accumulator A 28: c004 9b 01 ADDA VAR1 ;add VAR2 to accumulator A 29: c006 28 01 BVC LABEL1 ;jump if no overflow 30: * We have overflow! 31: c008 53 COMB ;Invert accumulator B ($00 to $FF) 32: c009 97 02 LABEL1 STAA SUM ;store result of addition 33: c00b d7 03 STAB OVERFL ;store accB into overflow flag 34: c00d 3f SWI ;stop the microcontroller
Symbols: data * label1 *c overfl * program *c sum * var0 * var1 *
Take a formatted empty floppy disk write on the label:
EMCH 367 LASTNAME, Firstname Email address Contact telephone #
This way, if you loose the disk, there is a good chance that you might have it recovered.
Download the template.asm file and place it on the floppy disk. This template will always be a good to start your programming.
Download the file VAR_DEF.ASM and place it in the root of the directory structure on your floppy disk. (This will allow the programs to find it when executing the instruction #INCLUDE ‘A:VAR_DEF.ASM’.
Download example files from the course website onto this disk. (For safety, make copies into your folder or PC.)
An .asm template file is available on the course website. This template has the required instructions to make your program interface properly with the simulator. W hen generating a new program, open the template.asm file, save it under the new name you want to create (remember to save on a secure area, preferably your floppy disk), and then start typing in your program in the indicated area.
After you type and save your program (save as often as you can, use Ctrl S for productivity), assemble the program and test run it.
To capture the image of a window or of the complete screen:
The captured image can be viewed on the clip board.
To paste the captured image into a document:
Note: In most cases, you will need to capture just the active window, using Alt + PrintScreen.
Default windows are either (*.LST, .asm, and CPU registers), or (.LST, *.asm, Memory list, and CPU registers), as shown below. In the memory list, standard labels are shown. However, they can be removed if you use the pull down menu command Label/Remove all.
MiniIDE is an integrated development environment running under W indows 95/98/Me/NT/2000. It was developed by MGTEK in Germany. It is a tool for developers of embedded software who write software in assembler for Motorola's 68HC11 and 68HC12 microcontroller. MiniIDE incorporates an editor and a serial communication terminal. A command-line cross assembler, which is seamlessly integrated in the IDE, is also included.
With MiniIDE, user can edit compile and download program to microcontroller, then debug program interactively. As shown above, a user can edit ASM program in editor window 1; then compile the program, if there are syntax errors, warning messages will be shown in output window 2; at last, download the program and interact with the microcontroller in terminal window 3 to debug and run the program.
In this course, MiniIDE is used to download codes into the MCU Evaluation Board (EVB). In this context, it acts as a terminal program.
You do not need to install this software on your PC.
The programming flow chart is shown in the figure below. First, the source code is written in Assembly language on the THRSim11 simulator. The simulator assembles the .asm code and generates a list file (.LST). The simulator is then used to step through the program and debug it until it performs the intended functionality. All this can be done remotely, in the computer room, or on a personal computer. Once the program has been debugged, it can be taken on a floppy disk to the EMCH 367 lab (A 235). The MCU evaluation board (EVB) hardware is accessed through the MiniIDE emulator software installed on the lab computers. MiniIDE reads the .asm file from your floppy disk and transforms it into machine language executable code (.S19). This code is downloaded to the MCU. After downloading the code into the MCU, you can make the MCU run your code using the MiniIDE interface screens. The MiniIDE also generates a list file (.LST) that can be used during debugging.
THRSim Software
MiniIDE Software
MCU EVB Hardware
Executable code MACHINE LANGUAGE *. S
Source code ASSEMBLY LANGUAGE *.asm
List file *.LST
List file *.LST
Figure 1 Flowchart of typical programming steps used in the EMCH 367 course.
Note : To quickly grasp the use of binary and hex arithmetic, use your binary/hex pocket calculator and the website http://homepage.ntlworld.com/interactive/BinaryAddition.html
The binary number system is a base-2 numbering system. In binary representation, any value is represented using a combination of 1's and 0's. For example: 14 10 = 1110 2 in binary. The subscript 10
on the first number indicates that the number 14 is represented in the decimal (base 10) system. The subscript 2 on the second number indicates that 1110 is represented in the binary (base 2) system.
The binary representation is also called "digital". "Digit" also means finger, and you can imagine a numbering representation in which you use your 8 digits to for number containing 1's and 0's. The ability to represent numbers in terms of 1's and 0's is important because it is the easiest most unambiguous way to represent and communicate information. In a computer, a 1 is represented by a "high" voltage (5V) and a 0 by a "low" voltage (~0V). The binary system is the backbone of all digital computers and other high-tech applications.
To understand how the binary system works, let's first examine how the conventional base-10 system works. The base-10, or decimal, system constructs numbers using increasing powers of 10. For example, the number 135 10 is constructed using 3 powers of 10: 10^0 , 10^1 , and 10^2. These numbers
correspond to 1,10, and 100. The number 135 10 is constructed as:
1 x 100 + 3 x 10 + 5 x 1 or 1 x 10^2 + 3 x 10^1 + 5 x 10^0
The equivalent of number 135 10 in base two is 10000111 2. This is constructed as:
1 x 128+ 0 x 64 + 0 x 32 + 0 x 16 + 0 x 8 + 1 x 4 + 1 x 2 + 1 x 1
or
1 x 2^7 + 0 x 2^6 + 0 x 2^5 + 0 x 2^4 + 0 x 2^3 + 1 x 2^2 + 1 x 2^1 + 1 x 2^0
It can be seen that the only significant difference between the two systems is the base number.
Each one or zero in the binary representation is called a "bit". A collection of eight bits is called a "byte" and, in a somewhat humorous note, a collection of four bits is called a "nibble". The bit associated with the highest power of two is called the Most Significant Bit (MSB); the bit associated with the lowest power of two is the Least Significant Bit (LSB).
1
(^1 0 0 1 0 0 0 1) 2 nibbles = 1 byte
byte (1 byte = 8 bits)
bit
Hex number (nibble) (1 nibble = 4 bits)
Because most people are more comfortable using, and thinking in, the decimal system, it is important to know how to convert from the decimal to the binary system. This is most easily achieved through a series of divisions by two and by tracking the resulting remainders. Let's consider out example of 13210 :
132 ÷2 = 66 Remainder 0 66 ÷ 2 = 33 Remainder 0 33 ÷2 = 16 Remainder 1 13210 = 10000100 16 ÷2 = 8 Remainder 0 8 ÷2 = 4 Remainder 0 MSB LSB 4 ÷2 = 2 Remainder 0 2 ÷2 = 1 Remainder 0 1 ÷2 = 0 Remainder 1
The remainder 1 resulting from the last division is the MSB, while the first remainder is the LSB of the conversion. From this example we see that the decimal number 132 is equal to the binary number
The conversion from binary to decimal is done in the same manner as the first example, by adding together power of two values of the non-zero bits.
As one might have already surmised, binary numbers quickly become long and hard to remember. For this reason, it is more convenient to convert the binary values into hexadecimal numbers (hex). Hexadecimal numbers are base 16 numbers. This requires six additional characters to represent the values 10, 11, 12, 13, 14, and 15. These values will be represented by the letters A, B, C, D, E, and F. The counting order in hex is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. The reason hex notations are use is that it allows for a one to one correspondence between the 16-bit binary nibble and a single hexadecimal value. If the binary number is broken down into nibbles, and each nibble is replaced with the corresponding hexadecimal number, the conversion is complete. Consider 132 10. The binary
number is 10000100. It can be broken down into two separate nibbles: 1000 and 0100. Convert each nibble into the corresponding hex value (8 and 4, respectively), and the hex equivalent of 132 10 is
converted to 10100010001111100011 in binary without using any difficult calculations.
To convert decimal to hex numbers it is easiest to convert the decimal number to binary and then convert the binary to hex. In addition to these methods, there is a conversion chart in the back of the Programming Reference Guide for the conversion from decimal to hex.
Verification W e have establish that -3 10 = 11111101 2. Verify that -3 10 + 3 10 = 0 10 using 8-bit arithmetic.
BINARY DECIMAL 11111101 - -3 10 - (^00000011 ) (^00000000 )
Note that, in this operation, a carry of 1 was liberally lost in the 9th^ bit!
Example 2: Given the binary number 00110101, find it's 2's complement.
Solution : Subtract the number from 00000000, i.e.
BINARY HEX DECIMAL 00000000 - 00 - 010 - (^01110101 75 ) 10001011 8B -106 10
Verification : 01110101 + 10001011 = (1)00000000. Since the 9th^ bit is irrelevant, the answer is actually 00000000, as expected
The rule outlined above can be applied to both binary and hex numbers.
Example 3: Given the hex number 6A, find its 8-bit 2's complement.
Solution : Subtract the number from 00 16 using 8-bit arithmetic:
HEX DECIMAL 00 - 010 - 6A (^10610) 96 -106 10
Verification : 6A 16 + 96 16 = (1)00. Since the 9th^ binary bit is irrelevant, the answer is actually 00 16 , as expected
Example 4: 110010102 ‡ CA 16 ‡ 20210.
Decimal (base 10)
4-bit binary (base 2)
Hex (base
0 0000 0 1 0001 1 2 0010 2 3 0011 3 4 0100 4 5 0101 5 6 0110 6 7 0111 7 8 1000 8 9 1001 9 10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F