EET-323 Microcomputer Interfacing Week 1 PDF
Document Details
Uploaded by RomanticViolin
Centennial College
Tags
Summary
This document provides an overview of microcomputer interfacing, focusing on microcontrollers. The topics covered include numbering systems, microcontroller history, and various microcontroller examples. It appears to be lecture notes from an undergraduate course.
Full Transcript
EET-323 Microcomputer Interfacing School of Engineering Technology & Applied Science (SETAS) Week # 1 Microcontroller Objects Numbering conversion History of microcontroller Microprocessor VS M...
EET-323 Microcomputer Interfacing School of Engineering Technology & Applied Science (SETAS) Week # 1 Microcontroller Objects Numbering conversion History of microcontroller Microprocessor VS Microcontroller Microcontroller hardware architecture Memory organization Registers TRIS and PORT EET-323: Week #1 2 Numbering Conversion Human prefers decimal (2510) Machine prefers binary (11012) Programmer prefers hexadecimal (1916) EET-323: Week #1 3 History of Microcontroller In the 1940s, US government asked Harvard and Princeton university to come up with a computer architecture to be used in computing tables of naval artillery shell distances for varying elevation and environmental conditions. Princeton architecture won the competition because it was better suited to the technology of the time. (even before transistors were invented) Harvard architecture was largely ignored until the late 1970s, because uC manufacturers realized that the architecture did not have the instruction/data bottleneck. PIC MCU (Micro Computer Unit) employs the Harvard architecture. (PIC: Peripheral Interface Controller) EET-323: Week #1 4 Examples of 8-Bit uC Microchip: PIC10XXX, PIC12XXX, 14, 16, 18) - PIC16F747 will be used in our lab. Intel 8051 Atmel AVR and 8051 Philips 8051 Zilogs Z8 and Z80 Freescale 68HC11 and 68HC08 * Note: – PIC16FXXX has on-chip program ROM (Read-Only Memory) in form of flash memory type EEPROM (Electronically Erasable Programmable Read-Only Memory), it is great for testing and prototyping. – PIC16CXXX has program ROM in form of OTP memory (One-Time Programmable), it is great for mass production (low cost). EET-323: Week #1 5 Microprocessor VS Microcontroller EET-323: Week #1 6 Microcontroller Selection Microcontrollers are selected based on: Computing needs (Speed, RAM, ROM, I/O…etc) Power consumptions Availability and accessibility of software and hardware such as compiler, assembler, debugger, emulator…etc Wide variety and reliable sources (Manufacturers) Cost per unit EET-323: Week #1 7 PIC Microcontroller The PIC uC uses RISC (Reduced Instruction Set Computer) architecture It comes with standard features such as; - On-chip program ROM for storing codes or instructions - RAM for storing data (volatile or temporary) - EEPROM for storing data (none-volatile or permanent) - I/O ports for interfacing with outside world - Timer for creating delays - Counter for counting events - ADC (Analog to Digital Converter) for converting continues variable to binary number - USART (Universal Synchronous Asynchronous Receiver Transmitter) for communications EET-323: Week #1 8 PIC MICROCONTROLLER Feature Comparison PIC16F747 PIC16F767 PIC18F4580 Program Memory (ROM) 4096 8192 8192 Data Memory (RAM) 368 368 768 Data Memory (EEPROM) None None 256 Input / Output 36 23 36 Timer 3 3 4 # of ADC 14 11 11 # of Pins 28/40/44 28/40/44 28/40/44 EET-323: Week #1 SETAS - AMAT: EET-323 Week #1 9 PIC MICROCONTROLLER Hardware Architecture EET-323: Week #1 10 PIC MICROCONTROLLER Programming PIC uC - Programmer ICSP (In-Circuit Serial Programmer) is an interface used to program PIC uC PICkit 2 and 3 are used as Programmer and Debugger Programming pin configurations: Pin # Name Functions 1 MCLR Master Clear/Reset (Active Low) 2 VDD 5V Power 3 VSS 0V (Ground) Power 4 PGD Programming Data 5 PGC Programming Clock EET-323: Week #1 11 PIC MICROCONTROLLER Programming PIC uC - Software MPLAB x IDE is used to program a PIC uC. Compilers: mpasm= assembly language XC8 = C language Simple C programming could also be done on Code blocks (optional) or Notepad++ EET-323: Week #1 12 MEMORY ORGANIZATION ROM – Read Only Memory EET-323: Week #1 13 MEMORY ORGANIZATION RAM (Random Access Memory) RAM: - SFR (Special Function Register) - GPR (General Purpose Register) SFRs are dedicated to specific functions such as ALU Status, Port, Timer, ADC …etc. It has a fixed hardware address. Ex: STATUS 03h PORTA ? TRISA ? GPRs are used for data storage and scratch pad, 8 bits of data can be stored in each location or address. EET-323: Week #1 14 MEMORY ORGANIZATION Data Memory Map Bank 0 Bank 1 Bank 2 Bank 3 EET-323: Week #1 Page 19 of the Data Sheet 15 Registers CPU uses many registers to store data temporarily. WREG (Working Register) is the most important one which plays a role in processing data for Assembly language. WREG is used for all arithmetic and logic operations. WREG can only store 8 bits of data. Note: Any data larger than 8 bits must be broken into 8-bit chunks or byte (8 bit) size before it is able to process. EET-323: Week #1 16 REGISTERS Working Register (WREG) Arithmetic Logic Unit EET-323: Week #1 17 REGISTERS Status Register (STASTUS) - 1 The Status register contains the arithmetic status of the ALU, the Reset status and the bank select bits for data memory. The data memory is partitioned into 4 different banks (0-3) which contain the GPR and SFR. Bits RP1 (Status ) and RP0 (Status ) of STATUS Register are the bank select bits. (Ref. p21 of Data Sheet) You must select the proper bank to access/use the data memory. EET-323: Week #1 18 REGISTERS Status Register (STASTUS) - 2 ex) BSF STATUS, 5 ;access file registers bank 1 MOVLW 0xFF ;load value to WREG MOVWF TRISD ;make PORTD as input BCF STATUS, 5 ;access file registers bank 0 MOVF PORTD, W ;copy PORT D value to WREG MOVWF PORTB ;copy WREG value to PORT B BSF (Bit set file): Makes the particular bit (5 in this example) of the STATUS register 1 (set). In order to use TRISD register, Bank 1 is accessed. BCF (Bit clear file): Clears the bit 5 of the STATUS register. After tasks are done in bank 1, now Bank 0 is accessed in order to use PORT registers. EET-323: Week #1 19 REGISTERS TRIS and PORT Register - 1 PORT is an I/O point that allows microcontroller to interact with outside world. Some pins for these I/O Ports are multiplexed with an alternate function for the peripheral features on the device. TRIS determines PORT’s direction as input or output. Set TRIS bit (= 1) will make PORT’s pin input Clear TRIS bit (= 0) will make PORT’s pin output ex) TRISA = 0xFF ➔ PORTA = input TRISB = 0 x00 ➔ PORTB = output EET-323: Week #1 20 REGISTERS TRIS and PORT Register - 2 EET-323: Week #1 21 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 EET-323 Microcomputer Interfacing School of Engineering Technology & Applied Science (SETAS) Week # 3 Assembly Programming (Continued) Objectives Active high vs Active low Bit-Oriented opcodes Variable Declaration GOTO vs CALL instruction EET-323: Week #3 2 DIGITAL INPUTS Active Low and Active High Digital inputs on PIC uC can be configured/wired as “Active Low” or “Active High”. In an active low configuration, the device (pin) will be active when VSS (0v) is applied to it. In other words, when the pushbutton is pressed, the value on the input pin is ‘0’ and when it’s released, the value would be ‘1’ (5v). EET-323: Week #3 3 DIGITAL INPUTS Active Low and Active High In an active high configuration, the device (pin) will be active when VDD (5v) is applied to it. In most digital circuitries, active low signals are used to reduce errors caused by interference (noise). Note: Our lab-kit is configured/wired as active high. EET-323: Week #3 4 BIT-ORIENTED INSTRUCTIONS BCF and BSF – Bit Clear/Set File BCF f,b: is used to clear (force to 0) one particular bit (b) of the file register (f). ex) BCF PORTB,2 ;Bit #2 of PORTB is 0 BSF f,b: is used to set (force to 1) one particular bit (b) of the file register (f). ex) BSF TRISB,5 ;Bit #5 of PORTB set as input EET-323: Week #3 5 BIT-ORIENTED INSTRUCTIONS BTFSC – Bit Test File, Skip if Clear BTFSC f,b: It is used to check one particular bit (b) of file register (f); if particular bit is clear (0), it will skip the next instruction. ex) CHECK BTFSC PORTD,2 ;checks Bit #2 of PORTD and if it’s GOTO CHECK ;clear (zero), this line is skipped RETURN ;otherwise, it will continuously repeat EET-323: Week #3 6 BIT-ORIENTED INSTRUCTIONS BTFSS – Bit Test File, Skip if Set BTFSS f,b: It is used to check one particular bit (b) of file register (f); if particular bit is set (1), it will skip the next instruction. ex) LOOP BTFSS PORTB,3 ;checks Bit #3 of PORTB and if it’s GOTO LOOP ;set (1), this line is skipped. RETURN ;otherwise, it will continuously repeat EET-323: Week #3 7 Variable Declaration A Variable is a storage location that contains a value that can be changed depending on conditions or information passed to the program. Each variable is linked to a GPR (General Purpose Register) and can store up to 8 bits of information. [NAME] EQU [GPR ADDRESS] ex) #include COUNT0 EQU 0x20 ;COUNT0 is linked to GPR address - 0x20 COUNT1 EQU 0x21 ;COUNT1 is linked to GPR address - 0x21 COUNT2 EQU 0x22 ;COUNT2 is linked to GPR address - 0x22 ORG 0x00 ;program starts MAIN EET-323: Week #3 8 GOTO vs CALL Instruction Both GOTO and CALL opcodes are used to link to subroutines or instructions under different labels. However, they behave differently after they are done with the subroutine. GOTO simply ‘goes’ (up/down) to the specified label (subroutine) and continues from there when it’s done with the subroutine. Whereas, CALL ‘calls’ and uses subroutines, which are ended with RETURN instruction. It will return to the place and continue where it left off. EET-323: Week #3 9 GOTO VS CALL INSTRUCTION GOTO – Go to Address GOTO k: It is used to go to a an address [label]. ex) CHECK1 BTFSC PORTD,0 ;if Bit #0 of PORTD is pressed, GOTO INC ;it ‘goes’ to the subroutine, INC CHECK2 BTFSC PORTD,1 ;if Bit #1 of PORTD is pressed, GOTO DEC ;it goes to DEC GOTO CHECK1 ;if Bit #1 is not pressed, it will repeat INC INCF COUNT, 1 ;increases the variable COUNT by 1 GOTO CHECK2 ;goes back to CHECK2 DEC DECF COUNT, 1 ;decreases COUNT by 1 GOTO CHECK1 ;goes back to CHECK1 NOTE: PORTD is in active high configuration. EET-323: Week #3 10 GOTO VS CALL INSTRUCTION CALL – Call Subroutine CALL k: It is used to call and use a subroutine which ends with “RETURN” instruction. ex) CHECK1 BTFSC PORTD,0 ;if Bit #0 of PORTD is pressed, CALL INC ;it ‘calls’ the subroutine, INC CHECK2 BTFSC PORTD,1 ;if Bit #1 of PORTD is pressed, CALL DEC ;it calls and uses DEC GOTO CHECK1 ;if Bit #1 is not pressed, it will repeat INC INCF COUNT, 1 ;increases the variable COUNT by 1 RETURN ;indicates the end of subroutine DEC DECF COUNT, 1 ;decreases COUNT by 1 RETURN ;indicates the end of subroutine NOTE: PORTD is in active high configuration. EET-323: Week #3 11 CASE STUDIES Case Study - 1 Write a complete assembly program to turn a LED ON and OFF by using a Push Button. - LED (Output) is connected to RB1 (bit 1 of PORTB) - PB (Input) is connected to RD0 (bit 0 of PORTD) Electrical diagram: Assembly program: EET-323: Week #3 12 CASE STUDIES Case Study - 2 Write a completed assembly program for the following requirement: - When PB1 is held pressed, counter should count up - When PB2 is held pressed, counter should count down Electrical diagram: Assembly program: EET-323: Week #3 13