Hi Tomas,
Thank you for your quick reply.
I have followed steps on page 11 of Motion detection using MMA8452 accelerometer.
My C code is :
#include "16F1946.h"
#use delay (clock=16000000)
#fuses HS, NOWDT, NOPUT, NOPROTECT, BROWNOUT,INTRC_IO
#Use i2c(master,SCL=PIN_D6 ,SDA=PIN_D5 , FAST=400000)
void Init(void) {
T1CON = 0b00000000;
INTCON = 0b11011011;
TMR1ON = 0;
TMR1IF = 0;
TMR1IE = 1;
T1CON = 0b00001101;
}
#INT_TIMER1
void INTERRUP_TIMER1() {
timecount++;
TMR1IF = 0; //clear INT0 interrupt flag
}
main()
{
init();
write_Reg(CTRL_REG1,0x18);
write_Reg(FF_MT_CFG_REG,0xD8);
write_Reg(FT_MT_THS_REG,0x30);
write_Reg(FF_MT_COUNT_REG,0x0A);
write_Reg(CTRL_REG4,0x04);
write_Reg(CTRL_REG5,0x04);
write_Reg(CTRL_REG1,0x19);
while(True)
{
if (TimeCount % 1000 == 0)
{
write_Reg(I2C_WRITE_ADDR, INT_SOURCE_REG);
Intsourcesystem = read_reg(INT_SOURCE_REG);
if ((Intsourcesystem & 0x04)==0x04)
{
n++;
write_Reg1(I2C_WRITE_ADDR, FF_MT_SRC_REG);
read_Reg1(FF_MT_SRC_REG);
}
}
if (input(Int_acc)==1) {
output_high(LED); }
else {
output_low(LED); }
}
}
void Write_Reg(byte address, byte reg,byte val)
{
i2c_Start(); // Send Start
i2c_Write(address); // Send IIC "Write" Address
i2c_Write(reg); // Send Register
i2c_write(val); // Send Value
i2c_Stop(); // Send Stop
}
byte Read_Reg(byte address, byte reg)
{
byte b;
i2c_Start(); // Send Start
i2c_Write(address); // Send IIC "Write" Address
i2c_Write(reg); // Send Register
i2c_Start(); // Send Repeat Start
i2c_Write(address+1); // Send IIC "Read" Address
b = i2c_Read(1); // *** Dummy read: reads "IIC_ReadAddress" value ***
/b = i2c_Read(1); // Read Register Value
i2c_Stop(); // Send Stop
return b;
}