Exemplo n.º 1
0
        ADCChip whichADCChip = ADCChip.mcp3208;  //ADCChip.mcp3008;

        public MainPage()
        {
            this.InitializeComponent();
            this.InitializeComponent();
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(500);
            timer.Tick += Timer_Tick;
            timer.Start();

            whichADCChip = ADCChip.mcp3208; // mcp3008;
            switch (whichADCChip)
            {
                case ADCChip.mcp3002:
                    {
                        // To line everything up for ease of reading back (on byte boundary) we 
                        // will pad the command start bit with 1 leading "0" bit

                        // Write 0SGO MNxx xxxx xxxx
                        // Read  ???? ?N98 7654 3210
                        // S = start bit
                        // G = Single / Differential
                        // O = Chanel data 
                        // M = Most significant bit mode 
                        // ? = undefined, ignore
                        // N = 0 "Null bit"
                        // 9-0 = 10 data bits

                        // 0110 1000 = 1 0 pad bit, start bit, Single ended, odd (channel 0), MSFB only, 2 clocking bits
                        // 0000 0000 = 8 clocking bits
                        readBuffer = new byte[2] { 0x00, 0x00 };
                        writeBuffer = new byte[2] { 0x68, 0x00 };
                    }
                    break;

                case ADCChip.mcp3008:
                    {
                        // To line everything up for ease of reading back (on byte boundary) we 
                        // will pad the command start bit with 7 leading "0" bits

                        // Write 0000 000S GDDD xxxx xxxx xxxx
                        // Read  ???? ???? ???? ?N98 7654 3210
                        // S = start bit
                        // G = Single / Differential
                        // D = Chanel data 
                        // ? = undefined, ignore
                        // N = 0 "Null bit"
                        // 9-0 = 10 data bits

                        // 0000 01 = 7 pad bits, start bit
                        // 1000 0000 = single ended, channel bit 2, channel bit 1, channel bit 0, 4 clocking bits
                        // 0000 0000 = 8 clocking bits
                        readBuffer = new byte[3] { 0x00, 0x00, 0x00 };
                        writeBuffer = new byte[3] { 0x01, 0x80, 0x00 };
                    }
                    break;

                case ADCChip.mcp3208:
                    {
                        /* mcp3208 is 12 bits output */
                        // To line everything up for ease of reading back (on byte boundary) we 
                        // will pad the command start bit with 5 leading "0" bits

                        // Write 0000 0SGD DDxx xxxx xxxx xxxx
                        // Read  ???? ???? ???N BA98 7654 3210
                        // S = start bit
                        // G = Single / Differential
                        // D = Chanel data 
                        // ? = undefined, ignore
                        // N = 0 "Null bit"
                        // B-0 = 12 data bits


                        // 0000 0110 = 5 pad bits, start bit, single ended, channel bit 2
                        // 0000 0000 = channel bit 1, channel bit 0, 6 clocking bits
                        // 0000 0000 = 8 clocking bits
                        readBuffer = new byte[3] { 0x00, 0x00, 0x00 };
                        //writeBuffer = new byte[3] { 0x06, 0x00, 0x00 };
                        //通道4
                        writeBuffer = new byte[3] { 0x07, 0x00, 0x00 };
                    }
                    break;
            }

            InitSPI();
        }
Exemplo n.º 2
0
        public MainPage()
        {
            this.InitializeComponent();
            this.InitializeComponent();
            timer          = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(500);
            timer.Tick    += Timer_Tick;
            timer.Start();

            whichADCChip = ADCChip.mcp3008;
            switch (whichADCChip)
            {
            case ADCChip.mcp3002:
            {
                // To line everything up for ease of reading back (on byte boundary) we
                // will pad the command start bit with 1 leading "0" bit

                // Write 0SGO MNxx xxxx xxxx
                // Read  ???? ?N98 7654 3210
                // S = start bit
                // G = Single / Differential
                // O = Chanel data
                // M = Most significant bit mode
                // ? = undefined, ignore
                // N = 0 "Null bit"
                // 9-0 = 10 data bits

                // 0110 1000 = 1 0 pad bit, start bit, Single ended, odd (channel 0), MSFB only, 2 clocking bits
                // 0000 0000 = 8 clocking bits
                readBuffer = new byte[2] {
                    0x00, 0x00
                };
                writeBuffer = new byte[2] {
                    0x68, 0x00
                };
            }
            break;

            case ADCChip.mcp3008:
            {
                // To line everything up for ease of reading back (on byte boundary) we
                // will pad the command start bit with 7 leading "0" bits

                // Write 0000 000S GDDD xxxx xxxx xxxx
                // Read  ???? ???? ???? ?N98 7654 3210
                // S = start bit
                // G = Single / Differential
                // D = Chanel data
                // ? = undefined, ignore
                // N = 0 "Null bit"
                // 9-0 = 10 data bits

                // 0000 01 = 7 pad bits, start bit
                // 1000 0000 = single ended, channel bit 2, channel bit 1, channel bit 0, 4 clocking bits
                // 0000 0000 = 8 clocking bits
                readBuffer = new byte[3] {
                    0x00, 0x00, 0x00
                };
                writeBuffer = new byte[3] {
                    0x01, 0x80, 0x00
                };
            }
            break;

            case ADCChip.mcp3208:
            {
                /* mcp3208 is 12 bits output */
                // To line everything up for ease of reading back (on byte boundary) we
                // will pad the command start bit with 5 leading "0" bits

                // Write 0000 0SGD DDxx xxxx xxxx xxxx
                // Read  ???? ???? ???N BA98 7654 3210
                // S = start bit
                // G = Single / Differential
                // D = Chanel data
                // ? = undefined, ignore
                // N = 0 "Null bit"
                // B-0 = 12 data bits


                // 0000 0110 = 5 pad bits, start bit, single ended, channel bit 2
                // 0000 0000 = channel bit 1, channel bit 0, 6 clocking bits
                // 0000 0000 = 8 clocking bits
                readBuffer = new byte[3] {
                    0x00, 0x00, 0x00
                };
                writeBuffer = new byte[3] {
                    0x06, 0x00, 0x00
                };
            }
            break;
            }

            InitSPI();
        }
        public MainPage()
        {
            this.InitializeComponent();
            this.InitializeComponent();
            _azureIoTHubService = new AzureIoTHubService();
            timer          = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(3000);
            timer.Tick    += async(s, e) =>
            {
                SpiDisplay.TransferFullDuplex(writeBuffer, readBuffer);
                res = convertToInt(readBuffer);
                var forceSensorValue = res.ToString();

                textPlaceHolder.Text = forceSensorValue;
                System.Diagnostics.Debug.WriteLine("FORCE SENSOR VALUE: " + forceSensorValue);

                if (res > 0)
                {
                    await _azureIoTHubService.SendDataToAzure(forceSensorValue);
                }
            };
            timer.Start();

            whichADCChip = ADCChip.mcp3008;
            switch (whichADCChip)
            {
            case ADCChip.mcp3002:
            {
                // To line everything up for ease of reading back (on byte boundary) we
                // will pad the command start bit with 1 leading "0" bit

                // Write 0SGO MNxx xxxx xxxx
                // Read  ???? ?N98 7654 3210
                // S = start bit
                // G = Single / Differential
                // O = Chanel data
                // M = Most significant bit mode
                // ? = undefined, ignore
                // N = 0 "Null bit"
                // 9-0 = 10 data bits

                // 0110 1000 = 1 0 pad bit, start bit, Single ended, odd (channel 0), MSFB only, 2 clocking bits
                // 0000 0000 = 8 clocking bits
                readBuffer = new byte[2] {
                    0x00, 0x00
                };
                writeBuffer = new byte[2] {
                    0x68, 0x00
                };
            }
            break;

            case ADCChip.mcp3008:
            {
                // To line everything up for ease of reading back (on byte boundary) we
                // will pad the command start bit with 7 leading "0" bits

                // Write 0000 000S GDDD xxxx xxxx xxxx
                // Read  ???? ???? ???? ?N98 7654 3210
                // S = start bit
                // G = Single / Differential
                // D = Chanel data
                // ? = undefined, ignore
                // N = 0 "Null bit"
                // 9-0 = 10 data bits

                // 0000 01 = 7 pad bits, start bit
                // 1000 0000 = single ended, channel bit 2, channel bit 1, channel bit 0, 4 clocking bits
                // 0000 0000 = 8 clocking bits
                readBuffer = new byte[3] {
                    0x00, 0x00, 0x00
                };
                writeBuffer = new byte[3] {
                    0x01, 0x80, 0x00
                };
            }
            break;

            case ADCChip.mcp3208:
            {
                /* mcp3208 is 12 bits output */
                // To line everything up for ease of reading back (on byte boundary) we
                // will pad the command start bit with 5 leading "0" bits

                // Write 0000 0SGD DDxx xxxx xxxx xxxx
                // Read  ???? ???? ???N BA98 7654 3210
                // S = start bit
                // G = Single / Differential
                // D = Chanel data
                // ? = undefined, ignore
                // N = 0 "Null bit"
                // B-0 = 12 data bits


                // 0000 0110 = 5 pad bits, start bit, single ended, channel bit 2
                // 0000 0000 = channel bit 1, channel bit 0, 6 clocking bits
                // 0000 0000 = 8 clocking bits
                readBuffer = new byte[3] {
                    0x00, 0x00, 0x00
                };
                writeBuffer = new byte[3] {
                    0x06, 0x00, 0x00
                };
            }
            break;
            }

            InitSPI();
        }