Variable changing mid-code in 16F883?


+ Reply to Thread
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,653


    Did you find this post helpful? Yes | No

    Default Re: Variable changing mid-code in 16F883?

    for tempvar=0 to 105
    address=tempvar<<3
    that looked wrong at first if address started a 1600 but it doesn't.
    perhaps address gets corrupted every time readout is run,
    spaghetti code like that is hard to unravel
    Last edited by richard; - 4th September 2024 at 05:59.
    Warning I'm not a teacher

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Variable changing mid-code in 16F883?

    I'm not saying this is related to your problem but here:
    Code:
    hold1:
      tempbyt=TMR1H
      if tempbyt<>128 then hold1
    This will "hold" until TMR1H is 128 and then countinue.
    But for how long is TMR1H 128? Are you sure it's not (still) 128 the next time you check?

    I would rewrite this.
    * For pacing the loop, poll the TMR1 interrupt flag.
    * Create a short subroutine for each "task". MeasureA, MeasureB, MeasureC, WriteEEPROM and so on.
    * Have a LoopCount variable that counts from 0 to whatever.
    * Use a Select Case block to execute whatever tasks are needed at each increment of the loop.

    Crude example:
    Code:
    TMR1IF VAR PIR1.0
    LoopCount VAR BYTE
    
    TMR1IF = 0
    T1CON = %00110001 ' Prescaler = 1:8,  Timer ON
    
    Main:
      IF TMR1IF=0 THEN Main    ' Wait for TMR1 roll over
    
      TMR1IF=0		   ' Reset interrupt flag.
    
      Select Case LoopCount
        Case 0
          GOSUB MeasureA
          GOSUB MeasureB
    
        Case 1
         GOSUB MeasureA
         GOSUB MeasureC
    
        Case 2
          GOSUB MeasureA
          GOSUB MeasureB
          GOSUB MeasureC
    
        Case 3
          GOSUB MeasureB
          GOSUB MeasureC
          GOSUB WriteEEPROM
      END SELECT
    
      LoopCount = LoopCount + 1
      IF LoopCount = 4 THEN LoopCount = 0
    
    Goto Main
    
    
    MeasureA:
      ' Do stuff
    RETURN
    
    MeasureB:
      ' Do Stuff
    RETURN
    
    MeasureC:
      ' Do Stuff
    RETURN
    
    WriteEEPROM:
      ' Do Stuff
    RETURN

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,653


    Did you find this post helpful? Yes | No

    Default Re: Variable changing mid-code in 16F883?

    i had a slightly different take but am amazed at the similarity

    Code:
    #CONFIG
        __config _CONFIG1, _HS_OSC & _WDT_OFF & _MCLRE_OFF & _LVP_OFF & _CP_OFF
        __CONFIG _CONFIG2, _WRT_OFF & _BOR21V
    #ENDCONFIG
    
    
    DEFINE INTHAND   ticker  
    
    
    
    
    
    
    ANSEL=0      'all analogue ports to digital
    ANSELH=0     'all analogue ports to digital
    
    
    
    
    TRISA=%00101011      '(0=output, 1=input)
    TRISB=%00100001      '(0=output, 1=input)
    TRISC=%00000000      '(0=output, 1=input) 
    TRISE=%00001000      '(0=output, 1=input) 
    
    
    OPTION_REG=%11000101
    PIN_OUT       var PORTC.3  'serial out pin
    LED           var portA.2
    wsave         var byte    $70 SYSTEM
    ssave         var byte    SYSTEM
    psave         var byte    SYSTEM
    taskflag      var byte
    taskswitch    var byte
    ro_counter    var byte
    ibit          var byte
    address       var word
    icount        var word
    
    
    define OSC 20
    
    
    intcon =$c0
    pir1.0=0
    pie1.0=1
    
    
    serout2 PIN_OUT,32,["THERMAL START",13,10] '19200bps
    T1CON=$31
    
    
    
    
    main:
        if taskflag then
              taskflag = 0
              taskswitch = taskswitch + 1
              if taskswitch == 4 then  taskswitch = 0
              select case taskswitch 
                  CASE 0
                    gosub task0
                    serout2 PIN_OUT,32,["A",dec ibit,"-",dec icount,"-",dec address,13,10] 
                    serout2 PIN_OUT,32,["B",dec ibit,"-",dec icount,"-",dec address,13,10]  
                  case  1
                    gosub task1
                    icount=icount+1
                    serout2 PIN_OUT,32,["C",dec ibit,"-",dec icount,"-",dec address,13,10] 
                    address=address+16
                    serout2 PIN_OUT,32,["D",dec ibit,"-",dec icount,"-",dec address,13,10] 
                  case  2 
                    gosub task2
                    ro_counter =ro_counter +1
                    if ro_counter == 16 then
                        serout2 PIN_OUT,32,["READOUT",13,10] 
                        ro_counter =0
                    endif
                  case  3
                    gosub task3 
                    
              end select    
        endif
    goto main
    
    
    task0:
    'do voltage stuff
    return
    
    
    task1:
    'do current stuff
    return
    
    
    task2:
    'do other stuff
    return
    
    
    task3:
         LED = ! led 
    return
    
    
    
    
    END
    
    
    asm    
    ticker      ;tmr1 isr
        banksel  _taskflag
        bsf _taskflag,0
        banksel PIR1
        bcf PIR1,0
        movf psave,W ; restore PCLATH
        movwf PCLATH
        swapf ssave,W ; restore STATUS
        movwf STATUS
        swapf wsave,F ; restore W (swap avoids changing STATUS)
        swapf wsave,W 
        RETFIE    
    endasm
    Warning I'm not a teacher

Similar Threads

  1. Change variable increment/decrement into mid-loop?
    By CuriousOne in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th January 2021, 08:38
  2. 16F883 Code Verify Problem
    By munromh in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th February 2009, 11:47
  3. changing code from 16F to 18F microprocessor
    By SCC_699 in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 14th May 2008, 13:27
  4. Changing Swordfish code to PBP
    By Ron Marcus in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 29th April 2008, 01:28
  5. Changing position of program code
    By aerostar in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 15th December 2006, 07:34

Members who have read this thread : 12

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