Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

MPLAB switches and leds

Status
Not open for further replies.

ktkoota

New Member
:( anybody can help me with this code on MPLAB7, when switch is pressed led should light and when another switch is pressed the other led must light and any switch is pressed the led must light even if more than one switch is pressed,, please try with me or if you have anycode help..
code:
#include "pic18.h"
#include "config.h"
#include "delay.h"

main(void){

TRISB = 0x0F; //RB4,RB5,RB6 and RB7 are outputs, others inputs

RB4 = 0; // turn LED off
RB5 = 0; // turn LED off
RB6 = 0; // turn LED off
RB7 = 0; // turn LED off

while(1)
{
while(RB3 &&RB2 && RB1 && RB0);
if(RB3 && RB2 && RB1 && !RB0)
{
RB4=1;
//RB5=0;
//RB6=0;
//RB7=0;
while(RB3 &&RB2 && RB1 && !RB0);
}
//---------------------------------------------------------------------
if(RB3 &&RB2 && !RB1 && RB0)
{
//RB4=0;
RB5=1;
//RB6=0;
//RB7=0;
while(RB3 &&RB2 && !RB1 && RB0);
}
//-------------------------------------------------------------------------
//---------------------------------------------------------------------
if(RB3 &&RB2 && !RB1 && !RB0) //12
{
RB4=1;
RB5=1;
//RB6=0;
//RB7=0;
while(RB3 &&RB2 && !RB1 && !RB0);
}
//-------------------------------------------------------------------------
//---------------------------------------------------------------------
if(RB3 && !RB2 && RB1 && RB0)
{
//RB4=0;
//RB5=0;
RB6=1;
//RB7=0;
while(RB3 &&!RB2 && RB1 && RB0);
}
//-------------------------------------------------------------------------
//---------------------------------------------------------------------
if(RB3 &&!RB2 && RB1 && !RB0)
{
RB4=1;
//RB5=0;
RB6=1;
//RB7=0;
while(RB3 &&!RB2 && RB1 && !RB0);
}
//-------------------------------------------------------------------------
//---------------------------------------------------------------------
if(RB3 &&!RB2 && !RB1 && RB0)
{
//RB4=0;
RB5=1;
RB6=1;
//RB7=0;
while(RB3 &&!RB2 && !RB1 && RB0);
}
//-------------------------------------------------------------------------

//---------------------------------------------------------------------
if(RB3 &&!RB2 && !RB1 && !RB0)
{
RB4=1;
RB5=1;
RB6=1;
//RB7=0;
while(RB3 &&!RB2 && !RB1 && !RB0);
}
//-------------------------------------------------------------------------
//---------------------------------------------------------------------
if(!RB3 &&RB2 && RB1 && RB0)
{
//RB4=0;
//RB5=0;
//RB6=0;
RB7=1;
while(!RB3 &&RB2 && RB1 && RB0);
}
//-------------------------------------------------------------------------
//---------------------------------------------------------------------
if(!RB3 && RB2 && RB1 && !RB0)
{
RB4=1;
//RB5=0;
//RB6=0;
RB7=1;
while(!RB3 &&RB2 && RB1 && !RB0);
}
//-------------------------------------------------------------------------
//---------------------------------------------------------------------
if(!RB3 &&RB2 && !RB1 && RB0)
{
//RB4=0;
RB5=1;
//RB6=0;
RB7=1;
while(!RB3 &&RB2 && !RB1 && RB0);
}
//-------------------------------------------------------------------------
//---------------------------------------------------------------------
if(!RB3 &&RB2 && !RB1 && !RB0)
{
RB4=1;
RB5=1;
//RB6=0;
RB7=1;
while(!RB3 &&RB2 && !RB1 && !RB0);
}
//-------------------------------------------------------------------------
//---------------------------------------------------------------------
if(!RB3 &&!RB2 && RB1 && RB0)
{
//RB4=0;
//RB5=0;
RB6=1;
RB7=1;
while(!RB3 &&!RB2 && RB1 && RB0);
}
//-------------------------------------------------------------------------
//---------------------------------------------------------------------
if(!RB3 &&!RB2 && RB1 && !RB0)
{
RB4=1;
//RB5=0;
RB6=1;
RB7=1;
while(!RB3 &&!RB2 && RB1 && !RB0);
}
//-------------------------------------------------------------------------
//---------------------------------------------------------------------
if(!RB3 &&!RB2 && !RB1 && RB0)
{
//RB4=0;
RB5=1;
RB6=1;
RB7=1;
while(!RB3 &&!RB2 && !RB1 && RB0);
}
//-------------------------------------------------------------------------
//---------------------------------------------------------------------
if(!RB3 &&!RB2 && !RB1 && !RB0)
{
RB4=1;
RB5=1;
RB6=1;
RB7=1;
while(!RB3 &&!RB2 && !RB1 && !RB0);
}
//-------------------------------------------------------------------------


}
}
 
You might want to use a small time delay between switch samples to reduce the effects of switch bounce. Then use a switch state latch or switch state memory bit for each switch which allows you to use simple logic to detect a "new press" state while ignoring the other switch states. Exclusive-Or a "new press" bit with a "flag" bit to toggle the "flag" bit from on-to-off or from off-to-on.

If there's a direct relationship between your RB3..RB0 inputs and the RB7..RB4 outputs then you could simply copy the bit3..bit0 "flag" bits onto your RB7..RB4 outputs at the end of each 20-msec sample interval.

Code:
  while(1)
  { 
    delay_ms(20);              // 20-msec debounce delay
    swnew = ~portb;            // sample active lo switches
    swnew &= 0x0F;             // on RB3..RB0
    swnew ^= swold;            // changes, press or release
    swold ^= swnew;            // update switch state latch
    swnew &= swold;            // filter out "new release" bits
    sflag ^= swnew;            // toggle switch flag bits
    portb = sflag << 4;        // copy b3..b0 flag bits onto RB7..RB4 outputs
  }
Now if you want to get really fancy you can add "new press" beep feedback with just a few instructions;

Code:
  while(1)
  { 
    delay_ms(20);              // 20-msec debounce delay
    swnew = ~portb;            // sample active lo switches
    swnew &= 0x0F;             // on RB3..RB0
    swnew ^= swold;            // changes, press or release
    swold ^= swnew;            // update switch state latch
    swnew &= swold;            // filter out "new release" bits
    if(swnew)                  // if any "new press" bits
    { for(i = 0; i < 32; i++)  // do a 32-msec 500-Hz "new press" beep
      { porta ^= 1 << spkr;    // toggle speaker pin
        delay_ms(1);           // at 1-msec intervals for 500-Hz
      }
    }
    sflag ^= swnew;            // toggle switch flag bits
    portb = sflag << 4;        // copy b3..b0 flag bits onto RB7..RB4 outputs
  }
 
Last edited:
can you please add your code to mine(if it is correct) and tell me the results on simulation(pic18242)..
Today it is the first day to use MPLAB and I'm given this task.. can you please help :(
 
I'm working on a tutorial which may help you understand the multi-switch logic used in the examples I posted. Please look for it in coming weeks.

Regards, Mike
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top