• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC as a Keyboard

Suraj143

Active Member
I want PIC to send data via UART to PC so the PC will see as a key press.Is there any simple program to see this? All I found on internet using arduino & they using a keyboard library.
Is there any PIC example? Sending one or two key data is ok for me to understand.

Thanks
 

Nigel Goodwin

Super Moderator
Most Helpful Member
What do you mean 'see as a key press'? - all you do is send ASCII data via the serial port (or on a modern PC via a serial/USB converter). You need some kind of program running on the PC in order to access the incoming text data.
 

Suraj143

Active Member
What do you mean 'see as a key press'? - all you do is send ASCII data via the serial port (or on a modern PC via a serial/USB converter). You need some kind of program running on the PC in order to access the incoming text data.
I mean there is no any keyboard connect to the PC.From PIC I send keyboard commands from UART.
 

Nigel Goodwin

Super Moderator
Most Helpful Member
It's not really anything to do with the PIC, or the Arduino, both just send plain ASCII data out the serial port - the keyboard library for the Arduino is simply for connecting a matrix keypad to the PIC.

If you want the PC to do something with the data it needs to be running a program to do so, usually that's just a comms program.
 

Suraj143

Active Member
I mean when you just send ASCII characters via serial it needs a terminal program to see.

But when you send keystroke commands it will act likes a button pressed on the keyboard.

1595865151358.png
 

Nigel Goodwin

Super Moderator
Most Helpful Member
OK, not a standard Arduino library then - you could probably do it with a suitable USB capable PIC? - essentially you're trying to build a USB PC keyboard using a PIC.

A quick google finds at least one suitable starting point.

 

Suraj143

Active Member
Many thanks That will make my requirement.I'm making a MIDI octopad, software is FL studio.Want to give MIDI signals via serial port to "serial MIDI" bridge software.All the examples are done with arduinos.SO I have to write from the begining to PIC micro.
 

rjenkinsgb

Well-Known Member
Most Helpful Member
I'm making a MIDI octopad, software is FL studio.Want to give MIDI signals via serial port to "serial MIDI" bridge software.
MIDI just uses absolutely conventional serial comms and easily be done with a PIC, nothing special is required.
See below for the relevant MIDI transmit routines from a product we designed for a customer many years ago. I've commented them to try and make the various parts clear.

The full unit used piezo disc transducers, read by the PIC ADC channels to give touch sensitivity, with a 20KHz interrupt routine reading the input values. We made the electronics to fit the customers device enclosure.


The UART output just needs to drive a buffer with a couple of 220 Ohm resistors connected to that and 5V to give a standard MIDI output.
Ideally it should be buffered - but a typical PIC had got adequate current drive capability if you do not mind a direct external connection.

Or you can use a PNP transistor and switch the positive side, so UART pin low is still "current on" at the output.

Hardware interface info here:


You will need to add the main program with initialisation and a loop that continually checks for new keypresses.
If you want "note off" as well, send note on for that note using the same keypress function, but with a velocity of zero.


C:
// Buffer allocation

int     tx_chan;
int     buf[64];
int     bufin;
int     bufout;
int     bufcount;

// Device setup, using CCS C compiler:
#include <18f452.h>

#device ADC=10
//#device icd=true

// HS = Normal xtal, H4 = pll * 4...
#fuses    H4, NOWDT
#use delay(clock = 40000000)
#use rs232(BAUD = 31250, XMIT = PIN_C6, RCV = PIN_C7)


// Initialisation

void clearall(void)
{
    bufin = 0;
    bufout = 0;
    bufcount = 0;
    tx_chan = 10; // Channel 10 is MIDI percussion
}         

// Routine to add a character/byte to the buffer
void putbuf(int i)
{
    buf[bufin++] = i;
    bufcount++;
    bufin &= 0x3f;
}


// MIDI keypress function:
// n is note number, v is velocity
void keydown(int n, int v)
{
    if(bufcount < 61)
    {
        putbuf(0x90+tx_chan);
        putbuf(n & 0x7f);
        putbuf(v & 0x7f);
    }
}

// Routine to send "all off" command
void all_off(void)
{
    putbuf(0xb0 + tx_chan);
    putbuf(0x7b);
    putbuf(0);
}

// Buffer transmit routine - call continuously from the program main loop
void sendata(void)
{
    if(bufin == bufout)
    {
        bufcount = 0;
        return;
    }
  
    if(tx_ready())
    {             
        putchar(buf[bufout++]);
        bufout &= 0x3f;
        bufcount--;
     } 
}

// Function to chek if UART TX ruffer is empty & ready for a new character
// For PIC18f452 - adjust as required, or your compiler may have a busy/ready fucntion.
int tx_ready(void)
{
#bit    pir1_txif=0xf9e.4;

#asm
    movlw    1
    movwf    _return_
    btfss    pir1_txif
    clrf    _return_
    nop
#endasm
}
 

Suraj143

Active Member
Many thanks rjenkinsgb

Here is my structure. I read piezo analog readings & process it & make MIDI strings & send via PIC UART to "MIDI Serial" software.That part I can do.After that from FL studio I map MIDI notes to its octopad note map.

The case I may occur in future is everytime I need FL studio.Is there any method to play MIDI without a PC?I just thinking to store MIDI wave files to an SD card MP3 player & call them from a UART thing.

1596083082629.png
 

rjenkinsgb

Well-Known Member
Most Helpful Member
MIDI existed before PCs!
It was created to allow instruments to be directly connected and control each other.

I do not understand the "MIDI - Serial bridge" ??
The MIDI out from the PIC device can directly connect to any other standard MIDI instrument, or to a MIDI<>USB interface for a PC.

Re. storing .WAV files and playing them - that's exactly what this gadget does, another product from the same time.
The required set of .WAV files were built in to an EPROM that fitted the big socket, in this case.

MIDI_Soundstore.JPG

That's not got "touch sensitivity" though, the PIC used was nowhere near powerful enough for complex audio processing.


For MIDI music playback, you can link to any keyboard/synthesiser that has MIDI in, or to a stand-alone MIDI sound module such as a Roland Sound Canvas. I have an SC-88 in my setup.

It appear you want near enough the exact same device as the mini drum kit, but set to MIDI channel 1 (or whichever) rather than channel 10 for percussion.

What PIC device do you have? As long as it is suitable, I can build you a version and let you have the .hex file for it.

ps. Personally, I do not like FL. I would highly recommend Tracktion though, and there is a completely free version of that with no restrictions at all.
 

Suraj143

Active Member
I do not understand the "MIDI - Serial bridge" ??
The MIDI out from the PIC device can directly connect to any other standard MIDI instrument, or to a MIDI<>USB interface for a PC.
In my method you dont need any musical instruments.All you need is a PC/laptop.PIC serial output connected to the PC via a TTL-USB cable Thats it.

The FL studio only supports MIDI devices.We dont have such devices/ instruments.We have only PIC part. So here comes the "MIDI - Serial bridge" which will makes a MIDI port to FL studio.


Whats the eeprom you using to store wave files? This is where I got stucked.
 

rjenkinsgb

Well-Known Member
Most Helpful Member
So here comes the "MIDI - Serial bridge" which will makes a MIDI port
Gotcha!
That allows a normal RS232 port to appear as a MIDI interface to programs. Neat!

The midi player device used a 27C1001, if I remember right. I wrote a PC program that that could be used to import .wav files and build a binary image for the eprom, with an index to the various sound samples so the PIC program could associate the MIDI note with the appropriate sample, once the EPROM version was created.
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Gotcha!
That allows a normal RS232 port to appear as a MIDI interface to programs. Neat!
I'm still a bit baffled? - MIDI is simply a serial port?, just of a specific speed. The Commodore Amiga could easily be used for Midi via it's serial port, and I did projects for a couple of simple interfaces which were published in a couple of magazines. For those old enough to remember Joystick (game) ports on PC's, that also usually included a Midi specific serial port.
 

rjenkinsgb

Well-Known Member
Most Helpful Member
I'm still a bit baffled? - MIDI is simply a serial port?, just of a specific speed.
Hardware wise, yes, just a serial port with a simple current loop interface; but no present music software will see a normal COM port as a MIDI interface.
They all expect a software translation layer between a physical (or virtual) port and the logical MIDI devices.

That's what the "Hairless MIDI" driver does, it provides the logical MIDI port that can be recognised by music software, allowing a COM port to be used for MIDI.
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Hardware wise, yes, just a serial port with a simple current loop interface; but no present music software will see a normal COM port as a MIDI interface.
They all expect a software translation layer between a physical (or virtual) port and the logical MIDI devices.

That's what the "Hairless MIDI" driver does, it provides the logical MIDI port that can be recognised by music software, allowing a COM port to be used for MIDI.
OK, so just PC software.
 

rjenkinsgb

Well-Known Member
Most Helpful Member
Yep. That's what initially confused me, as "Midi to serial" seemed nonsensical - but it was not a physical conversion.
 

Suraj143

Active Member
Is there is a way to store WAV files into a flash memory in binary format? I have never done that.....!!
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Is there is a way to store WAV files into a flash memory in binary format? I have never done that.....!!
Yes, it's just a simple data array.

Depending how much 'quality' you're looking for even an 8 bit audio file is pretty decent quality - the Commodore Amiga (second mention in this thread :D) only had 8 bit audio, but it gave absolutely stunning 8 bit stereo sound. Even more impressive as way back then all a PC could do was make a 'beep'.

If you google for 'esp32 play wav file' there are plenty of examples.

Or, a simple PIC version:

 

Suraj143

Active Member
Thanks.That links help me a lot.

Finally I want to play wav files when I get UART commands.Let say I have 300+ wave files stored in SD card.PIC will send UART number ' 1- 300'.The player should instantly play that particular WAV file.

I think ESP32 is much a better choice for this.It's supports more files than that PIC project.
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Thanks.That links help me a lot.

Finally I want to play wav files when I get UART commands.Let say I have 300+ wave files stored in SD card.PIC will send UART number ' 1- 300'.The player should instantly play that particular WAV file.

I think ESP32 is much a better choice for this.It's supports more files than that PIC project.
An ESP32 is a VERY high spec processor, with LOT'S of memory :D
 

Latest threads

EE World Online Articles

Loading
Top