📄 mpr083.c
字号:
/*!
* \file mpr083.c
* \brief Simple driver MPR083 using I2C
* \version $Revision: 1.1 $
* \author Anthony Huereca
*/
#include "mpr083.h"
/*********************************** Variables ***********************************/
unsigned char SlaveID;
unsigned char MasterTransmission;
/*******************************************************************/
/*!
* I2C Initialization
* Set Baud Rate and turn on I2C
*/
void init_I2C(void)
{
if(!PTGD_PTGD1) /* If connected to USB and thus 12MHz bus */
{
IIC1F = 0x14; /* set MULT and ICR */
}
else /* 750kHz bus */
{
IIC1F = 0x00; /* set MULT and ICR */
}
IIC1C1 = 0x80; /* enable IIC */
}
/*******************************************************************/
/*!
* MPR083 Initialization
*/
void MPR083_init(void)
{
/* Delay Loop to ensure MPR083 is powered up */
int i;
for(i=0;i<10000;i++) {
asm("nop");
}
/* Turn on I2C Clock */
SCGC1_IIC1=1;
/* Configure I2C */
init_I2C();
/* Turn on ATTN Pin */
PTCDD_PTCDD6=1;
ATTN=1;
/* Ensure MPR083 is awake */
TOGGLE_ATTN
/* Put in Stop 1 Mode for initial Config */
MPR083WriteRegister(CONFIGURATION_REGISTER,0x14);
/* Set Sensitivity Threshold to 0x3F */
MPR083WriteRegister(SENSITIVITY_REGISTER,0x3F);
/* Turn off Sounder */
MPR083WriteRegister(SOUNDER_CONFIGURATION_REGISTER,0x00);
/* Sound On, Auto Calibration On, Touch and Release Buffer On, Sensor On */
MPR083WriteRegister(ROTARY_CONFIGURATION_REGISTER, 0x9D);
/* Set 5ms Master Clock */
MPR083WriteRegister(MASTER_TICK_COUNTER_REGISTER,0x00);
/* TASP Multiplier to 2 */
MPR083WriteRegister(TOUCH_ACQUISITION_SAMPLE_PERIOD_REGISTER,0x01);
/* No Delay for Touches */
MPR083WriteRegister(LOW_POWER_CONFIGURATION_REGISTER, 0x00);
/* Turn on MPR083 IRQ and put in Run 2 Mode */
MPR083WriteRegister(CONFIGURATION_REGISTER,0x13);
/* Turn off I2C Clock */
SCGC1_IIC1=0;
/* Turn on IRQ. Falling edge and low-level triggered */
IRQSC=0x53;
}
/*******************************************************************/
/*!
* IRQ Service Routine
* Toggles the Attention pin to wake up MPR083 for I2C communications
* Then reads Status register, clears FIFO buffer, and put back in low power mode
* Only executes when button is either pushed or released
*/
interrupt VectorNumber_Virq void IRQ_ISR (void)
{
byte fault;
byte touch;
/* Turn on I2C Clock and Re-init I2C */
SCGC1_IIC1=1;
init_I2C();
/* Put MPR083 into Run1 Mode for I2C Communication */
TOGGLE_ATTN
/* Turn off interrupts */
MPR083WriteRegister(CONFIGURATION_REGISTER,0x15);
/* Check for fault */
fault=u8MPR083ReadRegister(FAULT_REGISTER);
if(fault&0x00000003)
{
//ignore interrupt generated by fault
}
else
{
/* IRQ generated by touch. Get current status of touch pad */
touch=u8MPR083ReadRegister(ROTARY_STATUS_REGISTER);
/* See if IRQ caused by button touch or button release */
/* If button touch */
if(touch&0x10)
{
/* Send out value to left LED's */
PTED=~((touch&0x0F)+1);
/* Turn on all rows */
PTAD=0x1F;
}
/* Else if no button is touched */
else
{
/* Turn off LED's */
PTED=~(0x00);
PTAD=0x00;
}
}
/* Clear interrupt signal */
while(IRQSC_IRQF)
{
MPR083WriteRegister(FAULT_REGISTER,0x00);
MPR083WriteRegister(FIFO_REGISTER,0x00);
IRQSC_IRQACK=1;
}
/* Put back in Low Power Mode */
MPR083WriteRegister(CONFIGURATION_REGISTER,0x13);
/* Turn off I2C Clock */
SCGC1_IIC1=0;
}
/*******************************************************************/
/*!
* Pause Routine
*/
void Pause(void){
int n;
for(n=1;n<50;n++) {
asm("nop");
}
}
/*******************************************************************/
/*!
* Start I2C Transmision
* @param SlaveID is the 7 bit Slave Address
* @param Mode sets Read or Write Mode
*/
void IIC_StartTransmission (unsigned char SlaveID,unsigned char Mode)
{
if(Mode == MWSR)
{
/* set transmission mode */
MasterTransmission = MWSR;
}
else
{
/* set transmission mode */
MasterTransmission = MRSW;
}
/* shift ID in right possition */
SlaveID = (byte) MPR083_I2C_ADDRESS << 1;
/* Set R/W bit at end of Slave Address */
SlaveID |= (byte)MasterTransmission;
/* send start signal */
i2c_Start();
/* send ID with W/R bit */
i2c_write_byte(SlaveID);
}
/*******************************************************************/
/*!
* Read a register from the MPR083
* @param u8RegisterAddress is Register Address
* @return Data stored in Register
*/
byte u8MPR083ReadRegister(byte u8RegisterAddress)
{
byte result, result2;
/* Set Register Pointer on MPR083 */
IIC_StartTransmission(SlaveID,MWSR);
i2c_Wait();
IIC1D = u8RegisterAddress;
i2c_Wait();
i2c_Stop();
Pause();
/* Request data from Register */
IIC_StartTransmission(SlaveID,MRSW);
i2c_Wait();
i2c_EnterRxMode();
result2 = IIC1D;
i2c_Wait();
result = IIC1D;
i2c_DisableAck();
i2c_Wait();
i2c_Stop();
result2 = IIC1D;
Pause();
return result;
}
/*******************************************************************/
/*!
* Write a byte of Data to specified register on MPR083
* @param u8RegisterAddress is Register Address
* @param u8Data is Data to write
*/
void MPR083WriteRegister(byte u8RegisterAddress, byte u8Data)
{
/* send data to slave */
IIC_StartTransmission(SlaveID,MWSR);
i2c_Wait();
IIC1D = u8RegisterAddress;
i2c_Wait();
IIC1D = u8Data;
i2c_Wait();
i2c_Stop();
Pause();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -