Wii Classic Controller to PICAXE

Bill.b

Senior Member
This is a sample of code I used to adapt a Wii cassic controller for my latest robot.
I used the controller to set the speed and direction of the drives as well as controlling the video camera ( manual or auto pan and camera ON / OFF)
in this sample I have not seperated the buttons from bytes 4 and 5, the table supplied indicates the individual bits assocated with each button.

I will present the full program and schematic for the robot when I have completed testing.

Code:
'  Program to read data form a Wii Classic Controller into a picaxe 28X2.
'  Bill Brown  Bill.b   29th July 2013
'
'  I have tested the program with a 40X2 and it worked OK.  The program does not work correctlly with M2 chips
'  Some more work is required for M2s
'
'
' The Wii Receiver module required a 3.3volt supply. 
' In my test unit i derived the 3.3v by connection two IN4001 diodes in series  to the 5v supply giving approximatly 3.4v.
' 4k7 pullup resitors are unsed on the SCL and SDA - Connected to the +3.4v supply
'
' Data format for the 6 bytes of data from the Wii Classic Controller  (WiiByte0 - 5)
'---------------------------------------------------------------
'|Byte  |                            Bit                        |
'|------|-------------------------------------------------------|
'|      |  7   |   6  |   5  |   4  |   3  |   2  |   1  |   0  |
'|--------------------------------------------------------------|
'|  0   |    RX<4:3>  |         LX<5:0>                         |
'|--------------------------------------------------------------|
'|  1   |    RX<2:1>  |         LY<5:0>                         |
'|--------------------------------------------------------------|
'|  2   |RX<0> |  LT<4:3>    |         RY<4:0>                  |
'|--------------------------------------------------------------|
'|  3   |    LT<2:0>         |         RY<4:0>                  |
'|--------------------------------------------------------------|
'|  4   | BDR  |  BDD | BLT  | B-   |  BH  |  B+  | BRT  |  1   |
'|--------------------------------------------------------------|
'|  5   | BZL  |  BB  |  BY  |  BA  |  BX  | BZR  | BDL  | BDU  |
'!--------------------------------------------------------------! 
'
'LX,LY are the left analogue stick, X and Y (0 - 63). RX and RY are the right analogue stick, X and Y (0 - 31).
'LT and RT are the left and right triggers (0 - 31). The left analogue stick has twice the precision of the other stick
'
' BD(L,R,U,D) are the D-Pad direction buttons.  B(ZR,ZL,A,B,X,Y,+,H,-) are the discrete buttons.
'B(LT,RT) are the digital button click of the LT and RT. all Buttons are 0 when pressed.

#picaxe 28x2
setfreq m8
symbol WiiByte0   = b7        'Wii byte 0
symbol WiiByte1   = b8        'Wii byte 1
symbol WiiByte2   = b9        'Wii byte 2
symbol WiiByte3   = b10       'Wii byte 3
symbol WiiByte4   = b11       'Wii byte 4
symbol WiiByte5   = b12       'Wii byte 5
symbol WiiTemp0   = b13       'Wii Temperary storage byte 
symbol WiiLeftX   = b14       'Left stick X axis 0 - 63
symbol WiiLeftY   = b15       'Left stick Y axis 0 - 63
symbol WiiRightX  = b16       'Right stick X axis 0 - 31 
symbol WiiRightY  = b17       'Right stick Y axis 0 - 31
symbol WiiLeftT   = b18       'Left trigger 0 - 31
symbol WiiRightT  = b19       'Right trigger 0 - 31

i2cslave %010100100, i2cfast_16, i2cbyte     'I2C setup for Wii controller
hi2cout 0,($F0,$55)                          'Wii Configuration bytes 
pause 10
hi2cout 0,($FB,00)                           'Wii Configuration bytes 
pause 10
hi2cout 0, ($00)                             'Wii Request data 

'Main program start here

main:

hi2cout 0, ($00)                              'Wii Request data
pause 70
hi2cin 0,(WiiByte0,WiiByte1,WiiByte2,WiiByte3,WiiByte4,WiiByte5)        'Data from Wii Controller

'The following section arranges the data from the Wii bytes 0 - 4 into seperate bytes for used in the program

pause 50
WiiLeftX = WiiByte0 & %00111111     'Left stick X = Bits 0 - 5 of wiibyte0
WiiLeftY = WiiByte1 & %00111111     'Left stick Y = Bits 0 - 5 of wiibyte1
WiiRightX = WiiByte2 & %00011111    'Right stick X = Bits 0 - 4 of wiibyte2
WiiRightY = WiiByte2 & %10000000    'Move bit 7 of wiibyte2 to Right stick Y
WiiRightY = WiiRightY >> 2          'shift right 2 places
WiiTemp0 = WiiByte1 & %11000000     'move bits 6 & 7 of wiibyte1 to temp
WiiRightY = WiiRightY OR WiiTemp0   'Combine temp and right stick Y
WiiRightY = WiiRightY >> 2          'Shift right 2 places
WiiTemp0 = WiiByte0 & %11000000     'move bits 6 & 7 of wiibyte0 to temp
WiiRightY = WiiRightY OR WiiTemp0   'Combine temp and right stick Y
WiiRightY = WiiRightY >> 3          'Shift right 3 places.  Right stick Y byte completed
WiiRightT = WiiByte3 & %00011111    'move Right Tigger bits 0 - 5 form wiibyte 3 to wiiRightT 
WiiLeftT = WiiByte3 & %11100000     'move left trigger bits 5 - 7 form wiibyte 3 to wiiLeftT
WiiLeftT = WiiLeftT >> 3            'shift right 3 positions.
WiiTemp0 = WiiByte2 & %01100000     'Move Left trigger bits 3 & 4 to temp	
WiiLeftT = WiiLeftT OR WiiTemp0     ' combine wiiLeftT and Temp
WiiLeftT = WiiLeftT >> 2            'Shift right 2 positions to complete wiiLeftT byte.

'debug

goto main

Bill
 
Last edited:
Top