My own PC in disguise

I wrote this for an ATmega 328P (8MHz stock) because the 2313 runs at 1MHz meaning communication is generally unreliable at 9600 baud. I have no idea if it works. Kept the format largely the same so others can see how it's ported and work from there.

/* * Wireless_controller_adapter_port.c * * Created: 15-Feb-15 8:11:48 PM * ATmega328P port of Arduino code to communicate with xbox 360 RF module. * Original work by (yaywoop) / additional ideas from Alexander Martinez - modified by dilandou (www.dilandou.com, www.diru.org/wordpress), initial port attempt by LusciousFox * First sends LED initialisation code followed by LED startup animation code, then sleeps until a button press for sync command. * RF module must be powered with 3.3V, two diodes in series with USB 5v will do. Connect the USB wires to a host computer, and the data and serial wires to Arduino. * of course, make sure to have a common ground */

define F_CPU 8000000UL

include <avr/io.h>

include <avr/sleep.h>

include <avr/interrupt.h>

include <util/delay.h>

define USART_BAUDRATE 9600

define BAUD_PRESCALE (F_CPU/(16L*USART_BAUDRATE)) - 1

define pin_register DDRD

define pin_port PORTD

define sync_pin PD2 //power button repurposed for sync button (pin 5 on the module)

define data_pin PD3 //data line (pin 6 on the module)

define clock_pin PD4 //clock line (pin 7 on module)

int led_cmd[10] = {0,0,1,0,0,0,0,1,0,0}; //Activates/initializes the LEDs, leaving the center LED lit. int anim_cmd[10] = {0,0,1,0,0,0,0,1,0,1}; //Makes the startup animation on the ring of light. int sync_cmd[10] = {0,0,0,0,0,0,0,1,0,0}; //Initiates the sync process.

volatile int sync_enable = 0;

void initialiseUART() { UBRR0 = BAUD_PRESCALE; UCSR0B |= (1<<RXEN0) | (1<<TXEN0); UCSR0C |= (1<<USBS0) | (1<<UCSZ00); PRR &= ~(1<<PRUSART0); }

void sendData(int cmd_do[]) {

pin_register |= (1<<data_pin); //set data_pin to OUTPUT
pin_port &= ~(1<<data_pin); //set data pin low to start sending data.
int prev = 1;

for(int i = 0; i < 10; i++){

    while (prev == clock_pin){} //detects change in clock
    prev = clock_pin;
    // should be after downward edge of clock, so send bit of data now
    if (cmd_do[i]== 1){
        pin_port |= (1<<data_pin);
    }
    else{
        pin_port &= ~(1<<data_pin);
    }           

    while (prev == clock_pin){} //detects upward edge of clock
    prev = clock_pin;
}

pin_port |= (1<<data_pin);      //set data_pin HIGH
pin_register &= ~(1<<data_pin); //set data_pin as input

}

void initLEDs(){

sendData(led_cmd);
delay(50);
sendData(anim_cmd);
delay(50);

}

ISR(INT0_vect){ sync_enable = 1; }

void sleepNow() { SMCR = 0x04; // 0b00000100 set sleep mode

sei();//enable interrupts
SMCR |= (1<<SE); //enable sleep mode
SMCR &= ~(1<<SE); //disable sleep mode
// interrupt 0 on pin 2 is automatically disabled upon returning from interrupt

}

void setup() { initialiseUART(); EICRA = 0x00;//initialize interrupts to activate on LOW signal but not enable EIMSK = 0x01;//enable interrupt 0 when I flag in SREG is set pin_register &= ~((1<<sync_pin)|(1<<data_pin)|(1<<clock_pin)); //set sync, data and clock pins as inputs pin_port |= (1<<sync_pin); //set pull up resistor on

_delay_ms(2000);
initLEDs();
//  sendData(sync_cmd);

}

void USART_putstring(char* StringPtr){

while(*StringPtr != 0x00){
    while(!(UCSR0A & (1<<UDRE0)));
    UDR0 = *StringPtr;
    StringPtr++;
    }

}

int main(void) { setup();

while(1){
    USART_putstring("Sleeping.");
    sleepNow();
    _delay_ms(200);
    if(sync_enable==1) {
        USART_putstring("Syncing.");
        sendData(sync_cmd);
        sync_enable = 0;
    }
}

}

/r/pcmasterrace Thread Link - imgur.com