2019年11月17日 星期日

108 V4


#include <at89x52.h>
#define key_pwm   0x10
#define key_mode  0x20       //增加一個按鍵P3.5
#define key_start 0x40
#define key_pause 0x80
int P2table[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0x02,0x04,0x08,0x10,0x20,0x40};
char  P0table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
char bdata flag;
sbit Mode0_flag=flag^0;
sbit Mode1_flag=flag^1;
sbit up_flag=flag^2;
int task;
int anti_cck_i;
int pili_led_i;
int i=2;
int ii;
int j=0;
int count=0;
char k=0;
int time=0;
char Trg;
char Cont;
int x;
sbit output=P3^0;
int pwm_add=-1;
unsigned char zero_cnt;
sbit buzzer = P3^2;
bit mode_flag;
char bcd[2]={0,0};
char P1table[]={0xFE,0xfD};
void delay_4ms(int k1)//245~248hz250hz
{
 int i,j;
 for(i=0;i<k1;i++)
   for(j=0;j<40;j++)
       ;
}
void delay_1ms(int k)
{
 int i,j;
 for(i=0;i<k;i++)
  for(j=0;j<110;j++)
       ;
}
void task1_seg7_rotate3_cck()//???????
 {
   for(anti_cck_i=0;anti_cck_i<3;anti_cck_i++)
   {
   P1=0XFE;P0=0XFE;delay_1ms(50);//?a ???
   P1=0XFD;P0=0XFE;delay_1ms(50); //?a???
   P1=0XFB;P0=0XFE;delay_1ms(50); //?a???
   P1=0XF7;P0=0XFE;delay_1ms(50); //?a???
 
   P1=0XF7;P0=0XDF;delay_1ms(50); //?f???
   P1=0XF7;P0=0XEF;delay_1ms(50); //?e???

   P1=0XF7;P0=0XF7;delay_1ms(50); //?d???
   P1=0XFB;P0=0XF7;delay_1ms(50); //?d???
   P1=0XFD;P0=0XF7;delay_1ms(50); //?d???
   P1=0XFE;P0=0XF7;delay_1ms(50); //?d???

   P1=0XFE;P0=0XFB;delay_1ms(50); //?c???
   P1=0XFE;P0=0XFD;delay_1ms(50); //?b???
   }
   P1=0XFE;P0=0XFE;delay_1ms(500); //?a???
 }

void task2_8LED_rotate3()
{
 for(ii=0;ii<3;ii++)
 {
  for(pili_led_i=0;pili_led_i<14;pili_led_i++)
  {
  P2=~P2table[pili_led_i];delay_1ms(50);
  }
 }
}

void task3_buzzer_beep3(int count,int TH,int TL)
{
 int i;
 for(i=0;i<count;i++)
 {
  buzzer=0;
  delay_1ms(TL);
  buzzer=1;
  delay_1ms(TH);
 }
}
void init()
{
 int task=1;//??
 if(task==1)
 {
  P0=~0xFF;P1=0XFF;
  task1_seg7_rotate3_cck();//
  P0=~0xFF;P1=0XFF;//OFF
  task=2;
 }
 if(task==2)
 {
   task2_8LED_rotate3();
   P2=~0x80;delay_1ms(500);P2=0xFF;
   task=3;
 }
 if(task==3)
 {
    int i;
    for(i=0;i<3;i++)
    {  
     task3_buzzer_beep3(100,1,1);delay_1ms(10);
     }
 }
}
void T0_int(void) interrupt 1
{
   TH0=(65536-5000)/256;
   TL0=(65536-5000)%256;
   P0=P0table[ bcd[j] ];
   P1=P1table[j];
   j++;        
   if(j==2) j=0;
}
void T1_int(void) interrupt 3
{
 TH1=(65536-50000)/256;
 TL1=(65536-50000)%256;
 time++;
 if(time %2==0)
 {
    count++;
    if(count==100)   count=0;
    bcd[1]=count/10;
    bcd[0]=count%10;
 }
}
void KeyRead()
{
    unsigned char ReadData = P3^0xff;   // 1
      ReadData &= 0xF0;                                                                         //按鍵只讀取 P3.6 P3.7
    /*
      11000000    11000000
    &)10000000   &01000000
            10000000    01000000
    */
    Trg = ReadData & (ReadData ^ Cont); // 2
    Cont = ReadData;                    // 3
    /*
      不是0x80不然就是0x40
    */
}
void KeyProc()
{
    if(Trg & key_pwm)
    {
        TR0=0;TR1=0;
        pwm_add++;
        if(pwm_add>9) pwm_add=0;
    }

    if(Trg & key_mode)
  {
     mode_flag=~mode_flag;
     if( mode_flag){TR0=1;TR1=1;}
     else  {TR0=0;TR1=0;P1=0xfe;P0=0xc0;pwm_add=0;}
  }
    if(Trg & key_start)       TR1=1;
    /*按一次開始計數 P3.6 0x40 & 0x40 =0x40*/
    if(Trg & key_pause)    
    /*按一次開始計數   P3.7 0x80 & 0x80=0x80*/
    {                                                                                   //短按兩次停止計數
        TR1=1;
        k++;
        if(k == 2)
                TR1=0;
    }
    if(!Trg & !Cont)                    //按一次放開後 k 為零
    {                    
        zero_cnt++;
        if(zero_cnt>10)
        {
                zero_cnt=0;
                k=0;;
        }
    }
    if (Cont & key_pause)
    {
        zero_cnt++;
        if(zero_cnt>50)       
        {
                zero_cnt=0;
                TR1=0;
                count=0;    
                bcd[1]=0;
                bcd[0]=0; 
        }
    }
}
void main()
{
    init();
    P3=0xFF;
  IE=0x8A;  
  TMOD=0x11;  
  TH0=(65536-5000)/256;
  TL0=(65536-5000)%256;
  TH1=(65536-50000)/256;
  TL1=(65536-50000)%256;
  Mode0_flag=1;
  TR0=1; TR1=0;
  while(1)
  {
        if(Mode0_flag==1)
        {
                delay_1ms(80);       //防止按鍵彈跳,delay時間配合短按兩下的時間調整
                KeyRead();
                KeyProc();
        }
        if(pwm_add==0)
        {
                P1=0xfe;P0=P0table[0];
                P2=0xFF;//二進制0
                x=0;
      output=0;delay_4ms(x);
      output=1;delay_4ms(10-x);
        }
        if(pwm_add==1)
        {
                P1=0xfe;P0=P0table[1];
                P2=0xF5;//二進制10
                x=1;
      output=0;delay_4ms(x);
      output=1;delay_4ms(10-x);
        }
        if(pwm_add==2)
        {
                P1=0xfe;P0=P0table[2];
                P2=0xEB;//二進制20
                x=2;
      output=0;delay_4ms(x);
      output=1;delay_4ms(10-x);
        }
        if(pwm_add==3)
        {
                P1=0xfe;P0=P0table[3];
                P2=0xE1;//二進制30
                x=3;
      output=0;delay_4ms(x);
      output=1;delay_4ms(10-x);
        }
        if(pwm_add==4)
        {
                P1=0xfe;P0=P0table[4];
                P2=0xD7;//二進制40
                x=4;
      output=0;delay_4ms(x);
      output=1;delay_4ms(10-x);
        }
        if(pwm_add==5)
        {
                P1=0xfe;P0=P0table[5];
                P2=0xCD;//二進制50
                x=5;
      output=0;delay_4ms(x);
      output=1;delay_4ms(10-x);
        }
        if(pwm_add==6)
        {
                P1=0xfe;P0=P0table[6];
                P2=0xB3;//二進制60
                x=6;
      output=0;delay_4ms(x);
      output=1;delay_4ms(10-x);
        }
        if(pwm_add==7)
        {
                P1=0xfe;P0=P0table[7];
                P2=0xB9;//二進制70
                x=7;
      output=0;delay_4ms(x);
      output=1;delay_4ms(10-x);
        }
        if(pwm_add==8)
        {
                P1=0xfe;P0=P0table[8];
                P2=0xA5;//二進制80
                x=8;
      output=0;delay_4ms(x);
      output=1;delay_4ms(10-x);
        }
        if(pwm_add==9)
        {
                P1=0xfe;P0=P0table[9];
                P2=0xA5;//二進制90
                x=9;
      output=0;delay_4ms(x);
      output=1;delay_4ms(10-x);
        }
  }
}

沒有留言:

張貼留言

algorithm

 #include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv)  { for(int j=2;j<=100;j++)//j...