Traffic light


Traffic light controller

Toolkit:AVR Development System

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

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 switch1,switch2;
	//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 UART */
	uart0Init(19200);

	LED_RED_OFF();   //turn off Red LED

	LED_YELLOW_OFF();//turn off Yellow LED					

	LED_GREEN_OFF(); //turn off Green LED		


	while(1)// loop forever

	{
		// check SW1 & SW2

		switch1 = Read_SW(SWITCH_ONE);// read SW1 status

		switch2 = Read_SW(SWITCH_TWO);// read SW2 status

		
		if( (switch1 == INACTIVE) & (switch2 == INACTIVE))
		{
			LED_RED_OFF();   //turn off Red LED

			LED_YELLOW_OFF();//turn off Yellow LED					

			LED_GREEN_ON(); //turn on Green LED				

		}
		else if( (switch1 == INACTIVE) & (switch2 == ACTIVE))
			{
				LED_RED_ON();   //turn on Red LED

				LED_YELLOW_ON();//turn on Yellow LED					

				LED_GREEN_OFF(); //turn off Green LED				

			}
			else if( (switch1 == ACTIVE) & (switch2 == INACTIVE))
				{
					LED_RED_OFF();   //turn off Red LED

					LED_YELLOW_ON(); //turn on Yellow LED					

					LED_GREEN_OFF(); //turn off Green LED				

				}
				else
				{
					LED_RED_OFF();   //turn off Red LED

					LED_YELLOW_OFF();//turn off Yellow LED					

					LED_GREEN_OFF(); //turn off Green LED			

				}

	}

}