MTCH112 Eval Board Microchip capacitive proximity sensor

Committer:
duchonic
Date:
Thu Aug 23 06:17:50 2018 +0000
Revision:
0:8203e12416bb
First

Who changed what in which revision?

UserRevisionLine numberNew contents of line
duchonic 0:8203e12416bb 1 #include "mtch112.h"
duchonic 0:8203e12416bb 2 #include "main.h"
duchonic 0:8203e12416bb 3
duchonic 0:8203e12416bb 4 MTCH112::MTCH112(PinName sda, PinName scl, uint8_t addr) : m_i2c(sda, scl), m_addr(addr) {}
duchonic 0:8203e12416bb 5
duchonic 0:8203e12416bb 6 uint8_t MTCH112::MTCH112_Init(void)
duchonic 0:8203e12416bb 7 {
duchonic 0:8203e12416bb 8 printf("MTCH112_Init\n");
duchonic 0:8203e12416bb 9
duchonic 0:8203e12416bb 10 SetRegister(CONFIG_RESET_CALIB, CONFIG_RESET_CALIB_VALUE);
duchonic 0:8203e12416bb 11 wait(0.5);
duchonic 0:8203e12416bb 12 SetRegister(CONFIG_TIMEOUT_L, 0x03);
duchonic 0:8203e12416bb 13 wait(0.1);
duchonic 0:8203e12416bb 14 SetRegister(CONFIG_TIMEOUT_H, 0xff);
duchonic 0:8203e12416bb 15 wait(0.1);
duchonic 0:8203e12416bb 16 SetRegister(CONFIG_OUTCON, CONFIG_OUTCON_VALUE);
duchonic 0:8203e12416bb 17 wait(0.1);
duchonic 0:8203e12416bb 18 SetRegister(CONFIG_LPCON, SLEEP_16ms | CLK_16MHZ);
duchonic 0:8203e12416bb 19 wait(0.1);
duchonic 0:8203e12416bb 20 SetRegister(CONFIG_PROX_THRESH, CONFIG_PROX_THRESH_VALUE);
duchonic 0:8203e12416bb 21 wait(0.1);
duchonic 0:8203e12416bb 22 SetRegister(CONFIG_PRESS_THRESH, CONFIG_PRESS_THRESH_VALUE);
duchonic 0:8203e12416bb 23 wait(0.5);
duchonic 0:8203e12416bb 24
duchonic 0:8203e12416bb 25 printf("CONFIG_OUTCON:0x%02x\n", CONFIG_OUTCON_VALUE);
duchonic 0:8203e12416bb 26 printf("CONFIG_PROX_THRESH:0x%02x\n", CONFIG_PROX_THRESH_VALUE);
duchonic 0:8203e12416bb 27 printf("CONFIG_PRESS_THRESH:0x%02x\n", CONFIG_PRESS_THRESH_VALUE);
duchonic 0:8203e12416bb 28
duchonic 0:8203e12416bb 29 return(0);
duchonic 0:8203e12416bb 30 }
duchonic 0:8203e12416bb 31
duchonic 0:8203e12416bb 32 MTCH112::~MTCH112(void)
duchonic 0:8203e12416bb 33 {
duchonic 0:8203e12416bb 34
duchonic 0:8203e12416bb 35 };
duchonic 0:8203e12416bb 36
duchonic 0:8203e12416bb 37 uint8_t MTCH112::MTCH112_GetState(void)
duchonic 0:8203e12416bb 38 {
duchonic 0:8203e12416bb 39 return(GetRegister(OUTPUT_STATE));
duchonic 0:8203e12416bb 40 }
duchonic 0:8203e12416bb 41
duchonic 0:8203e12416bb 42 /** Private Functions */
duchonic 0:8203e12416bb 43
duchonic 0:8203e12416bb 44 void MTCH112::SetRegister(uint8_t registerAddr, uint8_t data)
duchonic 0:8203e12416bb 45 {
duchonic 0:8203e12416bb 46 char dataWrite[5];
duchonic 0:8203e12416bb 47
duchonic 0:8203e12416bb 48 dataWrite[0] = WRITE_PROTECT_BYTE_H;
duchonic 0:8203e12416bb 49 dataWrite[1] = WRITE_PROTECT_BYTE_L;
duchonic 0:8203e12416bb 50 dataWrite[2] = registerAddr;
duchonic 0:8203e12416bb 51 dataWrite[3] = data;
duchonic 0:8203e12416bb 52 dataWrite[4] = WRITE_PROTECT_BYTE_H ^ WRITE_PROTECT_BYTE_L ^ registerAddr ^ data;
duchonic 0:8203e12416bb 53
duchonic 0:8203e12416bb 54 m_i2c.write(m_addr, dataWrite, 5);
duchonic 0:8203e12416bb 55 }
duchonic 0:8203e12416bb 56
duchonic 0:8203e12416bb 57 uint8_t MTCH112::GetRegister(uint8_t registerAddr)
duchonic 0:8203e12416bb 58 {
duchonic 0:8203e12416bb 59 char dataWrite[1];
duchonic 0:8203e12416bb 60 char dataRead[1];
duchonic 0:8203e12416bb 61
duchonic 0:8203e12416bb 62 dataWrite[0] = registerAddr;
duchonic 0:8203e12416bb 63 dataRead[0] = 0x00;
duchonic 0:8203e12416bb 64
duchonic 0:8203e12416bb 65 m_i2c.write(m_addr, dataWrite, 1);
duchonic 0:8203e12416bb 66 m_i2c.read(m_addr, dataRead, 1);
duchonic 0:8203e12416bb 67
duchonic 0:8203e12416bb 68 return(dataRead[0]);
duchonic 0:8203e12416bb 69 }