LED switch in stk600 with atemega2560

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Hello everyone,

 

I'm a beginner and use stk600 with atemega2560. Switch is connected with PORTD and LED is connected with PORTB.

 

if each switch is down illuminate the led properly. but the problem is when the switch is up turn still the LED on.

 

I want to make LED off once switch up.

 

Code is followed as:

 

#include <avr/io.h>

 

unsigned char val=0;  //senses the switch pressed

 

int main(void)

{

 DDRB=0xff;           //define PORTB as output

  

 while(1)

 {

    if(!(PIND & 0x01))

    

           val=0xfe;

         

    else if(!(PIND & 0x02))

    

           val=0xfd;

         

    else if(!(PIND & 0x04))

    

           val=0xfb;

         

    else if(!(PIND & 0x08))

    

           val=0xf7;

         

    else if(!(PIND & 0x10))

    

           val=0xef;

         

    else if(!(PIND & 0x20))

    

           val=0xdf;

         

    else if(!(PIND & 0x40))

    

           val=0xbf;                                               

    

    else if(!(PIND & 0x80))

    

           val=0x7f;

    

    switch(val)

   {

        case 0xfe:

              

             PORTB=val;

    

             break;

    

        case 0xfd:

              

             PORTB=val;

    

             break;

    

        case 0xfb:

              

             PORTB=val;

    

             break;

    

       case 0xf7:

              

             PORTB=val;

    

            break;

    

      case 0xef:

              

             PORTB=val;

    

            break;

    

     case 0xdf:

              

             PORTB=val;

    

            break;

    

     case 0xbf:

              

             PORTB=val;

    

             break;

    

    case 0x7f:

              

             PORTB=val;

    

            break;

    

   default:

              

             PORTB=0xff;      //initially all LEDs are off

    

             break;        

    }

 }

}

 

before while(1), I want to add it in current code (without any change in other parts) to make it work. But currently I have no idea to fix it.

 

I'd like to appreciate it if anyone would give me any comments.

 

 

This topic has a solution.
Last Edited: Sun. May 6, 2018 - 07:35 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The STK600 has active low switches(pushbuttons) if you are using those.  The LES's are also active low meaning they illuminate on a logic low so it kinda throws off many new folks.

 

Why so complicated a program?

 

Try this:

 

#include <avr/io.h>

int main(void)
{
    DDRB = 0xff;    //all outputs
    
    while(1)
    {
        uint8_t inputs = PINB;
        
        PORTD = inputs;
    }
}

untested.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

Last Edited: Sun. May 6, 2018 - 01:56 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

 

else if(!(PIND & 0x80))

   val=0x7f;

 

else  val=0xff;  // no switches pressed  <-- Add this line

 

switch(val)

{

    case 0xfe:

             PORTB=val;

             break;

    

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

all leds stay on and switch doesn't work with this code.