Indexing WORD variables goes wrong after index(0) - 18F1330


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891

    Default Indexing WORD variables goes wrong after index(0) - 18F1330

    Hi All,

    I'm trying to store between 10 and 100 word variables in an array. But more than ONE stored variable will mess up all other values and the first one will be messed up too.

    For my tests, I've made this short portion of code:
    Code:
    ' ====== FUSES =====================================================================================
    ' Fuses PIC18F1330 for MPASM
    @ __CONFIG _CONFIG1H, _OSC_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
    @ __CONFIG _CONFIG2L, _PWRT_OFF_2L & _BOR_OFF_2L & _BORV_0_2L
    @ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
    @ __CONFIG _CONFIG3L, _HPOL_HIGH_3L & _LPOL_HIGH_3L & _PWMPIN_OFF_3L
    @ __CONFIG _CONFIG3H, _FLTAMX_RA7_3H & _T1OSCMX_LOW_3H & _MCLRE_OFF_3H
    @ __CONFIG _CONFIG4L, _STVREN_ON_4L & _BBSIZ_BB256_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    @ __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
    @ __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
    @ __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
    @ __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
    @ __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
    @ __CONFIG _CONFIG7H, _EBTRB_OFF_7H    
    
    ' ====== REGISTERS =================================================================================
    '          76543210
    OSCCON  = %00000000 'OSCILLATOR CONTROL REGISTER
    OSCTUNE = %00000000 'OSCILLATOR TUNING REGISTER
    ADCON0  = %00000000 'A/D CONTROL REGISTER 0
    ADCON1  = %00000000 'A/D CONTROL REGISTER 1
    ADCON2  = %00000000 'A/D CONTROL REGISTER 2
    INTCON  = %10000000 'INTERRUPT CONTROL REGISTER, Enable global interrupts
    INTCON2 = %00000000 'INTERRUPT CONTROL REGISTER 2
    INTCON3 = %00000000 'INTERRUPT CONTROL REGISTER 3
    PORTA   = %00000000 'State High (1) or Low (0)
    TRISA   = %00000000 'Data Direction Control Register (Input/Output)
    PORTB   = %00000000 'State High (1) or Low (0) 
    TRISB   = %00010000 'Data Direction Control Register (Input/Output)
    
    T0CON   = %10000000 'TMR0=ON, prescaler=xxx
    
    ' ====== DEFINES ===================================================================================
    Define OSC 4
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    
    ' ====== VARIABLES =================================================================================
    INIT_VARIABLES:
    Ctr_A           var byte        ' Counter "A"
    IR_Time_Data    var WORD(100)   ' Incomming TIME length
    Serial_Data_Out var PORTB.1     ' Serial Data Out Pin
    IR_Input_Pin    var PORTB.4     ' IR Data In
    Bd_Rate         VAR byte        ' SEROUT baudrate setting
    
    ' ====== INITIALIZE ================================================================================
    Bd_Rate = 84 ' 6=38400 / 32=19200 / 84=9600)
    
    INIT_VALUES:
    For Ctr_A = 0 to 100
        IR_Time_Data(Ctr_A) = 0
    next
    
    Ctr_a = 0
    IR_State_Data = 0
    Serial_Data_Out = 1 ' set this port to avoid SEROUT garbage
    
    ' ====== PROGRAM ===================================================================================
    START:
        ' Learning process starts idling HIGH
        IF IR_Input_Pin THEN START ' stay here as long as Pin is HIGH
    
    LEARN1:
        TMR0H = 0 : TMR0L = 0    ' initialise TMR0 
        T0CON.7 = 1 ' start TMR0
        STATE_00: 
        IF !IR_Input_Pin THEN STATE_00 ' stay here as long as Pin is LOW ' TMR0 "counting" loop
        T0CON.7 = 0 ' stop TMR0
        IR_Time_Data.byte0(0) = TMR0L ' save to variable array
        IR_Time_Data.byte1(0) = TMR0H ' save to variable array
    
        TMR0H = 0 : TMR0L = 0
        T0CON.7 = 1
        STATE_11: 
        IF IR_Input_Pin THEN STATE_11 ' stay here as long as Pin is HIGH
        T0CON.7 = 0
        IR_Time_Data.byte0(1) = TMR0L
        IR_Time_Data.byte1(1) = TMR0H
    
        serout2 Serial_Data_Out, Bd_Rate,[DEC IR_Time_Data(0),13]
        serout2 Serial_Data_Out, Bd_Rate,[DEC IR_Time_Data(1),13]
    
        pause 200
    
    GOTO INIT_VALUES
    Actually, starting and stopping TMR0 doesn't seem to make any difference but as long as I can't get it to work, lets stay methodic.

    As long as I try to print IR_Time_Data(0), values are correct; but when I want to print more than index(0), values are messed up.

    I've found several threads about index WORD sized variables in arrays (especially this famous one bye Melanie), but, if the answer is there, I still don't get what I'm missing.

    Any help is very welcome
    Roger

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Indexing WORD variables goes wrong after index(0) - 18F1330

    I noticed you declared the array as WORD(100)

    and your for to loop as 0 to 100 (= 101)

    maybe 0 to 99 will make it work?

  3. #3
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Indexing WORD variables goes wrong after index(0) - 18F1330

    Rodger, Try something like this:

    declare this:
    @TIMER0 = TMR0L
    TIMER0 VAR WORD EXT

    This will treat TMR0L as a word. Then all you need to do is something like this:

    IR_Time_Data(index) = TIMER0 'READ CURRENT TIMER VALUE and place into word array by (index).
    Dave Purola,
    N8NTA
    EN82fn

  4. #4
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Re: Indexing WORD variables goes wrong after index(0) - 18F1330

    I noticed you declared the array as WORD(100)

    and your for to loop as 0 to 100 (= 101)

    maybe 0 to 99 will make it work?
    No chance here, doesn't change anything, but good observation - I missed that one
    Roger

  5. #5
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Re: Indexing WORD variables goes wrong after index(0) - 18F1330

    Thanks a lot Dave.

    I'll have to dig in this "EXT" to see exactly what this does, but it (sorry: "you") actually solved my problem.

    Thank you very much again
    Roger

  6. #6
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Indexing WORD variables goes wrong after index(0) - 18F1330

    Rodger, Perhaps this thread will help:

    http://www.picbasic.co.uk/forum/showthread.php?t=3891
    Dave Purola,
    N8NTA
    EN82fn

  7. #7
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Re: Indexing WORD variables goes wrong after index(0) - 18F1330

    Thank you Dave.

    This is indeed very interesting and helpful (at least in my case)
    Roger

Similar Threads

  1. writing word variables to data memory
    By malc-c in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 6th August 2010, 10:21
  2. Replies: 13
    Last Post: - 24th April 2010, 17:44
  3. WRITECODE stores wrong 14-bit word values in FlashMEM
    By BobPigford in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 26th June 2009, 05:35
  4. About writing word variables into the eeprom
    By Ted's in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th August 2008, 22:20
  5. Declaring word-variables in bank15 on a pic18f2431 ?
    By BigWumpus in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 25th October 2006, 23:32

Members who have read this thread : 2

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