AVR STK-500 Logical Gates

Discussion in 'Embedded Systems and Microcontrollers' started by miniElectronics, Aug 30, 2009.

  1. miniElectronics

    Thread Starter Member

    Aug 4, 2009
    20
    0
    Hello,

    Can someone tell me how do i program an AVR STK-500 such as if i press SW0 and SW1 so Led0 will light up?

    I have tried many different things but not work.

    example.
    Code ( (Unknown Language)):
    1. PORTB.0 = PIND.0 && PIND.1
    Thanks
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Go ahead and post your entire source code for your program so that we can see the entire program. We should be able to help you then.

    hgmjr
     
  3. miniElectronics

    Thread Starter Member

    Aug 4, 2009
    20
    0
    Here is the code:
    Code ( (Unknown Language)):
    1.  
    2. #include <90s8515.h>
    3.  
    4. void main(void)
    5. {
    6.  
    7. PORTA=0x00;
    8. DDRA=0x00;
    9.  
    10. PORTB=0x00;
    11. DDRB=0x01;
    12.  
    13. PORTC=0x00;
    14. DDRC=0x00;
    15.  
    16. PORTD=0x00;
    17. DDRD=0x00;
    18.  
    19. TCCR0=0x00;
    20. TCNT0=0x00;
    21.  
    22. TCCR1A=0x00;
    23. TCCR1B=0x00;
    24. TCNT1H=0x00;
    25. TCNT1L=0x00;
    26. OCR1AH=0x00;
    27. OCR1AL=0x00;
    28. OCR1BH=0x00;
    29. OCR1BL=0x00;
    30.  
    31. GIMSK=0x00;
    32. MCUCR=0x00;
    33.  
    34. TIMSK=0x00;
    35.  
    36. ACSR=0x80;
    37.  
    38. while (1)
    39.       {
    40.       PORTB.0 = PIND.0 && PIND.1;
    41.       };
    42. }
    43.  
     
  4. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    What problems are you facing?

    How are your switches connected to the AVR?
     
  5. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    I don't know the processor or compiler, but...

    You appear to be trying to input from two pins and output to another pin.
    Should that be PINB.0 rather than PORTB ?

    (Or is the different format to do with Input & Output.)
     
  6. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    This depends on the compiler. Maybe you can use both.

    Which compiler are you using miniElectronics?
     
  7. miniElectronics

    Thread Starter Member

    Aug 4, 2009
    20
    0
    I am using CodeVision 2.

    I have connected PortD to Switches. And Portb to Leds. The thing i am trying is i want Led0 only to switch on when two buttons SW0 & SW1 is pressed together.

    SW0 & SW1 = Led0 // Led0 have to light up only when 2 buttons are pressed together.

    The code i have given up doesnt work. Maybe i have written wrong code.

    Thanks
     
  8. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    Which AVR are you using?
     
  9. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    The following is a list of assumptions I am making.

    - Your program compiled with no errors.
    - You have correctly jumpered the two switches you intend to use to PortD bit0 and PortD bit1 on your STK500 board.
    - You have correctly jumpered the LED you wish to illuminate when both switches are pressed at the same time.
    - You have installed the AT90S8515 AVR device into the correct socket on the STK500. Specifically location SCKT3000D3 outlined in RED.
    - you have jumpered the PROG3 programming header to the ISP6PIN header on the STK500 board.
    - You successfully downloaded the hex file into your target AVR.

    With all the above assumptions satisfied then you will need to make a small correction to your program code.

    The operator && is the logical AND compare operator and as such it is normally used in an IF statement. The & is the logical AND assignment operator.

    At this point it is important to recognize that the switches on the STK500 board are "normally open" with one side tied to ground and the other side pulled to +5V with a pullup resistor. That means that the AVR input to which the switch is connected is normally HIGH. Pressing the switch applies a low to the AVR's input pin.

    The LEDs on the STK500 board are wired such that they require a LOW to turn them on.

    The active states for the two inputs and the output in effect constitute negative-logic. That means that where you might expect to use an AND function to turn the LED on when both switches are pressed you will instead need an OR function. That is because a positive-logic AND is equivalent to a negative-logic OR.

    That means that your statement should be
    Code ( (Unknown Language)):
    1. PORTB.0 = PIND.0 | PIND.1;
    Hope this makes sense.

    ADDED:

    I would also recommend that you add a delay on the order of a few milliseconds to give the two inputs some time to settle between updates to the LED's state. You need to look into the delay.h header file since it contains a quick and dirty function that you can call to provide you a crude delay.

    hgmjr
     
    Last edited: Aug 31, 2009
  10. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    && is a relational operator
    & is the bitwise AND

    The && is used to connect logical statements resulting in a value of true or false. An a example might be something like
    Code ( (Unknown Language)):
    1.  
    2.     if((ch >= 'A') && (ch <= 'F'))
    3.  
    The & operator has the following truth table
    Code ( (Unknown Language)):
    1.  
    2.   a  b    y
    3. -------|---
    4.   0  0 | 0
    5.   0  1 | 0
    6.   1  0 | 0
    7.   1  1 | 1
    8.  
    That may clear things up a bit
     
    Last edited: Sep 1, 2009
  11. miniElectronics

    Thread Starter Member

    Aug 4, 2009
    20
    0
    Hello Freinds,

    Solution of hgmjr works.

    PORTB.0 = PIND.0 | PIND.1;

    Thank you all.
     
  12. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    Yes but this is the OR logic function. The LED lights with either switch being pressed.
     
  13. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Mik3,

    The reason the OR function is needed is that the STK500 board LEDs and Switches are wired as negative-logic inputs.

    hgmjr
     
  14. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    Ok, I didn't know that.
     
  15. miniElectronics

    Thread Starter Member

    Aug 4, 2009
    20
    0
    In case if i take out chip from STK500 so and put it in a real circuit so i have to use AND & gate?
     
  16. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    Yes, if you take out the microcontroller and use it in another circuit with the switches directly connected to the microcontroller you have to use an AND gate.
     
  17. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    The answer to your question depends on how you design your switches and your LED interfaces.

    If you maintain the negative-logic on your inputs and your outputs then you will continue to need the OR function. If on the otherhand you wire the switch inputs as positive logic and the LED enable signal as positive-logic then you will indeed need to change your statement to use the AND function.

    If you have any questions concerning your design, we are here to assist you.

    hgmjr
     
Loading...