ghi-electronics / TinyCLR-Libraries

Official Libraries supporting TinyCLR OS
https://www.ghielectronics.com/tinyclr/
17 stars 16 forks source link

Temperature becomes incorrect if initialize analog touch. #1125

Closed Palomino34 closed 2 years ago

Palomino34 commented 2 years ago
// Definitions in the class
        static IntPtr ts_reg1;
        static IntPtr ts_reg2;
        static IntPtr enable_reg;
        static AdcController controller;
        static AdcChannel channel;
        Text txtIntTemp;

// initializations in the Main()

            ts_reg1 = (IntPtr)0x1FF1E820;
            ts_reg2 = (IntPtr)0x1FF1E840;
            enable_reg = (IntPtr)((0x40000000U + 0x18020000 + 0x6300 + 8));
            controller = AdcController.FromName(TestHCRotaPins.ADC_Controller3);
            channel = controller.OpenChannel(TestHCRotaPins.ADC_InternalTemperature);            

            //while(true)
            //{
            var t = ReadDeviceTemperature();
           //Thread.Sleep(1000);
            //}
            // Timer to update temperature to a textbox
            Clock_Timer = new Timer(updateTenp, null, 0, 1000);
private static double ReadDeviceTemperature()
        {
            int disable_val = Marshal.ReadInt32(enable_reg);

            int enable_val = disable_val |= (1 << 23);

            Marshal.WriteInt32(enable_reg, enable_val);

            var v = channel.ReadValue() * 1.0;

            var ts1 = Marshal.ReadInt32(ts_reg1);
            var ts2 = Marshal.ReadInt32(ts_reg2);

            Marshal.WriteInt32(enable_reg, disable_val);

            var t1 = (110 - 30) * 1.0;
            var t2 = (ts2 - ts1) * 1.0;
            var t3 = (v - ts1) * 1.0;

            double temperature = t1 / t2 * t3 + 30;

            Debug.WriteLine("T = " + temperature + " Celsius");

            return temperature;
        }
        private void updateTemp(object o)
        {
            txtIntTemp.Dispatcher.BeginInvoke(new DispatcherOperationCallback(delegate
            {
                txtIntTemp.TextContent = ReadDeviceTemperature().ToString("F1");
                return null;
            }), null);
                 }
        private void updateTemp(object o)
        {
            txtIntTemp.Dispatcher.BeginInvoke(new DispatcherOperationCallback(delegate
            {
                txtIntTemp.TextContent = ReadDeviceTemperature().ToString("F1");
                return null;
            }), null);
                 }
var touch = new ResistiveTouchController(
               480, // Screen width
               272, // Screen height
               SC20260.GpioPin.PF10,  // digital pin support analog YU
               SC20260.GpioPin.PC2,  // digital pin support analog XL
               SC20260.GpioPin.PG7,  // digital pin YD
               SC20260.GpioPin.PG6,  // digital pin XR
               SC20260.Adc.Controller3.Id, // Analog controller id
               SC20260.Adc.Controller3.PF10, // analog channel id YU
               SC20260.Adc.Controller1.PC2 // analog channel id XL
               );
Palomino34 commented 2 years ago

This solution will help for now.

private static double ReadDeviceTemperature() { channel = controller.OpenChannel(SC20260.Adc.Controller3.InternalTemperatureSensor); // GHI added

        int disable_val = Marshal.ReadInt32(enable_reg);

        int enable_val = disable_val |= (1 << 23);

        Marshal.WriteInt32(enable_reg, enable_val);

        var v = channel.ReadValue() * 1.0;

        var ts1 = Marshal.ReadInt32(ts_reg1);
        var ts2 = Marshal.ReadInt32(ts_reg2);

        Marshal.WriteInt32(enable_reg, disable_val);

        var t1 = (110 - 30) * 1.0;
        var t2 = (ts2 - ts1) * 1.0;
        var t3 = (v - ts1) * 1.0;

        double temperature = t1 / t2 * t3 + 30;

        Debug.WriteLine("T = " + temperature + " Celsius");

        channel.Dispose();  // GHI added

        return temperature;
    }
Palomino34 commented 2 years ago

Done. Correct analog touch then this issue gone.