borgware-2d/joystick/nes_pad.c

47 lines
1008 B
C
Raw Normal View History

2009-07-09 20:08:40 +00:00
#include <avr/io.h>
#include <util/delay.h>
#include "../makros.h"
#include "../config.h"
#define NES_PAD_DDR_PL DDR(NES_PAD_PORT_PL)
#define NES_PAD_DDR_CLK DDR(NES_PAD_PORT_CLK)
2009-07-11 16:50:11 +00:00
#define NES_PAD_PIN_DS PIN(NES_PAD_PORT_DS)
#define NES_PAD_DDR_DS DDR(NES_PAD_PORT_DS)
2009-07-09 20:08:40 +00:00
volatile unsigned char fakeport;
unsigned char waitForFire;
void joy_init(){
NES_PAD_DDR_PL |= 1<<NES_PAD_BIT_PL;
NES_PAD_DDR_CLK |= 1<<NES_PAD_BIT_CLK;
2009-07-11 16:50:11 +00:00
NES_PAD_PORT_DS |= (1<<NES_PAD_BIT_DS);//pullup
2009-07-09 20:08:40 +00:00
NES_PAD_DDR_DS &= ~(1<<NES_PAD_BIT_DS);
}
void readButtons(){
NES_PAD_PORT_PL |= 1<<NES_PAD_BIT_PL;
asm volatile("nop\r\t");
asm volatile("nop\r\t");
asm volatile("nop\r\t");
NES_PAD_PORT_PL &= ~ (1<<NES_PAD_BIT_PL);
uint8_t i, port=0;
for(i=0; i<8; i++){
port >>= 1;
_delay_us(1);
if(NES_PAD_PIN_DS & (1 << NES_PAD_BIT_DS))
port |=0x80;
NES_PAD_PORT_CLK |= 1<<NES_PAD_BIT_CLK;
asm volatile("nop\r\t");
asm volatile("nop\r\t");
NES_PAD_PORT_CLK &= ~(1<<NES_PAD_BIT_CLK);
}
fakeport= ~port;
}