Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

STK600 : Using LEDs and Switches

In the last article we saw how to use ASF wizard. Now in this article (and in few further articles) we will see tutorials on STK600.

STK600:

STK600

STK600 is complete starter kit for developers. As you can see in the picture it comes with different hardware functions such as LEDs, swithces, dataflash. It has connectors for all on-board peripherals and debugging interfaces, which gives you easy access to all PORT pins. It can be powered from USB or from 10-15V DC external power supply.

It comes with all the cables required for connections such as 10-wire cable for JTAG, 6-wire cable for PDI and 2-wire cables to connect different peripheral headers with corresponding PORT pins. It also comes with DC power cable and USB cable to power board from external power supply and USB respectively.

For more detail information read STK600 user guide here

Before starting tutorial,

Working environment :

IDE                                         AVR Studio 6.2

Operating Voltage              3.5 V (STK600 is powered from USB)

Fuse settings for board are

EXTENDED                         0xFF

HIGH                                     0x99

LOW                                      0x62

Debug Interface                   JTAG

System Clock                        1 MHz

To begin with tutorials on STK600, we will start with LEDs first.

LED Tutorial :

STK600 has 8 LEDs labeled from LED0 to LED7 and LED header is also available on kit. In order to light these LEDs, we must connect this LED header with any of PORT header on kit. We will connect LEDs to PORTB.

ATmega2560 has three registers allocated for each port. Data Register PORTx where we actually write value to particular port. Data Direction Register DDRx which decides direction of that port, if 0 then input and if 1 then output. The port input pin register PINx which can be used to read port pin irrespective of direction of port.

In order to light LEDs we need to define PORTB as output first and then we will write value to the PORTB register.

DDRB=0xff;                  //define PORTB as output port

Remember, you can also define only particular pin of port as output keeping rest of pins as input or vice versa. This can be done by writing particular bit of DDRx register as 0 or 1 as required.

Now we will write value to PORTB register.

PORTB=0x00;
#include <avr/io.h>

int main(void)
{
 DDRB=0xff;		//make PORTB as output
 
 PORTB=0x00;
 
 while(1);
}

Now burn this program on your kit.

Surprised with result!? Even if I have written PORTB as 0x00 and still all LEDs are on. If you read STK600 user guide carefully, you will find that hardware of LEDs is designed such a way that in order to light LED we need to write 0 to that bit in PORTx register.

Now let us move on to next tutorial on switches.

Switch Tutorial :

STK600 has 8 switches labeled from SW0 to SW7 and also switch header is available on kit. We will connect switch with PORTD. As per hardware of switches, when switch is pressed corresponding pin to that switch is pulled low and releasing switch pulls it back to VTG through external pull up of 10K.

In the code below for switches, initially all LEDS are off. As we press any switch only the LED corresponding to that switch turns on. For that we need to define PORTB as output to which LEDs are connected.

As I mentioned above, STK600 has external pull up of 10K at switches, hence no need to enable internal pull ups.

To check whether the switch is pressed or not we need to read PIND register as it reads state of external pin irrespective of direction of that pin.

For switch which is pressed that bit of PIND register will be 0.

#include <avr/io.h>

unsigned char val=0;  //senses the switch pressed

int main(void)
{
 DDRB=0xff;           //define PORTB as output
 
 while(1)
 {
   if(!(PIND & 0x01))
   
	       val=0xfe;
		
   else if(!(PIND & 0x02))
   
	       val=0xfd;
		
   else if(!(PIND & 0x04))
   
	       val=0xfb;
		
   else if(!(PIND & 0x08))
   
	       val=0xf7;
		
   else if(!(PIND & 0x10))
   
	       val=0xef;
		
   else if(!(PIND & 0x20))
   
	       val=0xdf;
		
   else if(!(PIND & 0x40))
   
	       val=0xbf;												
   
   else if(!(PIND & 0x80))
   
	       val=0x7f;
   
   switch(val)
  {
   case 0xfe:
             
             PORTB=val;
   
   break;
   
   case 0xfd:
             
             PORTB=val;
   
   break;
   
   case 0xfb:
             
             PORTB=val;
   
   break;
   
   case 0xf7:
             
             PORTB=val;
   
   break;
   
   case 0xef:
             
             PORTB=val;
   
   break;
   
   case 0xdf:
             
             PORTB=val;
   
   break;
   
   case 0xbf:
             
             PORTB=val;
   
   break;
   
   case 0x7f:
             
             PORTB=val;
   
   break;
   
   default:
             
             PORTB=0xff;      //initially all LEDs are off
   
   break;		  
  } 
 }
}

Thanks for reading. Don’t forget to share your views and questions as comments below.




This post first appeared on Embedded Tutorials, please read the originial post: here

Share the post

STK600 : Using LEDs and Switches

×

Subscribe to Embedded Tutorials

Get updates delivered right to your inbox!

Thank you for your subscription

×