Author Topic: [z80] Problem with the port and an Arduino  (Read 2244 times)

0 Members and 1 Guest are viewing this topic.

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
[z80] Problem with the port and an Arduino
« on: February 05, 2013, 03:39:01 am »
Heya guys,

For my school project I want my calc to set and unset the two wires of the port 0 to drive a robot. Since I only want 4 commands, I use direct output instead of a real protocol.

So my problem is here : while I set at least one line low, there is no problem (the Arduino always receives the right bits). But when I set both lines high, sometimes the Arduino receives high-high, sometimes it receives low-low (and never low-high nor high-low).

Here's my ASM code for the link stuffs :
Code: [Select]
.org $9D93
 .db $BB,$6D
keys5thCol = $FD
keysArrows = $FE
; equates for sending values
; R → red → tip, W → white → ring
; L → low, H → high
wRLWL = 3
wRHWL = 2
wRLWH = 1
wRHWH = 0

 ld b,RHWH
Start:
 ld a,$FF
 out (1),a
 ld a,b
 out (0),a
 ld a,keysArrows
 out (1),a
 nop
 in a,(1)
 cpl
 and %00001110
; no arrow pressed
 jr z,setRHWH
 bit 1,a
 call nz,setRHWL
 bit 2,a
 call nz,setRLWH
 bit 3,a
 call nz,setRLWL
Skip:
 ld a,keys5thCol
 out (1),a
 nop
 in a,(1)
 bit 6,a
 jr nz,Start

 ld a,wRHWH
 out (0),a
 ret

setRLWH:
 ld b,wRLWH
 ret
setRHWL:
 ld b,wRHWL
 ret
setRHWH:
 ld b,wRHWH
 jr Skip
setRLWL:
 ld b,wRLWL
 ret
.end

And here's the Arduino code I wrote (it's C++) :
Code: [Select]
int ring = 2, tip = 4; // let's say that the ring is port 2 and the tip port 4

void setup(void)
{
  pinMode(ring, INPUT);
  pinMode(tip, INPUT);
  Serial.begin(9600);
}

int getBit(int port)
{
  if(digitalRead(port) == HIGH) return 0;
  else return 1;
}

void loop(void)
{
  Serial.print("Command received : ");
  switch((getBit(ring) << 1) | getBit(tip))
  {
    case 0:
      Serial.println("don't move");
      break;
    case 1:
      Serial.println("rotate right");
      break;
    case 2:
      Serial.println("rotate left");
      break;
    case 3:
      Serial.println("go forward");
      break;
  }
delay(1000);
}

I know that it's not a matter of cables nor connection.

Thanks by advance :)

EDIT : oh but, what's this wire ? The ground ? So I think it goes in this hole ... -_____________________-'

It's okay, I just forgot to plug the ground in the hole where the ground has to go. Forget that.
« Last Edit: February 05, 2013, 03:58:11 am by Matrefeytontias »