예제 #1
0
        private void buttonCalibrate_Click(object sender, EventArgs e)
        {
            float Vdd = 0, Vpp = 0;
            float measuredVdd = 0;
            bool  calSucceed  = true;

            try
            {
                measuredVdd = float.Parse(textBoxVDD.Text);
            }
            catch
            {
                MessageBox.Show("Invalid 'volts measured' value.");
                return;
            }

            // Cal the ADC results
            Pk2.ReadPICkitVoltages(ref Vdd, ref Vpp);
            measuredVdd /= Vdd;  //ratio
            if (measuredVdd > 1.25F)
            {
                measuredVdd = 1.25F;
                calSucceed  = false;
            }
            if (measuredVdd < 0.75F)
            {
                measuredVdd = 0.75F;
                calSucceed  = false;
            }
            float calFactor = 256F * measuredVdd;              // 512 is 1:1 calibration factor

            Pk2.SetVoltageCals((ushort)calFactor, 0x00, 0x80); // leave Vdd cals unchanged for now.

            // Now that we have an accurate ADC reading, we can self-cal the VDD setpoints.
            // Vdd Offset = (3 - (4*V3)/V4)*CCP4
            // Vdd CalFactor = 1/(V4 - V3) * 128.
            //     where V3 = actual voltage at SetVDDVoltage(3.0)
            //           V4 = actual voltage at SetVDDVoltage(4.0)
            //           CCP4 = CalculateVddCPP(4.0F) >> 6
            float offset = 0;
            float calCCP = 0;
            float Vdd3v  = 0;

            Pk2.SetVDDVoltage(3.0F, 2.00F);
            Thread.Sleep(150);
            Pk2.ReadPICkitVoltages(ref Vdd, ref Vpp);
            Vdd3v = Vdd;
            Pk2.SetVDDVoltage(4.0F, 2.70F);
            Thread.Sleep(150);
            Pk2.ReadPICkitVoltages(ref Vdd, ref Vpp);
            offset = (3 - (4 * Vdd3v) / Vdd) * (Pk2.CalculateVddCPP(4.0F) >> 6);
            if (offset > 127F)
            {
                offset     = 127F;
                calSucceed = false;
            }
            if (offset < -128F)
            {
                offset     = -128F;
                calSucceed = false;
            }
            calCCP = (1 / (Vdd - Vdd3v)) * 128;
            if (calCCP > 173) // 135%
            {
                calCCP     = 173;
                calSucceed = false;
            }
            if (calCCP < 83) // 65%
            {
                calCCP     = 83;
                calSucceed = false;
            }

            if (calSucceed)
            {
                labelGoodCal.Visible = true;
                labelBadCal.Visible  = false;
                Pk2.SetVoltageCals((ushort)calFactor, (byte)offset, (byte)(calCCP + 0.5));
            }
            else
            {
                labelGoodCal.Visible = false;
                labelBadCal.Visible  = true;
                Pk2.SetVoltageCals(0x0100, 0x00, 0x80); // leave uncal'd
            }
            buttonCalibrate.Enabled = false;
            Pk2.VddOff();
        }