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 

Files at this revision

API Documentation at this revision

Comitter:
rlanghbv
Date:
Mon Nov 09 13:48:59 2015 +0000
Parent:
0:87a84e072ec7
Commit message:
fixed ; error

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 87a84e072ec7 -r d4624c8ba9f5 main.cpp
--- a/main.cpp	Mon Nov 09 13:42:34 2015 +0000
+++ b/main.cpp	Mon Nov 09 13:48:59 2015 +0000
@@ -45,7 +45,6 @@
     i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 1) ;
 
     wait(.01);
-
     i2cBus.read(NUNCHUK_8BIT_ADDR, buffer, 6) ;
 
     data.xStick   = buffer[0];
@@ -65,7 +64,7 @@
     while(true) {
         GetNunchukData(nunchukData);
         printf("x-yoy %3d,y-yoy %3d ",nunchukData.xStick,nunchukData.yStick );
-        printf("\r\n")
+        printf("\r\n");
     }
 }