Department of Electronic & Electrical Engineering Template program. Input / Output (IO) ● Template program. ● Introduction to IO ● Setting up for IO ● PORTA PORTB (IO ports) ● TRISA TRISB (direction IN/OUT ) ● Register banks ● GOTO statement
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 I often miss out some of this stuff to fit examples on a single slide! 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 Registers and banks. There are only 7 bits in an instruction for addressing the registers. TRISA is ‘H’85 or B’ ’ But we need 8 bits ! Solution: One of the bits in the STATUS is used as a bank select. (Most significant bit of the address)
Department of Electronic & Electrical Engineering PORTA and TRISA (addressing) BANK 0BANK 1
RPO Selects the bank 7 bits from opcode Don't worry about this yet! Address Register Memory Map
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 Notes and mpasm conventions: 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 -> W movwf PORTB ; output W -> 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.