accelerometer og akse
Dependencies: mbed
Fork of LAB11_Oppgave2 by
Diff: main.cpp
- Revision:
- 0:87a84e072ec7
- Child:
- 1:d4624c8ba9f5
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Nov 09 13:42:34 2015 +0000 @@ -0,0 +1,71 @@ +#include "mbed.h" +//SDA , SCL +I2C i2cBus(PB_9, PB_8); + +Serial pc(SERIAL_TX, SERIAL_RX); + +#define NUNCHUK_8BIT_ADDR 0x52<<1 // 8-bit I2C slave address = ( ( 7bit i2c address) << 1 | (read/write) bit + +typedef struct { + int xStick; + int yStick; + int xAcc; + int yAcc; + int zAcc; + bool bButtonC; + bool bButtonZ; +} NunchukData; + +void initNunchuk() +{ + i2cBus.frequency(100000); + + wait(0.5); + + char cmd[2]; + cmd[0] = 0x40; + cmd[1] = 0x00; + i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 2) ; + + cmd[0] = 0xF0; + cmd[1] = 0x55; + i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 2) ; + + cmd[0] = 0xFB; + cmd[1] = 0x00; + i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 2) ; +} + +void GetNunchukData(NunchukData &data) +{ + char buffer[6]; + + char cmd[1]; + cmd[0] = 0x00; + i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 1) ; + + wait(.01); + + i2cBus.read(NUNCHUK_8BIT_ADDR, buffer, 6) ; + + data.xStick = buffer[0]; + data.yStick = buffer[1]; + data.xAcc = (buffer[2] << 2) + ((buffer[5] & 0x0C) >> 2); + data.yAcc = (buffer[3] << 2) + ((buffer[5] & 0x30) >> 4); + data.zAcc = (buffer[4] << 2) + ((buffer[5] & 0xC0) >> 6); + data.bButtonZ = (buffer[5] & 1) == 0; + data.bButtonC = (buffer[5] & 2) == 0; +} + +int main() +{ + NunchukData nunchukData; + + initNunchuk(); + while(true) { + GetNunchukData(nunchukData); + printf("x-yoy %3d,y-yoy %3d ",nunchukData.xStick,nunchukData.yStick ); + printf("\r\n") + } +} +