懒猫爱飞

AVR学习日志(五)---- LED模拟交通灯

0
阅读(1346)

呵呵,其实还是一个流水灯,不过流的貌似有点用,嘻嘻……
/******************************************************************
//文件名称:Tri_light
//功    能:模拟交通灯功能
//作    者:懒猫爱飞
//日    期:2010.05.24
*******************************************************************/
#include<iom8515v.h>
#include<macros.h>
#define uchar unsigned char
#define uint  unsigned int
uchar Flash_Count = 0;    //write down the flicker frequency
uchar Operation_Type = 1; //write down the status
#define Bit(n) (1<<n)
//define the light status
#define Green_ew_on()  PORTA&=~Bit(2)
#define Yellow_ew_on() PORTA&=~Bit(1)
#define Red_ew_on()    PORTA&=~Bit(0)
#define Green_sn_on()  PORTA&=~Bit(5)
#define Yellow_sn_on() PORTA&=~Bit(4)
#define Red_sn_on()    PORTA&=~Bit(3)
#define Green_ew_off()  PORTA|=Bit(2)
#define Yellow_ew_off() PORTA|=Bit(1)
#define Red_ew_off()    PORTA|=Bit(0)
#define Green_sn_off()  PORTA|=Bit(5)
#define Yellow_sn_off() PORTA|=Bit(4)
#define Red_sn_off()    PORTA|=Bit(3)
/****************************************************************
//函数名称: delay(void)
//功    能:延时
//入口参数:无
//出口参数:无
*****************************************************************/
void delay(void)
{
 uint i;
 for(i=1;i<100;i++)
    ;
}
/****************************************************************
//函数名称:delay_1ms(void)
//功    能:延时1ms
//入口参数:无
//出口参数:无
*****************************************************************/
void delay_1ms(void)//1ms
{
 uint i;
 for(i=1;i<(uint)(8*143-2);i++)
    ;
}
/****************************************************************
//函数名称:delay_ms(uint time)
//功    能:延时time ms
//入口参数:time
//出口参数:无
*****************************************************************/
void delay_ms(uint time)//time*1ms
{
   uint i="0";
   while(i<time)
   {
   delay_1ms();
    i++;
   }
}
/****************************************************************
//函数名称:Port_init(void)
//功    能:端口初始化
//入口参数:无
//出口参数:无
*****************************************************************/
void Port_init(void)
{
 DDRA  = 0xff;   //A口输出口控制Led灯   
 PORTA = 0xff;
 
 DDRB  = 0x00;         //B口设为输入,呈高阻状态
 PORTB = 0x00;
 
 DDRC  = 0x00;   //C口
 PORTC = 0x00;
 
 DDRD  = 0x00;   //D口
 PORTD = 0x00;
}
/****************************************************************
//函数名称:Traffic_lignt(void)
//功    能:实现灯的切换
//入口参数:无
//出口参数:无
*****************************************************************/
void Traffic_lignt(void)
{
  switch(Operation_Type)
 {
   case 1:
   Red_ew_off();Yellow_ew_off();Green_ew_on(); //east and west walk,south and north stop
   Red_sn_on();Yellow_sn_off();Green_sn_off();
   delay_ms(2000);
   Operation_Type = 2;
   break;
  case 2:
   for(Flash_Count=0;Flash_Count<10;Flash_Count++)//suspend status
   {
    Yellow_ew_on();
    delay_ms(200);
    Yellow_ew_off();
    delay_ms(200);
   }
   Flash_Count=0;
   Operation_Type = 3;
   break;
  case 3:
    Red_ew_on();Yellow_ew_off();Green_ew_off(); // south and north walk,east and west stop
    Red_sn_off();Yellow_sn_off();Green_sn_on();
    delay_ms(2000);
    Operation_Type = 4;
    break;
  case 4:
   for(Flash_Count=0;Flash_Count<10;Flash_Count++)//suspend status
   {
    Yellow_sn_on();
    delay_ms(200);
    Yellow_sn_off();
    delay_ms(200);
   }
   Flash_Count=0;
   Operation_Type = 1;
   break;
  default: break;
 }
}
/****************************************************************
//函数名称:main(void)
//功    能:主程序
//入口参数:无
//出口参数:无
*****************************************************************/
void main(void)
{
 Port_init();
 while(1)
 {
  Traffic_lignt();
 }
}
 
 呵呵,好了,今天就先报告这么多吧,改天再来交剩下的作业,懒猫比较懒,不过也爱飞,心中总有飞翔的梦想,总是梦见自己有一天能展翅高飞,所以懒猫每天坚持学点东东,也许只是雕虫小技,可是懒猫很高兴,因为懒猫相信,那一天不远了,是的,懒猫爱飞,懒猫也一定能展翅高飞!
 
每天进步一点点,开心多一点^_^