Hello,
I am having issue with DEBUG which output garbage when used in a program, but using the same hardware and same declaration for debug_mode, baud... works OK with a tiny test program and I cannot figure out what I am doing wrong. I am quite beginner as well, so it might be something very simple I cannot see...

The code is used to monitor the rotation of a windlass and measures the chain out on a boat. I then want to send back the data to a display in the boat, but as the display only accepts the GPS type signal, I use the SPEED channel to pass the data to the display.
I have also HSEROUT included to check that the program does what it is supposed to do and all the outputs from HSEROUT are OK but not the DEBUG output.

The code which is not working is just below. I am using interrupt from Darrel and the include "Write_Int.bas" is just the time_elapsed from Darrel from which I have removed what I don't need. I can post it if needed.

The one working is at the bottom of this post.

I could not figure out how to enter the code in the box as I see in other post, I apologize for the messy post.

thanks,
Olivier


'************************************************* ***************
'* Name : chaine *
'* Author : Olivier Desport *
' Compiler : PICBASIC PRO Compiler 2.6
' Assembler : MPASM
' Target PIC : 40-pin 16F877A
' Hardware : olimex 20 MHz
' Oscillator : 20MHz external crystal * *
'* Date : 11/16/2015 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************

define OSC 20
DEFINE LOADER_USED 1 ' Using boot-Loader
Define HSER_CLROERR 1 'to automatically clear any buffer overflow of serial input
Define HSER_BAUD 4800 'Used for debugging program, to be removed later
define DEBUG_REG PORTD 'set D.3 to NMEA standard for DEBUG
define DEBUG_BIT 3
define DEBUG_BAUD 4800
DEFINE DEBUG_MODE 1

DEFINE WRITE_INT 1 'to halt interrupt during WRITE

ADCON1 = %10010110 'Set PortA to digital
TRISA = %11111110

;---------------------------------------------------------------------------
;wsave VAR BYTE $20 SYSTEM ' location for W if in bank0
wsave VAR BYTE $70 SYSTEM ' alternate save location for W
' if using $70, comment wsave1-3

' --- IF any of these three lines cause an error ?? ------------------------
' Comment them out to fix the problem ----
' -- Which variables are needed, depends on the Chip you are using --
wsave1 VAR BYTE $A0 SYSTEM ' location for W if in bank1
wsave2 VAR BYTE $120 SYSTEM ' location for W if in bank2
wsave3 VAR BYTE $1A0 SYSTEM ' location for W if in bank3
' --------------------------------------------------------------------------

LED var PORTA.0 ' Alias PORTA.0 to LED
iturn var word ' rotation of windlass
'to avoid negative number, 100 is added (100 = anchor on boat)
x var word 'used for computation
lchaine var word ' longueur de chaine sortie en dm
calcoef var word 'calibration coefficient to go from iturn to chaine length
bflag var bit ' flag turns ON when interrupt received
onflag var bit ' 1 when windlass is ON
wflag var bit 'set to 0 once iturn is written to RAM
TimeFlag var bit 'set to 1 when counter reached time
T0Count Var WORD
gUP var PORTA.3 'connected to UP windlass command (1 when ON)
gDOWN var PORTA.4 'connected to Down windlass command (1 when ON)
sCal var PORTE.2 'switch to reset counter to zero (iturn to 100) (0 when depressed)

hpwm 1,250,2500
pause 1000
hpwm 1,15,2500


'initialization
calcoef=288
wflag=0
timeflag=0


' read eeprom to load chaine position
bflag=0
write 0, bflag ' this WRITE comand is only to avoid a bug in the compiler
read 10, word iturn

bflag = 0
hserout ["hello",10,"read=",dec iturn,10]

INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
INCLUDE "Write_INT.bas" ' Elapsed Timer Routines

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler INT_INT, _Barbotin, PBP, yes
INT_Handler TMR0_INT, _ToggleLED2, PBP, yes
INT_Handler TMR1_INT, _ClockCount, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor

INT_ENABLE INT_INT ; enable external (INT) interrupts
INT_ENABLE TMR0_INT ; enable Timer 0 interrupts
INT_ENABLE TMR1_INT
ENDASM
OPTION_REG = OPTION_REG & $80 | 1 ; Set TMR0 Prescaler to 256, leave RBPU alone
Gosub ResetTime ' Reset Time to 0d-00:00:00.00
Gosub StartTimer ' Start the Elapsed Timer


Main:
if sCal=0 then iturn=100 'reset chain counter when pressing ZERO switch

if wflag and timeflag then 'windlass has stopped for over 3 seconds
'has changed position previously and value has not been written yet,
'so value needs to be written
write 10, word iturn 'saving chaine position
hserout ["writing",10]
wflag=0 'indicates that RAM has been written
timeflag=0 'reset time flag
endif

if bflag = 1 then 'interrupt received
bflag = 0 'reset sensor/interrupt flag
T0Count=0 'reset timer count
wflag=1 'flag to indicates new value needs to be written
onflag = 1 'Windlass motion detected
if gUP = 1 then
iturn = iturn -1
else
iturn = iturn + 1
endif
if iturn>=100 then
x=iturn-100
lchaine=x*calcoef 'en mm
lchaine=lchaine/100 'en dm
else
lchaine=0
endif
hserout ["iturn=",dec iturn," chaine=",dec lchaine/10,".",dec1 lchaine,10]
endif

if SecondsChanged = 1 then
hserout ["$GPVTG,0.0,T,0.0,M,",dec lchaine/10,".",dec1 lchaine,",N,0.0,K",10]
debug "$GPVTG,0.0,T,0.0,M,",dec lchaine/10,".",dec1 lchaine,",N,0.0,K",10
SecondsChanged = 0
endif

GOTO Main

'---[INT - interrupt handler]---------------------------------------------------
Barbotin:
bflag = 1
@ INT_RETURN

'---[TMR0 - interrupt handler]-------------------------------(Blinky Light)------
ToggleLED2:
T0Count = T0Count + 1
if T0Count = 15000 then
T0Count = 0
timeflag=1
endif
@ INT_RETURN

' ################################################## #####################

and using the same hardware, if I use this code, the DEBUG data makes sense. I cannot figure out what is wrong with the first code.

define OSC 20
DEFINE LOADER_USED 1 ' Using boot-Loader
Define HSER_CLROERR 1 'to automatically clear any buffer overflow of serial input
Define HSER_BAUD 4800 'Used for debugging program, to be removed later
define DEBUG_REG PORTD 'set D.3 to NMEA standard for DEBUG
define DEBUG_BIT 3
define DEBUG_BAUD 4800
DEFINE DEBUG_MODE 1

i var word
debug "Hello"

i=0

mainloop:
i=i+1
pause 1000
debug "i=",dec i,10
goto mainloop