hello!
I try to simulate a nes controller with an Arduino to communicate through the controller port of a Nes.
the idea is to use module SPI of the AVR in SLAVE mode. To use pin MOSI to transmit data towards the console, and SCK to follow the clock coming from the console.
put the LATCH on interrupt pin (pin2) and write the data SPDR in the routine
/*
Emulate a nes controller with arduino
like a chip 4021 shift out register
Nes data
bit 0 1 2 3 4 5 6 7
A B select start up down left right
every 16,67 millisecond the nes CPU send to arduino a 12 microSec Data Latch
6 microSec after a clock pulse at 6microsec indicate on each rising edge the button state bit by bit :
A,B,select,start,....
SPIE - Enables the SPI interrupt when 1
SPE - Enables the SPI when 1
DORD - Sends data least Significant Bit First when 1, most Significant Bit first when 0
MSTR - Sets the Arduino in master mode when 1, slave mode when 0
CPOL - Sets the data clock to be idle when high if set to 1, idle when low if set to 0
CPHA - Samples data on the falling edge of the data clock when 1, rising edge when 0
SPR1 and SPR0 - Sets the SPI speed, 00 is fastest (4MHz) 11 is slowest (250KHz)
SPIE en 1
SPE en 1
DORD 0
MSTR 0
CPOL 0
CPHA 0
SPR1 et SPR0 à 00
11000000
SPCR = (1<<SPIE)|(1<<SPE);
*/
#define DATAOUT 11//MOSI
#define SPICLOCK 13//sck
#define LATCHPIN 0//NesLATCH
// valeur des boutons
#define NES_A B00000001
#define NES_B B00000010
#define NES_SELECT B00000100
#define NES_START B00001000
#define NES_UP B00010000
#define NES_DOWN B00100000
#define NES_LEFT B01000000
#define NES_RIGHT B10000000
// controller data Start in first
byte controller_data = B11110111;
//
byte ClearSPI;
void setup() {
attachInterrupt(LATCHPIN, SendData,FALLING );
pinMode(DATAOUT, OUTPUT);
pinMode(SPICLOCK,INPUT);
//SPCR est 11000000
SPCR = (1<<SPIE)|(1<<SPE);
// clear SPSR
ClearSPI=SPSR;
ClearSPI=SPDR;
delay(10);
}
void loop() {
}
void SendData()
{
SPDR = controller_data; // ecrire la valeur des boutons dans SPDR
while (!(SPSR & (1<<SPIF))) //
{
};
}
If someone have an idea if my code is right
because I hav just one interrupt when i boot my arduino and after nothing.