NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Sending MIDI data using serial bus

August 24, 2012
by HexManiac
HexManiac's Avatar

I've used the piezo transducer from the NerdKit to make a drum trigger, the intention being to send appropriate midi messages to a drum synth to make a sound when I hit the transducer.

I've got the trigger part working nicely, I can light an LED by tapping the transducer. But when I try to send out midi data I get absolutely zilch.

I've pieced together the code below from various sources, and the hardware is just a midi socket on piece of stripboard, with header pins so I can plug it into the breadboard.

The midi socket's pin 4 is connected directly to the ATMega168's TX pin, pin 5 is connected via a 220R resistor to +5v.

I know the midi cable is good, when I unplug it from Nerdkit and connect it to the midi out socket of my roland synth I can play notes and hear drums from the drum synth. When I connect it back to the Nerdkit, back to zilch again.

Can anyone help me please?

Drum Trigger Board

Source code (with the boring buts cut out):

#define F_CPU 14745600

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <inttypes.h>
#include <avr/pgmspace.h>
#include <avr/io.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include "../libnerdkits/uart.h"

#define TRUE 1
#define FALSE 0
#define MIDI_BAUD 31250

void midiInit(bool out, bool in)
{
    // Set baud rate to 31250
    uint8_t midiUBRR = (uint8_t)(F_CPU/16.0f/MIDI_BAUD); // = 29;
    UBRR0H = (uint8_t)(midiUBRR   >> 8);
    UBRR0L = (uint8_t)(midiUBRR & 0xFF);
    if(out)
    {
        // Enable transmitter
        UCSR0B |= (1<<TXEN0);
    }
    if(in) 
    {
        //Enable receiver
        UCSR0B |= ((1<<RXEN0) | (1<<RXCIE0));
    }
    //Set frame format: Async, 8data, 1 stop bit, 1 start bit, no parity
    UCSR0C = (1<<UCSZ01) | (1<<UCSZ00);
}

void sendmidi(uint8_t databyte)
{
    while ((UCSR0A & (1 << UDRE0)) == 0); // Do nothing until UDR is ready for more data to be written to it 
    UDR0 = databyte; // Send out the byte value in the variable "databyte"
}

int main() 
{......................

    while(1) 
    { 
        // get sample
        trig_sample = adc_read();

        if(trig_sample>0)
        {
            is_trig = TRUE;
            PORTB |= (1<<PB1); // LED ON
            sendmidi(0x90); // note on, midi channel 1
            sendmidi(0x3C); // C3
            sendmidi(0x7F); // max velocity (127)

            if(trig_sample>last_trig_sample)
            {
                last_trig_sample = trig_sample;
            }

            delay_ms(50);
            sendmidi(0x90); // note on, midi channel 1
            sendmidi(0x3C); // C3
            sendmidi(0x00); // min velocity (0)

        }
        else
        {
            is_trig = FALSE;
            PORTB &= ~(1<<PB1); // LED OFF
            last_trig_sample = 0;
        }

    }........
August 24, 2012
by HexManiac
HexManiac's Avatar

Sorry, messed up the image url! Drum Trigger Board

August 24, 2012
by Ralphxyz
Ralphxyz's Avatar

re: image URL

Here is what Google says about it:

403. That’s an error.

Your client does not have permission to get URL /Fggx8o8Bgi7vz-NrwHgooes746gD8Ntb71RA7VGHe2ow3Oj69E4DAePuSKFRJRdP5aMyzdVXd94 from this server. (Client IP address: 24.45.153.118)

Forbidden That’s all we know.

Ralph

August 24, 2012
by HexManiac
HexManiac's Avatar

Sorry about that - I uploaded the image using Google Drive, and I changed the share settings to 'anyone who has the link'. Guess I'll have to find somewhere else to host the image. Check back later...

August 24, 2012
by HexManiac
HexManiac's Avatar

I think this is should work... DrumTrigBoard

August 25, 2012
by HexManiac
HexManiac's Avatar

And here's the trigger circuit:

Piezo Trigger Circuit

Post a Reply

Please log in to post a reply.

Did you know that a NerdKit can take control of a remote-controlled car? Learn more...