Home     Contact     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS     Terms of services     Privacy policy  
   
 Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science   



The TRIS and PORT registers

The PIC16f88 has 18 pins. Those are two sets of 8 I/O ports and the VDD / VSS pins for the power supply. Let's see how does Microchip draws the PIC pinout:

Except the power pins (#14 and #5), all others have more than one function. The first function that all pins have, is the RA or the RB function. This is the one that we will discuss at the moment.

Taking a closer look to the above picture, you will notice that there are actually two sets of 8 ports, the RA0 through RA7 and the RB0 through RB7. These are our two main PORT registers. They are called PORTA and PORTB and you will deal with them all the time.

So, what can you do with these ports? Suppose that you set the binary byte '11111111' to the PORTA register. If you have set the PORTA to be all outputs, then what will really happen is that all RA outputs of the PIC shall become '1'!




Setting the type of the port pins (Input or Output) - The TRIS register

Before you start playing with the port pins, you should declare their type. Each one can be Input or Output. There is also an analog state, but this will discussed on the advanced section. On power on of the PIC or after a reset, all ports become inputs. This is done to avoid driving an output pin directly to the VDD or VSS. To define the type of an I/O pin, you use the TRIS register. There are two TRIS registers, the TRISA and the TRISB that corresponds to the 8 pins of each port set, the A and the B. The TRIS registers are 8-bit registers. The LSB (Less significant byte) corresponds to pin #0 and the MSB (Most significant byte) to the pin #7. When one bit of this register is set to '1', the corresponding port pin shall become an input. When it is set to '0', then this port pin shall become an output. Let's see an example:

        bank1
        movlw b'00000001'
        movwf TRISA
      
        movlw b'11110000'
        movwf TRISB
        bank0

What someone would notice at the very first are the bank1 and bank0 macros. The TRIS registers are located in the bank 1. On the other hand, the PORT registers are located in the bank 0. I have experience many hours of hard mind-crashing debugging on my non-working programs, simply because i forgot to switch to bank 1 when setting the TRIS registers or because i forgot to switch back to bank 0. Once more, the bank 0 is the position with the most common registers. This is good because it saves you from switching between bank positions. But if you forger to check a register's bank position and fail to change bank, then funny results will happen

The above code, will run as follows: The first line will switch to bank 1. The second line will load the binary number '00000001' into the W register. The third line will write the contents of the W register (00000001) into the TRISA register. This will set the type of port pins for the RA as follows:

PIN # RA# PIN TYPE
17 RA0 INPUT
18 RA1 OUTPUT
1 RA2 OUTPUT
2 RA3 OUTPUT
3 RA4 OUTPUT
4 RA5 OUTPUT
15 RA6 OUTPUT
16 RA7 OUTPUT

The LSB bit of the number that was written into the TRISA register was '1', that is why the RA0 pin has become an INPUT. On the contrary, all other bits are '0', therefore, all other RA pins are outputs.

Before you see the following table, try to imagine what will happen to the RB port pins. According to the program, the byte '11110000' was written in the TRISB register:

PIN # RB# PIN TYPE
6 RB0 OUTPUT
7 RB1 OUTPUT
8 RB2 OUTPUT
9 RB3 OUTPUT
10 RB4 INPUT
11 RB5 INPUT
12 RB6 INPUT
13 RB7 INPUT

The first four bits are '0', therefore the pins RB0 through RB3 shall become OUTPUTS. The other four bits are '1' therefore the pins RB4 through RB7 are inputs.




Controlling the ports - The PORT registers

You have just learn how to set the type of the ports. Now you only need to learn how to control and use them. This is done by reading or writing bits to the PORT registers.




The output state

An output, is a port type where you can set HIGH or LOW ('1' or '0') it's state. If for example you wish to light an LED, you can connect it to one port and set it's state to '1' (HIGH). Suppose that we have the following code:

        bank1
        movlw b'00000001'
        movwf TRISA
        bank0

        movlw b'10000000'
        movwf PORTA

The first line will switch to bank1. The next two lines will set the type of the RA pins. Note the MSB bit. This is set to output, the RA7 is an output. Following we do not forget to switch back to bank 0. Then, we load the byte '10000000' to the PORTA register. If you notice the MSB bit, this is set to '1'. This means that the pin RA7, that is an output, will become HIGH. If an LED (and a resistor of course) was connected to this pin (pin #16), the LED would light. That is exactly the procedure how to set the port states. Alternatively, you can set or clear just one bit of a PORT register using the bsf and bcf instructions. The above program could have be written as follows:

        bank1
        movlw b'00000001'
        movwf TRISA
        bank0

        bsf PORTA,7

The fifth line is now changed. This line will set ('1') the seventh bit of the PORTA register. Two turn off this port, you need to set this bit to zero. You can do it either by writing with the movwf instruction a whole byte that has this bit 0 (for exmaple '00000000'), or by clearing this bit with the bcf instruction (bcf PORTA,7)




The input state

An input, is a port where you can read to your program the state of this pin. You can read this pin and find if it is conneted to HIGH or LOW state. Suppose that we have the following code:

        bank1
        movlw b'00000001'
        movwf TRISA
        bank0

        movf PORTA,W

The first line will switch to bank 1. The next two lines will set the RA pin types. The RA0 will become an input. We do not forget to switch back to bank 0. Then, at the fifth line, the PORTA register shall be read into the W register. Because the first (LSB) port was set to input, the LSB bit of the W register will become '1' IF the RA0 pin was connected to HIGH (VDD) or shall become '0' IF the RA0 was connected to LOW (VSS).

If for example a push button was conencted to this pin, we could read the state of the pushbutton simply by reading the corresponding bit of the PORT register.






Confirm your knowledge

There is an online test to check your knowledge on this page. You may reveal the test with the following button:






Previous page ---- Next page



Go back to the book contents

Go to the discussion forum of this book





 

Comments

  Name

  Email (shall not be published)

  Website

Notify me of new posts via email


Write your comments below:
BEFORE you post a comment:You are welcome to comment for corrections and suggestions on this page. But if you have questions please use the forum instead to post it. Thank you.


      

  • At 17 October 2014, 19:10:49 user Fung wrote:   [reply @ Fung]
    • For an input only pin, since it has no output driver, clearing the corresponding bit in TRISx may not affect its function because that bit is readable only.

      It states clearly in some models such as 16F685 and 16F887. TRISA3 in 16F685 and TRISE3 in 16F887 are always read as 1 and they are not writable, their corresponding port bits "RA3" and "RE3" are also readable only, depends on the input state.

      That means, writing 0x3F to port A in 16F685 will give 0x37 as result even TRISA is set to 0.

      But it did not state in 16F84A and 16F628A...


  • At 30 August 2013, 16:48:39 user Giorgos Lazaridis wrote:   [reply @ Giorgos Lazaridis]
    • @L. Abdul kadher I'm sorry i do not do circuits/programs on demand.


  • At 28 August 2013, 5:18:42 user L. Abdul kadher wrote:   [reply @ L. Abdul kadher]
    • Sir,
      i want a sample program for blinking led in pic 16f72 microcontroller, please reply fast


  • At 21 June 2012, 10:28:10 user Gham Tamu wrote:   [reply @ Gham Tamu]
    • How do i do this?

      Run a programme application that reads the digital inputs from PORTA and that outputs those digital inputs to PORTC so that the LEDs on PORTC mimic the input data.


  • At 12 June 2012, 6:53:07 user kamran alam wrote:   [reply @ kamran alam]
    • please i really need ur help sir. plz publish tutorial of pic16f877a or of 8051 micro controller.
      i'll be very thankful for this favor.

      Thanking you
      With Regards
      Kamran Alam
      India.(City-Indore)


  • At 12 June 2012, 6:32:48 user kamran alam wrote:   [reply @ kamran alam]
    • great job buddy


  • At 30 September 2010, 9:39:18 user Kammenos wrote:   [reply @ Kammenos]
    • What is it exactly that you want?


  • At 30 September 2010, 8:04:04 user farrukh waheed wrote:   [reply @ farrukh waheed]
    • i want help in this code :
      if some one demonstrate this so plz

      PORTA = %00000000
      PORTB = %00000000

      Dim Serdata As Byte

      start:

      SerIn PORTA.0 , 16780 , [Serdata]


      If Serdata = 1 Then
      PORTB.0 = 1
      DelayMS 2000
      PORTB.0 = 0
      EndIf

      If Serdata = 2 Then
      PORTB.1 = 1
      DelayMS 1000
      PORTB.1 = 0
      EndIf


  • At 5 September 2010, 14:15:15 user yadav wrote:   [reply @ yadav]
    • doing great job
      plz carry on
      thank you
     






    No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise without the prior written permission of the author.

    Read the Disclaimer


    All trademarks used are properties of their respective owners.
    Copyright © 2007-2009 Lazaridis Giorgos.
    All rights reserved.






     HOT in heaven!


  • Disclaimer
  • Book Contents
  • Discussion forum

  • Basics
  • What will you need
  • Choosing the right PIC
  • The MPLAB
  • Getting familiar with the MPLAB environment
  • Creating a new project
  • Open and close projects
  • Creating new files and including them in the project
  • Your very first assembly program
  • Compile a program and transfer to the PIC
  • Section 1: Beginner's theory
  • Memory Organization
  • The Data Memory Organization
  • The Program Memory Organization
  • The instructions
  • General knowledge about instructions
  • Value Loading Instructions
  • Program Flow Instructions
  • Mathematic Instructions
  • Logic Function Instructions
  • Bit Orientated Instructions
  • Byte Orientated Instructions
  • Miscellaneous Instructions
  • The Basic Special Function Registers
  • The Status Register
  • The Option_Reg Register
  • The TRIS and PORT registers
  • Beginner's PIC Tutorials
  • How to use our PIC Tutorials
  • A Pushbutton turning an LED on and off
  • A Simple LED Flasher
  • Interfacing Multiple Switches - The internal Pull-Up resistors
  • An LED Sequencer
  • Interface a Single 7seg Digit
  • Interface Multiple 7seg Digits
  • A 3-digits Decimal Counter
  • A Clever Button
  • Section 2: Intermediate theory
  • Instruction Cycle Duration and Calculated Delays
  • The Timer Modules - Timer0
  • The Timer Modules - Timer1
  • The Timer Modules-Timer2



  • NEW in heaven!



    New Theory: AC electric motor working principle






     Contact     Forum     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS   

    Site design: Giorgos Lazaridis
    © Copyright 2008
    Please read the Terms of services and the Privacy policy