News:

SMF for DIYStompboxes.com!

Main Menu

EUSART help!?!?!?!?!

Started by G. Hoffman, November 07, 2010, 01:56:41 AM

Previous topic - Next topic

G. Hoffman

Does anybody have any experience with the EUSART in PICs?  I'm using a PIC 16F887, and I'm trying to get a simple program to send MIDI data.  This one is just a test, but it should be sending a program change message every second or so, with some LED's turning on and off just so I can see if its working.  But its not.  Now, as far as I can tell I've got everything setup right, all the EUSART registers (SPBGH, SPBRG, etc.), but its not working, so I BELIEVE I've got the timing wrong, but I'm afraid I can't figure out where I got it wrong!!!

Can anyone help? 


Gabriel


;*******************************************************************************************************
; Name: MIDI Send
; Date: October 27, 2010
; Version: 1.00
; Programer: Gabriel Hoffman
; Oscilator Frequency; 20Mhz
;
;********************************************************************************************************
; Discription: MIDI Send module for PIC 16F887, using E/USART
;********************************************************************************************************
List p=16f887 ;type of microcontroller
#include <p16f887.inc> ;defines all standard Special Function registers and bits
COUNT EQU 20H ;
TEMP EQU 21H ;


errorlevel -302

GOTO START

;********************************************************************************************************



;********************************************************************************************************
;Config bits

__CONFIG _CONFIG1, _HS_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOR_OFF & _IESO_ON & _FCMEN_ON & _LVP_OFF & _DEBUG_OFF

__CONFIG _CONFIG2, _BOR40V & _WRT_OFF

;*********************************************************************************************************
;Subroutines

;Aprox. one second delay
DELAY1S MOVLW .10
MOVWF COUNT ;
CLRF TMR1H ;
CLRF TMR1L ;
DELAY1S1 BTFSS PIR1,TMR1IF ;check if TMR1 is zero
GOTO DELAY1S1
BCF PIR1,TMR1IF
DECFSZ COUNT
GOTO DELAY1S1
RETURN



;**************************************************************************************************
;SETUP
;**************************************************************************************************

START ORG 0X0020 ; Address of the first program
; instruction
; RESET vector)

BANKSEL PORTA ;Select bank 0

CLRF INTCON ;Turns off all inturupts

CLRF TMR1H
CLRF TMR1L
MOVLW B'00110101' ;
MOVWF T1CON ;Sets up TMR1

CLRF PORTA
CLRF PORTB
CLRF PORTC
CLRF PORTD
CLRF PORTE
MOVLW B'10000000'
MOVWF RCSTA

BANKSEL ANSEL ;Selects port 3

CLRF ANSEL ;No analog
CLRF ANSELH ;
BSF BAUDCTL,BRG16 ;

BANKSEL TRISA ;Selects bank 1

MOVLW B'11111000'
MOVWF OSCCON ;Clock is external osc
MOVLW B'00001000' ;
MOVWF OPTION_REG ;Enable port B pullups
;prescaler assigned to WDT
MOVLW B'10100000' ;
MOVWF TXSTA ;Enable EUSART
MOVWF SPBRGH ;
MOVLW .39 ;Set EUSART BAUD rate, 31.25 kBAUD
MOVWF SPBRG ;

MOVLW B'00111100' ;SET PORT A TO INPUT
MOVWF TRISA ;
MOVLW B'11111111' ;SET PORTB TO INPUT
MOVWF TRISB ;
MOVLW B'10111101' ;SET PORTC TO INPUTS
MOVWF TRISC ;
CLRF TRISD ;SET PORTD TO OUTPUTS
MOVLW B'00001111' ;
MOVWF TRISE ;SET PORTE TO INPUTS

BANKSEL PORTA ;Reture to BANK0
GOTO BEGIN

;***************************************************************************


BEGIN CLRF TEMP ;Turn on LEDs
LOOP MOVLW 0XFF ;
MOVWF PORTD ;

MIDISEND MOVLW 0XC0 ;MIDI status bit
MOVWF TXREG ;Program change
BSF      STATUS,RP0 ;bank1
BTFSC TXSTA,TRMT ;Check for end of transmission
DECF PCL ;
BCF       STATUS,RP0 ;bank0

MOVFW TEMP ;MIDI data bit
MOVWF TXREG ;Program change
INCF TEMP ;program +1
BSF      STATUS,RP0 ;bank1
BTFSC TXSTA,TRMT ;Check for end of transmission
DECF PCL ;
BCF       STATUS,RP0 ;bank0
CALL DELAY1S ;Delay for ~1 sec
MOVLW 00H ;turn off LEDs
MOVWF PORTD ;

MIDISEND2 MOVLW 0XC0 ;MIDI status bit
MOVWF TXREG ;
BSF      STATUS,RP0 ;bank1
BTFSC TXSTA,TRMT ;Check for end of transmission
DECF PCL ;
BCF       STATUS,RP0 ;bank0
MOVFW TEMP ;MIDI data bit
MOVWF TXREG ;
BSF      STATUS,RP0 ;bank1
BTFSC TXSTA,TRMT ;Check for end of transmission
DECF PCL ;
BCF       STATUS,RP0 ;bank0
INCF TEMP ;program +1

CALL DELAY1S ;~1 sec delay
MOVLW .127 ;
SUBLW TEMP ;Is it program 128?
BTFSC STATUS,Z ;
CLRF TEMP ;Then go back to program 1
GOTO LOOP ;




END


slotbot

#1
not sure if this is the problem but you start running timer1 when you dont need it. IE its running when you try to clear TMR1H and TMR1L. from the data sheet:

"For writes, it is recommended that the user simply stop
the timer and write the desired values. A write
contention may occur by writing to the timer registers,
while the register is incrementing. This may produce an
unpredictable value in the TMR1H:TTMR1L register
pair."

may or may not be the realissue.  its ugly anyways. imagine TMR1L is at 255 and about to roll over when you clear TMR1H. ;).


G. Hoffman

Quote from: slotbot on November 09, 2010, 02:57:05 AM
not sure if this is the problem but you start running timer1 when you dont need it. IE its running when you try to clear TMR1H and TMR1L. from the data sheet:

"For writes, it is recommended that the user simply stop
the timer and write the desired values. A write
contention may occur by writing to the timer registers,
while the register is incrementing. This may produce an
unpredictable value in the TMR1H:TTMR1L register
pair."

may or may not be the realissue.  its ugly anyways. imagine TMR1L is at 255 and about to roll over when you clear TMR1H. ;).



I don't think that's related to the EUSART issue, but its helpful.  Thanks!


Gabriel

slotbot

this is maybe another small thing but try setting the Baud rate and other options BEFORE turning on the module. are your LED's even turning on though? (IE do you make it to begin?)


G. Hoffman

Quote from: slotbot on November 09, 2010, 04:34:09 PM
this is maybe another small thing but try setting the Baud rate and other options BEFORE turning on the module. are your LED's even turning on though? (IE do you make it to begin?)




I'll try that.

Yes the LEDs are turning on.  It seems to be working fine, except that the MIDI module I'm plugged into doesn't change channels.  I know that IT is setup to accept program changes because I've tested it with another controller.  And a couple times it has gone to an error screen when I turned on this thing, so I assume the signal is getting through, but that it is just the wrong speed.

Thank you for your help, by the way.  If I ever come off as a bit terse, its because I'm frustrated by this thing!!!!


Gabriel

slotbot

Quote from: G. Hoffman on November 09, 2010, 09:24:23 PM

Thank you for your help, by the way.  If I ever come off as a bit terse, its because I'm frustrated by this thing!!!!


ya no problem, i didnt get any bad vibes. troubleshooting assembly = the best  ::)

im going on a vacation today otherwise i would actually try out the code to see whats happening. if your still stumped next week though...

good luck!