Reading the switch states on an STK300

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

#include

typedef unsigned char u08;
I have an STK300 running an ATmega103l. Using a software delayed loop I can get the LEDs to flash. Now I'm trying to read the switches and flash the LEDs in response. Why doesn't the code below work?

I'm using AVR Studio 3.2, AVR ISP 2.4 and GNU AVRGCC20010211. Is there something newer/better that is at a low price that I should be using?

I found the inp() function in the GNU Development Environment for the AVR Microcontroller paper by Rich Nesworl, Jan 23, 2001 that I using below. Is there any really good documentation of the functions available in the GNU? Some, make that many, of Rich's documentation went right over my head.

Thanks,
Mike

int main( void )
{
u08 a;

outp(0xff,DDRB); /* Set Direction Register of PortB for output */
outp(0x00,DDRD); /* Set Direction Register of PortD for input */

for (;;) {
a = inp(PORTD);
if (a == 0)
outp(0xff,PORTB); /* Switch All LEDs off */
else
outp(0x00,PORTB); /* Switch All LEDs on */

}
}

admin's test signature
 

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

Hi Mike,

If I remember right, the switches on the STK300 are pulled high when the buttons are not pressed. For your program, this should mean that you need to press all the 8 switches in order to turn the LEDs on. Is this what you have done ?

An alternative of the main loop would be:

for(;;)
{
a = inp(PORTD);
outp(a);
}

Then, pressing a switch should immediately light the corresponding LED.

If this does not work, I would try to generate a file that AVR Studio can read to simulate it. You can also look at the disassembled code to see if there is anything strange in the generated code.

Dr.M

admin's test signature
 

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

I did have some errors in my code.

Tried this code

for(;;)
{
a = inp(PORTD);
outp(a);
}

Had to change to

outp(a, PORTB);

Still doesn't work. Any ideas?

Mike

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

Hi again Mike,

Sorry, my fault, you need to read from PIND, not PORTD. I.e.

for(;;)
{
a=inp(PIND);
outp(b,PORTB);
}

Reading the PORTD register actually returns whether the pull-ups are enabled. You must read the PIN register instead (a more detailed explanation is given in the datasheet with respect to PORT, PIN and DDR registers).

Dr.M

admin's test signature