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   



PIC Tutorial - Interfacing Multiple Switches - The internal Pull-Up resistors
Before reading this tutorial, make sure you have read the How to use our PIC Tutorials page!



The circuit on a breadboard.

The very first tutorial was how to interface a pushbutton. This circuit will interface 4 switches which are able to operate simultaneously 4 different LEDs. Instead of switches you can use pushbuttons as well. The purpose of this tutorial is to become familiar with interfacing switches and pushbuttons and the use of the internal Pull-Up resistors.


In Action



The circuit




Seeing the circuit, the title of this page starts making sense. Normally, there should be pull-up resistors between the switches and the input pins of the PIC, in our case the ports RB0 through RB3. The circuit above does not contain any pull-up resistor. So, what happens when a switch is not actuated? Is the corresponding pin floating in an unknown state?

Actually, no. The PIC 16F88 (and not only) are equipped with internal software-activated pull-up resistors. Those resistors are connected to each one of the 8 ports of the PORT B. They are controlled from the software and can be turned on or off all together from the OPTION_REG register. The bit to control them is the 7th bit and it is a .NOT. bit. This means that if the bit is set, the resistors are NOT enabled. If the bit is 1, the resistors are enabled. For more information about the OPTION)REG registers, see the corresponding page ( The OPTION_REG Register ).

The first 4 ports of PORTB (RB0 through RB3) are set as Inputs and the other 4 (RB4 through RB7) are set as Outputs. Each of the PORTB pins has it's own pull-up resistor. The pull-up resistors are only available for the PORTB pins that are set as input. This means that the first 4 PORTB pins do have a pull-up resistor, but the other 4 pins, that are outputs to the LEDs, does NOT have a pull-up resistor enabled.




The Code

Let's take a look at the code

; Main Program ------------------------------------------------------------
Start
				bank1					;Go to bank 1
				movlw b'11111111'		;
				movwf TRISA				;Set the port pin types of the RA

				movlw b'00001111'		;
				movwf TRISB				;Set the port pin types of the RB
				bank0					;Go to bank 0

				bank1
				bcf option_reg,NOT_RBPU	;Enable RB Port Pull-Up resistors
				bank0
MainLoop
				btfss Switch1
				goto Switch_1_Is_0
				bcf Output1
				goto Check_Switch_2
Switch_1_Is_0
				bsf Output1

Check_Switch_2
				btfss Switch2
				goto Switch_2_Is_0
				bcf Output2
				goto Check_Switch_3
Switch_2_Is_0
				bsf Output2
Check_Switch_3
				btfss Switch3
				goto Switch_3_Is_0
				bcf Output3
				goto Check_Switch_4
Switch_3_Is_0
				bsf Output3
Check_Switch_4
				btfss Switch4
				goto Switch_4_Is_0
				bcf Output4
				goto MainLoop
Switch_4_Is_0
				bsf Output4
				goto MainLoop

At the end of this tutorial, you will find all the files including the above asm file in one zip to download and test it.


What's interesting to notice in the above code, is how the Pull-Up resistors are enabled. After the PORT type setup, at the beginning of the code, there is this line 'bcf option_reg,NOT_RBPU'. This eill clear the 7th bit of the OPTION_REG register and thus, the pullup resistors shall be enabled for the PORTB pins that are set as inputs.

The rest of the code is composed by 4 similar subroutines to check the condition of an input and set the corresponding output accordingly:

				btfss Switch1
				goto Switch_1_Is_0
				bcf Output1
				goto Check_Switch_2
Switch_1_Is_0
				bsf Output1
Check_Switch_2
				.
				.
				.

The first line will check the input pin. If the pin is set, then the program flow will skip the next line and will not execute the following instruction. The next instruction to be executed will clear the corresponding output, and afterwards, a GOTO instruction to the next input check is executed. If the input was NOT set and the next instruction was excecuted, the program flow would GOTO label 'Switch_1_Is_0'. Then, the corresponding output would be SET and the program flow would continue normally to the next input check.

You should notice that an output is set when an input is clear. The output is inversed. This is done on purpose because of the pull-up resistors and the type of the switch used. The switches are Normal Open contacts. This means that when they are NOT actuated, the input pin is pulled high due to the pull-up resistors. When the switch is actuated, then the pin is driven low through the switch. You should of course implement the turn-on/off process as will.




The project Files

Following are the files for this project:

 PIC Tutorial - Interfacing Multiple Switches - The internal Pull-Up resistors











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.


      

No comment yet...

Be the first to comment on this page!
 






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