Hello guys!
This is my first time on this forum and in need of some help.
I've been trying to read incoming MIDI serial data (31250 kbps, start bit, 8 data bits, stop bit) on the RB5 pin. The MIDI data is connected via an optocoupler (6N137) directly to the pin, which is pulled up by the weak-pull-up feature built into the pic. The PIC runs on either an external crystal (8MHz, 22pF caps) or the internal 8MHz oscillator.
In the software, I poll the RB5 pin until it is low, then wait 48 usec (start bit + 16 usec to reach the middle of the first bit), read the bit, store it and wait the appropriate amount of time for the next bit. After storing 8 bits, I check for the presence of the stop bit and then the program goes into a stall.
With the debug feature of the PicKit2 I've been able to read the stored data. I am sending this midi message:
1011cccc 0nnnnnnn (where c = the midi channel number, n = CC number. I've left out the start/stop bit here for ease of viewing)
And the computer reads this message perfectly, so the sending part works.
However, when I look at the stored value for the serial data, it is not correct. By changing the value for c and for n, I've managed to decipher what it IS recording and come to the following conclusion:
It records the bits too slow!
Now, seeing how this happens both when using external or internal oscillators, I have no clue where to look. What can destabalize the clock? Anything in the hardware/software I should look at? Can the debug mode of the PicKit2 cause destabalization?
Thank you in advance for your help,
Bart
PS, I've included my midi_in code:
This is my first time on this forum and in need of some help.
I've been trying to read incoming MIDI serial data (31250 kbps, start bit, 8 data bits, stop bit) on the RB5 pin. The MIDI data is connected via an optocoupler (6N137) directly to the pin, which is pulled up by the weak-pull-up feature built into the pic. The PIC runs on either an external crystal (8MHz, 22pF caps) or the internal 8MHz oscillator.
In the software, I poll the RB5 pin until it is low, then wait 48 usec (start bit + 16 usec to reach the middle of the first bit), read the bit, store it and wait the appropriate amount of time for the next bit. After storing 8 bits, I check for the presence of the stop bit and then the program goes into a stall.
With the debug feature of the PicKit2 I've been able to read the stored data. I am sending this midi message:
1011cccc 0nnnnnnn (where c = the midi channel number, n = CC number. I've left out the start/stop bit here for ease of viewing)
And the computer reads this message perfectly, so the sending part works.
However, when I look at the stored value for the serial data, it is not correct. By changing the value for c and for n, I've managed to decipher what it IS recording and come to the following conclusion:
It records the bits too slow!
Code:
scccc1101ssnnnnnnn0s
1 234 56 78
Now, seeing how this happens both when using external or internal oscillators, I have no clue where to look. What can destabalize the clock? Anything in the hardware/software I should look at? Can the debug mode of the PicKit2 cause destabalization?
Thank you in advance for your help,
Bart
PS, I've included my midi_in code:
Code:
midi_in
btfsc PORTB,5 ;Detect start bit
goto midi_in
;
; Found start...could be as much as 4 cycles late if start happened
; just after the btfsc. Not a problem. Wait 32 cycles (16us) to get
; to center of start bit.
;
get_midi_message
movlw 7 ;(1) Started -- get to center of bit-time
movwf z ;(1)
m_in0 nop ;(6 x 4) + 3 = 27 cycles
decfsz z,F
goto m_in0 ;/
movlw 8 ;(1) Eight bits of data to get
movwf l ;(1)
clrf recv ;(1) [32 cycles to here]
;
; Delay one bit-width (64 cycles) to get to center of LSB.
; Gather the bits into recv.
;
m_in1 movlw 14 ;(1)
movwf z ;(1)
mdy_0 nop ;\
decfsz z,F ;((13 x 4) + 3) = 55
goto mdy_0 ;/
bcf STATUS,C ;(1) Default 0
rrf recv,F ;(1) Put 0 in MSB of recv, then
btfsc PORTB,5 ;(1/2) sample the serial data and
bsf recv,7 ;(1) set the bit if sample was=1
decfsz l,F ;((7 x 3) + 2) = 23, Do all bits
goto m_in1 ;/ [total cycles = 64, MSB = 63]
bsf flags,4 ;Say byte is good (test below). [MSB=64]
;
; Time to first third of stop bit. (55 cycles)
;
movlw 18 ;(1)
movwf z ;(1)
mdy_1 decfsz z,F ;((17 x 3) + 2) = 53
goto mdy_1 ;/
btfss PORTB,5 ;Stop bit showed up?
bcf flags,4 ;If not, indicate framing error
retlw 0