skinner927

xbox360controllercontroller

Feb 12th, 2012
533
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Written by Dennis Skinner
  2. ; Last updated: 2/12/2012
  3. ; Version: 1.0
  4. ;
  5. ; Designed for PICAXE 08M
  6. ;
  7. ; Original code written by Jeremy Fright for the Arduino: http://diru.org/wordpress/hacking/xbox-360-rf-module-arduino/
  8. ; I've done a little bit of tweaking, but lots of the foot work is on his site.
  9.  
  10.  
  11. ;Pins
  12. symbol syncbtn = pin3  ;connect to pin 5 on xbox board (switch 1)
  13. symbol datapin = 4     ;serial data on xbox board
  14. symbol clockpin = pin1 ;serial clock on xbox board
  15.  
  16.  
  17. ;Vars
  18. symbol writethis = w5  ;set this to binary that you want writedata to write
  19. symbol counter = b2
  20. symbol prev = b3
  21. symbol shift = w6
  22.  
  23. ;Commands
  24. symbol ledcmd  = %0010000100 ;132
  25. symbol animcmd = %0010000101 ;133
  26. symbol synccmd = %0000000100 ;4
  27. symbol ctrloff = %0000001001 ;9
  28. symbol redblnk = %0011000001 ;193
  29. symbol blnkoff = %0011000000 ;192
  30. symbol startshift = %10000000000 ;1024
  31.  
  32.  
  33. setup:
  34.     ; don't let that clock start
  35.     high datapin
  36.    
  37.     ;wait for startup (I had inconsistencies with 2 seconds)
  38.     pause 3000
  39.    
  40.     gosub init_led
  41.    
  42.     goto main
  43.  
  44. main:
  45.     pause 200
  46.    
  47.     ;Button press
  48.     if syncbtn = 0 then
  49.    
  50.     ;cheap debounce
  51.     pause 100
  52.     if syncbtn = 0 then
  53.         ;after the button is pushed, wait (about) half a second
  54.         ; to see if it's still being pressed
  55.         pause 600
  56.        
  57.         ;if it's still pressed, turn off controllers
  58.         if syncbtn = 0 then
  59.             ;blink red
  60.             writethis = redblnk
  61.             gosub writedata
  62.            
  63.             ;wait so we see the lights
  64.             pause 2000
  65.            
  66.             ;turn off controlers
  67.             writethis = ctrloff
  68.             gosub writedata
  69.            
  70.             pause 50
  71.             ;turn off blink
  72.             writethis = blnkoff
  73.             gosub writedata
  74.                    
  75.         ;else sync
  76.         else
  77.             writethis = synccmd
  78.             gosub writedata
  79.         EndIf
  80.        
  81.         ;let the user get their finger off the button
  82.         pause 3000
  83.    
  84.     EndIf  
  85.     EndIf
  86.    
  87.     goto main
  88.    
  89.    
  90. ;****************************
  91. ;****        SUBS        ****
  92. ;****************************
  93.  
  94. ; we use this to write the data
  95. ; set what you want to write in
  96. ; writethis
  97. writedata:
  98.     ; change frequency
  99.     setfreq m8
  100.     ; start sending data
  101.     low datapin
  102.     prev = 1
  103.    
  104.     ; start the shift var at 1024
  105.     ;  We could start at 512 but we have the first shift we need to deal with
  106.     shift = startshift
  107.    
  108.     ;loop over whatever is in writethis
  109.     ; and write it to the data pin
  110.         for counter = 0 to 9
  111.            
  112.             ; do all the shifting while we wait
  113.             ; shift the bit
  114.             shift = shift / 2
  115.             w4 = writethis & shift
  116.            
  117.        
  118.             do while prev = clockpin:loop   ;waits for a change in clock
  119.             prev = clockpin
  120.             ; should be after downward edge of clock,
  121.             ;  so send bit of data now
  122.            
  123.             if  w4 != 0 then
  124.                 high datapin
  125.             else
  126.                 low datapin
  127.             EndIf
  128.            
  129.            
  130.            
  131.             do while prev = clockpin:loop ;detects upward edge of clock
  132.             prev = clockpin
  133.              
  134.         next counter
  135.    
  136.     ; stop data
  137.     high datapin
  138.    
  139.     ;set frequency back to 4MHz
  140.     setfreq m4
  141.     return
  142.    
  143. ;sends the initalization of the leds
  144. init_led:
  145.     writethis = ledcmd
  146.     gosub writedata
  147.     pause 50
  148.     writethis = animcmd
  149.     gosub writedata
  150.     pause 50
  151.    
  152.     return
RAW Paste Data