peony3000

Gadgeteer MPR121 Touch

May 4th, 2012
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.90 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using Microsoft.SPOT;
  4. using Microsoft.SPOT.Presentation;
  5. using Microsoft.SPOT.Presentation.Controls;
  6. using Microsoft.SPOT.Presentation.Media;
  7. using Microsoft.SPOT.Touch;
  8.  
  9. using Gadgeteer.Networking;
  10. using GT = Gadgeteer;
  11. using GTM = Gadgeteer.Modules;
  12. using Gadgeteer.Modules.GHIElectronics;
  13.  
  14. namespace Gadgeteer_MPR121_Capacitance_01
  15. {
  16.  
  17.     public partial class Program
  18.     {
  19.  
  20.         //GT.Timer timer = new GT.Timer(1000, GT.Timer.BehaviorType.RunContinuously);
  21.         GT.Interfaces.I2CBus touch;
  22.         GT.Interfaces.InterruptInput interrupt;
  23.  
  24.         // This method is run when the mainboard is powered up or reset.  
  25.         void ProgramStarted()
  26.         {
  27.  
  28.             //timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
  29.             //timer.Start();
  30.  
  31.             GT.Socket socket = GT.Socket.GetSocket(10, true, null, null);
  32.             touch = new GT.Interfaces.I2CBus(socket, 0x5A, 400, extender);
  33.  
  34.             //touch.Address = 0x5A;
  35.  
  36.             interrupt = new GT.Interfaces.InterruptInput(socket, GT.Socket.Pin.Three, GT.Interfaces.GlitchFilterMode.Off, GT.Interfaces.ResistorMode.Disabled, GT.Interfaces.InterruptMode.RisingAndFallingEdge, extender);
  37.             interrupt.Interrupt += new GT.Interfaces.InterruptInput.InterruptEventHandler(interrupt_Interrupt);
  38.  
  39.             mpr121Setup();
  40.  
  41.  
  42.  
  43.             // print something so we know we have started
  44.             Debug.Print("Program Started");
  45.         }
  46.  
  47.         void mpr121Setup()
  48.         {
  49.             // MPR121 Register Defines
  50.             byte MHD_R = 0x2B;
  51.             byte NHD_R = 0x2C;
  52.             byte NCL_R = 0x2D;
  53.             byte FDL_R = 0x2E;
  54.             byte MHD_F = 0x2F;
  55.             byte NHD_F = 0x30;
  56.             byte NCL_F = 0x31;
  57.             byte FDL_F = 0x32;
  58.             byte ELE0_T = 0x41;
  59.             byte ELE0_R = 0x42;
  60.             byte ELE1_T = 0x43;
  61.             byte ELE1_R = 0x44;
  62.             byte ELE2_T = 0x45;
  63.             byte ELE2_R = 0x46;
  64.             byte ELE3_T = 0x47;
  65.             byte ELE3_R = 0x48;
  66.             byte ELE4_T = 0x49;
  67.             byte ELE4_R = 0x4A;
  68.             byte ELE5_T = 0x4B;
  69.             byte ELE5_R = 0x4C;
  70.             byte ELE6_T = 0x4D;
  71.             byte ELE6_R = 0x4E;
  72.             byte ELE7_T = 0x4F;
  73.             byte ELE7_R = 0x50;
  74.             byte ELE8_T = 0x51;
  75.             byte ELE8_R = 0x52;
  76.             byte ELE9_T = 0x53;
  77.             byte ELE9_R = 0x54;
  78.             byte ELE10_T = 0x55;
  79.             byte ELE10_R = 0x56;
  80.             byte ELE11_T = 0x57;
  81.             byte ELE11_R = 0x58;
  82.             byte FIL_CFG = 0x5D;
  83.             byte ELE_CFG = 0x5E;
  84.             byte GPIO_CTRL0 = 0x73;
  85.             byte GPIO_CTRL1 = 0x74;
  86.             byte GPIO_DATA = 0x75;
  87.             byte GPIO_DIR = 0x76;
  88.             byte GPIO_EN = 0x77;
  89.             byte GPIO_SET = 0x78;
  90.             byte GPIO_CLEAR = 0x79;
  91.             byte GPIO_TOGGLE = 0x7A;
  92.             byte ATO_CFG0 = 0x7B;
  93.             byte ATO_CFGU = 0x7D;
  94.             byte ATO_CFGL = 0x7E;
  95.             byte ATO_CFGT = 0x7F;
  96.  
  97.  
  98.             // Global Constants
  99.             byte TOU_THRESH = 0x06;
  100.             byte REL_THRESH = 0x0A;
  101.  
  102.             // Section A
  103.             // This group controls filtering when data is > baseline.
  104.             mpr121Write(MHD_R, 0x01);
  105.             mpr121Write(NHD_R, 0x01);
  106.             mpr121Write(NCL_R, 0x00);
  107.             mpr121Write(FDL_R, 0x00);
  108.  
  109.             // Section B
  110.             // This group controls filtering when data is < baseline.
  111.             mpr121Write(MHD_F, 0x01);
  112.             mpr121Write(NHD_F, 0x01);
  113.             mpr121Write(NCL_F, 0xFF);
  114.             mpr121Write(FDL_F, 0x02);
  115.  
  116.             // Section C
  117.             // This group sets touch and release thresholds for each electrode
  118.             mpr121Write(ELE0_T, TOU_THRESH);
  119.             mpr121Write(ELE0_R, REL_THRESH);
  120.             mpr121Write(ELE1_T, TOU_THRESH);
  121.             mpr121Write(ELE1_R, REL_THRESH);
  122.             mpr121Write(ELE2_T, TOU_THRESH);
  123.             mpr121Write(ELE2_R, REL_THRESH);
  124.             mpr121Write(ELE3_T, TOU_THRESH);
  125.             mpr121Write(ELE3_R, REL_THRESH);
  126.             mpr121Write(ELE4_T, TOU_THRESH);
  127.             mpr121Write(ELE4_R, REL_THRESH);
  128.             mpr121Write(ELE5_T, TOU_THRESH);
  129.             mpr121Write(ELE5_R, REL_THRESH);
  130.  
  131.             mpr121Write(ELE6_T, TOU_THRESH);
  132.             mpr121Write(ELE6_R, REL_THRESH);
  133.             mpr121Write(ELE7_T, TOU_THRESH);
  134.             mpr121Write(ELE7_R, REL_THRESH);
  135.             mpr121Write(ELE8_T, TOU_THRESH);
  136.             mpr121Write(ELE8_R, REL_THRESH);
  137.             mpr121Write(ELE9_T, TOU_THRESH);
  138.             mpr121Write(ELE9_R, REL_THRESH);
  139.             mpr121Write(ELE10_T, TOU_THRESH);
  140.             mpr121Write(ELE10_R, REL_THRESH);
  141.             mpr121Write(ELE11_T, TOU_THRESH);
  142.             mpr121Write(ELE11_R, REL_THRESH);
  143.  
  144.             // Section D
  145.             // Set the Filter Configuration
  146.             // Set ESI2
  147.             mpr121Write(FIL_CFG, 0x04);
  148.  
  149.             // Section E
  150.             // Electrode Configuration
  151.             // Enable 6 Electrodes and set to run mode
  152.             // Set ELE_CFG to 0x00 to return to standby mode
  153.             mpr121Write(ELE_CFG, 0x0C); // Enables all 12 Electrodes
  154.             //mpr121Write(ELE_CFG, 0x06);       // Enable first 6 electrodes
  155.  
  156.             // Section F
  157.             // Enable Auto Config and auto Reconfig
  158.             /*mpr121Write(ATO_CFG0, 0x0B);
  159.               mpr121Write(ATO_CFGU, 0xC9);  // USL = (Vdd-0.7)/vdd*256 = 0xC9 @3.3V
  160.               mpr121Write(ATO_CFGL, 0x82);  // LSL = 0.65*USL = 0x82 @3.3V
  161.               mpr121Write(ATO_CFGT, 0xB5);*/
  162.             // Target = 0.9*USL = 0xB5 @3.3V
  163.  
  164.         }
  165.  
  166.         void mpr121Write(byte settingName, byte settingValue)
  167.         {
  168.             byte[] writeBuffer = new byte[] { settingName, settingValue };
  169.             //byte[] readBuffer = new byte[2];
  170.             //touch.Address = 0x5A;
  171.             touch.Write(writeBuffer, 10);
  172.             //Debug.Print(readBuffer[]);
  173.         }
  174.  
  175.         void interrupt_Interrupt(GT.Interfaces.InterruptInput sender, bool value)
  176.         {
  177.             Debug.Print("interupt detected from " + sender);
  178.            
  179.             // create some array thing to hold onto the results
  180.             byte[] readResult = new byte[2];
  181.             // read the result from the MPR121
  182.             touch.Read(readResult, 10);
  183.             // the result comes in as two numbers like 11111111 and 00001111 needs to be 11111111 + 00001111 = 000011111111111
  184.             byte lowerByte = readResult[0];
  185.             Debug.Print("byte 0 = " + readResult[0].ToString());
  186.             byte higherByte = readResult[1];
  187.             Debug.Print("byte 1 = " + readResult[1].ToString());
  188.            
  189.             // start to add them together
  190.             // convert from an 8 bit to a 16 bit number
  191.             UInt16 readBuffer = higherByte;
  192.             // move this double byte (16 bit number) to the left by 8 steps (this is so machine code! I love it! 1982 Z80A anyone?)
  193.             readBuffer <<= 8;
  194.             // add them with an OR 0+0=0 0+1=1 1+0=1 1+1=1
  195.             readBuffer = (readBuffer |= lowerByte);
  196.             // print the result in decimal
  197.             Debug.Print("combined = " + readBuffer);
  198.            
  199.             // check what electrodes were pressed
  200.             for (int increment = 0; increment < 12; increment++)
  201.             {
  202.                  // reset the comparison bit
  203.                 UInt16 indexByte = 1;
  204.                 // shift the 1 to the left each increment
  205.                 indexByte <<= increment;
  206.                 //Debug.Print(indexByte.ToString());
  207.                 // compare each bit with an AND &= 0+0=0 0+1=0 1+0=0 1+1=1
  208.                 if ((readBuffer & indexByte) != 0)
  209.                 {
  210.                     Debug.Print("pin " + increment + " was touched");
  211.                 }
  212.             }
  213.         }
  214.  
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment