Controls LED's 3


Controls LED's and read switches on TB-1 board

Toolkit:AVR Development System

Location:/bipom/devtools/WinAVR/minimaxavrc/Examples/Labs/Lab03/LAB3_2

Code Example


#define LED_RED          6        //PB6

#define LED_YELLOW       4        //PB4

#define LED_GREEN        5        //PB5


#define LED_RED_ON() 		(PORTB	&=~ 0x40)
#define LED_YELLOW_ON()  	(PORTB	&=~ 0x10)
#define LED_GREEN_ON()  	(PORTB	&=~ 0x20)

#define LED_RED_OFF() 	    (PORTB	|= 0x40)
#define LED_YELLOW_OFF()   	(PORTB	|= 0x10)
#define LED_GREEN_OFF()   	(PORTB	|= 0x20)

#define SW1 0x04      //PH2

#define SW2 0x08      //PB3


#define INPUT_SW1 			0x02      
#define INPUT_SW2 			0x03      

#define SWITCH_ONE       1
#define SWITCH_TWO       2

#define INACTIVE 0
#define ACTIVE   1

#define COUNT_MAX  32  //define blinking frequence


unsigned char Ndx[2] = {0};

/***************************************************
; Function :Read_SW
;
; Description : Read Switch  stat?		
;
; Input  : unsigned char - SW1 or SW2
; Output : SW ACTIVE  or INACTIVE 
;
*****************************************************/
unsigned char Read_SW(unsigned char NumSwitch)
{
	unsigned char data;
	//

	switch (NumSwitch)
	{
		case SWITCH_ONE:
			if(PINH & SW1)Ndx[0] &= 0xFE;
			else	Ndx[0] |= 1	;
			Ndx[0] <<= 1;
			if((Ndx[0] & 0x0F)==0x00)	data = INACTIVE;
			if((Ndx[0] & 0x0F)==0x0E)
			{
				data = ACTIVE;
				uart0Printf("\n\r SW1 is ACTIVE!");
			}

			break;

		case SWITCH_TWO:
			if(PINB & SW2)		Ndx[1] &=0xFE;
			else	Ndx[1] |= 1	;
			Ndx[1] <<=1;

			if((Ndx[1] & 0x0F)==0x00)	data = INACTIVE;
			if((Ndx[1] & 0x0F)==0x0E)
			{
				data = ACTIVE;
				uart0Printf("\n\r SW2 is ACTIVE!");
			}

			break;
		
		default:
			data = 0;
			break;
	}
	return data;
}

int main (void)
{

	unsigned char count;
	//Initialize port pins

 	/* Define pull-ups and set outputs */
	/* Define directions for port pins */
	PORTB = (1<<LED_RED)|(1<<LED_YELLOW)|(1<<LED_GREEN)|(1<<INPUT_SW2);
	DDRB  = (1<<LED_RED)|(1<<LED_YELLOW)|(1<<LED_GREEN);

	PORTH = (1<<INPUT_SW1);
	DDRH  = 0;

 	/* Initialize the UART1 */
	uart0Init(19200);	

	LED_RED_ON() ;   //turn on Red LED

	LED_YELLOW_ON();//turn on Yellow LED					

	
	count=0;

	while(1)// loop forever

	{
		// check SW1

		if(Read_SW(SWITCH_ONE) == ACTIVE)    //SW1 is active - Blink RED LED

		{
			if(count<COUNT_MAX  2)   LED_RED_ON();
			else                      LED_RED_OFF();
		}	
		else                         //SW1 is inactive - Turn on  RED LED

		{
			LED_RED_ON();
		}
		
		// check SW2

		if(Read_SW(SWITCH_TWO)==ACTIVE)//SW2 is active - Blink YELLOW LED

		{
			if(count<COUNT_MAX  2)   LED_YELLOW_ON();
			else                      LED_YELLOW_OFF();
		}	
		//SW2 is inactive - Turn on YELLOW RED LED
else
		{
			LED_YELLOW_ON();
		}
				
		count++;
		if(count>COUNT_MAX)count=0;
	}
}