用定时器0控制切换流水灯顺序,用外部中断控制两种数码管显示方式

来源:互联网 发布:apache cxf 教程 编辑:程序博客网 时间:2024/05/29 04:23



#include "reg52.h"                     //此文件中定义了单片机的一些特殊功能寄存器

 

typedef unsigned int u16;         //对数据类型进行声明定义

typedef unsigned char u8;

 

sbit LSA=P2^2;

sbit LSB=P2^3;

sbit LSC=P2^4;

 

sbit k3=P3^2;

sbit k4=P3^3;

 

sbit led1=P2^0;

sbit led2=P2^1;

sbit led6=P2^5;

sbit led7=P2^6;

sbit led8=P2^7;

 

int tt=0,time=0,flag=0,flag1=0,k=0;

 

u8 codesmgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};     

 

 

//延时函数,i=1时,大约延时10us

void delay(u16 i)

{

while(i--);

}

 

void DigDisplay()

{

         u8i;

          

          

         for(i=0;i<8;i++)

         {

         switch(i)   //位选,选择点亮的数码管,

                   {

                            case(0):

                                     LSA=0;LSB=0;LSC=0;P0=smgduan[time%10];break;//显示第0位

                            case(1):

                                     LSA=1;LSB=0;LSC=0;P0=smgduan[time/10];break;//显示第1位

                            case(2):

                                     LSA=0;LSB=1;LSC=0;P0=smgduan[time/100];break;//显示第2位

                       case(3):

                                     LSA=1;LSB=1;LSC=0;P0=smgduan[0];break;//显示第3位

                            case(4):

                                     LSA=0;LSB=0;LSC=1;P0=smgduan[0];break;//显示第4位

                            case(5):

                                     LSA=1;LSB=0;LSC=1;P0=smgduan[1];break;//显示第5位

                            case(6):

                                     LSA=0;LSB=1;LSC=1;P0=smgduan[6];break;//显示第6位

                            case(7):

                                     LSA=1;LSB=1;LSC=1;P0=smgduan[1];break;//显示第7位

                            }                

                    

         delay(80);

                P0=0x00;//消隐

         }

}

 

void Int0Init();

void Int1Init();

void init() ;

 

void main()

{ static u8 k=0;

 Int0Init();

 Int1Init();

 init() ;

 while(1)

  {

 DigDisplay();

  }

}

 

 

 

void init()

P0=0;

P2=0;

time=0;

tt=0;

TMOD=0x11; 

TH0=(65536-50000)/256; //定时器装初值50ms

TL0=(65536-50000)%256;

EA=1;//开总中断

ET0=1;//开定时器0中断

TR0=1;//启动定时器0;使TR0置位=1,定时器0开始计数;停止时,使TR0=0复位;

 

 

void timer0() interrupt 1

static int j=0;

static u8 aa=0;

TH0 =(65536-50000)/256;// 0x4c;                    //晶振11.0592Mhz 

TL0 = (65536-50000)%256;//0x00;                    //若用12Mhz晶振 则改为TH0=0x3c;TL0=0xb0;

  

if(flag==1)

{       

tt++;

if(tt ==20)

{

     time++;

     if(time==100)         //到99后在从00开始      

     {

            time = 0;

           }

          

     tt = 0; 

}

}

if(flag==2)

{

tt++;

if(tt==40)

{

    time=time+2;

     if(time==100)         //到99后在从00开始      

     {

            time=0;

           }

           tt=0;

}

}       

  k++;

  if(k<=20)

   {

   flag1=1;

   }

  if(k>=120&&k<=220)

   {

  flag1=2;

   }

  if(k>220)

   {

  k=0;

   }

  

  if(flag1==1)

   {

  aa++;

  if(aa==20)

         {

         switch(j)

         {

         case0:

           led1=0;led2=1;led6=1;led7=1;led8=1;break;

         case1:

           led1=1;led2=0;led6=1;led7=1;led8=1;break;

         case2:

           led1=1;led2=1;led6=0;led7=1;led8=1;break;

         case3:

           led1=1;led2=1;led6=1;led7=0;led8=1;break;

         case4:

           led1=1;led2=1;led6=1;led7=1;led8=0;break;

         case5:

           led1=1;led2=1;led6=1;led7=0;led8=1;break;

         case6:

           led1=1;led2=1;led6=0;led7=1;led8=1;break;

         case7:

           led1=1;led2=0;led6=1;led7=1;led8=1;break;

         case8:

          led1=0;led2=1;led6=1;led7=1;led8=1;j=-1;break;

          default:break;

           }  

           j++;

           aa=0;

           }

           }

           

       if(flag1==2)

          {

          aa++;

          if(aa==20)

          {

           switch(j)      

  {

         case0:

           led1=1;led2=0;led6=0;led7=1;led8=1;break;

         case1:

           led1=0;led2=1;led6=1;led7=0;led8=1;break;

         case2:

           led1=1;led2=0;led6=1;led7=1;led8=0;break;

         case3:

           led1=0;led2=1;led6=0;led7=1;led8=1;break;

         case4:

           led1=1;led2=0;led6=1;led7=0;led8=1;break;

         case5:

           led1=0;led2=1;led6=1;led7=1;led8=0;break;

         case6:

           led1=1;led2=0;led6=0;led7=1;led8=1;break;

         case7:

           led1=0;led2=1;led6=1;led7=0;led8=1;break;

         case8:

          led1=0;led2=1;led6=1;led7=0;led8=1;j=-1;break;

          default:break;       

          }

          j++;

          aa=0;

          }

          }

   

}

 

 

void Int0Init()

{

 PX0=1;

 IT0=1;

 EX0=1;

 EA=1;

 }

 

void Int0()         interrupt0                 //外部中断0的中断函数

{

         delay(1000);     //延时消抖

         if(k3==0)

         {

           flag=1;

         }

}

 

void Int1Init()

{

 PX1=1;

 IT1=1;

 EX1=1;

 EA=1;

 }

 

void Int1()         interrupt2                 //外部中断0的中断函数

{

         delay(1000);     //延时消抖

         if(k4==0)

         {

         flag=2;     

         }

}

阅读全文
0 0
原创粉丝点击