PDA

View Full Version : My first DT_INT code has a problem.....



E Kizer
- 11th April 2009, 15:37
This is my first attempt at using a USART with DT's INT's and I ran into a problem that I can't figure out. I was using my LCD for debugging and everything was working fine until I remmed out the LCDOUT line. Now it turns the received characters into garbage. If I put back the LCDOUT line the received characters are fine.
Can anyone explain to me why it does this, I must be doing something fundamentally wrong.
Thanks,
Ed Kizer



<font color="#000080"><i>'DEVICE 16F882

</i></font><font color="#008000">@ __config _CONFIG1, _HS_OSC &amp; _WDT_ON &amp; _MCLRE_ON &amp; _LVP_OFF &amp; _CP_OFF

</font><b>DEFINE </b>OSC 16
<b>DEFINE </b>LCD_EREG PORTC <font color="#000080"><i>' SET ENABLE PORT
</i></font><b>DEFINE </b>LCD_EBIT 3 <font color="#000080"><i>' SET ENABLE BIT

</i></font>OPTION_REG.7=0 <font color="#000080"><i>' TURN ON PORTB WEAK PULL-UPS
</i></font>ANSELH=0 <font color="#000080"><i>' DISABLE PORT B ANALOG, ENABLE DIGITAL I/O
</i></font>CM1CON0=0 <font color="#000080"><i>' TURN OFF COMPARATORS PORTA(1)
</i></font>CM2CON0=0 <font color="#000080"><i>' TURN OFF COMPARTORS PORTB(2)
</i></font>VRCON=0 <font color="#000080"><i>' A/D Voltage reference disabled
</i></font>ANSEL=0 <font color="#000080"><i>' TURN OFF PORT A A/D, ENABLE DIGITAL I/O
</i></font>TRISA=0 <font color="#000080"><i>'set portA to OUTPUTS
</i></font>TRISB=$FF <font color="#000080"><i>' set port b TO ALL INPUTS %11111111
</i></font>TRISC=$80 <font color="#000080"><i>' SET PORT C BIT 7 TO INPUT %10000000


'***********INSTANT INTERRUPT ROUTINES BY DARRYL TAYLOR************
'******************* USE MPASM TO ASSEMBLE **************



</i></font><b>INCLUDE </b><font color="#FF0000">&quot;DT_INTS-14.bas&quot; </font><font color="#000080"><i>; Base Interrupt System
</i></font><b>INCLUDE </b><font color="#FF0000">&quot;ReEnterPBP.bas&quot; </font><font color="#000080"><i>; Include if using PBP interrupts

</i></font><b>ASM
</b><font color="#008000">INT_LIST macro </font><font color="#000080"><i>; IntSource, Label, Type, ResetFlag?
</i></font><font color="#008000">INT_Handler RX_INT, _ISR, PBP, yes
endm
INT_CREATE </font><font color="#000080"><i>; Creates the interrupt processor
</i></font><b>ENDASM

</b><font color="#008000">@ INT_ENABLE RX_INT </font><font color="#000080"><i>; enable EUSART (INT) interrupts


'************************************************* **********************
'*************** DT's BAUD SETUP ROUTINE *************************

</i></font><b>ASM
</b><font color="#008000">USART_Init macro Baud
CHK?RP TXSTA
clrf TXSTA
_SPBRG = (OSC * 1000000) / 16 / Baud - 1 </font><font color="#000080"><i>; calc SPBRG @ High baud rate
</i></font><font color="#008000">if _SPBRG &gt; 255 </font><font color="#000080"><i>; if SPBRG is too high
</i></font><font color="#008000">_SPBRG = (OSC * 1000000) / 64 / Baud - 1 </font><font color="#000080"><i>; calc for Low baud rate
</i></font><font color="#008000">bcf TXSTA, BRGH </font><font color="#000080"><i>; Set BRGH to Low Speed
</i></font><font color="#008000">if _SPBRG &gt; 255
_SPBRG = 255
endif
else
bsf TXSTA, BRGH </font><font color="#000080"><i>; Set BRGH to High Speed
</i></font><font color="#008000">endif
bsf TXSTA, TXEN </font><font color="#000080"><i>; Set Transmit Enable bit
</i></font><font color="#008000">movlw _SPBRG
CHK?RP SPBRG
movwf SPBRG </font><font color="#000080"><i>; load the calulated SPBRG
</i></font><font color="#008000">movlw B'10010000' </font><font color="#000080"><i>; enable USART
</i></font><font color="#008000">CHK?RP RCSTA
movwf RCSTA
endm
</font><b>ENDASM
</b><font color="#000080"><i>'************************************************* ***************************


</i></font>OERR <b>VAR </b>RCSTA.1 <font color="#000080"><i>' Alias USART over-run bit
</i></font>CREN <b>VAR </b>RCSTA.4 <font color="#000080"><i>' Alias USART continuous receive enable bit
</i></font>RCIF <b>VAR </b>PIR1.5 <font color="#000080"><i>' Alias USART received character interrupt flag bit

</i></font>BytesIn <b>VAR BYTE</b>[16]
ByteCnt <b>VAR BYTE
</b>Got <b>VAR BYTE
</b>Problem <b>VAR BYTE
</b>Prob_Cnt <b>VAR BYTE
</b>Stx <b>VAR BYTE
</b>String <b>VAR BYTE
</b>SW1 <b>VAR </b>PORTB.0
SW2 <b>VAR </b>PORTB.1
Reps <b>VAR WORD


</b><font color="#000080"><i>'************************************************* **********************

</i></font>MAIN:

<b>PAUSE </b>1000 <font color="#000080"><i>' LCD start-up delay
</i></font><b>CLEAR

LCDOUT </b>$FE,1
<b>LCDOUT </b>$FE,$80,<font color="#FF0000">&quot; PLEASE &quot;
</font><b>LCDOUT </b>$FE,$C0,<font color="#FF0000">&quot; WAIT &quot;
</font><b>PAUSE </b>500

<b>LCDOUT </b>$FE,$80,<font color="#FF0000">&quot; &quot; </font><font color="#000080"><i>' CLEARS SCREEN BETTER THAN $FE,1
</i></font><b>LCDOUT </b>$FE,$C0,<font color="#FF0000">&quot; &quot;
</font>Problem=0

<b>IF </b>!OERR <b>THEN </b>LOOP <font color="#000080"><i>' If OERR=0 no over-run condition's present
</i></font>CREN = 0 <font color="#000080"><i>' Disable receive
</i></font>CREN = 1 <font color="#000080"><i>' Re-enable &amp; clear OERR &quot;over-run&quot; flag
</i></font><b>GOTO </b>MAIN <font color="#000080"><i>' start over, including the count

</i></font>LOOP:


<font color="#008000">@ USART_Init 9600 ' SET BAUDRATE
</font>Stx=($01) <font color="#000080"><i>' SET START OF TEXT CHARACTOR
</i></font>String=8 <font color="#000080"><i>' SET LENGTH OF SERIAL STRING
</i></font>Prob_Cnt=56 <font color="#000080"><i>' SET # OF BYTES TO TRIGGER &quot;SETUP PROBLEM&quot; DISPLAY




</i></font><b>LCDOUT </b>$FE,$80,32 <font color="#FF0000"><i>' ^^^^^^BREAKS IF REMMED OUT (GARBAGE DATA)^^^^^^^^




</i></font><b>IF </b>Got=1 <b>THEN
GOSUB </b>Show_Routine_One
<b>ENDIF

IF </b>Problem=&gt;Prob_Cnt <b>THEN
GOSUB </b>Prob_Disp
<b>ENDIF


GOTO </b>LOOP

Show_Routine_One: <font color="#000080"><i>' DISPLAY DATA
</i></font><b>LCDOUT </b>$FE,$80,32,32,BytesIn(1),BytesIn(2),BytesIn(4),Byt esIn(5),BytesIn(7),32
<b>LCDOUT </b>$FE,$C0,<font color="#FF0000">&quot; &quot; </font><font color="#000080"><i>' BLANK SECOND ROW ON LCD
</i></font>ByteCnt=0
Got=0
Problem=0
<b>RETURN

</b>Prob_Disp:
<b>LCDOUT </b>$FE,$80,32,<font color="#FF0000">&quot;SETUP&quot;</font>,32,32
<b>LCDOUT </b>$FE,$C0,<font color="#FF0000">&quot;PROBLEM&quot;
</font>Problem=0
<b>RETURN

</b>No_Data:
<b>LCDOUT </b>$FE,$80,32,32,32,<font color="#FF0000">&quot;NO&quot;</font>,32,32,32
<b>LCDOUT </b>$FE,$C0,32,32,<font color="#FF0000">&quot;DATA&quot;</font>,32
Reps=0
<b>RETURN

</b><font color="#000080"><i>'======================== INTERRUPT SERVICE ROUTINE===========================
</i></font>ISR:


<b>IF </b>RCIF <b>THEN </b><font color="#000080"><i>' If RCIF=1 there's a new character in RCREG
</i></font>BytesIn[ByteCnt]=RCREG <font color="#000080"><i>' Yes. Then store it in array
</i></font>ByteCnt=ByteCnt+1 <font color="#000080"><i>' Increment array index pointer

</i></font><b>ENDIF
IF </b>BytesIn&lt;&gt;Stx <b>THEN
</b>ByteCnt=0
Problem=Problem+1
<b>ENDIF

IF </b>ByteCnt=&gt;String <b>THEN
</b>Got=1 <font color="#000080"><i>'INDICATE THAT THIS IS A NEW PACKET

</i></font><b>ENDIF


</b><font color="#000080"><i>'************************************************* **************************
</i></font><font color="#008000">@ INT_RETURN
</font><font color="#000080"><i>'************************************************* *************************




</i></font>

mister_e
- 11th April 2009, 16:48
I would think it's because you set the baudrate within this loop, and it need some time for the USART to initialize/settle. Usually, you want to set the USART at the top and only once. So my guess is to move the @ USART_Init 9600 line before the main.

You could still place the following lines at the top of your code (with all other register init), and forget @ USART_Init

RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $20 ' Enable transmit, BRGH = 0
SPBRG = 25 ' 9600 Baud @ 16MHz, 0.16%

E Kizer
- 13th April 2009, 13:37
Thanks Steve,

I'll look into that.