Department of Electronic & Electrical Engineering Lecture 3. ● Template program. ● Introduction to IO ● PORTA PORTB TRISA TRISB ● Using a subroutine ● call and retlw instructions ● lookup table ● Driving a 7seg display
Department of Electronic & Electrical Engineering Background reading Quintessential PIC. Chapter 11 has lots of (too much?) detail that we skip in the lecture.
Department of Electronic & Electrical Engineering Template Program #include P16F84A.INC __config _XT_OSC & _WDT_OFF & _PWRTE_ON ORG h'0' ; PROGRAM IN HERE END Useful constants Sets some hardware configuration bits Program starts at address 0 Tell assembler about the end of your program In the notes we often miss out some of this stuff comment
Department of Electronic & Electrical Engineering Input and output TRISA TRISB PORTA PORTB REGISTER BANKS STATUS REGISTER and RP0
Department of Electronic & Electrical Engineering
Input and output (from PIC16F84A sheet) PORTA is a 5-bit wide, bi-directional port. The corresponding data direction register is TRISA. Setting a TRISA bit will make the corresponding PORTA pin an input (i.e., put the corresponding output driver in a Hi-Impedance mode). Clearing a TRISA bit will make the corresponding PORTA pin an output (i.e., put the contents of the output latch on the selected pin)
Department of Electronic & Electrical Engineering PORTA and TRISA (addressing)
RPO Selects the bank 7 bits from opcode Don't worry about this yet! Address Register Memory Map
Department of Electronic & Electrical Engineering MOVWF instruction. 7 bits for register address opcode operand
Department of Electronic & Electrical Engineering Registers and banks. There are only 7 bits in an instruction for addressing the registers but we really need 8 bits ! Solution: One of the bits in the STATUS is used as a bank select.
Department of Electronic & Electrical Engineering
Selecting a data bank To select a data bank we must set/clear RP0 in the STATUS register. BCF STATUS,RP0 ; CLEAR RP0 selects BANK 0 BSF STATUS,RP0 ; SET RP0 selects BANK 1
Department of Electronic & Electrical Engineering
Configuring PORTA (from data sheet) (start of program not shown)
Department of Electronic & Electrical Engineering Literal means the data (operand) is contained in the instruction. There is no instruction to move a literal directly to a file register. We need to use MOVLW first then move W to the register.
Department of Electronic & Electrical Engineering Note: STATUS RP0 etc are symbolic constants defined in PIC16F84A.INC for example RP0 has the value 5 0x0F is a hexadecimal constant All the following are equivalent in MAPLABX (mpasm.exe) 0x0F ; hexadecimal H'F' ; hexadecimal B' ' ; binary D'15' ; decimal 15
Department of Electronic & Electrical Engineering Example: copying data from portA to portB bsf STATUS,5 ;select bank 1 movlw B' ' ;set up port B as all outputs movwf TRISB movlw B' ' ;set up port A 0-3 as inputs movwf TRISA bcf STATUS,5 ;select bank 0 loop movfw PORTA ; input PORTA movwf PORTB ; output to PORTB goto loop ; loop forever end
Department of Electronic & Electrical Engineering Labels and goto. In the previous example loop is an label (not an instruction) it is just a marker (the assembler knows the address of the next instruction. GOTO label is an instruction that makes the program execution goto the instruction that follows the label.
Department of Electronic & Electrical Engineering Seven Segment LED Display
Department of Electronic & Electrical Engineering Seven Segment Display Internal Circuit Common Anode
Department of Electronic & Electrical Engineering Circuit for 7seg display
Department of Electronic & Electrical Engineering Binary seven segment display ● We have 4 bits of input (hex number). ● We want to display the corresponding character on the 7seg display. ● Each segment can be represented by a bit of a binary number. ● The problem is to lookup the correct 7seg code for each 4bit number. ● This can be done using a lookup table.
Department of Electronic & Electrical Engineering Lookup table using programmed return. ; setup stuff here..... ; w contains the 4 bit number movl 4,w call conversion ; w now contains the 7 segment code ;....more stuff here ; convert 4bit number in w into a seven segment code. conversion: ; this is just a label for the assembler addwf PCL ; add w to the PC (jump) retlw B' ' ; 0 return 7 seg code. retlw B' ' ; 1 retlw B' ' ; 2 retlw B' ' ; 3 retlw B' ' ; 4
Department of Electronic & Electrical Engineering Demo Do a demo now Dr Leonard.... If there is time?