Sunday, April 27, 2014



When I got my mini-midi-keyboard, I decided to add USB support to the MIDI2VC, since this is what most modern MIDI devices use. This new feature is quite complex to add. It needs and MCU with USB host capabilities. The simplest PIC I found with this feature was the PIC24FJ64GA004, which is a 44 pin 16-bit MCU. Obviously changing to this device will be a major change in the project, so I decided to name the new board MIDI2VC+. I'm working on the schematic and the layout right now, and as soon as I've sent the design to OshPark, I'll finish the MIDI2VC and release it as an open source hardware project.

I2C: 3.3V and 5V bus

When working on the new MIDI2VC+ design, I was bothered with the different supply voltage of the DAC and the new PIC. The new PIC is using CMOS (3.3V) levels and the DAC TTL (5V). This means a 3.3V I2C bus and a 5V I2C bus that needs to communicate. My first solution was using the PCA9517A, a bidirectional, level changing, bus repeater. This seemed all well and dandy, until I realised that I needed to order this part from RS Components, and the shipping would cost more than 10 times than the part. This went against my principle of making a project with cheap, and easy to source components.
 After doing some research I understood that sometimes it's possible to run different I2C level buses on the same voltage bus. Here's how it works. The I2C bus is an open drain bus. This means that the bus has pull up resistors to the higher voltage level. So if no one does anything on the bus, the bus sits at its high level. As soon as any device on the bus wants to communicate, it sinks current from the bus, making it go low, to  send a logic 1. For a logic 0 the device lets the bus go to the high level. The whole protocol is well explained on several places on the web...
 Lets look at our to devices then: 
The DAC is a five volt logic device. It sinks the bus down to 0.4V when signaling a low level. Thresholds are VIL = 0.3*5V = 1.5V and VIH = 0.7*5V = 3.5V.
The PIC is a 3.3V device, but it tolerates up to 5.5V on digital only pins (which we will use). It sinks the bus to 0V for a low level. It's thresholds are VIL = 0.3*3.3V = 0.99V and VIH = 0.7*3.3V = 2.31V.
Seems like the two devices can communicate over I2C despite the different logic levels!

Tuesday, April 8, 2014

MIDI2VC, finally stable

Phew, another rewrite of the whole code.. This time I changed pretty much all variables to chars, which is 8bits wide, the size that the processor use native. I also increased the clock speed to 32MHz, which is the highest frequency with the built in oscillator.
 Now I cannot provoke an overrun error, or buffer overflow. This means that it's now time to add the pitch bend feature along with the trigger output (and then it's done ; ) .

Wednesday, April 2, 2014

MIDI2VC, Inline Assembly optimisation

I've been trying to use inline assembly to optimise the push and pop uart queue functions, since I still have some problems with overflowing of the UART reception.
 Even though the free version of XC8 seems to add some assembly instructions that doesn't do anything, it still seems to be doing a pretty good job. At least better than mine. When writing inline assembly, there are some precautions you need to take so that assembly code doesn't mess up what XC8 have been doing. This takes quite a bit of code to do, so it seems my try at optimisation wasn't that successful...
 In the end, learning assembly gives some insight into how the MCU works, so I still did some optimisation of the C code, by changing the queue to char's(8 bit), instead of int's(16bit).
 Right now it is quite difficult to provoke an UART overflow, but it's still possible. So I'm going to turn up the clock speed of the PIC, and think some more about how to make the UART functions faster.