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

📄 f350_weigh_scale.c

📁 his header file that is used to define all preprossor directives, global variables, and prototype
💻 C
📖 第 1 页 / 共 2 页
字号:
   FLASH_PageUpdate ((signed char *)&temp_LONG_1.b[0], pwrite, 4);
}

//-----------------------------------------------------------------------------
// Calculate_One_Count
//-----------------------------------------------------------------------------
// This routine calculates the tare weight of the scale in digital counts
// Then this function assumes the user places 100 equal units to be weighed
// on scale.  From these two measurements the count of one unit is computed.
// From this, the number of units on the scales can be determined in future
// measurements
void Calculate_One_Count(void)
{  unsigned char xdata *idata pwrite;// FLASH write pointer
   char i;

   ADC0CN   = 0x00;                    // Unipolar; Gain = 1
   ADC0DECH = 0x04;                    // Set Output word rate at for ~23 Hz
   ADC0DECL = 0x00;                    // Set Output word rate at for ~23 Hz
   ADC0MUX  = VSCALE;                  // Select appropriate input for AMUX
   
   Tare_Count = 0;                     // Initialize to zero
   for(i=5;i;--i)                      // Average next 5 conversions
   {	
      temp_LONG_1.l = 0;
      AD0INT = 0;                      // Clear end-of-conversion indicator
      ADC0MD   = 0x82;                 // Enable ADC; Single conversions
      while(!AD0INT);                  // Wait for conversion to complete
      temp_LONG_1.l = ADC0H;
      temp_LONG_1.l = temp_LONG_1.l <<16;
	  temp_LONG_1.l += ADC0M <<8;
      temp_LONG_1.l += ADC0L ;
      Tare_Count += temp_LONG_1.l;
   }
   Tare_Count = Tare_Count/5;          // Store the Tare Digital Count Value

   Full_Counts = 0;                    // Initialize to zero
   for(i=5;i;--i)                      // Average next 5 conversions
   {	
      temp_LONG_1.l = 0;
      AD0INT = 0;                      // Clear end-of-conversion indicator
      ADC0MD   = 0x82;                 // Enable ADC; Single conversions
      while(!AD0INT);                  // Wait for conversion to complete
      temp_LONG_1.l = ADC0H;
      temp_LONG_1.l = temp_LONG_1.l <<16;
	  temp_LONG_1.l += ADC0M <<8;
      temp_LONG_1.l += ADC0L ;
      Full_Counts += temp_LONG_1.l;
   }

   Full_Counts = Full_Counts/ 5;       // Store the Calibration Count Value; 
                                       //     unually 100 units
   
   One_Count = Full_Counts - Tare_Count;
   One_Count = One_Count/100;          // Divide by 100 assumes there are 
                                       //     100 cal units on scale

   ADC0MD   = 0x00;                    // Turn off ADC Module

                                       // Memory's already been erased at 0x1A00
                                       // Store Gain Coefficients to FLASH
   temp_LONG_1.l = 1234;               // Prepare for FLASH Write
   pwrite = (char xdata *)&(DATA_PAGE[tare_count].l);
   FLASH_PageUpdate ((signed char *)&temp_LONG_1.b[0], pwrite, 4);

   temp_LONG_1.l = 1234;               // Prepare for FLASH Write
   pwrite = (char xdata *)&(DATA_PAGE[full_counts].l);
   FLASH_PageUpdate ((signed char *)&temp_LONG_1.b[0], pwrite, 4);

   temp_LONG_1.l = 1234;               // Prepare for FLASH Write
   pwrite = (char xdata *)&(DATA_PAGE[one_count].l);
   FLASH_PageUpdate ((signed char *)&temp_LONG_1.b[0], pwrite, 4);
}


//-----------------------------------------------------------------------------
// Monitor Weigh_Scale
//-----------------------------------------------------------------------------
// This routine configures the ADC's AMUX, acquires conversions and returns
// appropriate parameter (weight or temperature) via variable result.
// Weight is returned in Unit counts. Example 100 pennies is 100.
// Temperature is returned in degree Kelvin.  Ex 273 Kelvin returns a value 273
// 
int Monitor_Weigh_Scale(unsigned char value)
{  char i;
   unsigned long av =0,delay_count=0;
   long signed result;

   ADC0DECH = 0x04;                    // Set Output word rate at for 23 Hz
   ADC0DECL = 0x00;                    // Set Output word rate at for 23 Hz
 
   switch (value)
   {
      case TEMPERATURE:
         ADC0CN  = 0x00;               // Unipolar; Gain = 1
         ADC0MUX = TSCALE;             // Select appropriate input for AMUX	  
         break;
     
      case WEIGHT: 
         ADC0CN   = 0x00;              // Unipolar; Gain = 1
         ADC0MUX  = VSCALE;            // Select appropriate input for AMUX
         break;   
   }  
   //Compute average of next 5 A/D conversions
   av = 0;                             // Initialize to Zero
   for(i=5;i;--i)                      // Average next 5 conversions
   {	
      temp_LONG_1.l = 0;
      AD0INT = 0;                      // Clear end-of-conversion indicator
      ADC0MD   = 0x82;                 // Enable ADC; Single conversions
      while(!AD0INT);                  // Wait for conversion to complete
      temp_LONG_1.l = ADC0H;
      temp_LONG_1.l = temp_LONG_1.l <<16;
	  temp_LONG_1.l += ADC0M <<8;
      temp_LONG_1.l += ADC0L ;
      av += temp_LONG_1.l;
   }
   
   ADC0MD   = 0x00;                    // Turn off ADC Module

   av = av/5;                          // Compute the average
	
   switch (value)
   {	
      case TEMPERATURE:
         result =  (long) av /TEMP_SLOPE*1000; // Account for Temp. Slope
         result -= 100*TEMP_SENSOR_OFFSET;     // Account for Temp. Offset
         result = result + 27315;      // Convert to Degrees Kelvin 
         break;
      
      case WEIGHT:
        result = av - Tare_Count;
        result = result/One_Count;
        break;
   }
   return (signed int) result;  	
}

void Update_Display_Via_UART(void)
{
   // Send Information to Hyper Terminal at 9600 Baud,8,n,1
   //printf ("Temperature = %d hundredths degrees K\n", Temperature);
   printf ("Counts = %d  units\n", (int)Weight);
}
//-----------------------------------------------------------------------------
// Update_Display_Via_LCD
//-----------------------------------------------------------------------------
// This function provides starter algorithms for the HT1620,(Holtek) LCD driver.
// It provides algorithms to write to te HT1620 and Read from the HT1620.
// Specific algorithms to activate and deactivate the elements on the LCD that 
// correspond to specific letters and numerals will need to be develop.
//
void Update_Display_Via_LCD(void)
{  unsigned char BIT_count; // counter for SPI transaction

   // Here is an example of a write command to the HT1620
   Data_Word = 0x1400;                 // Prepare information for writing to LCD
   Data_Word = Data_Word << 3;         // Prepare information for writing to LCD
   CS   = 0;                           // Select Serial Port
   for (BIT_count = 13; BIT_count > 0;BIT_count--)// 13 bits 
   {
      DATA = Data_Word & 0x8000;       // put current outgoing bit on DATA
      Data_Word = Data_Word <<1;       // shift next bit into MSB
      
      WR = 0x01;                       //set sck high

      WR = 0x00;                       // set sck low
   } 

   CS   = 1;                           // Deselect LCD

   // Here is an example of a read command to the HT1620

   Data_Word = 0x1800;                 // Prepare information for writing to LCD
   Data_Word = Data_Word << 3;         // Prepare information for writing to LCD
   CS   = 0;                           // Select LCD
   for (BIT_count = 9; BIT_count > 0;BIT_count--)// 9 bits 
   {
      DATA = Data_Word & 0x8000;       // put current outgoing bit on DATA
      Data_Word = Data_Word <<1;       // shift next bit into MSB

      WR = 0x01;                       // set sck high

      Data_Word |= DATA;               // capture current bit on DATA
      
      WR = 0x00;                       // set sck low
   } 
   for (BIT_count = 4; BIT_count > 0;BIT_count--)// 4 bits 
   {
      DATA = Data_Word & 0x8000;       // put current outgoing bit on DATA
      Data_Word = Data_Word <<1;       // shift next bit into MSB
      
      RD = 0x01;                       // set sck high
      
      Data_Word |= DATA;               // capture current bit on DATA
      
      RD = 0x00;                       // set sck low
   } 

   CS   = 1;                           // Deselect LCD   
}

//-----------------------------------------------------------------------------
// Main Function
//-----------------------------------------------------------------------------
// - Main calls all the functions necessary to configure the C8051F350.
// 		It also calls routines to calibrate the electronic scale.  Once setup
//      and calibration are complete, Main() determines the Unit count on 
//      the scale and outputs via the UART. 
//
//      System Calibration is only performed the first time through the cycle.
//
void main(void)
{  
   Config_F350();                      // Config F350

   CalibrateADCforMeasurement();       // Calibrate ADC

   Calculate_One_Count();              // Calculate the size of one count
   
   // Update Temperature  and Bridge Monitoring Algorithms
   while(1)
   {                                   // Once pressed get temperature and weight
	  Temperature = Monitor_Weigh_Scale (TEMPERATURE);// Get Latest Temperature 
                                       // Acquire Pack's Voltages
      Weight = Monitor_Weigh_Scale (WEIGHT);
 
                                       // Update PC Via UART      
      Update_Display_Via_UART();
                                       // Update PC Via LCD     
      //Update_Display_Via_LCD();	  
   }
}
// END of File

⌨️ 快捷键说明

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