CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Transmitting MIDI messages with PIC16F628A doesn't work?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
FunkyLabRat



Joined: 13 Feb 2012
Posts: 10

View user's profile Send private message

Transmitting MIDI messages with PIC16F628A doesn't work?
PostPosted: Mon Feb 13, 2012 1:36 am     Reply with quote

Hi everybody! I'm very new to PIC programming. I've done my first projects with blinking LED's etc. a few days ago and it all worked fine.

Now I'm working on a new project where the PIC has to send MIDI messages to a MIDI DIN chassis.

The program starts with a delay of 1000ms, followed by sending the first midi messages in three bytes by using the putc() command, and a LED that turns on at pin A0. The putc transmit-pin is set to B2.

After another delay of 1000ms it sends another three bytes, and it turns the LED off. Then the program loops.


The LED is turning on and off like it should every 1000ms, but I'm not receiving any MIDI messages from the PIC. I'm sure the hardware part is hooked up fine and the problem is in the code.

The only strange problem I've encountered (also in my previous test projects) is that the PIC only works if I choose INTRC I/O as Osc while sending the HEX data to the PIC using a programmer.

Can someone please help me figuring out this problem?

The information on MIDI I've used so far comes from this article: http://alumni.media.mit.edu/~dmerrill/mas863/circuit.html

Thanks in advance Smile

The code:

Code:


#include <16F628A.h>
#use delay(clock=20000000)

#fuses HS, NOWDT, NOPROTECT, PUT, NOBROWNOUT, NOMCLR, NOLVP, NOCPD

#use fast_io(B)
#use fast_io(A)

#use rs232(BAUD=31250,XMIT=PIN_B2,RCV=PIN_B1,BRGH1OK)

// I've also tried the next line without success:
// #use rs232(baud=31250,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8)


void send_3byte_midi_msg(char b1, char b2, char b3)
{
  putc(b1);
  putc(b2);
  putc(b3);
 }

void main()
{

  disable_interrupts(GLOBAL);
  set_rtcc(0);
  setup_counters(RTCC_INTERNAL, RTCC_DIV_128);   

   set_tris_a(0x00); // make all A pins outputs
   set_tris_b(0x00); // make all B pins outputs

while(1) {

delay_ms (1000);

           send_3byte_midi_msg(0x80, 0x3C, 0x00);
           output_high(pin_a0);
           
delay_ms (1000);

           send_3byte_midi_msg(0x90, 0x3C, 0x7F);
           output_low(pin_a0);

   }

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 13, 2012 2:34 pm     Reply with quote

Quote:
#use fast_io(B)
#use fast_io(A)

disable_interrupts(GLOBAL);
set_rtcc(0);
setup_counters(RTCC_INTERNAL, RTCC_DIV_128);

set_tris_a(0x00); // make all A pins outputs
set_tris_b(0x00); // make all B pins outputs

You don't need any of the lines above to transmit bytes by #use rs232().
Remove them.

Quote:

The only strange problem I've encountered (also in my previous test
projects) is that the PIC only works if I choose INTRC I/O as Osc while
sending the HEX data to the PIC using a programmer.

Do you have a 20 MHz crystal and two 22pf capacitors installed in the
recommended circuit configuration, on the two oscillator pins of the PIC ?
If you can't make something simple such as a blinking LED loop, then
your crystal oscillator is not working.


Quote:
I'm sure the hardware part is hooked up fine and the problem is in the code.

How do you know the hardware serial port connections are correct ?
And, how do you know that the Midi codes in your program will cause
the required action in your Midi device ?
FunkyLabRat



Joined: 13 Feb 2012
Posts: 10

View user's profile Send private message

PostPosted: Mon Feb 13, 2012 4:25 pm     Reply with quote

Quote:
Do you have a 20 MHz crystal and two 22pf capacitors installed in the
recommended circuit configuration, on the two oscillator pins of the PIC ?
If you can't make something simple such as a blinking LED loop, then
your crystal oscillator is not working.


Gee, no wonder "the PIC only works if I choose INTRC I/O as Osc". I really thought the crystal Oscillators where included in the PIC itself Shocked Shocked Shocked
I told you I'm a total beginner Embarassed

Do you know if it's possible to do what I want by using the INTRC_IO, or do I NEED an external Oscillator for RS-232 transfer?

Quote:
How do you know the hardware serial port connections are correct ?


There are not much options (+5v, data pin and ground). Connected like this: http://devices.sapp.org/circuit/midi-out/

Quote:
And, how do you know that the Midi codes in your program will cause
the required action in your Midi device ?


I'm using MIDI-OX to monitor all incoming MIDI data.

Here's my updated code;

Code:
#include <16F628A.h>

#use delay(clock=20000000)

#fuses INTRC_IO, NOWDT, NOPROTECT, PUT, NOBROWNOUT, NOMCLR, NOLVP, NOCPD

#use rs232(baud=31250,parity=N,xmit=PIN_B2,rcv=PIN_B1,stop=1,bits=8,BRGH1OK)

void send_3byte_midi_msg(char b1, char b2, char b3)
{
  putc(b1);
  putc(b2);
  putc(b3);
 }

void main()
{

while(1) {

  output_high(PIN_A0);
           
  delay_ms (1000);

  output_low(PIN_A0);

  send_3byte_midi_msg(0x80, 0x3C, 0x00);
  send_3byte_midi_msg(0x90, 0x3C, 0x7F);
 
  delay_ms (1000);

   }

}


Thank you for your time!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 13, 2012 4:38 pm     Reply with quote

You need to put the pre-processor lines in the correct order, as shown
below. Note how the #use delay() comes after the #fuses.
Code:
#include <16F628A.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, PUT, NOBROWNOUT, NOMCLR, NOLVP, NOCPD
#use delay(clock=20000000)
#use rs232(baud=31250,parity=N,xmit=PIN_B2,rcv=PIN_B1,stop=1,bits=8,BRGH1OK)



If you do that, then you will get a helpful error message:
Quote:
*** Error 99 Line 3(5,28): Option invalid Internal Osc Freq Wrong

The internal oscillator can only run at 4 MHz on this PIC. You need to
edit the #use delay to this:
Code:

#use delay(clock=4M)

If you do those two changes, the baud rate coming out of the PIC's Tx
pin should now be correct. It will match the value in your #use rs232()
statement and your program might now work.
FunkyLabRat



Joined: 13 Feb 2012
Posts: 10

View user's profile Send private message

PostPosted: Mon Feb 13, 2012 7:02 pm     Reply with quote

Wow, that worked!

Thank you very very much, PCM programmer! You've saved me from many troublesome head-spinning days and nights Very Happy

Next step is figuring out how to add some buttons etc.
RoGuE_StreaK



Joined: 02 Feb 2010
Posts: 73

View user's profile Send private message

PostPosted: Tue Feb 14, 2012 8:40 pm     Reply with quote

FunkyLabRat wrote:
Next step is figuring out how to add some buttons etc.

Don't forget to add a debounce routine, otherwise it may think you've sent multiple button presses.
FunkyLabRat



Joined: 13 Feb 2012
Posts: 10

View user's profile Send private message

PostPosted: Tue Feb 14, 2012 10:50 pm     Reply with quote

Quote:
Don't forget to add a debounce routine, otherwise it may think you've sent multiple button presses.


Thanks. I'd already thought about that, but I'm not sure how sensitive the buttons and transmitting will be. I'll try hooking up some buttons first (waiting for them to arrive in the mail) and will see for myself Smile

Is hooking up multiple buttons on one pin using resistors totally safe? If there IS a chance it could fail under circumstances (room temperature etc.) I really won't use that option, but get a PIC with enough pins.
CodeSearcher



Joined: 03 Aug 2013
Posts: 1

View user's profile Send private message

Searched Code Found!
PostPosted: Sat Aug 03, 2013 4:25 pm     Reply with quote

I've looking for this code some months! Thank you very much. I had found some codes or too much complex or for another compilers (Micro C, MPLab - Assembly).

The most interesting thing is the simplicity of this code.

It was because this code, that I registered myself to thank you.

Very Happy
_________________
I'm Just another brick on the wall.... versaopoetica.blogspot.com
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group