Problem blinking LEDs on STK500 (ATMEGA16)

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

Hi all!

 

I've dug out my old (2006) STK500 to program an ATMEGA16 microcontroller. The STK500 is configured with "default" jumper settings: all "2-pin" headers are closed (VTARGET, AREF, RESET, XTAL1, and OSCSEL is off to the right side). BSEL2 and PJUMP are all open. I have not got an external crystal plugged in. 

 

At first, I couldn't get Atmel Studio 7 to communicate properly with the STK; I got error messages of the kind "Got 0xc0 expected 0x00"... However, I tried unplugging the 10-pin headers (PORTB-LEDS and PORTD-SWITCHES) and then the STK could do its thing! I was able to read and verify the signature on the ATMEGA16. Is this expected behavior, that the STK can't read/write the uc if the ports are connected to anything?

 

Anyway, next I found a nice tutorial on GCC programming, using this precise uc as example: https://ita.ovh/files/atmelstudi...

// File: led.c
// Description: Simple C program for the ATMEL AVR uC (ATmega16 chip)
// This program lets the user turn on LEDs by pressing the switches on STK500 board
#include <avr/io.h> // avr header file for IO ports
int main(void){
	unsigned char i; // temporary variable
	DDRA = 0x00; // set PORTA for input
	DDRB = 0xFF; // set PORTB for output
	PORTB = 0x00; // turn ON all LEDs initially

	while(1){
		// Read input from PORTA.
		// This port will be connected to the 8 switches
		i = PINA;

		// Send output to PORTB.
		// This port will be connected to the 8 LEDs
		PORTB = i;
	}
	return 1;
}

 

The next confusion is with the behavior of the example "led.c" program, running on/in the STK. The program configures PORTA for input and PORTB for output, so I hook up PORTA-SWITCHES and PORTB-LEDS (after programming and powering down the STK). Funny thing is that the LEDs on the STK are all "dimly" lit, and when I press a switch -- e.g. SW4 -- then LED3 (not LED4!) goes bright for as long as I keep the switch pressed. Several strange things here: 

 

  1. Why won't the LEDs ever switch entirely off?
  2. Why does pressing switch i result in LED i-1 being turned on?
  3. Changing the code to keep all LEDs off still won't turn off the LEDs:     PORTB = 0b11111111;       // turn LED off
  4. Changing the code to offset input by one bit, allows switch i to turn on LED i:      PORTB = i >> 1;  // doesn't work for SW7

 

Any clues or tips are greatly appreciated! For instance, maybe the LEDs on the STK are supposed to be dim when off?

 

 

Joakim

Last Edited: Fri. Nov 9, 2018 - 09:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Inverted logic (so it works at both 3.3 and 5V)

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

But that only explains why 0 turns the LEDs on, and 1 off, and not vice versa. That part is sort of the only thing I'm not finding confusing.

Notice I tried writing an all-1 byte to PORTB. That should turn all LEDs off, wouldn't you agree? Doesn't happen, at least not on my stk...

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

Figured out the problem, the ATMEGA16 was actually sitting in the wrong red socket (the STK500 has two) ;-)

The ISP programming still worked, though, so it was not obvious that I had placed it wrong.

 

Anyway, all is working now.