LAB11 Template for oppgave 2
Dependencies: mbed
Import program
00001 #include "mbed.h" 00002 //SDA , SCL 00003 I2C i2cBus(PB_9, PB_8); 00004 00005 Serial pc(SERIAL_TX, SERIAL_RX); 00006 00007 #define NUNCHUK_8BIT_ADDR 0x52<<1 // 8-bit I2C slave address = ( ( 7bit i2c address) << 1 | (read/write) bit 00008 00009 typedef struct { 00010 int xStick; 00011 int yStick; 00012 int xAcc; 00013 int yAcc; 00014 int zAcc; 00015 bool bButtonC; 00016 bool bButtonZ; 00017 } NunchukData; 00018 00019 void initNunchuk() 00020 { 00021 i2cBus.frequency(100000); 00022 00023 wait(0.5); 00024 00025 char cmd[2]; 00026 cmd[0] = 0x40; 00027 cmd[1] = 0x00; 00028 i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 2) ; 00029 00030 cmd[0] = 0xF0; 00031 cmd[1] = 0x55; 00032 i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 2) ; 00033 00034 cmd[0] = 0xFB; 00035 cmd[1] = 0x00; 00036 i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 2) ; 00037 } 00038 00039 void GetNunchukData(NunchukData &data) 00040 { 00041 char buffer[6]; 00042 00043 char cmd[1]; 00044 cmd[0] = 0x00; 00045 i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 1) ; 00046 00047 wait(.01); 00048 i2cBus.read(NUNCHUK_8BIT_ADDR, buffer, 6) ; 00049 00050 data.xStick = buffer[0]; 00051 data.yStick = buffer[1]; 00052 data.xAcc = (buffer[2] << 2) + ((buffer[5] & 0x0C) >> 2); 00053 data.yAcc = (buffer[3] << 2) + ((buffer[5] & 0x30) >> 4); 00054 data.zAcc = (buffer[4] << 2) + ((buffer[5] & 0xC0) >> 6); 00055 data.bButtonZ = (buffer[5] & 1) == 0; 00056 data.bButtonC = (buffer[5] & 2) == 0; 00057 } 00058 00059 int main() 00060 { 00061 NunchukData nunchukData; 00062 00063 initNunchuk(); 00064 while(true) { 00065 GetNunchukData(nunchukData); 00066 printf("x-yoy %3d,y-yoy %3d ",nunchukData.xStick,nunchukData.yStick ); 00067 printf("\r\n"); 00068 } 00069 } 00070
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") + } +} +