Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

stop toggle LED switch 18f877

Status
Not open for further replies.

ismild

Newbie level 1
Newbie level 1
Joined
Jun 20, 2016
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
17
stop toggle LED switch 16f877

Hello everyone
I'm beginner for this and sorry for my english language

I need a pic 16f877a program in PIC C Complier
one switch control the led On at time one press
next press the LED will OFF
but my problem is when i press and hold the switch button, it's toggle

" how can i fix this code for stop toggle LED when i press and hold the button "

this is my code

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <16F877.h>
#device adc=8
 
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES WRT                      //Program Memory Write Protected
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOPROTECT                //Code not protected from reading
 
#use delay(clock=20000000)
#byte  TRISA = 0x85
#byte  TRISB = 0x86
#byte PORTA = 0x05
#byte PORTB = 0x06
#bit LED_RED = PORTA.0
#bit PUSH_SWITCH = PORTB.1
 
void main()
{
  
   TRISB = 0b00000010;
   TRISA = 0b00000000;
   LED_RED = 0;
   
 
   while(TRUE)
   {
      if (PUSH_SWITCH == 0)
      {
      
       LED_RED ^= 1;
      }
      else
      {
 
      }
   }
}

 

What is missing is a check that the push switch has been released. Your code only checks for it being pressed at the moment.

I will leave the coding to you but the idea is to do this:
Code:
if(PUSH_SWITCH == 0)
{
if(switch_was_down == FALSE)
{
LED_RED ^= 1;
switch_was_down = TRUE;
}
else switch_was_down = FALSE;
}

So you use a new variable 'switch_was_down' to remember if the switch is already pressed.

Brian.
 

Hi,

since your condition is within while loop, it will be keep on executing. Better use switch input as interrupt. So when you press then only control will go to that function and execute only once till you press again.



Amit
 
Hi,

--> remember the last_state of the switch.

--> (pseudo code):
Code:
* new_state = switch
* if (last_state = inactive) & (new_state = active) then....do something
* last_state = new_state

(this is called reacting on the "edge", instead of reacting on the "state" or "level".)


Klaus
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top