Skip to content

Instantly share code, notes, and snippets.

@goran-mahovlic
Created September 1, 2016 07:02
Show Gist options
  • Save goran-mahovlic/79806e269e2ad6a0841e5ade9fd119cb to your computer and use it in GitHub Desktop.
Save goran-mahovlic/79806e269e2ad6a0841e5ade9fd119cb to your computer and use it in GitHub Desktop.
#include "mbed.h"
#include "wire.h"
/*
*/
//P0_18 TX -- I have borken pin so Im using RX for TX
//P0_17 RX
#define KX122 //KX122, Accelerometer Sensor
#include "mbed.h"
//#include "UARTService.h"
#include "nrf_temp.h"
//#include "I2C.h"
#define SCL 16
#define SDA 14
TwoWire Wire = TwoWire(NRF_TWI1);
#define SENSOR_READ_INTERVAL_S (1.0F)
#define ADV_INTERVAL_MS (100UL)
// Function Prototypes
void PBTrigger(); //Interrupt function for PB4
// Global Variables
Serial mySerial(P0_17, P0_18); // TX pin, RX pin Original
DigitalOut m_cmd_led(P0_7);
DigitalIn testButton(P0_4); //Original
//I2C i2c(P0_14,P0_16); //Original DK Kit
bool RepStart = true;
bool NoRepStart = false;
int i = 1;
#define KX122_addr_w 0x3E
//int KX122_addr_w = 0x3E;
#define KX122_addr_r 0x3F
#define KX022_WHO_AM_I 0x0F
#define KX022_WAI_VAL 0x14
#define KX122_Accel_CNTL1_1 0x18
#define KX122_Accel_CNTL1_2 0x41
#define KX122_Accel_ODCNTL_1 0x1B
#define KX122_Accel_ODCNTL_2 0x02
#define KX122_Accel_CNTL3_1 0x1A
#define KX122_Accel_CNTL3_2 0xD8
#define KX122_Accel_TILT_TIMER_1 0x22
#define KX122_Accel_TILT_TIMER_2 0x01
#define KX122_Accel_CNTL2_1 0x18
#define KX122_Accel_CNTL2_2 0xC1
//static uint8_t KX122_Accel_CNTL1_2 = 0x41;
//static uint8_t KX122_Accel_ODCNTL_2 = 0x02;
//static uint8_t KX122_Accel_CNTL3_2 = 0xD8;
//static uint8_t KX122_Accel_TILT_TIMER_2 = 0x01;
//static uint8_t KX122_Accel_CNTL2_2 = 0xC1;
uint8_t KX122_Content_ReadData[6];
#define KX122_Addr_Accel_ReadData 0x06
//uint16_t KX122_Addr_Accel_ReadData = 0x06;
float KX122_Accel_X;
float KX122_Accel_Y;
float KX122_Accel_Z;
short int KX122_Accel_X_RawOUT = 0;
short int KX122_Accel_Y_RawOUT = 0;
short int KX122_Accel_Z_RawOUT = 0;
int KX122_Accel_X_LB = 0;
int KX122_Accel_X_HB = 0;
int KX122_Accel_Y_LB = 0;
int KX122_Accel_Y_HB = 0;
int KX122_Accel_Z_LB = 0;
int KX122_Accel_Z_HB = 0;
int m_addr;
int reading;
void Sensor_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t length)
{
Wire.beginTransmission(KX122_addr_w);
Wire.write( (uint8_t)addr>>8 );
Wire.write( (uint8_t)addr );
Wire.write(pbuf, length);
Wire.endTransmission();
}
void Sensor_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t length)
{
Wire.beginTransmission(KX122_addr_w);
Wire.write( (uint8_t)addr>>8 );
Wire.write( (uint8_t)addr );
Wire.endTransmission();
Wire.requestFrom(KX122_addr_w+1, length);
while( Wire.available() > 0 )
{
*pbuf = Wire.read();
pbuf++;
}
}
void getAcc(){
//Read KX122 Portion from the IC
Wire.beginTransmission(KX122_addr_w);
Wire.write(KX122_Addr_Accel_ReadData);
Wire.endTransmission();
Wire.requestFrom(KX122_addr_w, 2);
if(2 <= Wire.available()) // if two bytes were received
{
KX122_Content_ReadData[0] = Wire.read(); // receive high byte (overwrites previous reading)
//reading = reading << 8; // shift high byte to be high 8 bits
KX122_Content_ReadData[1] = Wire.read(); // receive low byte as lower 8 bits
//mySerial.printf("%i\n\r",reading); // print the reading
}
//Sensor_ReadBytes(KX122_Addr_Accel_ReadData, &KX122_Content_ReadData[0], 6);
//reconfigure the data (taken from arduino code)
KX122_Accel_X_RawOUT = (KX122_Content_ReadData[1]<<8) | (KX122_Content_ReadData[0]);
// KX122_Accel_Y_RawOUT = (KX122_Content_ReadData[3]<<8) | (KX122_Content_ReadData[2]);
// KX122_Accel_Z_RawOUT = (KX122_Content_ReadData[5]<<8) | (KX122_Content_ReadData[4]);
//apply needed changes (taken from arduino code)
KX122_Accel_X = (float)KX122_Accel_X_RawOUT / 16384;
// KX122_Accel_Y = (float)KX122_Accel_Y_RawOUT / 16384;
// KX122_Accel_Z = (float)KX122_Accel_Z_RawOUT / 16384;
//transmit the data
mySerial.printf("KX122 Sensor:");
wait_ms(20);
mySerial.printf(" ACCX= %0.2f g\n\r", KX122_Accel_X);
wait_ms(20);
// mySerial.printf(" ACCY= %0.2f g", KX122_Accel_Y);
// wait_ms(20);
// mySerial.printf(" ACCY= %0.2f g\n\r", KX122_Accel_Y);
// wait_ms(20);
}
/**
* This callback is scheduled to be called periodically via a low-priority interrupt.
*/
void periodicCallback(void)
{
getAcc();
}
int main(void)
{
Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);
wait(10);
Ticker ticker;
m_cmd_led = 0;
// uint8_t rd_data;
// Sensor_ReadBytes(0x0F, &rd_data, 1);
Wire.beginTransmission(KX122_addr_w);
Wire.write(KX022_WHO_AM_I);
Wire.endTransmission();
Wire.requestFrom(KX122_addr_w, 1);
if(1 <= Wire.available()) // if two bytes were received
{
reading = Wire.read(); // receive high byte (overwrites previous reading)
//reading = reading << 8; // shift high byte to be high 8 bits
// reading |= Wire.read(); // receive low byte as lower 8 bits
mySerial.printf("%x",reading); // print the reading
}
mySerial.printf("Read data from Sensor \r\n");
mySerial.printf("Data: %x\r\n");
ticker.attach(periodicCallback, SENSOR_READ_INTERVAL_S);
Wire.beginTransmission(KX122_addr_w);
Wire.write(KX122_Accel_CNTL1_1);
Wire.write(KX122_Accel_CNTL1_2);
Wire.endTransmission();
// Sensor_WriteBytes(KX122_Accel_CNTL1_1,&KX122_Accel_CNTL1_2, 1);
Wire.beginTransmission(KX122_addr_w);
Wire.write(KX122_Accel_ODCNTL_1);
Wire.write(KX122_Accel_ODCNTL_2);
Wire.endTransmission();
// Sensor_WriteBytes(KX122_Accel_ODCNTL_1,&KX122_Accel_ODCNTL_2, 1);
Wire.beginTransmission(KX122_addr_w);
Wire.write(KX122_Accel_CNTL3_1);
Wire.write(KX122_Accel_CNTL3_2);
Wire.endTransmission();
// Sensor_WriteBytes(KX122_Accel_CNTL3_1,&KX122_Accel_CNTL3_2, 1);
Wire.beginTransmission(KX122_addr_w);
Wire.write(KX122_Accel_TILT_TIMER_1);
Wire.write(KX122_Accel_TILT_TIMER_2);
Wire.endTransmission();
// Sensor_WriteBytes(KX122_Accel_TILT_TIMER_1,&KX122_Accel_TILT_TIMER_2, 1);
Wire.beginTransmission(KX122_addr_w);
Wire.write(KX122_Accel_CNTL2_1);
Wire.write(KX122_Accel_CNTL2_2);
Wire.endTransmission();
// Sensor_WriteBytes(KX122_Accel_CNTL2_1,&KX122_Accel_CNTL2_2, 1);
while (true) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment