Still HSEROUT woes


Closed Thread
Results 1 to 35 of 35

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107

    Default Still HSEROUT woes

    I'm using an 18F part and have a 1mSec timer interrupt using TMR0.

    I wanted to test everything by building a simple seconds counter. Inside my ISR I would count to 1000, and then set a flag. My PBP program would sit in a tight loop and test for that flag, and when it detected it was set, it would increment a counter and display the running count via the HSEROUT command. The trouble is the interrupt would crash HSEROUT. The first few characters get sent out, then a bunch of garbage. This happens every second, so I have proof that the interrupt is still running.

    I thought my trouble was because I was trashing a system variable inside my assembly-language ISR, so I went back to Tim Box's routine where virturally every PBP variable is saved and restored, and I rewrote the ISR in PBP.

    I STILL have the same problem. Is there some software timing loop inside HSEROUT that is getting screwed up?

    The program is below:

    '************************************************* ***************
    '* Name : INT_CTRL.BAS *
    '* Author : Tim Box *
    '* Notice : Copyright (c) 2002 TJBSYSTEMS *
    '* : All Rights Reserved *
    '* Date : 28-Sep-02 *
    '* Version : 1.0 *
    '* Notes : *
    '* : Modified by C. Linquist for 18F parts 5/1/04 *
    '************************************************* ***************

    DEFINE OSC 20 ' Set for 20Mhz operation
    DEFINE NO_CLRWDT 1
    DEFINE _18F8720 1
    DEFINE HSER_RCSTA 90H
    DEFINE HSER_TXSTA 20H
    DEFINE HSER_BAUD 9600
    DEFINE HSER_CLROERR 1
    DEFINE CCP1_REG PORTC
    DEFINE CCP1_BIT 2
    DEFINE LOADER_USED 1 ' Bootloader
    Define USE_LFSR 1


    TRISA = %11111111
    TRISB = %11111111
    TRISC = %10010001
    TRISD = %00111000
    TRISE = %00000000
    TRISF = %01111111
    TRISG = %11111100
    TRISH = %00000000
    TRISJ = %10010000




    ADCON2 = %10000010 ' Right justify 10 bit output, Clock = Fosc/32

    ADCON1 = %00010011 ' Set Vref on RA3, 12 chan A/D (AN0-AN11)

    ADCON0 = %00000001 ' Set A/D to ON, chan 0

    PSPCON = %00000000 ' No PSP
    MEMCON = %10000000 ' No External Memory
    CMCON = %00000111 ' No Comparator used, turn it off

    T0CON = %10001000
    INTCON = %11100000 ' Timer0 and Peripheral interrupts enabled
    PIE1 = %00100000 ' USART 1 receive buffer int enable

    CCPR1L = %00010000
    CCPR1H = %00000000 ' Initialize to 0, PWM register
    PR2 = $1F ' PWM register,158 Khz@20Mhz, 7 bits
    T2CON = %00000100 ' Prescale 1 - Needed for PWM
    CCP1CON= %00001100 ' Set up for PWM



    '************************************************* ***************************************
    '* *
    '* Declare variables *
    '* *
    '************************************************* ***************************************




    wsave VAR BYTE $20 SYSTEM ' Save location for the W register if in bank0

    ssave VAR BYTE Bank0 SYSTEM ' Save location for the STATUS register
    psave VAR BYTE Bank0 SYSTEM ' Save location for the PCLATH register
    fsave VAR BYTE Bank0 SYSTEM ' Save location for the FSR register

    R0_2 var WORD BANK0 ' PBP VARS TO SAVE
    R1_2 VAR WORD BANK0
    R2_2 VAR WORD BANK0
    R3_2 VAR WORD BANK0
    R4_2 VAR WORD BANK0
    R5_2 VAR WORD BANK0
    R6_2 VAR WORD BANK0
    R7_2 VAR WORD BANK0
    R8_2 VAR WORD BANK0
    FLAGS_2 VAR BYTE BANK0
    GOP_2 VAR BYTE BANK0
    RM1_2 VAR BYTE BANK0
    RM2_2 VAR BYTE BANK0
    RR1_2 VAR BYTE BANK0
    RR2_2 VAR BYTE BANK0

    TIMER VAR WORd
    X VAR WORD
    Y VAR BYTE
    SecondsCounter VAR BYTE
    TimerFlag VAR BIT
    DEFINE INTHAND INT_CODE 'Tell PBP Where your code starts on an interrupt

    T0CON = %10001000
    INTCON = %11100000 ' Timer0 and Peripheral interrupts enabled
    PIE1 = %00100000 ' Usart1 int enabled


    TimerFlag = 0
    Timer = 0
    X = 0
    SecondsCounter = 0

    GOTO OVER_INT_CODE



    asm


    INT_CODE
    MOVF R0,W
    MOVWF _R0_2
    MOVF R0 + 1,W
    MOVWF _R0_2 + 1

    MOVF R1,W
    MOVWF _R1_2
    MOVF R1 + 1,W
    MOVWF _R1_2 + 1

    MOVF R2,W
    MOVWF _R2_2
    MOVF R2 + 1,W
    MOVWF _R2_2 + 1

    MOVF R3,W
    MOVWF _R3_2
    MOVF R3 + 1,W
    MOVWF _R3_2 + 1

    MOVF R4,W
    MOVWF _R4_2
    MOVF R4 + 1,W
    MOVWF _R4_2 + 1

    MOVF R5,W
    MOVWF _R5_2
    MOVF R5 + 1,W
    MOVWF _R5_2 + 1

    MOVF R6,W
    MOVWF _R6_2
    MOVF R6 + 1,W
    MOVWF _R6_2 + 1

    MOVF R7,W
    MOVWF _R7_2
    MOVF R7 + 1 ,W
    MOVWF _R7_2 + 1

    MOVF R8,W
    MOVWF _R8_2
    MOVF R8 + 1 ,W
    MOVWF _R8_2 + 1


    IFDEF T2
    MOVF T2,W
    MOVWF _T2_2
    ENDIF
    IFDEF FLAGS
    MOVF FLAGS,W
    MOVWF _FLAGS_2
    ENDIF
    IFDEF GOP
    MOVF GOP,W
    MOVWF _GOP_2
    ENDIF
    IFDEF RM1
    MOVF RM1,W
    MOVWF _RM1_2
    ENDIF
    IFDEF RM2
    MOVF RM2,W
    MOVWF _RM2_2
    ENDIF
    IFDEF RR1
    MOVF RR1,W
    MOVWF _RR1_2
    ENDIF
    IFDEF RR2
    MOVF RR2,W
    MOVWF _RR2_2
    ENDIF


    endasm
    ;%%%%%%%%%%%%%%%%%%%%%%%%% Interrupt handler written in PBP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    IF INTCON.2 = 1 THEN
    TMR0H = $EC
    TMR0L = $77
    Timer = Timer + 1
    IF Timer >= 1000 THEN
    TimerFlag = 1
    Timer = 0
    ENDIF
    INTCON.2 = 0
    ENDIF
    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%``

    ASM

    MOVF _R0_2,W
    MOVWF R0
    MOVF _R0_2 + 1,W
    MOVWF R0 + 1

    MOVF _R1_2,W
    MOVWF R1
    MOVF _R1_2 + 1,W
    MOVWF R1 + 1

    MOVF _R2_2,W
    MOVWF R2
    MOVF _R2_2 + 1,W
    MOVWF R2 + 1

    MOVF _R3_2,W
    MOVWF R3
    MOVF _R3_2 + 1,W
    MOVWF R3 + 1

    MOVF _R4_2,W
    MOVWF R4
    MOVF _R4_2 + 1,W
    MOVWF R4 + 1

    MOVF _R5_2,W
    MOVWF R5
    MOVF _R5_2 + 1,W
    MOVWF R5 + 1

    MOVF _R6_2,W
    MOVWF R6
    MOVF _R6_2 + 1,W
    MOVWF R6 + 1

    MOVF _R7_2,W
    MOVWF R7
    MOVF _R7_2 + 1,W
    MOVWF R7 + 1

    MOVF _R8_2,W
    MOVWF R8
    MOVF _R8_2 + 1,W
    MOVWF R8 + 1

    IFDEF FLAGS
    MOVF _FLAGS_2,W
    MOVWF FLAGS
    ENDIF
    IFDEF FLAGS
    MOVF _GOP_2,W
    MOVWF FLAGS
    ENDIF
    IFDEF RM1
    MOVF _RM1_2,W
    MOVWF RM1
    ENDIF
    IFDEF RM2
    MOVF _RM2_2,W
    MOVWF RM2
    ENDIF
    IFDEF RR1
    MOVF _RR1_2,W
    MOVWF RR1
    ENDIF
    IFDEF RR2
    MOVF _RR2_2,W
    MOVWF RR2
    ENDIF

    IFDEF T2
    MOVF _T2_2,W
    MOVWF T2
    ENDIF

    Movf psave,w ; Restore the PCLATH reg
    Movwf PCLATH
    swapf ssave,w ; Restore the STATUS reg
    movwf STATUS
    swapf wsave,f
    swapf wsave,w ; Restore W reg
    Retfie ; Exit the interrupt routine

    ENDASM
    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


    OVER_INT_CODE:

    Topp:
    IF TimerFlag = 1 THEN
    SecondsCounter = SecondsCounter + 1
    HSEROUT ["Seconds = ",#SecondsCounter,13,10]
    TimerFlag = 0
    INTCON.7 = 1 ; Turn global ints back on
    ENDIF
    Goto Topp
    Charles Linquist

  2. #2
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Charles,
    It's late, so I have only taken the briefest look, but a couple of things jumped out.

    1) You never actually saved wsave, ssave, psave and fsave at the beginning of your ISR.

    2) You defined wsave, ssave, psave and fsave(and all the rest) as if you are using a PIC16. Change $20 and bank0 to banka for all of the context variables.

    3) You should use the movff command for all the context saving. It does not effect the STATUS reg. (see the datasheet example under interrupts)

    4) If you need to save a FSR, you should be saving FSRXH and FSRXL. (It doesn't at first glance look like you need to since I didn't see any commands/reference that use that register) Read the datasheet in the Memory Organization section about Data Addressing Modes (particularly Indirect Addressing)

    Start with these items. If I have some time tomorrow I'll take a closer look.
    Good night,
    Steve

  3. #3
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    I forgot to modify Tim Box's routine.

    Here is the modified version. I went ahead and used movff instructions for ALL the registers, even the PBP ones.

    The code below has some strange behavior: It prints
    "secsecsecsecsecsec..." on the screen at one second intervals which proves that the interrupt is still running, but HSEROUT apparently gets trashed when an interrupt occurs.

    Also, I have added a USART interrupt to the mix. Hitting any key kills the routine.

    I'm stuck!

    Here is the updated code
    ---------------------------------------------------------------------


    DEFINE OSC 20 ' Set for 20Mhz operation
    DEFINE NO_CLRWDT 1
    DEFINE _18F8720 1
    DEFINE HSER_RCSTA 90H
    DEFINE HSER_TXSTA 20H
    DEFINE HSER_BAUD 9600
    DEFINE HSER_CLROERR 1
    DEFINE CCP1_REG PORTC
    DEFINE CCP1_BIT 2
    DEFINE LOADER_USED 1 ' Bootloader
    Define USE_LFSR 1
    DEFINE ADC_BITS 10
    DEFINE ADC_SAMPLEUS 50


    TRISA = %11111111 'AUX1,VM12,V12,V3,V5
    TRISB = %11111111 'PDAT,PCLK,TACH6,TACH5,TACH4,TACH3,TACH2,TACH1
    TRISC = %10010001 'RX1,TX1,SDO,SDA,LEDTG,PWMOUT,LEDFG,TACH7
    TRISD = %00111000 'LED5R,LED3R,TACH8,TACH9,CTS1,RTS1,LED12G,LEDM12G
    TRISE = %00000000 'LEDV2R,LEDV2G,LED3G,LED5G,LEDFR,LEDTR,LEDM12R,LED 12R
    TRISF = %01111111 'OPTO1,FlowSig,POT2,POT1,Temp4,Temp3,Temp2,Temp1
    TRISG = %11111100 'NC,NC,NC,XPGP3,XPCP1,RX2,TX2,OPTO2
    TRISH = %00000000 'PINT2,PINT1,LCDRS,LCDRW,LCDDB7,LCDDB6,LCDDB5,LCDD B4
    TRISJ = %10010000 'SWitchIN,LEDout,LEDout,XCTSin,XPReset,LCDe,RLY,PI NT3




    ADCON2 = %10000010 ' Right justify 10 bit output, Clock = Fosc/32

    ADCON1 = %00010011 ' Set Vref on RA3, 12 chan A/D (AN0-AN11)

    ADCON0 = %00000001 ' Set A/D to ON, chan 0

    PSPCON = %00000000 ' No PSP
    MEMCON = %10000000 ' No External Memory
    CMCON = %00000111 ' No Comparator used, turn it off

    T0CON = %10001000
    INTCON = %11100000 ' Timer0 and Peripheral interrupts enabled
    PIE1 = %00100000 ' USART 1 receive buffer int enable

    CCPR1L = %00010000
    CCPR1H = %00000000 ' Initialize to 0, PWM register
    PR2 = $1F ' PWM register,158 Khz@20Mhz, 7 bits
    T2CON = %00000100 ' Prescale 1 - Needed for PWM
    CCP1CON= %00001100 ' Set up for PWM



    '************************************************* ***************************************
    '* *
    '* Declare variables *
    '* *
    '************************************************* ***************************************




    wsave VAR BYTE BANKA SYSTEM ' Save location for the W register if in BANKA

    ssave VAR BYTE BANKA SYSTEM ' Save location for the STATUS register


    R0_2 var WORD BANKA ' PBP VARS TO SAVE
    R1_2 VAR WORD BANKA
    R2_2 VAR WORD BANKA
    R3_2 VAR WORD BANKA
    R4_2 VAR WORD BANKA
    R5_2 VAR WORD BANKA
    R6_2 VAR WORD BANKA
    R7_2 VAR WORD BANKA
    R8_2 VAR WORD BANKA
    FLAGS_2 VAR BYTE BANKA
    GOP_2 VAR BYTE BANKA
    RM1_2 VAR BYTE BANKA
    RM2_2 VAR BYTE BANKA
    RR1_2 VAR BYTE BANKA
    RR2_2 VAR BYTE BANKA

    TIMER VAR WORd
    X VAR WORD
    Y VAR BYTE

    SecondsCounter VAR BYTE
    TimerFlag VAR BIT
    KeyHitFlag VAR BIT

    DEFINE INTHAND INT_CODE 'Tell PBP Where your code starts on an interrupt

    T0CON = %10001000
    INTCON = %11100000 ' Timer0 and Peripheral interrupts enabled
    PIE1 = %00100000 ' Usart1 int enabled


    TimerFlag = 0
    Timer = 0
    X = 0
    SecondsCounter = 0
    KeyHitFlag = 0

    GOTO OVER_INT_CODE

    asm
    MOVFF WREG,wsave ; copy W to wsave register
    MOVFF STATUS,ssave
    CLRF STATUS ; change to bank 0 regardless of current bank

    INT_CODE

    MOVFF R0,_R0_2
    MOVFF R0+1,_R0_2+1
    MOVFF R1,_R1_2
    MOVFF R1+1,_R1_2+1
    MOVFF R2,_R2_2
    MOVFF R2+1,_R2_2+1
    MOVFF R3,_R3_2
    MOVFF R3+1,_R3_2+1
    MOVFF R4,_R4_2
    MOVFF R4+1,_R4_2+1
    MOVFF R5,_R5_2
    MOVFF R5+1,_R5_2+1
    MOVFF R6,_R6_2
    MOVFF R6+1,_R6_2+1
    MOVFF R7,_R7_2
    MOVFF R7+1,_R7_2+1
    MOVFF R8,_R8_2
    MOVFF R8+1,_R8_2+1


    IFDEF T2
    MOVFF T2,_T2_2
    ENDIF
    IFDEF FLAGS
    MOVFF FLAGS,_FLAGS_2
    ENDIF
    IFDEF GOP
    MOVFF GOP,_GOP_2
    ENDIF
    IFDEF RM1
    MOVFF RM1,_RM1_2
    ENDIF
    IFDEF RM2
    MOVFF RM2,_RM2_2
    ENDIF
    IFDEF RR1
    MOVFF RR1,_RR1_2
    ENDIF
    IFDEF RR2
    MOVFF RR2,_RR2_2
    ENDIF


    endasm
    ;%%%%%%%%%%%%%%%%%%%%%%%%% Interrupt handler written in PBP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    IF INTCON.2 = 1 THEN
    TMR0H = $EC ; Reload TMR0 to 65535 - 5000
    TMR0L = $77
    Timer = Timer + 1
    IF Timer >= 1000 THEN
    TimerFlag = 1
    ENDIF

    ENDIF

    IF PIR1.5 = 1 THEN
    KeyHitFlag = 1
    ENDIF
    INTCON.2 = 0 ; Clear the timer flag
    ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%``

    ASM

    MOVFF _R0_2,R0
    MOVFF _R0_2 + 1,R0 + 1
    MOVFF _R1_2,R1
    MOVFF _R1_2 + 1,R1 + 1
    MOVFF _R2_2,R2
    MOVFF _R2_2 + 1,R2 + 1
    MOVFF _R3_2,R3
    MOVFF _R3_2 + 1,R3 + 1
    MOVFF _R4_2,R4
    MOVFF _R4_2 + 1,R4 + 1
    MOVFF _R5_2,R5
    MOVFF _R5_2 + 1,R5 + 1
    MOVFF _R6_2,R6
    MOVFF _R6_2 + 1,R6 + 1
    MOVFF _R7_2,R7
    MOVFF _R7_2 + 1,R7 + 1
    MOVFF _R8_2,R8
    MOVFF _R8_2 + 1,R8 + 1

    IFDEF FLAGS
    MOVFF _FLAGS_2,FLAGS
    ENDIF
    IFDEF FLAGS
    MOVFF _GOP_2,FLAGS
    ENDIF
    IFDEF RM1
    MOVFF _RM1_2,RM1
    ENDIF
    IFDEF RM2
    MOVFF _RM2_2,RM2
    ENDIF
    IFDEF RR1
    MOVFF _RR1_2,RR1
    ENDIF
    IFDEF RR2
    MOVFF _RR2_2,RR2
    ENDIF

    IFDEF T2
    MOVFF _T2_2,T2
    ENDIF


    MOVFF ssave,STATUS ; Restore the STATUS reg
    MOVFF wsave,W ; Restore W reg
    Retfie ; Exit the interrupt routine

    ENDASM

    OVER_INT_CODE:

    Topp:
    IF TimerFlag = 1 THEN
    SecondsCounter = SecondsCounter + 1
    HSEROUT ["Seconds = ",#SecondsCounter,13,10]
    TimerFlag = 0
    Timer = 0
    ENDIF
    IF KeyHitFlag = 1 THEN
    HSERIN [Y]
    HSEROUT[Y]
    KeyHitFlag = 0
    ENDIF

    INTCON.7 = 1 ; Turn global Ints back on
    Goto Topp
    Charles Linquist

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    i'm out of stock of this PIC but i'd notice few things here..
    Code:
    DEFINE _18F8720 1  ' <- What is the purpose of this one
    '
    '
    Define USE_LFSR 1 ' <- try to remove it
    i'll try to surf in the ASM... i'll try. Yuk! i agree sometimes we don't have choice
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Post

    mmm,
    Code:
    asm
    MOVFF WREG,wsave ; copy W to wsave register
    MOVFF STATUS,ssave
    CLRF STATUS ; change to bank 0 regardless of current bank
    
    INT_CODE
    
    MOVFF R0,_R0_2
    MOVFF R0+1,_R0_2+1
    should be..
    Code:
    asm
    INT_CODE
        MOVFF WREG,wsave ; copy W to wsave register
        MOVFF STATUS,ssave
        CLRF STATUS ; change to bank 0 regardless of current bank
    
    
    
        MOVFF R0,_R0_2
        MOVFF R0+1,_R0_2+1
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    Steve, you are absolutely correct. One of my edits went awry.

    Unfortunately, when I put the INT_CODE line in the correct place,
    nothing changes. Still no joy.


    And mister_e,

    The lines are part of my "standard" header file. I can't tell you why they
    are there. Probably some legacy issue. Anyway. I took them out.
    No change in behavior.
    Charles Linquist

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Nicely done! That show and confirm my assembler limitation and some basic register knowledge. I really have to work harder on that.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. I2C Slave with a PIC
    By ralfmayr in forum mel PIC BASIC Pro
    Replies: 129
    Last Post: - 21st August 2016, 17:44
  2. hserin and sms controller
    By xxxxxx in forum mel PIC BASIC Pro
    Replies: 25
    Last Post: - 10th February 2010, 15:49
  3. HSEROUT and Commas, What? Help Me Understand
    By altech6983 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 20th July 2009, 19:12
  4. Is HSEROUT corrupting bit 7 ?????
    By Robert Wells in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 23rd October 2008, 16:26
  5. Controlling an 240x128 LCD Touchpanel Display
    By Vincent in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 7th June 2006, 23:36

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts