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.

Led blink using Switch Button

Status
Not open for further replies.

Fan174

Member level 2
Joined
Mar 27, 2018
Messages
51
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
413
I want to blink Led when I press switch button

PIC16F877A
Compiler MPLABX

This is my code
Code:
// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#define _XTAL_FREQ 16000000

#include <xc.h>

int main()
{
  TRISB0 = 0; //RB0 as Output PIN
  TRISD7 = 1; //RD7 as Input PIN

  RB0 = 0; //LED Off

  while(1)
  {
    if(RD7 == 0) //If Switch Pressed
    {
      RB0 = 1; //LED ON
      __delay_ms(3000); //3 Second Delay
      RB0 = 0; //LED OFF
    }
  }
  return 0;
}

Output report
BUILD SUCCESSFUL (total time: 5s)
Loading code from E:/shyam/1. Embedded System/Project/PIC/Switch/Switch1/Switch1.X/dist/default/production/Switch1.X.production.hex...
Loading completed

But when I press switch button my Led doesn't blink it stay only ON. Please someone help me
 

Attachments

  • Connection.jpg
    Connection.jpg
    488 KB · Views: 113

But when I press switch button my Led doesn't blink it stay only ON.
That's exactly what you have coded! There's no delay after LED OFF, it's turned on immediately again.
 
That's exactly what you have coded! There's no delay after LED OFF, it's turned on immediately again.

Thank its working
Code:
int main()
{
  TRISB0 = 0; //RB0 as Output PIN
  TRISD7 = 1; //RD7 as Input PIN

  RB0 = 0; //LED Off

  while(1)
  {
    if(RD7 == 0) //If Switch Pressed
    {
      RB0 = 1; //LED ON
      __delay_ms(3000); //3 Second Delay
      RB0 = 0; //LED OFF
      __delay_ms(3000); //3 Second Delay
    }
  }
  return 0;
}

- - - Updated - - -

When I press button led should be ON else led should be OFF

Code:
int main()
{
  TRISB0 = 0; //RB0 as Output PIN
  TRISD7 = 1; //RD7 as Input PIN

  RB0 = 0; //LED Off

  while(1)
  {
    if(RD7 == 1) //If Switch Pressed
    {
      RB0 = 1; //LED ON
    }
    else
    {  
      RB0 = 0; //LED OFF
    }
  }
  return 0;
}

But when I press switch button LED doesn't turn ON forever. its turn ON for some time and automatically turn OFF.

I want led ON when I press button else it should be turn OFF
 
Last edited:

Is pin RD7 pulled the opposite way to the switch? In other words, if the switch goes to VSS is there a pull-up resistor to VDD or alternatively, if the switch goes to VDD is there a pull-down resistor to VSS? If you haven't used a resistor to make it revert to the opposite logic level, add one, I suggest 10K.

Brian.
 

Also you need to debounce the switch. You will not be seeing the effect of the switch/button contact bounce with this code but it will be happening - just too fast for your eye to detect.
Handling contact bounce is a very good learning exercise for beginner programmers and there are lots of examples on the Internet. It is a good habit to get in to before you try to make a 'real' application that uses a switch and get yourself into problems.
Susan
 

Is pin RD7 pulled the opposite way to the switch? In other words, if the switch goes to VSS is there a pull-up resistor to VDD or alternatively, if the switch goes to VDD is there a pull-down resistor to VSS? If you haven't used a resistor to make it revert to the opposite logic level, add one, I suggest 10K.

Brian.

Please look at connection
 

Attachments

  • IMG_20150101_060713.jpg
    IMG_20150101_060713.jpg
    499.4 KB · Views: 102

The picture doesn't help I'm afraid, we would have to see the schematic for the board.

The point is: to make the program work, the logic level on RD7 has to change from low to high. The RD7 pin is just an input, it doesn't have a default state so if you leave it floating (disconnected) it could assume either logic level or drift between them. If your switch connects RD7 to VDD and you press it, you can be sure that RD7 is at high logic level but releasing the switch doesn't make it go to a low level, it just lets it float.

What you need to do is ensure there is something in the circuit that guarantees the logic level changes when the switch is pressed and reverts to it's previous level when the switch is released. If pressing the switch links RD7 to ground, you need to add a resistor (10K suggested) from RD7 to VDD. If the switch links RD7 to VDD you need the opposite, a resistor from RD7 to VSS. The resistor value isn't critical because the input pin draws very little current but if you make it very low (say < 100 Ohms) all you do is make the circuit draw more current when the switch is closed.

From the photograph it isn't possible to tell if there is already a resistor or whether it goes to VSS or VDD. Did the board come with a schematic?

Brian.
 

The picture doesn't help I'm afraid, we would have to see the schematic for the board.

From the photograph it isn't possible to tell if there is already a resistor or whether it goes to VSS or VDD. Did the board come with a schematic?

Brian.
I have only this connection
 

Attachments

  • Button_connection.jpg
    Button_connection.jpg
    18.8 KB · Views: 100

That helps!

Each switch connects a pin on CN9 to ground and the resistor in RP1 pulls it high. That means the pin is high until the switch is pressed and it goes low only while the switch is held down (ignoring bounce).

The final program in post #3 should work but if it still has the line '#pragma config WDTE = ON' it will automatically reset after a short time. This might be what you are seeing when the LED changes by itself. The watchdog timer (WDT) is a free running oscillator that is internally connected to the PICs reset line. On that PIC you can only enable or disable the reset when you program the PIC, it can't be done from inside the code. The "#pragma" embeds the enable/disable information in the hex file so it gets loaded into the PIC when you program it.

The WDT counts upward by itself and if it reaches maximum count and overflows, the reset is triggered. If you want to keep it enabled you will have to make sure it can never overflow by resetting it periodically or by disabling it in the #pragma directive. The idea is that your program, whatever it is, makes sure WDT gets reset as it runs, then if your program crashes and it is allowed to overflow, it resets the PIC automatically.
Try this:
Code:
int main()
{
  TRISB0 = 0; //RB0 as Output PIN
  TRISD7 = 1; //RD7 as Input PIN

  RB0 = 0; //LED Off

  while(1)
  {
    if(RD7 == 1) //If Switch Pressed
    {
      RB0 = 1; //LED ON
    }
    else
    {  
      RB0 = 0; //LED OFF
    }

   [COLOR="#FF0000"] CLRWDT();[/COLOR]
  }
  return 0;
}
so the watchdog is reset each time the loop executes.

Brian.
 

That helps!

so the watchdog is reset each time the loop executes.

Brian.
thanks Brian. I tried that program but there is no change. I tried with different ports and pin but no luck
 

What exactly is it doing now. With the last program including the watchdog reset, the LED should light whenever the switch is held down and go off as soon as it is released.

Brian.
 

If it helps, my *guess* is that the board is this one - **broken link removed** - and there is a link on that page to the schematic.
The switch connections are as above (looks like it was taken from the schematics PDF) but the LEDs are simply connected from the CN4 connector to ground.
Therefore looking at the picture and the code, RB0 needs to be high to light the LED and low to turn it off.
HOWEVER there also needs to be a resistor in there if you are not to possibly destroy the output. Looking at the data sheet for the PIC16F877A, the output pin is (effectively) taken high and low by a couple of FETs. The maximum current that can be sourced/sunk by these pins is 25mA. By placing the LED directly on the pin, I suspect you may have destroyed the FET that pulls up the pin voltage by exceeding the current that it can supply.
I would suggest that you use a 270 to 470 ohm resistor between another output port pin and another LED on the board ad see if that works.
Susan
 

Hi,

I think it is a switch bouncing issue.

I just tried it out with mikroC and encountered the same problem. It's solved now. If you need the mikroC code, I can upload it here.

You actually need to have the output look at a dedicated bit in the code. I do not know a thing about mplab but I think it could give an idea on how to go about it.
 
Last edited:

Switch bounce plays no part in this problem, it only matters when the number of switch closures 'mis-clocks' something but here it is simple case of on or off that matters.

Having seen Susan's reply and the full schematic, there does seem to be an oversight on the board design. As she says, connecting the LED directly to the PIC might have damaged it but more likely, the excessive current it would draw through the port pin would crash the PIC. This might account for the long delay before the LED changed by itself, it would be the output driver in the PIC overheating and eventually either giving up or corrupting some other part of the silicon.

I suggest you make up another patch wire but add a 470 Ohm resistor in line with it to limit the LED current.

Brian.
 

Here's how I tackled it:


Code:
void main()
{

     bit switch_bounce;
     unsigned char lamp_status;
     unsigned char input_internal = 1;

    // unsigned char Switch;
     TRISD = 0;      //Set the 1st 7bits of PORTB as Output
     TRISB4_bit = 1;    //Set RB4 as Input
     LATD = 0;   //Turn off all LEDs
     switch_bounce = 0;
     lamp_status = 0x00;
      
   do  {
        if (lamp_status == 0x00)
           {
            if ((switch_bounce == 0x00) && Button(&PORTB, 4, 37, 0 ))
               {
                lamp_status = 0xFF;
                switch_bounce = 1;
                delay_ms(5);
                switch_bounce = 0;
                }
            if ((switch_bounce == 0) && Button(&PORTB, 4, 37, 0))
               {
                   LATD = lamp_status;

                //if (oldstate && Button(&PORTB, 4, 37, 1)){
                }
           }
          
        if (lamp_status == 0xFF)
           {
            //input_internal = PORTB.RB4;
            if((switch_bounce == 0) && Button(&PORTB, 4, 37, 1 ))
               {
                lamp_status = 0x00;
                switch_bounce = 1;

                delay_ms(5);
                switch_bounce = 0;
                }
            if ((switch_bounce == 0) && Button(&PORTB, 4, 37, 1))
               {

                LATD = lamp_status;

                }
            }
        }while(1);
}

- - - Updated - - -

Hi betwixt,

You may be right. I learn a whole lot from all of you here on the forum day after day.

It's just something I tried out and it seemed to solve the problem code-wise. Except the device is damaged like you said, the OP might just give it a try. If it's actually damaged, then he'll have to go with replacing it. I'd recommend he should just try it out.
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top