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 
Committer:
rlanghbv
Date:
Mon Nov 09 13:42:34 2015 +0000
Revision:
0:87a84e072ec7
Child:
1:d4624c8ba9f5
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rlanghbv 0:87a84e072ec7 1 #include "mbed.h"
rlanghbv 0:87a84e072ec7 2 //SDA , SCL
rlanghbv 0:87a84e072ec7 3 I2C i2cBus(PB_9, PB_8);
rlanghbv 0:87a84e072ec7 4
rlanghbv 0:87a84e072ec7 5 Serial pc(SERIAL_TX, SERIAL_RX);
rlanghbv 0:87a84e072ec7 6
rlanghbv 0:87a84e072ec7 7 #define NUNCHUK_8BIT_ADDR 0x52<<1 // 8-bit I2C slave address = ( ( 7bit i2c address) << 1 | (read/write) bit
rlanghbv 0:87a84e072ec7 8
rlanghbv 0:87a84e072ec7 9 typedef struct {
rlanghbv 0:87a84e072ec7 10 int xStick;
rlanghbv 0:87a84e072ec7 11 int yStick;
rlanghbv 0:87a84e072ec7 12 int xAcc;
rlanghbv 0:87a84e072ec7 13 int yAcc;
rlanghbv 0:87a84e072ec7 14 int zAcc;
rlanghbv 0:87a84e072ec7 15 bool bButtonC;
rlanghbv 0:87a84e072ec7 16 bool bButtonZ;
rlanghbv 0:87a84e072ec7 17 } NunchukData;
rlanghbv 0:87a84e072ec7 18
rlanghbv 0:87a84e072ec7 19 void initNunchuk()
rlanghbv 0:87a84e072ec7 20 {
rlanghbv 0:87a84e072ec7 21 i2cBus.frequency(100000);
rlanghbv 0:87a84e072ec7 22
rlanghbv 0:87a84e072ec7 23 wait(0.5);
rlanghbv 0:87a84e072ec7 24
rlanghbv 0:87a84e072ec7 25 char cmd[2];
rlanghbv 0:87a84e072ec7 26 cmd[0] = 0x40;
rlanghbv 0:87a84e072ec7 27 cmd[1] = 0x00;
rlanghbv 0:87a84e072ec7 28 i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 2) ;
rlanghbv 0:87a84e072ec7 29
rlanghbv 0:87a84e072ec7 30 cmd[0] = 0xF0;
rlanghbv 0:87a84e072ec7 31 cmd[1] = 0x55;
rlanghbv 0:87a84e072ec7 32 i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 2) ;
rlanghbv 0:87a84e072ec7 33
rlanghbv 0:87a84e072ec7 34 cmd[0] = 0xFB;
rlanghbv 0:87a84e072ec7 35 cmd[1] = 0x00;
rlanghbv 0:87a84e072ec7 36 i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 2) ;
rlanghbv 0:87a84e072ec7 37 }
rlanghbv 0:87a84e072ec7 38
rlanghbv 0:87a84e072ec7 39 void GetNunchukData(NunchukData &data)
rlanghbv 0:87a84e072ec7 40 {
rlanghbv 0:87a84e072ec7 41 char buffer[6];
rlanghbv 0:87a84e072ec7 42
rlanghbv 0:87a84e072ec7 43 char cmd[1];
rlanghbv 0:87a84e072ec7 44 cmd[0] = 0x00;
rlanghbv 0:87a84e072ec7 45 i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 1) ;
rlanghbv 0:87a84e072ec7 46
rlanghbv 0:87a84e072ec7 47 wait(.01);
rlanghbv 0:87a84e072ec7 48
rlanghbv 0:87a84e072ec7 49 i2cBus.read(NUNCHUK_8BIT_ADDR, buffer, 6) ;
rlanghbv 0:87a84e072ec7 50
rlanghbv 0:87a84e072ec7 51 data.xStick = buffer[0];
rlanghbv 0:87a84e072ec7 52 data.yStick = buffer[1];
rlanghbv 0:87a84e072ec7 53 data.xAcc = (buffer[2] << 2) + ((buffer[5] & 0x0C) >> 2);
rlanghbv 0:87a84e072ec7 54 data.yAcc = (buffer[3] << 2) + ((buffer[5] & 0x30) >> 4);
rlanghbv 0:87a84e072ec7 55 data.zAcc = (buffer[4] << 2) + ((buffer[5] & 0xC0) >> 6);
rlanghbv 0:87a84e072ec7 56 data.bButtonZ = (buffer[5] & 1) == 0;
rlanghbv 0:87a84e072ec7 57 data.bButtonC = (buffer[5] & 2) == 0;
rlanghbv 0:87a84e072ec7 58 }
rlanghbv 0:87a84e072ec7 59
rlanghbv 0:87a84e072ec7 60 int main()
rlanghbv 0:87a84e072ec7 61 {
rlanghbv 0:87a84e072ec7 62 NunchukData nunchukData;
rlanghbv 0:87a84e072ec7 63
rlanghbv 0:87a84e072ec7 64 initNunchuk();
rlanghbv 0:87a84e072ec7 65 while(true) {
rlanghbv 0:87a84e072ec7 66 GetNunchukData(nunchukData);
rlanghbv 0:87a84e072ec7 67 printf("x-yoy %3d,y-yoy %3d ",nunchukData.xStick,nunchukData.yStick );
rlanghbv 0:87a84e072ec7 68 printf("\r\n")
rlanghbv 0:87a84e072ec7 69 }
rlanghbv 0:87a84e072ec7 70 }
rlanghbv 0:87a84e072ec7 71