LAB
Dependencies: mbed
Fork of LAB11_Oppgave2 by
main.cpp@1:d4624c8ba9f5, 2015-11-09 (annotated)
- Committer:
- rlanghbv
- Date:
- Mon Nov 09 13:48:59 2015 +0000
- Revision:
- 1:d4624c8ba9f5
- Parent:
- 0:87a84e072ec7
- Child:
- 2:132db1b8730d
fixed ; error
Who changed what in which revision?
User | Revision | Line number | New 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 | i2cBus.read(NUNCHUK_8BIT_ADDR, buffer, 6) ; |
rlanghbv | 0:87a84e072ec7 | 49 | |
rlanghbv | 0:87a84e072ec7 | 50 | data.xStick = buffer[0]; |
rlanghbv | 0:87a84e072ec7 | 51 | data.yStick = buffer[1]; |
rlanghbv | 0:87a84e072ec7 | 52 | data.xAcc = (buffer[2] << 2) + ((buffer[5] & 0x0C) >> 2); |
rlanghbv | 0:87a84e072ec7 | 53 | data.yAcc = (buffer[3] << 2) + ((buffer[5] & 0x30) >> 4); |
rlanghbv | 0:87a84e072ec7 | 54 | data.zAcc = (buffer[4] << 2) + ((buffer[5] & 0xC0) >> 6); |
rlanghbv | 0:87a84e072ec7 | 55 | data.bButtonZ = (buffer[5] & 1) == 0; |
rlanghbv | 0:87a84e072ec7 | 56 | data.bButtonC = (buffer[5] & 2) == 0; |
rlanghbv | 0:87a84e072ec7 | 57 | } |
rlanghbv | 0:87a84e072ec7 | 58 | |
rlanghbv | 0:87a84e072ec7 | 59 | int main() |
rlanghbv | 0:87a84e072ec7 | 60 | { |
rlanghbv | 0:87a84e072ec7 | 61 | NunchukData nunchukData; |
rlanghbv | 0:87a84e072ec7 | 62 | |
rlanghbv | 0:87a84e072ec7 | 63 | initNunchuk(); |
rlanghbv | 0:87a84e072ec7 | 64 | while(true) { |
rlanghbv | 0:87a84e072ec7 | 65 | GetNunchukData(nunchukData); |
rlanghbv | 0:87a84e072ec7 | 66 | printf("x-yoy %3d,y-yoy %3d ",nunchukData.xStick,nunchukData.yStick ); |
rlanghbv | 1:d4624c8ba9f5 | 67 | printf("\r\n"); |
rlanghbv | 0:87a84e072ec7 | 68 | } |
rlanghbv | 0:87a84e072ec7 | 69 | } |
rlanghbv | 0:87a84e072ec7 | 70 |