⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 Simple Timer/Counter application for cock fighting
💻 C
📖 第 1 页 / 共 2 页
字号:
/* This code is used to implement the counting functions (10:00:00 - 0:00:00)
  it incorporates the following functions:
  start - starts the timer
  stop  - stops the timer and enables the alarm at 3sec in length
  show - shows the consumed time (enabled only when timer is idle)
  reset - defaults the timer to 10:00:00
  on/off - turns on and off the display (optional function)
  PORT declarationns

  input:
  RA4,RA5(MCLR) and RB0 as inputs
  start - RB0
  stop/show - RA5
  reset - RA4

  output:
  RB1 - RB7 : seven segments RB1 as segment a onwards .....
  RA0 - RA2 : multiplexer outputs (for ghost scanning)
  RA3 - relay output
  */
  unsigned int lapsed_count,delay,repitition;
  unsigned char up_time[6],down_time[6];
  unsigned short time_count,flag ;
  char check_value;
  void increment_up_time()
       {
       up_time[5]++;
       if (up_time[5] == 10)
          {
          up_time[5] = 0;
          up_time[4]++;
          if (up_time[4] == 10)
             {
             up_time[4] = 0;
             up_time[3]++;
             if (up_time[3] == 10)
                {
                up_time[3] = 0;
                up_time[2]++;
                if (up_time[2] == 6)
                   {
                   up_time[2] = 0;
                   up_time[1]++;
                   if (up_time[1] == 10)
                      {
                      up_time[1] = 0;
                      up_time[0]++;
                      }
                   }
                }
             }
          }
       }

  void decrement_down_time()
       {
       down_time[5]--;
       if (down_time[5] == 255)
          {
          down_time[5] = 9;
          down_time[4]--;
          if (down_time[4] == 255)
             {
             down_time[4] = 9;
             down_time[3]--;
             if (down_time[3] == 255)
                {
                down_time[3] = 9;
                down_time[2]--;
                if (down_time[2] == 255)
                   {
                   down_time[2] = 5;
                   down_time[1]--;
                   if (down_time[1] == 255)
                      {
                      down_time[1] = 9;
                      down_time[0]--;
                      }
                   }
                }
             }
          }
       }


  void interrupt()
       {
       TMR0 = 0;
       if (INTCON.T0IF)
          {
          time_count++;
          if (time_count == 38)
             {
             time_count = 0;
             lapsed_count++;
             increment_up_time();
             decrement_down_time();
             if (lapsed_count >= 60000)
                {
                INTCON.T0IE = 0;
                flag.f1 = 1; // enable lapsed flag
                }
             }
          }
       INTCON.T0IF = 0;
       }


  unsigned short mask(unsigned short num)
       {
       switch (num)
           {
           case 0 : return 0xf6;
           case 1 : return 0x60;
           case 2 : return 0xba;
           case 3 : return 0xf8;
           case 4 : return 0x6c;
           case 5 : return 0xdc;
           case 6 : return 0xde;
           case 7 : return 0x70;
           case 8 : return 0xfe;
           case 9 : return 0xfc;
           case 11 : return 0x1e;
           }
       }

  void Short_delay()
       {
       Delay_us(500);
       }


  void offset_up_time() // displays the requested time
       {
       PORTB = mask (up_time[5]); //load millisecond value to PORTB
       PORTA = PORTA | 0x05; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       PORTB = mask (up_time[4]);
       PORTA = PORTA | 0x01; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       PORTB = mask (up_time[3]);
       PORTA = PORTA | 0x06; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       PORTB = mask (up_time[2]);
       PORTA = PORTA | 0x03; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       PORTB = mask(up_time[1]);
       PORTA = PORTA | 0x04; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       if (up_time[0] == 0)Short_delay();
       else
          {
          PORTA = PORTA | 0x02; //enable the display of ones millisecond
          Short_delay();
          PORTA = PORTA & 0xff; //disable the display
          }
       }

void offset_down_time() // displays the requested time
       {
       PORTB = mask (down_time[5]); //load millisecond value to PORTB
       PORTA = PORTA | 0x05; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       PORTB = mask (down_time[4]);
       PORTA = PORTA | 0x01; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       PORTB = mask (down_time[3]);
       PORTA = PORTA | 0x06; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       PORTB = mask (down_time[2]);
       PORTA = PORTA | 0x03; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       PORTB = mask(down_time[1]);
       PORTA = PORTA | 0x04; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       if (down_time[0]== 0)Short_delay();
       else
          {
          PORTA = PORTA | 0x02; //enable the display of ones millisecond
          Short_delay();
          PORTA = PORTA & 0xff; //disable the display
          }
       }

void offset_up_time2() // displays the requested time
       {
       PORTB = mask (up_time[5]); //load millisecond value to PORTB
       PORTA = PORTA | 0x0d; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       PORTB = mask (up_time[4]);
       PORTA = PORTA | 0x09; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       PORTB = mask (up_time[3]);
       PORTA = PORTA | 0x0e; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       PORTB = mask (up_time[2]);
       PORTA = PORTA | 0x0b; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       PORTB = mask(up_time[1]);
       PORTA = PORTA | 0x0c; //enable the display of ones millisecond
       Short_delay();
       PORTB = 0;
       PORTA = PORTA & 0xff; //disable the display

       if (up_time[0] == 0)Short_delay();
       else
          {
          PORTA = PORTA | 0x0a; //enable the display of ones millisecond
          Short_delay();
          PORTA = PORTA & 0xff; //disable the display
          }
       }


void offset_down_time2() // displays the requested time
       {
       PORTB = mask (down_time[5]); //load millisecond value to PORTB

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -