📄 f42x_weigh_scale.c
字号:
}
//------------------------------------------------------------------------------
// Converts the 32-Bit integer number 'Value' to BCD and outputs it
// on the LCD display
//------------------------------------------------------------------------------
void Disp_Signed_Long(long Value)
{
unsigned int i;
unsigned long Output;
char fNeg = 0;
if (Value < 0) // Test for negative value
{
Value = -Value; // Negate value
fNeg = 1; // Set negative flag
}
for (i = 32, Output = 0; i; i--) // BCD Conversion, 32-Bit
{
Output = __bcd_add_long(Output, Output);
if (Value & 0x80000000)
Output = __bcd_add_long(Output, 1);
Value <<= 1;
}
if (fNeg) // Display neg sign?
Output |= 0x80000000; // Bit 31 indicates neg. number
Disp_BCD(Output);
}
//------------------------------------------------------------------------------
// Displays the BCD number 'Value' on the LCD display
//------------------------------------------------------------------------------
void Disp_BCD(unsigned long Value)
{
char *pLCD = (char *)&LCDM6;
int i;
for (i = 0; i < 7; i++) // Process 7 digits
{
*pLCD++ = LCD_Tab[Value & 0x0f]; // Segments to LCD
Value >>= 4; // Process next digit
}
if (Value & 0x01)
LCDM4 |= 0x08; // Display "1" (8th digit)
else
LCDM4 &= ~0x08; // Clear "1" (8th digit)
if (Value & 0x08) // Bit 31 indicates neg. number
LCDM4 |= 0x40; // Display "-"
else
LCDM4 &= ~0x40; // Clear "-"
}
//------------------------------------------------------------------------------
void StartNextConversion(void)
{
SD16Temp = 0;
SD16TempCtr = 0;
P2OUT |= BRIDGE_SUPPLY; // Power-up bridge sensor
VoltageSettleCtr = 46; // Allow voltages to settle
// (46+4)x244us=12ms
SD16CCTL0 |= SD16SC; // Start conversion
}
//------------------------------------------------------------------------------
#pragma vector = SD16_VECTOR
__interrupt void SD16_ISR(void)
{
long CurrentResult;
GET_RESULT(CurrentResult); // Read SD16 result, clear IFG
if (VoltageSettleCtr) // Wait for voltages to settle
{
VoltageSettleCtr--; // Decrement counter
return; // Exit ISR
}
SD16Temp += CurrentResult; // Sum up results
if (++SD16TempCtr >= 256)
{
SD16Result = SD16Temp >> 8; // Div 256
SD16CCTL0 &= ~SD16SC; // Disable conversions
P2OUT &= ~BRIDGE_SUPPLY; // Power down bridge voltage
if (ProgramMode == PM_MEASURE)
if (Flags & FLAG_UPDATE_DISPL || LastADCValue != SD16Result)
{
Flags &= ~FLAG_UPDATE_DISPL; // Reset flag
LastADCValue = SD16Result; // Store new value
Disp_Signed_Long(((long)SD16Result - CalMin) * CAL_MIN_MAX_SPAN /
(CalMax - CalMin));
}
__bis_SR_register_on_exit(LPM3_bits); // Enter LPM3 on ISR exit
}
}
//------------------------------------------------------------------------------
#pragma vector = BASICTIMER_VECTOR
__interrupt void BT_ISR(void)
{
switch (ProgramMode)
{
case PM_MEASURE :
if (Flags & FLAG_BUTTON_DOWN)
if (P1IN & PUSH_BUTTON) // Was button released?
{ // Yes, then power down circuit
Flags &= ~FLAG_BUTTON_DOWN;
IE2 &= ~BTIE; // Disable Basic Timer interrupt
P2OUT &= ~BRIDGE_SUPPLY; // Power down bridge voltage
LCDCTL &= ~LCDON; // Disable LCD
SD16CCTL0 &= ~SD16SC; // Disable conversions
IFG2 &= ~BTIFG; // Clear Basic Timer int flag
ProgramMode = PM_PWRDOWN;
__bis_SR_register_on_exit(LPM3_bits); // Enter LPM3 on ISR exit
break;
}
else if (++ButtonDownCtr > 5) // Button still pressed,
{ // time expired?
Disp_BCD(0xfcadfd0); // Display 'CAL LO'
ProgramMode = PM_CAL_LO; // Enter calibration mode
}
StartNextConversion();
__bic_SR_register_on_exit(SCG1 + SCG0); // Enter LPM0 on exit
break;
case PM_CAL_LO :
case PM_CAL_HI :
StartNextConversion();
__bic_SR_register_on_exit(SCG1 + SCG0); // Enter LPM0 on exit
break;
}
}
//------------------------------------------------------------------------------
#pragma vector = PORT1_VECTOR
__interrupt void PORT1_ISR(void)
{
volatile unsigned int i; // 'volatile' to prevent opt.
switch (ProgramMode)
{
case PM_MEASURE :
Flags |= FLAG_BUTTON_DOWN;
ButtonDownCtr = 0;
break;
case PM_CAL_LO :
CalMinTmp = SD16Result; // Get conversion result
Disp_BCD(0xfcadfb1); // Display 'CAL HI'
ProgramMode = PM_CAL_HI; // Enter calibration mode (high)
break;
case PM_CAL_HI :
CalMaxTmp = SD16Result; // Get conversion result
if (CalMinTmp == CalMaxTmp) // Are calibr constants OK?
{
Disp_BCD(0xfcadfd0); // Display 'CAL LO'
ProgramMode = PM_CAL_LO; // Enter calibration mode
}
else // Calibration OK
{
StoreCalInFlash(); // Yes, program constants and
ProgramMode = PM_MEASURE; // Enter normal mode
Flags |= FLAG_UPDATE_DISPL; // Request display update
Flags &= ~FLAG_BUTTON_DOWN; // Clear button flag
}
break;
case PM_PWRDOWN :
IE2 |= BTIE; // Enable Basic Timer interrupt
P2OUT |= BRIDGE_SUPPLY; // Power-up bridge sensor
LCDCTL |= LCDON; // Enable LCD
StartNextConversion(); // Start conversion
Flags |= FLAG_UPDATE_DISPL; // Request display update
ProgramMode = PM_MEASURE;
__bic_SR_register_on_exit(SCG1 + SCG0); // Enter LPM0 on exit
break;
}
for (i = 0x7fff; i; i--); // Delay for key-debounce
P1IFG = 0x00; // Clear all P1 int flags
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -