Spring Break Recap

So my spring break ended about a week ago and I am back at school for another quarter.  This would be my last quarter but I'm doing the blended Master's program so I have one more year.  I'm looking forward to taking some more sweet classes.

Anyway,  what did I accomplish over spring break?  A little less than I would have liked.  I spent most of my time relaxing and recovering from the previous quarter.  But I did accomplish what I set out to do, which was make a stem for a new bike that I'm building up and finally get around to do a project with the LaunchPad.

For the stem I decided I would just make one with standard clamping, unlike my fancy internal clamping one.  I made it some what small because of the size of aluminum I had.  But thats ok because I wanted something with a short reach.  Like usual I failed at taking pictures while I was machining but here's one after the first couple rounds of machining. 
A vast majority of the machining for stems is drilling and boring the holes for steer tube and handle bars.  Other than that it pretty straight forward and quick jobs after setup (which always takes time).  It was super nice to come home and use tools that were actaully sharp unlike the tools we have at the student machine shops on campus.  

There's the finished stem with some bars in it.  No pictures of it on a bike since I don't have a fork yet.  It came out pretty nice.  My friend has been bugging my to make him a stem with internal clamping so I'll add that to my list of summer projects.


So, what have I got here?  Well it's a NES controller connected to a LaunchPad which also has a 595 shift register attached (ignore the mosfets).
So I had this NES controller laying around and I decided that I would investigate what its communication protocol was.  And then I decided to add some LEDs that would light up when the corresponding button was pressed.  To figure out the communications I searched online and found plently of information on the subject.  Its very simple serial communication using shift registers.  The NES sends out a pulse on the latch line, which latches the button states into the shift register(4021) and then it sends out 8 clock pulses on the clock line and data travels along the data line.  Each of the 8 data bits corresponds to a button.  The NES performs this at roughly 60 Hz.

Knowing how the NES and controller communicate, it was easy to code up a program that emulated the NES sending out pulses.  However I did have to get used to coding the MSP430 but its not like I haven't done that before with other uC's.  Once I read in the data, I then shifted it out the 8 LEDs using a  595 shift register.  The 595 is pretty cool since you load the data and then latch it to have it appear on the outputs.

I did run into a minor problem which I thought was my code button it turned out to be a bad connection between the 430 and the latch line.  Once that was fixed it worked perfectly.  I had a good time pressing buttons and having LEDs light up.

This can be extended to SNES controller by sending/reading 16 pulses/bits since the SNES controller has 2 shift registers(2x 4021) while the NES controller has only one.  My goal for learning the protocol is to make a SNES to USB and/or SNES to GameCube adapter(s).   The inspiration comes from this guy and his awesome projects.  I think his SNES controller to parallel port was one of my first electronics projects and what got me interested in hacking and modifying electronics.  My second project also came from him too, the PSX controller to parallel port.  I never finished that one since I didn't have a power source for the controller.

Anyway here's the code for interested parties:
//NES Controller Data Reader
//By Andrew Olson
//3/23/11

#include <msp430g2231.h>
#include <stdint.h>

void delay_us(unsigned int us )
{
    unsigned int i;
    for (i = 0; i<= us/2; i++) 
       __delay_cycles(1);
}

void main(void)
{
 //stop watchdawg
 WDTCTL = WDTPW + WDTHOLD;
 BCSCTL1 = CALBC1_1MHZ;                    // Set range
   DCOCTL = CALDCO_1MHZ;                     // Set DCO step + modulation 
 //setup pins, could have used #define 
 uint8_t nesLatch = 3;
 uint8_t nesClock = 4;
 uint8_t nesData = 5;
 uint8_t sregLatch = 1;
 uint8_t sregClock = 2;
 uint8_t sregData = 0;
 //holds controller data
 uint8_t nesByte = 0;
 uint8_t i;
 //set pins to output
 P1DIR |= (1<<nesLatch)|(1<<nesClock)|(1<<sregLatch)|(1<<sregClock)|(1<<sregData);
 P1OUT = 0;
 
 while(1)
 {
  //clear controller data
  nesByte = 0;
  //latch controller data
  P1OUT |= (1<<nesLatch);
  //delay_us(12);
  P1OUT &= ~(1<<nesLatch);
  //send out 8 clock pulses and read incoming data
  for(i = 0;i<8;i++)
  {
   nesByte <<= 1;
   nesByte |= ((P1IN & (1<<nesData))>>nesData);
   P1OUT |= (1<<nesClock);
   //delay_us(6);
   P1OUT &= ~(1<<nesClock); 
   //delay_us(6);
  }
  //send out byte to shift register
  for(i = 0;i<8;i++)
  {
   //put each bit on shift register data line
   if((nesByte & (1<<i))) 
    P1OUT |= (1<<sregData);
   else
    P1OUT &= ~(1<<sregData);
   //clock shift register
   P1OUT |= (1<<sregClock);
   P1OUT &= ~(1<<sregClock);
  }
  //latch shift register
  P1OUT |= (1<<sregLatch);
  P1OUT &= ~(1<<sregLatch);
  P1OUT &= ~(1<<sregData);
 }
}
Anyway I think I'll start with the SNES to GC first and will use a attiny2313.

Recent Posts