PDA

View Full Version : Indexing WORD variables goes wrong after index(0) - 18F1330



flotulopex
- 11th May 2018, 16:10
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:
' ====== 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 (http://www.picbasic.co.uk/forum/showthread.php?t=544)), but, if the answer is there, I still don't get what I'm missing.

Any help is very welcome ;)

mark_s
- 11th May 2018, 16:42
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?

Dave
- 11th May 2018, 19:27
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).

flotulopex
- 11th May 2018, 19:50
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 ;)

flotulopex
- 11th May 2018, 19:55
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 :)

Dave
- 11th May 2018, 22:24
Rodger, Perhaps this thread will help:

http://www.picbasic.co.uk/forum/showthread.php?t=3891

flotulopex
- 14th May 2018, 13:29
Thank you Dave.

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