Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using Microsoft.SPOT;
- using Microsoft.SPOT.Presentation;
- using Microsoft.SPOT.Presentation.Controls;
- using Microsoft.SPOT.Presentation.Media;
- using Microsoft.SPOT.Touch;
- using Gadgeteer.Networking;
- using GT = Gadgeteer;
- using GTM = Gadgeteer.Modules;
- using Gadgeteer.Modules.GHIElectronics;
- namespace Gadgeteer_MPR121_Capacitance_01
- {
- public partial class Program
- {
- //GT.Timer timer = new GT.Timer(1000, GT.Timer.BehaviorType.RunContinuously);
- GT.Interfaces.I2CBus touch;
- GT.Interfaces.InterruptInput interrupt;
- // This method is run when the mainboard is powered up or reset.
- void ProgramStarted()
- {
- //timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
- //timer.Start();
- GT.Socket socket = GT.Socket.GetSocket(10, true, null, null);
- touch = new GT.Interfaces.I2CBus(socket, 0x5A, 400, extender);
- //touch.Address = 0x5A;
- interrupt = new GT.Interfaces.InterruptInput(socket, GT.Socket.Pin.Three, GT.Interfaces.GlitchFilterMode.Off, GT.Interfaces.ResistorMode.Disabled, GT.Interfaces.InterruptMode.RisingAndFallingEdge, extender);
- interrupt.Interrupt += new GT.Interfaces.InterruptInput.InterruptEventHandler(interrupt_Interrupt);
- mpr121Setup();
- // print something so we know we have started
- Debug.Print("Program Started");
- }
- void mpr121Setup()
- {
- // MPR121 Register Defines
- byte MHD_R = 0x2B;
- byte NHD_R = 0x2C;
- byte NCL_R = 0x2D;
- byte FDL_R = 0x2E;
- byte MHD_F = 0x2F;
- byte NHD_F = 0x30;
- byte NCL_F = 0x31;
- byte FDL_F = 0x32;
- byte ELE0_T = 0x41;
- byte ELE0_R = 0x42;
- byte ELE1_T = 0x43;
- byte ELE1_R = 0x44;
- byte ELE2_T = 0x45;
- byte ELE2_R = 0x46;
- byte ELE3_T = 0x47;
- byte ELE3_R = 0x48;
- byte ELE4_T = 0x49;
- byte ELE4_R = 0x4A;
- byte ELE5_T = 0x4B;
- byte ELE5_R = 0x4C;
- byte ELE6_T = 0x4D;
- byte ELE6_R = 0x4E;
- byte ELE7_T = 0x4F;
- byte ELE7_R = 0x50;
- byte ELE8_T = 0x51;
- byte ELE8_R = 0x52;
- byte ELE9_T = 0x53;
- byte ELE9_R = 0x54;
- byte ELE10_T = 0x55;
- byte ELE10_R = 0x56;
- byte ELE11_T = 0x57;
- byte ELE11_R = 0x58;
- byte FIL_CFG = 0x5D;
- byte ELE_CFG = 0x5E;
- byte GPIO_CTRL0 = 0x73;
- byte GPIO_CTRL1 = 0x74;
- byte GPIO_DATA = 0x75;
- byte GPIO_DIR = 0x76;
- byte GPIO_EN = 0x77;
- byte GPIO_SET = 0x78;
- byte GPIO_CLEAR = 0x79;
- byte GPIO_TOGGLE = 0x7A;
- byte ATO_CFG0 = 0x7B;
- byte ATO_CFGU = 0x7D;
- byte ATO_CFGL = 0x7E;
- byte ATO_CFGT = 0x7F;
- // Global Constants
- byte TOU_THRESH = 0x06;
- byte REL_THRESH = 0x0A;
- // Section A
- // This group controls filtering when data is > baseline.
- mpr121Write(MHD_R, 0x01);
- mpr121Write(NHD_R, 0x01);
- mpr121Write(NCL_R, 0x00);
- mpr121Write(FDL_R, 0x00);
- // Section B
- // This group controls filtering when data is < baseline.
- mpr121Write(MHD_F, 0x01);
- mpr121Write(NHD_F, 0x01);
- mpr121Write(NCL_F, 0xFF);
- mpr121Write(FDL_F, 0x02);
- // Section C
- // This group sets touch and release thresholds for each electrode
- mpr121Write(ELE0_T, TOU_THRESH);
- mpr121Write(ELE0_R, REL_THRESH);
- mpr121Write(ELE1_T, TOU_THRESH);
- mpr121Write(ELE1_R, REL_THRESH);
- mpr121Write(ELE2_T, TOU_THRESH);
- mpr121Write(ELE2_R, REL_THRESH);
- mpr121Write(ELE3_T, TOU_THRESH);
- mpr121Write(ELE3_R, REL_THRESH);
- mpr121Write(ELE4_T, TOU_THRESH);
- mpr121Write(ELE4_R, REL_THRESH);
- mpr121Write(ELE5_T, TOU_THRESH);
- mpr121Write(ELE5_R, REL_THRESH);
- mpr121Write(ELE6_T, TOU_THRESH);
- mpr121Write(ELE6_R, REL_THRESH);
- mpr121Write(ELE7_T, TOU_THRESH);
- mpr121Write(ELE7_R, REL_THRESH);
- mpr121Write(ELE8_T, TOU_THRESH);
- mpr121Write(ELE8_R, REL_THRESH);
- mpr121Write(ELE9_T, TOU_THRESH);
- mpr121Write(ELE9_R, REL_THRESH);
- mpr121Write(ELE10_T, TOU_THRESH);
- mpr121Write(ELE10_R, REL_THRESH);
- mpr121Write(ELE11_T, TOU_THRESH);
- mpr121Write(ELE11_R, REL_THRESH);
- // Section D
- // Set the Filter Configuration
- // Set ESI2
- mpr121Write(FIL_CFG, 0x04);
- // Section E
- // Electrode Configuration
- // Enable 6 Electrodes and set to run mode
- // Set ELE_CFG to 0x00 to return to standby mode
- mpr121Write(ELE_CFG, 0x0C); // Enables all 12 Electrodes
- //mpr121Write(ELE_CFG, 0x06); // Enable first 6 electrodes
- // Section F
- // Enable Auto Config and auto Reconfig
- /*mpr121Write(ATO_CFG0, 0x0B);
- mpr121Write(ATO_CFGU, 0xC9); // USL = (Vdd-0.7)/vdd*256 = 0xC9 @3.3V
- mpr121Write(ATO_CFGL, 0x82); // LSL = 0.65*USL = 0x82 @3.3V
- mpr121Write(ATO_CFGT, 0xB5);*/
- // Target = 0.9*USL = 0xB5 @3.3V
- }
- void mpr121Write(byte settingName, byte settingValue)
- {
- byte[] writeBuffer = new byte[] { settingName, settingValue };
- //byte[] readBuffer = new byte[2];
- //touch.Address = 0x5A;
- touch.Write(writeBuffer, 10);
- //Debug.Print(readBuffer[]);
- }
- void interrupt_Interrupt(GT.Interfaces.InterruptInput sender, bool value)
- {
- Debug.Print("interupt detected from " + sender);
- // create some array thing to hold onto the results
- byte[] readResult = new byte[2];
- // read the result from the MPR121
- touch.Read(readResult, 10);
- // the result comes in as two numbers like 11111111 and 00001111 needs to be 11111111 + 00001111 = 000011111111111
- byte lowerByte = readResult[0];
- Debug.Print("byte 0 = " + readResult[0].ToString());
- byte higherByte = readResult[1];
- Debug.Print("byte 1 = " + readResult[1].ToString());
- // start to add them together
- // convert from an 8 bit to a 16 bit number
- UInt16 readBuffer = higherByte;
- // move this double byte (16 bit number) to the left by 8 steps (this is so machine code! I love it! 1982 Z80A anyone?)
- readBuffer <<= 8;
- // add them with an OR 0+0=0 0+1=1 1+0=1 1+1=1
- readBuffer = (readBuffer |= lowerByte);
- // print the result in decimal
- Debug.Print("combined = " + readBuffer);
- // check what electrodes were pressed
- for (int increment = 0; increment < 12; increment++)
- {
- // reset the comparison bit
- UInt16 indexByte = 1;
- // shift the 1 to the left each increment
- indexByte <<= increment;
- //Debug.Print(indexByte.ToString());
- // compare each bit with an AND &= 0+0=0 0+1=0 1+0=0 1+1=1
- if ((readBuffer & indexByte) != 0)
- {
- Debug.Print("pin " + increment + " was touched");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment