So I'm trying to connect the chip with an 8-bit data bus. All works good, but...

Arduino Uno uses D0 and D1 for serial TX/RX (USB connection with PC). When I try to open a serial connection (on serial.begin stage) ports D0 and D1 blocks and the chip stops working. This is a problem.

I think I should relocate pins D0 and D1 to another port. But the chip uses 8 pins (8bit chip). So I need to relocate the first pin (D0) and the second pin (D1) to port b. Maybe I can use B4 and B3?

But I don't know how to use PORTD[PD2, PD3, PD4, PD5, PD6, PD7] and PORTB[PB4, PB3] in one time like:

var portX = PORTD[PD2, PD3, PD4, PD5, PD6, PD7] + PORTB[PB4, PB3];
portX = data;

My code:

#include <avr/io.h>     // For I/O and other AVR registers
#include <util/delay.h> // For timing

/* Pinmap (Arduino UNO compatible) */
#define YM_IC (5) // PC5 (= pin A5 for Arduino UNO)
#define YM_CS (4) // PC4 (= pin A4 for Arduino UNO)
#define YM_WR (3) // PC3 (= pin A3 for Arduino UNO)
#define YM_RD (2) // PC2 (= pin A2 for Arduino UNO)
#define YM_A0 (1) // PC1 (= pin A1 for Arduino UNO)
#define YM_A1 (0) // PC0 (= pin A0 for Arduino UNO)

#define YD0 (0)(= pin D0 for Arduino UNO)
#define YD1 (1)(= pin D1 for Arduino UNO)
#define YD2 (2)(= pin D2 for Arduino UNO)
#define YD3 (3)(= pin D3 for Arduino UNO)
#define YD4 (4)(= pin D4 for Arduino UNO)
#define YD5 (5)(= pin D5 for Arduino UNO)
#define YD6 (6)(= pin D6 for Arduino UNO)
#define YD7 (7)(= pin D7 for Arduino UNO)

#define YM_DATA_PORT PORTD // Whole PORT D for data bus (= pins D0 to D7 for Arduino UNO)
#define YM_MCLOCK (1) // PB1 = OC1A (= pin D9 for Arduino UNO)

extern uint8_t chflash[6];
uint8_t dacflash;
#define VGMWAIT 15

static void write_ym(uint8_t data) {
  YM_DATA_PORT = data;
  YM_CTRL_PORT &= ~_BV(YM_WR); // Write data

 * Write data into a specific register of the YM2612
 * @author Furrtek
 * @param reg Destination register address
 * @param data Data to write
static void setreg(uint8_t reg, uint8_t data) {
  YM_CTRL_PORT &= ~_BV(YM_A0); // A0 low (select register)
  YM_CTRL_PORT |= _BV(YM_A0);  // A0 high (write register)

void call() {
  setreg(0x28, 0xF0); // Key on
  setreg(0x28, 0x00); // Key off

uint8_t getfilebyte() {
  return Serial.read();

int main() { 

  /* Pins setup */
  YM_CTRL_DDR |= _BV(YM_IC) | _BV(YM_CS) | _BV(YM_WR) | _BV(YM_RD) | _BV(YM_A0) | _BV(YM_A1);
  YM_DATA_DDR |= _BV(YD0) | _BV(YD1) | _BV(YD2) | _BV(YD3) | _BV(YD4) | _BV(YD5) | _BV(YD6) | _BV(YD7); 
  YM_CTRL_PORT |= _BV(YM_IC) | _BV(YM_CS) | _BV(YM_WR) | _BV(YM_RD); /* IC, CS, WR and RD HIGH by default */
  YM_CTRL_PORT &= ~(_BV(YM_A0) | _BV(YM_A1)); /* A0 and A1 LOW by default */

  /* F_CPU / 2 clock generation */
  TCCR1A = _BV(COM1A0);            /* Toggle OCA1 on compare match */
  TCCR1B = _BV(WGM12) | _BV(CS10); /* CTC mode with prescaler /1 */
  TCCR1C = 0;                      /* Flag reset */
  TCNT1 = 0;                       /* Counter reset */
  OCR1A = 0;                       /* Divide base clock by two */

  /* Reset YM2612 */

  /* YM2612 Test code */ 
  setreg(0x22, 0x00); // LFO off
  setreg(0x27, 0x00); // Note off (channel 0)
  setreg(0x28, 0x01); // Note off (channel 1)
  setreg(0x28, 0x02); // Note off (channel 2)
  setreg(0x28, 0x04); // Note off (channel 3)
  setreg(0x28, 0x05); // Note off (channel 4)
  setreg(0x28, 0x06); // Note off (channel 5)
  setreg(0x2B, 0x00); // DAC off
  setreg(0x30, 0x71); //
  setreg(0x34, 0x0D); //
  setreg(0x38, 0x33); //
  setreg(0x3C, 0x01); // DT1/MUL
  setreg(0x40, 0x23); //
  setreg(0x44, 0x2D); //
  setreg(0x48, 0x26); //
  setreg(0x4C, 0x00); // Total level
  setreg(0x50, 0x5F); //
  setreg(0x54, 0x99); //
  setreg(0x58, 0x5F); //
  setreg(0x5C, 0x94); // RS/AR 
  setreg(0x60, 0x05); //
  setreg(0x64, 0x05); //
  setreg(0x68, 0x05); //
  setreg(0x6C, 0x07); // AM/D1R
  setreg(0x70, 0x02); //
  setreg(0x74, 0x02); //
  setreg(0x78, 0x02); //
  setreg(0x7C, 0x02); // D2R
  setreg(0x80, 0x11); //
  setreg(0x84, 0x11); //
  setreg(0x88, 0x11); //
  setreg(0x8C, 0xA6); // D1L/RR
  setreg(0x90, 0x00); //
  setreg(0x94, 0x00); //
  setreg(0x98, 0x00); //
  setreg(0x9C, 0x00); // Proprietary
  setreg(0xB0, 0x32); // Feedback/algorithm
  setreg(0xB4, 0xC0); // Both speakers on
  setreg(0x28, 0x00); // Key off
  setreg(0xA4, 0x22); // 
  setreg(0xA0, 0x69); // Set frequency


  while(1000) {
    if (Serial.available() > 0) {
      char var = getfilebyte();
      Serial.print("Yeah: ");
    } else {
      Serial.print("Nooo: ");
  • 1
    Consider a Leonardo type (ATmega32u4) board which does not rely on the hardware UART pins for host communication, or consider a different platform entirely. Aug 7, 2017 at 4:05

3 Answers 3


You cannot set pin states in multiple ports simultaneously, if that's what you're asking.

At the instruction set level, setting pin states is a single that writes to an 8-bit register which corresponds to the 8 pins in a port. There is no way to write to two different registers in the same instruction, so if you need all eight bits to change exactly at the same time you're out of luck. However, if you can tolerate a little bit of time between 6 bits in one port changing state and 2 bits in another port changing state, then you can simply write a wrapper function.

PORTD[PD2, PD3, PD4, PD5, PD6, PD7] + PORTB[PB4, PB3];
void set_ym_ctrl_port(uint8_t data) {
    PORTD &= ~(0x3f<<2);
    PORTB &= ~(0x3<<3);
    PORTD |= (data & (0x3f<<2));
    PORTB |= (data & 0x3)<<3;

Or something like that. If you need even less delay and you think you can do better than the compiler, inline assembly can be helpful.

As an aside, syntax like

var portX = PORTD[PD2, PD3, PD4, PD5, PD6, PD7] + PORTB[PB4, PB3];

is not valid for a number of reasons.

  • Thanks a lot! Maybe i can test my luck in fast serial.begin and .end.
    – Thane Krios
    Aug 6, 2017 at 18:58
  • 2
    @ThaneKrios no, that simply will not work. You either use the hardware UART pins as a hardware UART or you don't - you have no practical hope of coordinating changing their role on the chip side with changing the electrical connection to the pins, without horribly breaking things. Aug 7, 2017 at 4:03

Adding to what @uint128_t said, if you really need to change the state of many pins at the same time, you can add some external latched shift registers as a SIPO (serial-in/parallel-out) interface towards the outer world.

The price you pay is that you have to load the entire register with the data you want to send before you can update the output pin states. This requires a clock cycle for each loaded bit (here I'm speaking of the clock of the external register, not Arduino's) and thus may add too much latency in your system, depending on what you are trying to do.

The 74HC595 is a classic (8-bit shift register with output latches), and can be cascaded to obtain a register of the desired width.

Here is an excerpt from the datasheet of TI's 74HC595 (yellow emphasis mine):

  enter image description here  

It's also sold in 16-pin DIP package, very convenient for breadboarding, in case you need it (common need for people that play with Arduinos).

The internal logic diagram is the following (from the same datasheet):

  enter image description here  

It is so common in the Arduino world that there are also open libraries do drive it, for example this. But even without pre-written libraries, it is not difficult to drive it from an Arduino, if you have a little experience in digital logic and can understand the timing requirements stated in its datasheet (the table in section 7.6 is fairly easy to understand, once you know what setup and hold times are).


This is intended as a complement to uint128_t's answer. I would like to point out that the code in that answer changes the outputs in four steps:

  1. set to LOW the pins of PORTD that need to be LOW
  2. set to LOW the pins of PORTB that need to be LOW
  3. set to HIGH the pins of PORTD that need to be HIGH
  4. set to HIGH the pins of PORTB that need to be HIGH

I compiled it on my system (with gcc 5.4.0): the generated assembly shows that the whole process takes 18 CPU cycles. I.e. during 1.125 µs the outputs can be in an inconsistent state.

I then tried this alternative version:

void set_ym_ctrl_port(uint8_t data) {
    uint8_t new_portd = PORTD & ~(0x3f<<2) | (data & (0x3f<<2)),
            new_portb = PORTB & ~(0x03<<3) | (data & 0x03)<<3;
    PORTD = new_portd;
    PORTB = new_portb;

This changes the outputs on two steps:

  1. update PORTD
  2. update PORTB

The generated assembly showed that the updates are only one CPU cycle apart. I.e. the outputs are inconsistent during only 62.5 ns.

Your mileage may vary. In case of doubt check the generated assembly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy