PIC Tutorial - A Pushbutton turning an LED on and off
This is a circuit for understanding the ports. The LED connected to the RB0 port of the PIC will simply turn on and off when the user presses or releases the pushbutton connected on the RA0 port of the PIC accordingly.
The circuit is very simple. When the pushbutton (NO) is released, the input RA0 is taken LOW through the 10K resistor. When the pushbutton is pressed, the RA0 is taken HIGH through the pushbutton. The rest are taken care from the program of the PIC. Following is the assembly listing. Bellow is the PIC code of this tutorial. You will only find a part of the tutorial that is interesting for our discussion. The rest can be found attached at the end of this page.
; Main Program ------------------------------------------------------------ Start bank1 ;Go to bank 1 movlw b'11111111' ;Set the port pin types of the RA movwf TRISA ;All RA ports are inputs movlw b'11111110' ;Set the port pin types of the RB movwf TRISB ;All RB ports are input except RB0 that is output bank0 ;Go to bank 0 MainLoop btfss PORTA,0 ;Read RA0 pin goto Button_Is_Off ;If RA0 is '0', then goto Button_Is_Off Button_Is_On bsf PORTB,0 ;Set RB0 output goto MainLoop ;And go back to the Main Loop Button_Is_Off bcf PORTB,0 ;Clear RB0 output goto MainLoop ;And go back to the Main Loop
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.
It may look a little bit complicated but it is not. Everything up to the comment line 'Main Program' are bits and bytes to initialize and set the PIC into operation. There is also this 'Init_normal.inc' header file included along with the standard 'P16F88.inc'. If you are ne to programming, just do not give them any attention and include them as is. I will explain the code from the 'Main Program' and under.
The first thing to do is to set the RA and RB pin port types. I start with the RA port setupr. To access the TRISA register, i need to change to bank 1. Then i load the byte '11111111' in the TRISA register. With this way, i make all the RA pins inputs. Remember that i need one input for the pushbutton. If you look in the schematic, this input shall be the RA0 pin for me.
Then i declare the RB port pin types. For this, i load the byte '00000000' to the TRISB register. This way, i declare all RB port pins as outputs. I could have load the '11111110' as well, because i need only the RB0 pin to be an output.
When bith port pin types are declared, i switch back to bank 0, the place where the most commonly used registers are located.
Next, it is time for the conditional loop. I check the RA0 state. The btfss PORTA,0 does this work. If the button is NOT pressed, then the RA0 will be zero. If so, then the command right after the btfss PORTA,0 shall be executed. This command is a goto command and shall send the program flow to the Button_Is_Off label. Under this label, there is a bcf PORTB,0 command that will simply clear the RB0 output and the LED shall be turned off. The next command (goto MainLoop) will send the program flow back to the MainLoop label and the program will re-check the RA0 again
If the button is pressed, then the RA0 input shall be '1'. When the program flow reaches the btfss PORTA,0 under the MainLoop label, then the very next command shall be skipped. This means that the program flow shall NOT jump to the label 'Button_Is_Off'. Instead, the command 'bsf PORTB,0' shall be executed. This command will set the RB0 output and the LED will light. The next command (goto MainLoop) will send the program flow back to the MainLoop label and the program will re-check the RA0 and so on and so on...
The above described routine shall loop infinitely.
The project Files
Following are the files for this project:
Previous page ---- Next page
Go back to the book contents
Go to the discussion forum of this book
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.
Contact Forum Projects Experiments Circuits Theory BLOG PIC Tutorials Time for Science RSS
Site design: Giorgos Lazaridis
© Copyright 2008