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

📄 pulse_count.c

📁 《牛奶巴斯德杀菌系统》。一本嵌入式教材中的源码范例
💻 C
字号:
/*------------------------------------------------------------------*-

   Pulse_Count.C (v1.00)
 
  ------------------------------------------------------------------

   Count pulses from a mechanical switch or similar device.  
                                       ___
   Responds to falling edge of pulse:     |___


   COPYRIGHT
   ---------

   This code is associated with the book:

   EMBEDDED C by Michael J. Pont 
   [Pearson Education, 2002: ISBN: 0-201-79523-X].

   This code is copyright (c) 2001 by Michael J. Pont.
 
   See book for copyright details and other information.

-*------------------------------------------------------------------*/

#include "Main.H"
#include "Port.H"

#include "Bargraph.H"
#include "Pulse_Count.H"


// ------ Private function prototypes ------------------------------
void PULSE_COUNT_Check_Below_Threshold(const tByte);

// ------ Public variable declarations -----------------------------
// The data to be displayed
extern tBargraph Data_G;

// ------ Public variable definitions -------------------------------
// Set only after falling edge is detected
bit Falling_edge_G;

// ------ Private variable definitions ------------------------------
// The results of successive tests of the pulse signal
// (NOTE: Can't have arrays of bits...)
static bit Test4, Test3, Test2, Test1, Test0;

static tByte Total_G = 0;
static tWord Calls_G = 0;

// ------ Private constants ----------------------------------------

// Allows changed of logic without hardware changes
#define HI_LEVEL (0)
#define LO_LEVEL (1)

/*------------------------------------------------------------------*-

  PULSE_COUNT_Init()

  Initialization function for the switch library.

-*------------------------------------------------------------------*/
void PULSE_COUNT_Init(void)
   {
   Sw_pin = 1; // Use this pin for input

   // The tests (see text)
   Test4 = LO_LEVEL;
   Test3 = LO_LEVEL;
   Test2 = LO_LEVEL;
   Test1 = LO_LEVEL;
   Test0 = LO_LEVEL;
   }

/*------------------------------------------------------------------*-

  PULSE_COUNT_Check_Below_Threshold()

  Checks to see if pulse count is below a specified
  threshold value.  If it is, sounds an alarm.

-*------------------------------------------------------------------*/
void PULSE_COUNT_Check_Below_Threshold(const tByte THRESHOLD)
   {
   if (Data_G < THRESHOLD)
      {
      Alarm_pin = 0;
      }
   else
      {
      Alarm_pin = 1;
      }
   }

/*------------------------------------------------------------------*-

  PULSE_COUNT_Update()
  
  This is the main switch function.  

  It should be called every 30 ms 
  (to allow for typical 20ms debounce time).
 
-*------------------------------------------------------------------*/
void PULSE_COUNT_Update(void)
   {
   // Clear timer flag
   TF2 = 0;

   // Shuffle the test results
   Test4 = Test3;
   Test3 = Test2;
   Test2 = Test1;
   Test1 = Test0;

   // Get latest test result
   Test0 = Sw_pin;

   // Required result:
   // Test4 == HI_LEVEL
   // Test3 == HI_LEVEL
   // Test1 == LO_LEVEL
   // Test0 == LO_LEVEL

   if ((Test4 == HI_LEVEL) && 
       (Test3 == HI_LEVEL) && 
       (Test1 == LO_LEVEL) && 
       (Test0 == LO_LEVEL))
      {
      // Falling edge detected
      Falling_edge_G = 1;
      }
   else
      {
      // Default
      Falling_edge_G = 0;
      }

   // Calculate average every 45 calls to this task
   // - maximum count over this period is 9 pulses
   //   if (++Calls_G < 45)

   // 450 used here for test purposes (in simulator)
   // [Because there is a limit to how fast you can simulate pulses
   // by hand...]
   if (++Calls_G < 450)  
      {
      Total_G += (int) Falling_edge_G;
      }
   else
      {
      // Update the display
      Data_G = Total_G; // Max is 9
      Total_G = 0;
      Calls_G = 0;
      PULSE_COUNT_Check_Below_Threshold(3);
      BARGRAPH_Update();
      }
   }

/*------------------------------------------------------------------*-
  ---- END OF FILE -------------------------------------------------
-*------------------------------------------------------------------*/

⌨️ 快捷键说明

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