Wii Nunchuck Accelerometer Interfaced With 28X1 4MHz

tye_scott

New Member
This is my (Almost) first post so here goes...
The following code extracts the Wii Nunchuck Accelerometer data, including the LSB for x,y,and z acceleration. All information was found at http://www.wiili.org/index.php/Wiimote/Extension_Controllers/Nunchuk
and using the accelerometer datasheet at http://www.st.com/stonline/products/literature/ds/11668.pdf I notated that values are +-2G. There is a small conversion at the bottom to scale the X,Y, and Z accelerometers to their true value. I'm having difficulty scaling +-2G so for now their values are 0-4. 0 Being -2G and 4 being +2G. Please Evaluate code, I'm new to programming and this is my first real program. Thanks!
Code:
Symbol Analog_x = b8
Symbol Analog_y = b1
Symbol Accel_Xraw = W1
Symbol Accel_Yraw = w2
Symbol Accel_Zraw = w3 
Symbol Accel_X =b9 
Symbol Accel_Xdec =b10 
Symbol Accel_Y =b11
Symbol Accel_Ydec =b12 
Symbol Accel_Z =b13
Symbol Accel_Zdec =b14 


Init:
hi2csetup i2cmaster, $A4, i2cslow, i2cbyte

hi2cout ($40,$00)

Pause 10

Retrieve:

hi2cout (0)

Pause 10

hi2cin (analog_x,analog_y,Accel_Xraw,Accel_Yraw,Accel_Zraw,b0)

b0 =b0^0x17 + 0x17                'decrypts button state
poke $58,Bit0                    'Bit 0: "Z"-Button (0 = pressed, 1 = released)
Poke $59,Bit1                    'Bit 1: "C" button (0 = pressed, 1 = released)
                            'Bits 2-3: X acceleration LSB
                            'Bits 4-5: Y acceleration LSB
                            'Bits 6-7: Z acceleration LSB

analog_x= analog_x^0x17 + 0x17         'Decrypts x-axis Value of analog stick 
                            'Min(Full Left):0x1E
                            'Medium(Center):0x7E
                            'Max(Full Right):0xE1
            

analog_y=analog_y^0x17 + 0x17         'decrypts y-axis value of analog stick
                            'Min(Full Down):0x1D
                            'Medium(Center):0x7B
                            'Max(Full Right):0xDF
                        
                
Accel_Xraw=Accel_Xraw^0x17 + 0x17        'decrypts x-axis acceleration value
Accel_Xraw=Accel_Xraw << 2            'Min(at -2G):0x48
b0=b0 >> 2 & 0x03                     'Medium(at 0G):0x7D
Accel_Xraw=Accel_Xraw | b0            'Max(at 2G):0xB0


Accel_Yraw=Accel_Yraw^0x17 + 0x17        'decrypts y-axis acceleration value
Accel_Yraw=Accel_Yraw << 2            'Min(at -2G):0x46
b0=b0 >> 2 & 0x03                    'Medium(at 0G):0x7A
Accel_Yraw=Accel_Yraw | b0            'Max(at 2G):0xAF

                    
Accel_Zraw=Accel_Zraw^0x17 + 0x17        'decrypts z-axis acceleration value
Accel_Zraw=Accel_Zraw << 2            'Min(at -2G):0x4A
b0=b0 >> 2 & 0x03                    'Medium(at 0G):0x7E
Accel_Zraw=Accel_Zraw | b0            'Max(at 2G):0xB1
debug                
goto convert


Convert:
accel_xraw=accel_xraw*40 / 1024
accel_x = Accel_xraw dig 1
accel_xdec = Accel_xraw dig 0

accel_yraw=accel_yraw*40 / 1024
accel_y = Accel_yraw dig 1
accel_ydec = Accel_yraw dig 0

accel_zraw=accel_zraw*40 / 1024
accel_z = Accel_zraw dig 1
accel_zdec = Accel_zraw dig 0
Goto retrieve
 
Last edited:

westaust55

Moderator
The link you provided goes nowhere - well no info found there, only the message: "There is currently no text in this page"

A link to the datasheet for the i2c device you are communicating with may help get a response.

Have you tried it yourself?
Is it working and you just want to fine tune?

Exactly what are you after?
 

Jamster

Senior Member
Any chance you could create the code to access the buttons and joystick then output all the information via serial for me. I'm kinda a bit confused with the i2c system.

Thank you
:):):):)
 
Top