else statement not execute

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

Below in my code, else statement not simulate in proteus VSM

Discriptio: Switch is connected to PORTD.0 and LED is connected to PORTC.3; when switch pressed LED should be on and when switch realize or Unpressed LED should be OFF. 

 

Program:

#define F_CPU 8000000UL

#include <avr/io.h>

#include <util/delay.h>

int main(void)

{

DDRD = 0x00;   // set PORTD as input

DDRC = 0xFF;   // set PORTC as output

    while (1)

{

if(PIND == 0b0000001)     // if PD1 = High than PC2= High ow PORTC=0x00

{

PORTC = 0x03;

}

else                     

{

   PORTC = 0x00;

}

}

}

Error: When i pressed switch in Simulation (VSM) LED is ON, but when i OFF the switch , LED remain ON, but in this task i wish it's off. 

Please help me to solve this issue. 

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

What are you doing about pull-ups/downs for the NC state?
.
Also do you really want to check all 8 bits in PIND when only one is the input?

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

Hello 

 

I am very new for this, so i have no idea about it. 

 

But here in this code i made all D port as output, while i want to use just one byte/ pin to take a decision. Because i don't know how i can set one bit or reset one bit.

 

i also try another method to set bit: to set Pin2 of PORTA:  PORTA |= (0x01<<2) like that. 

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

Technohunt wrote:
if(PIND == 0b0000001)
Since the pins of PORTD are not connected, they may "float" so that looking at the whole port may not give the correct response to equal 0b00000001.

Consider:

if (PIND & 0x01) 
...

This will be true if PD0 is high and false if PD0 is low...

 

Also, does the switch on PD0 force the input both high and low?

If not then the pin again may "float" which can give indeterminate results.

If the switch is active low (pulls to GND) and there is no external pull-up resistor, then you may want to activate the internal pullup:

DDRD=0x00; // PORTD as input
PORTD=0x01; // Activate pullup on PD0

David (aka frog_jr)

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

You need to test only the bit you are actually interested in. Step 1 is to isolate that bit and make sure all other bits are zero, like so:

 

PIND & 0b0000001

The result of this expression will be either 0b00000000 or 0b00000001, whatever bits 7..1 of PIND is. The only thing that can vary in this expression is bit 0. Now you can test in an if-statement, and remember that only zero is regarded as "false" and anything non-zero is "true". So, the final test becomes

 

if(PIND & 0b0000001)     // if PD1 = High than PC2= High ow PORTC=0x00
{
   PORTC = 0x03;
}
else                     
{
   PORTC = 0x00;
}

Your project might still not work, but that could be because of what 'clawson' asked about above: Pull-up of (and current limitation on) the pin that the switch is connected to. The pin needs to be in a well defined state (i.e. pulled high or pulled low, and never floating with no pull either to + or ground). Since the AVRs have pull-ups built in (but no pull-downs) it is common to have the pin being pulled up to + by that internal AVR function. (Read about pull-ups in the data sheet). The switch hass one end connected to the pin, and the other to GND with a resistor in series. That resistor has to be chosen so that when the switch is closed it pulls the pin to ground stronger than the internal pull-up in the AVR pulls the pin towards +.

 

Read in the data sheet about pull-ups and how to enable it for a certain pin. Also note in the data sheet the nominal resistance (or impedance) of the pull-up. You need to pick a substantially lower value for the resistor that you put in series with the switch and then to GND. (Example: If the pull-up is 100 KOhm then you might pick 10 KOhm for the switch resistor pull-down. On the other hand you don't want a too low resistance on the pull down, because that will effectively be a short-circuit to ground and might destroy the internal AVR circuitry because of too high current. 10 KOhm should be reasonably safe.

 

Now, you don't have a real circuit but are only simulating in Proteus. Nevertheless it might be important to understand this pulling-up and pulling-down. You might take a course in microcontrollers and electronics and your teacher might require that you design a simulated circuit that would work in real life. Or you might just be simulating before moving on to a real circuit, where these things are crucial.

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

I tried but getting the wrong output. 

 

Now i am doing something more than the previous discussion. 2 LED as output. 

Task should be done is: First PA0 should become high via a switch and then if PA1 become high than PA4 (LED1) should be high and if PA0 become low then PA5 should high., so system should check PA1 first and then from Status of PA2 it should take a decision. 

connect two switches with mega1281, and will use 

/*

 * Smart ingition.c

 *

 * Created: 10/25/2017 2:18:37 AM

 * Author : TRIVEDI'S

 */ 

 

#include <avr/io.h>

 

 

int main(void)

{

    DDRA = 0xF0;   // first 4 bits are input and remaining 4 are output

while(1)

  {

if (PINA == 0b00000001)    // if PA0is high then check 2nd if

{

   if (PINA == 0b00000011)       // if PA0 and PA1 both high then 

      {

          PORTA = 0b00010000;    // PA4 become high 

       }

    else 

      {

         PORTA = 0b00100000;     // Othervise PA5 become high 

       }

  }

}

}

 

It's not working. 

Please check my attachment image, which including Schematic of it. 

When i not used else statement then upto else statement it's work properly but when i add else statement, it's direct High PA5 when i pressed 1st switch. 

Why ? 

Why it's happening?

Please help me out. 

either give me a complate code or give me some tips so i can do this as much as fast. 

Attachment(s): 

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

I tried but getting the wrong output. 

 

Now i am doing something more than the previous discussion. 2 LED as output. 

Task should be done is: First PA0 should become high via a switch and then if PA1 become high than PA4 (LED1) should be high and if PA0 become low then PA5 should high., so system should check PA1 first and then from Status of PA2 it should take a decision. 

connect two switches with mega1281, and will use 

/*

 * Smart ingition.c

 *

 * Created: 10/25/2017 2:18:37 AM

 * Author : TRIVEDI'S

 */ 

 

#include <avr/io.h>

 

 

int main(void)

{

    DDRA = 0xF0;   // first 4 bits are input and remaining 4 are output

while(1)

  {

if (PINA == 0b00000001)    // if PA0is high then check 2nd if

{

   if (PINA == 0b00000011)       // if PA0 and PA1 both high then 

      {

          PORTA = 0b00010000;    // PA4 become high 

       }

    else 

      {

         PORTA = 0b00100000;     // Othervise PA5 become high 

       }

  }

}

}

 

It's not working. 

Please check my attachment image, which including Schematic of it. 

When i not used else statement then up to else statement it's work properly but when i add else statement, it's direct High PA5 when i pressed 1st switch. 

Why ? 

Why it's happening?

Please help me out. 

either give me a complete code or give me some tips so i can do this as much as fast. 

Attachment(s): 

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

@Technohunt

Did you not bother to read any of the previous comments?

clawson, Johan, and I all gave you suggestions which apparently you have chosen not to follow.

Also, questions were asked that you did not bother to answer. ???

David (aka frog_jr)

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

Have you understood the point about requiring pull-ups?

 

Consider a pin with a switch attached. Now maybe you have connected the "other side" of this switch to Vcc or maybe you have connected it to Gnd? In the first case then when the switch is closed there is no question what you will read - there will be a direct connection to Vcc so the input will read '1' in that bit. If it's a switch connected to Gnd then when it's closed there's no question the bit will be '0'. OK, but what do you expect to read when the switch is open? What voltage is connected to the input in that case? Think about that for a while....

 

... the answer is you cannot possibly predict what you might read on the input when the switch is not closed. Without a direct connection to Vcc ('1') or Gnd ('0') you could actually read anything. The input is said to be "floating" and it could float either way. That's why you usually arrange to add a resistor that pulls the input "the other way" when the switch is not making a direct connection to one of the power rails. In effect you have either:

 

In each case the resistor here (perhaps 10K .. 50K) arranges to "pull the other way". So when the Gnd switch is not closed there is a weak connection to Vcc so the input sees a definite '1'. Or when the Vcc switch is open the pull-down makes that input a weak '0'.

 

Now the good news is that the AVR has been designed with pull-ups (but for tiny/mega not pull-downs) built in:

 

So as long as you don't mind having your input switches connected to Gnd (which means they read '0' when "active" and '1' when not active) then you can get the resistor "for free" as it is built into the AVR. To use it, with the bit in DDR for that pin set to 0 (input) you set the same bit in the PORT register to 1 and it "turns on" this pull up.

 

YOU need to do this.

 

If you really need the switch to read '1' when active (ie connected to Vcc) then you will have to go with the picture on the right above and put a real resistor on the other side of the switch to pull it down to Gnd when the switch is not closed.

 

If you don't do this then floating input may mean the switch apparently has no effect or "RC effects" in the circuit may mean that the switch appears to be active long after it has been opened again.

 

Also you persist in doing:

if (PINA == 0b00000001) 

understand that statement doesn't just say "if bit 0 of PINA is 1 then do this" but it really says ""if bit 0 of PINA is 1 AND all the other 7 bits in that port are reading 0 then do this". You are doing a full 8 bit comparison. If you want to check ONLY the state of bit 0 then you use:

if (PINA & (1 << 0)) 

or to test bit 3 instead it would be:

if (PINA & (1 << 3)) 

and so on. The conditional bit will then only be executed when that single bit is set to 1. If you go with "upside down" switches using internal pull ups where 0=active then you can use the ! (NOT) operation to invert the sense of this test:

if (!(PINA & (1 << 0)))

Now the conditional bit will only be done when that bit in A is 0, not 1.

 

Finally:

/*
 * Smart ingition.c
 *

PLEASE don't tell me that with this rudimentary knowledge of electronics and C you are going to go messing with ignition systems! You could be in for a lot of disaster real soon now if you do.

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

Perhaps the OP is making a model rocket ignition safety system, where two safety officers are required to approve the launch!

 

Jim

PS: clawson, can you make a tutorial out of the above reply, nice explanation of how to connect switches and the software to test them!

 

 

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

Technohunt wrote:
Task should be done is: First PA0 should become high via a switch and then if PA1 become high than PA4 (LED1) should be high and if PA0 become low then PA5 should high., so system should check PA1 first and then from Status of PA2 it should take a decision. 

No need to use AVR.
Use 2 dual pole toggle switch and 2 leds.
.
MG

I don't know why I'm still doing this hobby

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

Hello Frog jr 

 

 i read your reply and tried to code as your way, 

 

 i required both inputs (LOW & HIGH) from PD0. 

 

How can i prevent my pins again "float". Because when i put Ground terminal between my switch and controller with resister, then system not work for any input, PD0 high even. Before putting Ground with Resister, it accepts high value from PD0 and works PD4 become high. 

 

And what is Pull up / Down? 

 

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

ki0bk wrote:
PS: clawson, can you make a tutorial out of the above reply, nice explanation of how to connect switches and the software to test them!
Do you know that I had exactly the same idea myself. I had read this thread when out walking the cats and I even thought "I need some nice diagrams to explain pull-ups/downs" and "this could become a useful tutorial, Perhaps with some words about bounce and why INT0/INT1/PCINT probably aren't a good idea for these switches (though I don't want to undermine Ganssle).

 

So yeah, I will try to put something together (and maybe tidy up the diagrams - I guess it must be obvious to most that I drew one then vertically flipped it to make the other?)

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

clawson wrote:
yeah, I will try to put something together (and maybe tidy up the diagrams 

Excellent!

 

THere's some good basic electronics stuff here  - https://electronicsclub.info/ - which might be useful for reference - though I couldn't quickly see pull-ups/downs covered anywhere.

 

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

Technohunt wrote:
And what is Pull up / Down? 

Please, re-read 'clawsons' post #9 again. It explains it.

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]