Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Data table strange behavior with PIC microcontroller.

Status
Not open for further replies.

midsha1628

New Member
Hello everyone,
Thank you for taking the time to view my first post here on the forum.

I am having a problem with a part of a program I am currently working on which uses the 16hv616. The program uses a data table which only contains 29 values that can be 'looked up'. The program senses an input from a magnetic sensor. Time is measured between pulses from the magnetic sensor and if the time is less than a specific amount of uS. an LED will light stating time is less than my threshold. Now my program simulates fine (I use mplab v8.0 as well as, separately, a spin fixture that simulates the circuit in the actual environment) up to the data table. I have narrowed it down to the point that when it calls the data table, I have tried a 'call' as well as a macro program, it works but after it goes to the data table and reads the "movwf pcl" line it does not work correctly even though it returns the correct value from the table and I have watched it loading the pclath and pcl before and after returning from the 'call' and everything seems fine to me. I have used mplab to step through my program and when I 'call' the table and just place a 'return' inside the table location with nothing else, no movwf pcl, and return from it, the circuit sims and tests fine. But as soon as the 'movwf pcl' is inserted, It seems like the program crashes because the LED does not come on and when the RPM come down to zero the LED that I am using to tell me when a pulse is sensed does not go off(it should when there is no signal). I am thinking that it is somehow loosing it's location when it 'reads the movwf pcl' but am baffled as to how to correct it since it returns the correct value from the table. Ihave listed the code section below. Thank you for any help you can give. I am stumped!

;Main code

MOVLW HIGH (LOOKUP+1)
MOVWF PCLATH
MOVLW LOW (LOOKUP+1)
ADDWF TABLE, W ; 'TABLE' IS REG. WHERE 'JUMP TO' NUMBER IS STORED.
BTFSC STATUS, C
INCF PCLATH, F
CALL LOOKUP
MOVWF TABLE_2 ; 'TABLE_2' IS WHERE RESULT IS STORED.

;----------------------------------------
;MY DATA TABLE

LOOKUP
MOVWF PCL
DT 0X05, 0X53, 0X76, 0X97, 0X99, 0X51, 0X90, 0X64, 0X86, 0X54
DT 0X43, 0X44, 0X79, 0X89, 0X65, 0X56, 0X43, 0X39, 0X77, 0X01
DT 0X10, 0X24, 0X21, 0X88, 0X18, 0X16, 0X47, 0X58, 0X88
 
I think that the "MOVWF PCL" is correct. However, you need the lookup items to be RETLW commands, so it should read:-

Code:
Lookup
movlw PCL
retlw 0X05
retlw 0X53
retlw 0X76
retlw 0X97
retlw 0X99
retlw 0X51
retlw 0X90

etc

There should also be a trap to stop the code going somewhere silly if the routine is called with a value that is too large in the register called "Table"
 
I think that the "MOVWF PCL" is correct. However, you need the lookup items to be RETLW commands, so it should read:-

Code:
Lookup
movlw PCL
retlw 0X05
retlw 0X53
retlw 0X76
retlw 0X97
retlw 0X99
retlw 0X51
retlw 0X90

etc

There should also be a trap to stop the code going somewhere silly if the routine is called with a value that is too large in the register called "Table"

movwf PCL is NOT correct.

Let's say you wanted to fetch line 3 of the table. You load 3 into W, then call the table. If you simply movwf the value of 3 into PCL, it's gonna jump you to code address 3, and not 3 lines ahead.

However, by doing addwf PCL,F you will add the value of 3 to the program counter, which will jump you ahead 3 lines of code to line 3 of the table.
 
movwf PCL is NOT correct.

Let's say you wanted to fetch line 3 of the table. You load 3 into W, then call the table. If you simply movwf the value of 3 into PCL, it's gonna jump you to code address 3, and not 3 lines ahead.

However, by doing addwf PCL,F you will add the value of 3 to the program counter, which will jump you ahead 3 lines of code to line 3 of the table.

However, earlier in the code there are the lines:-

Code:
MOVLW LOW (LOOKUP+1) 
ADDWF TABLE, W

Those move the address of the start of the table into W, and then adds "TABLE", which is the lookup key, to that. So if "TABLE" is 3, that will be added to the address of the start of the table, so the MOVWF PCL will move ({start of lookup} + 3) into PCL, and the jump will be to the 3rd position in the table.

The addition is done earlier so that the any carry can be dealt with, if needed, by adding one to PCLATH
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top