EET-323 Week 2 Assembly Programming.pdf
Document Details
Uploaded by RomanticViolin
Centennial College
Tags
Full Transcript
EET-323 Microcomputer Interfacing School of Engineering Technology & Applied Science (SETAS) Week # 2 Assembly Programming Objectives Important facts of assembly language Structure of assembly language...
EET-323 Microcomputer Interfacing School of Engineering Technology & Applied Science (SETAS) Week # 2 Assembly Programming Objectives Important facts of assembly language Structure of assembly language Understanding opcodes (instructions) Creating delays EET-323: Week #2 2 Programming in Assembly Language - 1 Assembly provides mnemonics for the machine code instruction and other features that made programming faster and less prone to errors. Assembly codes are translated into machine codes by a program called Assembler. Assembly language is referred to as a low-level language because it deals directly with the internal structure of the CPU. EET-323: Week #2 3 Programming in Assembly Language - 2 All CPUs are able to work only in binary (0 and 1) but at a very high speed. A program that consists of only binary numbers (0 and 1) is called machine language. Hexadecimal numbering system is used to represent binary numbers because it is more efficient and less cumbersome. EET-323: Week #2 4 Structure of Assembly Language - 1 An Assembly Program consists of four fields. Column #1 #2 #3 #4 Fields [Label] Mnemonic Operand [;Comment] * Note: Brackets indicate that the field is optional and not required to be present in the code. Mnemonic = Opcode = Instruction ORG and END are directive to Assembler while Mnemonics are directive to CPU. EET-323: Week #2 5 Structure of Assembly Language - 2 Link section Start of Code : Resets Vector Mnemonics Operands Comments Labels End of code EET-323: Week #2 6 Special Function Registers The Special Function Registers are used by the CPU and peripheral modules for controlling the desired operation of the device. Each register has 8 bits (Bit 0 to Bit 7). The registers operate depending on how their bits are configured. To configure an SFR, refer to the Data Sheet (page 18 – SFR Summary), follow to the detailed description and set or clear each bit of the register using the information. EET-323: Week #2 7 SPECIAL FUNCTION REGISTERS Using the Data Sheet - 1 ex) OSCCON Description/Configuration Find the detailed description on page 38 of the Data Sheet. Write out which bits are going to be 1 (set) or 0 (clear). For this example, we will be configuring the OSCCON so that we are using the Internal RC and setting its frequency to 4MHz. EET-323: Week #2 8 SPECIAL FUNCTION REGISTERS Using the Data Sheet - 2 Bits Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Set/Clear 0 1 1 0 0 0 1 0 The value of the bits are written in binary. You can use as is (0b0110 0010), or make it simpler by using a hexadecimal number (0x62). Finally, you need to use it in the code. C Language - OSCCON = 0x62; Assembly - MOVLW 0x62 MOVWF OSCCON EET-323: Week #2 9 OPCODES (INSTRUCTIONS) MOVLW – Move Literal to WREG Refer to Section 29. Instruction Set for instructions to use Opcodes. ex) MOVLW 0xA0 ;moves a hexadecimal number to WREG(A016) EET-323: Week #2 10 OPCODES (INSTRUCTIONS) MOVWF – Move WREG to File Register MOVWF f : moves WREG’s value to a file register (f). ex) MOVWF PORTB ;moves what WREG has to PORTB register. In assembly, a file register cannot be modified directly. Instead, a literal value must be moved to the WREG, then moved to the desired file register. ex) MOVLW 0xFF ;moves FF(16) to WREG MOVWF TRISA ;PORTA set as input MOVLW 0x00 ;moves 00(16) to WREG MOVWF TRISB ;PORTB set as output * 0x00 can be expressed as just 0. MOVLW 0x00 is identical to MOVLW 0 EET-323: Week #2 11 OPCODES (INSTRUCTIONS) MOVLW and MOVWF Exercises 1. Write a code to move value 0xAA to PORTB register. 2. Write a code to flash all bits of PORTB on and off. EET-323: Week #2 12 OPCODES (INSTRUCTIONS) MOVF – Move File to WREG MOVF f,d : moves contests of register (f) to WREG or itself. If d=0, destination is WREG. If d=1, destination is itself. ex1) MOVF PORTB, 0 ;moves contents of PORTB to WREG MOVWF PORTD ;PORTD now has PORTB’s contents 1. Write a code to move value 0x77 to PORTB register, then move PORTB’s value to PORTD. EET-323: Week #2 13 Cycle Time Calculation - 1 𝐹𝑂𝑆𝐶 Cycle Frequency: Fcy = 4 1 𝑀𝐻𝑧 ex) = 250 𝑘𝐻𝑧 4 1 Cycle Time: Tcy = 𝐹𝑂𝑆𝐶 1 ex) = 4 𝜇𝑠 250 𝑘𝐻𝑧 ∴ If “1 MHz” oscillator frequency is used, each cycle takes “4 𝜇s” EET-323: Week #2 14 Cycle Time Calculation - 2 To figure out the cycle time of a program (or a part of a program), we need to know how many cycles the program uses. Each mnemonic (instruction) uses one or two cycles. (Refer to Section 29 of the instruction manual) ex) - Mnemonics; CALL, GOTO, RETLW, and RETURN use 2 cycles. - Some use 1 cycle - Some others use both 1 and 2 cycles EET-323: Week #2 15 CYCLE TIME CALCULATION Example If the oscillator frequency (FOSC) is set to 2 MHz, how long would it take to execute the following program? Answer: Cycles Fcy = 2 MHz / 4 = 500 kHz 1 Tcy = 1 / 500 kHz = 2 𝜇s 1 Total # of Cycles: 1 1 1+1+1+1+1+1+1 = 7 cycles 1 1 Total Cycle Time: 1 7 x 2 𝜇s = 14 𝜇s ∴ 14 𝜇s is required to run this program EET-323: Week #2 16 CREATING DELAYS Short Delays Short delay sub-routine DELAY MOVLW D’200’ ;decimal #200 is moved to WREG MOVWF COUNT1 ;200 is moved to variable, COUNT1 LOOP DECFSZ COUNT1, 1 ;decreases 1 from COUNT1, GOTO LOOP ;and skips this line if COUNT1 = zero RETURN ;otherwise it goes to LOOP If 1 MHz oscillator is used; 1. Calculate cycle frequency and cycle time. 2. Calculate total time delay. EET-323: Week #2 17 CREATING DELAYS Long Delays Long delay sub-routine (loop inside loop) DELAY MOVLW D’20’ ;decimal #20 is moved to WREG MOVWF COUNT1 ;20 is moved to variable, COUNT1 LOOP1 MOVLW D’100’ ;decimal #100 is moved to WREG MOVWF COUNT2 ;100 is moved to variable, COUNT2 LOOP2 DECFSZ COUNT2, 1 ;decreases 1 from COUNT2 until it GOTO LOOP2 ;becomes zero, then skips this line DECFSZ COUNT1, 1 ;decreases 1 from COUNT1 and GOTO LOOP1 ;goes to LOOP1 and run the whole RETURN ;procedure until COUNT1 = zero. If 31.25 kHz oscillator is used; 1. Calculate cycle frequency and cycle time. 2. Calculate total time delay. EET-323: Week #2 18