A Microcontroller Discussion Board
September 07, 2010, 03:29:32 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: P89V51 code for SDCC is now ready to download
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: How to make an on/off LED button with PIC16F882..??  (Read 848 times)
0 Members and 1 Guest are viewing this topic.
LocoRoco88
Newbie
*
Posts: 1


View Profile
« on: September 08, 2008, 04:32:50 PM »

Hello,

I need to program an on/off button for my school project. The MCU that I am using is the PIC16F882. I need to program it in a way, that the PIC16F882 can read in an input signal and once it receives that signal, it should give an output signal that will turn the LED on. If I press the button again, then it should turn the LED off. It should do that for 6 different input/output.

The short version is: if I press a button, LED goes on. If I press the same button again, LED goes off. I have already written a code, but it doesn't really work. It only turns the LED on, but not off. So can anybody please help me?

Here is my code(I have only made it for one button,but the final code needs 6) :

#include <16F882.h>
#define BUTTON1 PIN_A1
#define LED1 PIN_B1
#fuses NOPROTECT,NOWDT,BROWNOUT,PUT

int x=0;

int main(){

      while(1){
         if (!input(BUTTON1))
       {x++;
       }
      if(x ==1){
      output_low(LED1);     
      }   
      if(x ==2){
      output_high(LED1);
      x=0;
      }
   return 0;
   }
}


Thank you very much  Smiley
Logged
A Microcontroller Discussion Board
« on: September 08, 2008, 04:32:50 PM »

 Logged
king_diaw
Freshy
*
Posts: 11


View Profile
« Reply #1 on: September 09, 2008, 08:05:24 AM »

hai boy, i'm ever do the same project with you, but i'm done using PIC16F84 and using assembly language.
hope this project can give u an idea.
here, i'm attach the flow chart and schematic diagram of my project for your reference

the program in assembly is shown below:
Code:
               LIST P=16F84
        LIST R=DEC
            #INCLUDE <P16F84.INC>

 ;===================== RESET VECTOR ============================
            ORG     0X000
PAGE0 GOTO    MAIN
X3 EQU H'11'
X2 EQU H'12'
X1 EQU H'13'
     
;=====================Initialize I/O Port========================           
MAIN MOVLW 0X00
TRIS PORTB
MOVLW 0XFF
TRIS PORTA
CLRF PORTA
CLRF PORTB
;main Program
AGAIN BTFSS PORTA,0
CALL LED1
BTFSS PORTA,1
CALL LED2
GOTO AGAIN

LED1 BTFSS PORTB,0
GOTO ONLED1
BCF PORTB,0
CALL DELAY
RETURN
LED2 BTFSS PORTB,1
GOTO ONLED2
BCF PORTB,1
CALL DELAY
RETURN

ONLED1 BSF PORTB,0
CALL DELAY
GOTO AGAIN

ONLED2 BSF PORTB,1
CALL DELAY
GOTO AGAIN
;===========================DELAY subroutine======================
DELAY                 
            MOVLW       D'50'
            MOVWF       X3
      MOVLW       D'55'
            MOVWF       X2
            MOVLW      D'60'
            MOVWF       X1
            DECFSZ      X1
            GOTO        $-1
            DECFSZ      X2
            GOTO        $-5
            DECFSZ      X3
            GOTO        $-9 
RETURN

END
Logged
A Microcontroller Discussion Board
   

 Logged
Pages: [1]
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
How make the burner to burn PIC program's PIC AzkA 4 1213 Last post February 04, 2009, 10:31:59 PM
by gogo2520
Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC | Sitemap Valid XHTML 1.0! Valid CSS!