PICAXE 28X2 and MIDI IN to serin?

SolidWorksMagi

Senior Member
Hi,

I am using an Adafruit.com Adafruit MIDI FeatherWing Kit PRODUCT ID: 4740.

How do I input MIDI data to a PICAXE 28X2?

I want to use serin command, but when I try setting the MIDI speed with;

serin C.3,T31250_8,(MIDI) ; Fetch a MIDI Note

I get an error;
serin C.3,T31250_8,(MIDI) ; Fetch a MIDI Note
^
Syntax error on line 19 at/before position 10

Error: Unknown symbol - T31250_8
 

inglewoodpete

Senior Member
I think your only options would be to use the hSerIn command or background serial data reception. Both of these can only be used effectively on an X2 chip's internal hardware UART.

The baud rate parameter when configuring the hardware UART represents a divider number that is loaded into the UART's baud rate generator. You will need to calculate the value as defined in the PIC18F25K22 data sheet (Section 16.4) which can be found on the Microchip website.
 

SolidWorksMagi

Senior Member
I think your only options would be to use the hSerIn command or background serial data reception. Both of these can only be used effectively on an X2 chip's internal hardware UART.

The baud rate parameter when configuring the hardware UART represents a divider number that is loaded into the UART's baud rate generator. You will need to calculate the value as defined in the PIC18F25K22 data sheet (Section 16.4) which can be found on the Microchip website.

Hi, I'm not sure I did the math correctly because of a lack of brackets;

; For a device with FOSC of 16 MHz, desired baud rate of 9600, Asynchronous mode, 8-bit BRG:
; Desired Baud Rate FOSC/64([SPBRGHx:SPBRGx]] + 1)

; Solving for SPBRGHx:SPBRGx:
; X = FOSC/31250/64 -1 ? Should it be: X = (FOSC/31250/64) -1
; = 16000000/31250/64 -1

; Calculated Baud Rate = 16000000/64(7+1)
; = 2000000

; Error = Calc. Baud Rate - Desired Baud Rate/Desired Baud Rate
; = (2000000-31250)/31250 = 6300% ???

Sorry to say, I am not a genius, but I thought I did the math per the webpage PDF. https://ww1.microchip.com/downloads/en/DeviceDoc/40001412G.pdf

24543
 
Last edited:

AllyCat

Senior Member
Hi,

Yes, SERIN is "bit banged" by the PICaxe firmware so the speed is available only in multiples of two. However, HSERIN uses the base PIC hardware which can be loaded with almost any value that you need. You can discover what "numbers" that the interpreter uses and interpolate the value for HSERIN (or read the base PIC data sheet). For example try running the following in the simulator:
Code:
sertxd(#b19200_8,";",#t19200_32," : ")
sertxd(#b38400_8,";",#t38400_32)
Which should give 103 ; 0 : 51 ; 3 , so the value you need might be 63 (but it's too late here to check).

EDIT: Yes! b31250_8 is already defined in the PE ! :)

Cheers, Alan.
 
Last edited:

inglewoodpete

Senior Member
Mathematics convention says that without parenthesis, multiplications and divisions are handled first, followed by additions and subtractions.

So X = FOSC/31250/64 -1 is the same as X = (FOSC/31250/64) -1 (However, the final divisor should be 4 as this was the option chosen by RevEd to give 16-bit divisors and the widest range of baud rates)

Alan's figures above have a couple of errors (he admits it was late in the UK o_O): the second values in each of the sertxd statements are tokens for the bit-banged baud rates. When the source code is corrected, the values returned are 103;416 : 51;207 which is what I would expect

Using SolidWorksMagic's first example: 16MHz, 9600 baud (16-bit Async)

16,000,000/9,600/4 - 1
=1,666.6667/4 - 1
=416.6667 - 1
=416 when rounded to the closest integer, with an (acceptable) error of -0.08%

So, for the MIDI baud rate of 31,250 at SWM's chosen clock rate of 8 MHz
8,000,000/31,250/4 - 1
=256.0/4 - 1
=64 - 1
=63 with no error

Out of interest, it may be possible to play around with the BRG16 and BRGH bits using PeekSFR and PokeSFR after executing the hSerSetup command but I see no need to do that here.

Edit: ~sigh~ Yes, Alan's later update is correct. b31250_4, b31250_8, b31250_16, b31250_20, b31250_32, b31250_40, b31250_64 are all reserved words (refer to Appendix 2 of Manual 2 (Commands)). Of course, these parameter values can only be used on the chip's internal hardware UART.
 
Last edited:

Buzby

Senior Member
... b31250_8 is already defined in the PE ...
Yes, and I used it when I did my MIDI drum kit experiments. ( See https://picaxeforum.co.uk/threads/drum-machine-its-not-quite-a-roland-or-a-korg-but-not-bad-for-few-hours-effort.31696/#post-329162 )

That circuit only did transmission of MIDI, using 'hserout'.

One major problem I had was that even though the MIDI signal from the PICAXE looked perfect on the scope, with correct timing and currents, my Yamaha drum box would not respond. Only after I 'buffered' the signal using some third party software did the system work. I did not solve this problem at the time, but if I was to need MIDI again I would investigate the opto circuits very closely before doing any more development.

My suggestion is for you to take it a step at a time, and start with two PICAXES hardwired together. Use one to 'hserout' characters, and the other to 'hserin'. Once you have this working you can insert the opto circuits and test again, eventually removing the 'transmit' PICAXE and replacing it with your MIDI source.

MIDI is fun, but has some gotchas !.

Please keep us informed of your progress.

Cheers,

Buzby
 

AllyCat

Senior Member
Hi,
Alan's figures above have a couple of errors ....
Not exactly errors, I was intentionally trying to show that the SERIN parameters (T19200_32, etc.) are numerical tokens, which (strangely) have the sequence 1 , 2 , 3 , 0 . Those vary with the SETFREQ value, but no other division ratios are possible.

But my mistakes were to try to calculate the conversion ratio on a ten year old Android device in the middle of the night, and not having the sense to type B31250_8 into the PE at the beginning. ;)

Cheers, Alan.
 

SolidWorksMagi

Senior Member
Hi,

Yes, SERIN is "bit banged" by the PICaxe firmware so the speed is available only in multiples of two. However, HSERIN uses the base PIC hardware which can be loaded with almost any value that you need. You can discover what "numbers" that the interpreter uses and interpolate the value for HSERIN (or read the base PIC data sheet). For example try running the following in the simulator:
Code:
sertxd(#b19200_8,";",#t19200_32," : ")
sertxd(#b38400_8,";",#t38400_32)
Which should give 103 ; 0 : 51 ; 3 , so the value you need might be 63 (but it's too late here to check).

EDIT: Yes! b31250_8 is already defined in the PE ! :)

Cheers, Alan.
Hi, Thanks! I don't get an error with;
serin C.3,b31250_8,(MIDI) ; Fetch a MIDI Note

Now I just have to get the +5, GND and MIDI IN PINs identified on the Adafruit MIDI FeatherWing Kit. Adafruit support just say's goto the forums and they don't post a schematic for the board and I cannot read the chip ID on the chip!
 

SolidWorksMagi

Senior Member
Hi,

Not exactly errors, I was intentionally trying to show that the SERIN parameters (T19200_32, etc.) are numerical tokens, which (strangely) have the sequence 1 , 2 , 3 , 0 . Those vary with the SETFREQ value, but no other division ratios are possible.

But my mistakes were to try to calculate the conversion ratio on a ten year old Android device in the middle of the night, and not having the sense to type B31250_8 into the PE at the beginning. ;)

Cheers, Alan.

Hi, I tried looking in the manual 1, but although I see the b31250_8 in the Appendix, what does the "B" stand for? N = Inverted, T = Non Inverted, what is B?
 

AllyCat

Senior Member
Hi,

Probably Baud . ;)

The polarity of the HSERIN/OUT data is set by the HSERSETUP/Hardware instruction (flags), whilst for SERIN/OUT the polarity is defined as part of the "Baud Rate" parameter (N for Negative/Inverted and T for True/TTL or Non-Inverted).

Cheers, Alan.
 

Buzby

Senior Member
Hi, Thanks! I don't get an error with;
serin C.3,b31250_8,(MIDI) ; Fetch a MIDI Note
You won't get a syntax error, but neither will you get any notes !.

'b31250_8' translates to the value 63. The 'serin' instruction requires a value of 0 to 7. ( See https://picaxe.com/basic-commands/serial-rs232-interfacing/serin/ for details. )

The only way you can use 'b31250_8' is with the hardware serial, you are on a hiding to nothing trying to use 'serin'.

Cheers,

Buzby
 

Buzby

Senior Member
Now I just have to get the +5, GND and MIDI IN PINs identified on the Adafruit MIDI FeatherWing Kit.
If you are intending to use the PICAXE to receive from the Featherwing, the you need to identify MIDI OUT.

I've not looked at the Featherwing datasheet, but if it is real MIDI it will be using opto-isolated current loops through 5-pin DIN connectors. It may be possible to hack the Featherwing and connect to the circuit before the opto-isolators, but if you need to do this make sure you know what you are doing !.

Cheers,

Buzby
 

SolidWorksMagi

Senior Member
If you are intending to use the PICAXE to receive from the Featherwing, the you need to identify MIDI OUT.

I've not looked at the Featherwing datasheet, but if it is real MIDI it will be using opto-isolated current loops through 5-pin DIN connectors. It may be possible to hack the Featherwing and connect to the circuit before the opto-isolators, but if you need to do this make sure you know what you are doing !.

Cheers,

Buzby
Hi,

I don't have a Featherwing, I have the MIDI interface for the Featherwing.

MIDI out is to send MIDI data to an external device MIDI device like a syntesizer. I want to receive MIDI data from an external device and use it to drive things like stepper motors and FDDs etc. Play music.

From a few webpages I've read, MIDI IN should be opto isolated to protect my equipment.
 

SolidWorksMagi

Senior Member
Here is the main pdf !
Hi,

A quick look this looks like something I should build since it could be more than one project in the future. With EasyEDA I can create a board design and get five boards made cheaply. Since the Opto Isolator and resistors etc. are cheap, I can just leave a socket for a PICAXE or maybe make a board with a cable that could plug to my RoboGuts™ circuit board.
 

inglewoodpete

Senior Member
Hi, Thanks! I don't get an error with;
serin C.3,b31250_8,(MIDI) ; Fetch a MIDI Note
Didn't get an error? I don't think you've tried it yet. As far as I can tell, b31250_8 will not receive 31250 baud data on the actual hardware.

I never use the simulator for protocol/interfacing issues - only real hardware will confirm that it works.
 

SolidWorksMagi

Senior Member
Didn't get an error? I don't think you've tried it yet. As far as I can tell, b31250_8 will not receive 31250 baud data on the actual hardware.

I never use the simulator for protocol/interfacing issues - only real hardware will confirm that it works.
Hi, I didn't get an error when "Checking" the syntax. I haven't tried it with the actual chip yet because I need a MIDI connector to put on the MIDI cable coming from a keyboard synth.
 

Buzby

Senior Member
Hi, I didn't get an error when "Checking" the syntax. I haven't tried it with the actual chip yet because I need a MIDI connector to put on the MIDI cable coming from a keyboard synth.
You can test your code without a MIDI connector or the Featherwing interface board. Just use a PC or second PICAXE to generate serial data at 31250 baud, using a hardwired connection.

Have you got a USB-to-MIDI convertor ?. Using one of these with some free software, like MIDI-OX, will really help with getting MIDI working on a PICAXE.

Cheers,

Buzby
 

SolidWorksMagi

Senior Member
You can test your code without a MIDI connector or the Featherwing interface board. Just use a PC or second PICAXE to generate serial data at 31250 baud, using a hardwired connection.

Have you got a USB-to-MIDI convertor ?. Using one of these with some free software, like MIDI-OX, will really help with getting MIDI working on a PICAXE.

Cheers,

Buzby

Hi, There's a useful idea, although I have a larger version of that, the Presonus Audio Box (MIDI I/O + Audio I/O). It came with the MIDI editor I use to write music.
 
Top