AVR STK-500 Logical Gates

Thread Starter

miniElectronics

Joined Aug 4, 2009
20
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.
Rich (BB code):
PORTB.0 = PIND.0 && PIND.1
Thanks
 

hgmjr

Joined Jan 28, 2005
9,027
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.
Rich (BB code):
PORTB.0 = PIND.0 && PIND.1
Thanks
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
 

Thread Starter

miniElectronics

Joined Aug 4, 2009
20
Here is the code:
Rich (BB code):
#include <90s8515.h>

void main(void)
{

PORTA=0x00;
DDRA=0x00;

PORTB=0x00;
DDRB=0x01;

PORTC=0x00;
DDRC=0x00;

PORTD=0x00;
DDRD=0x00;

TCCR0=0x00;
TCNT0=0x00;

TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

GIMSK=0x00;
MCUCR=0x00;

TIMSK=0x00;

ACSR=0x80;

while (1)
      {
      PORTB.0 = PIND.0 && PIND.1;
      };
}
 

rjenkins

Joined Nov 6, 2005
1,013
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.)
 

mik3

Joined Feb 4, 2008
4,843
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.)
This depends on the compiler. Maybe you can use both.

Which compiler are you using miniElectronics?
 

Thread Starter

miniElectronics

Joined Aug 4, 2009
20
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
 

hgmjr

Joined Jan 28, 2005
9,027
Here is the code:
Rich (BB code):
#include <90s8515.h>
 
void main(void)
{
 
PORTA=0x00;
DDRA=0x00;
 
PORTB=0x00;
DDRB=0x01;
 
PORTC=0x00;
DDRC=0x00;
 
PORTD=0x00;
DDRD=0x00;
 
TCCR0=0x00;
TCNT0=0x00;
 
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
 
GIMSK=0x00;
MCUCR=0x00;
 
TIMSK=0x00;
 
ACSR=0x80;
 
while (1)
      {
      PORTB.0 = PIND.0 && PIND.1;
      };
}
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
Rich (BB code):
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:

Papabravo

Joined Feb 24, 2006
21,227
&& 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
Rich (BB code):
    if((ch >= 'A') && (ch <= 'F'))
The & operator has the following truth table
Rich (BB code):
  a  b    y
-------|---
  0  0 | 0
  0  1 | 0
  1  0 | 0
  1  1 | 1
That may clear things up a bit
 
Last edited:

hgmjr

Joined Jan 28, 2005
9,027
Mik3,

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

hgmjr
 

mik3

Joined Feb 4, 2008
4,843
In case if i take out chip from STK500 so and put it in a real circuit so i have to use AND & gate?
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.
 

hgmjr

Joined Jan 28, 2005
9,027
In case if i take out chip from STK500 so and put it in a real circuit so i have to use AND & gate?
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
 
Top