这个程序绝的地方是在switch语句中,指定下一个case的值。
程序如下:
/*
* GccApplication12.c
*
* Created: 2014-10-6 14:20:00
* Author: Administrator
*/
#define F_CPU 0x8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
#define RED_EW_ON() PORTC &= ~_BV(PC0)
#define YELLOW_EW_ON() PORTC &=~_BV(PC1)
#define GREEN_EW_ON() PORTC &=~_BV(PC2)
#define RED_EW_OFF() PORTC |= _BV(PC0)
#define YELLOW_EW_OFF() PORTC |= _BV(PC1)
#define GREEN_EW_OFF() PORTC |= _BV(PC2)
#define RED_SN_ON() PORTC &=~_BV(PC3)
#define YELLOW_SN_ON() PORTC &=~_BV(PC4)
#define GREEN_SN_ON() PORTC &=~_BV(PC5)
#define RED_SN_OFF() PORTC |= _BV(PC3)
#define YELLOW_SN_OFF() PORTC |= _BV(PC4)
#define GREEN_SN_OFF() PORTC |= _BV(PC5)
#define YELLOW_EW_BLINK() PORTC^=_BV(PC1)
#define YELLOW_SN_BLINK() PORTC^=_BV(PC4)
uint8_t Flash_Count = 0,Operation_Type = 1;
void Traffic_Light()
{
switch(Operation_Type)
{
case 1:
RED_EW_OFF(); YELLOW_EW_OFF();GREEN_EW_ON();
RED_SN_ON();YELLOW_SN_OFF();GREEN_SN_OFF();
_delay_ms(30);
Operation_Type = 2;
break;
case 2:
_delay_ms(30);
YELLOW_EW_BLINK();
GREEN_EW_OFF();
if(++Flash_Count != 3)return;
Flash_Count = 0;
Operation_Type = 3;
break;
case 3:
RED_EW_ON(); YELLOW_EW_OFF();GREEN_EW_OFF();
RED_SN_OFF();YELLOW_SN_OFF();GREEN_SN_ON();
_delay_ms(30);
Operation_Type = 4;
break;
case 4:
_delay_ms(30);
YELLOW_SN_BLINK();
GREEN_SN_OFF();
if(++Flash_Count != 3) return;
Flash_Count = 0;
Operation_Type = 1;
break;
}
}
int main(void)
{
DDRC=0xFF;PORTC=0xFF;
while(1)
{
Traffic_Light();
}
}
仿真截图:
Studio6.20编译截图:
|