Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

using pic16f877 need help on look up table

Status
Not open for further replies.

ray21

Newbie level 4
Newbie level 4
Joined
Jul 17, 2006
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,334
pic16f877 lookup table

Hi all, i happened to stumble upon this forum board.
I'm hoping to get some help for writing a lookup table

Currently i need to read from an input source (voltage) , convert it from analog to digital and then use a lookup table to display the results on the LCD

i've already done the ADC conversion & the revelant LCD stuffs

the problem left now is the lookup table

for example, if i take a reading of 3volts, i want it to be displayed in psi(pressure measurment) how to i write a look up table for volts to psi?

is there a sample or anything or the sort that i can take a look at?

thank you in adv.
 

pic16f8xx instruction table

What language are you using to develop code?

How many table entries? 256? 1024?

How many characters for each table entry?

Mike
 

pic16f877 lookup

i'm using mplab, .asm
it's not visual c
here, this is my adc conversion. cause i really do not know how to term this.

#INCLUDE<P16F877.INC>
COUNTER EQU H'22'
ORG H'00'
NOP
BSF STATUS,RP0
BCF STATUS,RP1

MOVLW B'00001110' ;set A/D
MOVWF ADCON1
BSF PIE1,ADIE
MOVLW B'00000001' ;right justified
MOVWF TRISA ;using port A
CLRF TRISC ;using port C
BCF STATUS,RP0


ADC MOVLW B'10000001'
MOVWF ADCON0
CALL DLY1MS
BCF PIR1,ADIF
BSF ADCON0,GO
CALL DLY1MS
LOOP BTFSC ADCON0,GO
GOTO LOOP
MOVF ADRESH,W
MOVWF PORTC ;output port C


GOTO ADC

DLY1MS MOVLW D'124'
MOVWF COUNTER
LOOP1 NOP
DECFSZ COUNTER
GOTO LOOP1
NOP
RETURN

END

table entries would be below 256. hopefully 100 or less
characters displayed for each entry would be a maximum of 6each
ect: 100psi , 124psi.

hope you can give me some advice on how to at least start the lookup table because i really have 0 knowledge on that topic.
thank you
 

branch examples for pic16f877

If I were you, I would just encode the values in 8-bit entries, and add the string 'psi' when I display them.

So, the table will fit the normal table with RETLW instructions.

Your piece of code for getting the value from the table looks like:
Code:
MOVLW 0x00
MOVWF PCLATH
MOVF index,W
ADDWF PCL,F

And the table itself will look like:
Code:
RETLW 0xFC
RETLW 0xF9
RETLW 0xF3
RETLW 0xE7
RETLW 0xCF
RETLW 0x9F
RETLW 0x3F

The table should follow right after the last instruction (ADDWF PCL,F) of the piece of code above. In that case, your 'index' will start with 0. The value from the table will be in the W register. You can implement maximum 256 entries for this kind of table, and you would need some tricks to play with larger tables.

Cheers,
 

    ray21

    Points: 2
    Helpful Answer Positive Rating
pic16f877 multiplication movlw

Hi
You may think that you have 4 variables
ADC output, entry of look up table,value in certain entry of look up table, output to display in psi

Save in the look up table your list of pressure values. According to your operating range and your resolution you will need ceratin size of look up table. Data stored in the look up table should be the converted values of these input as if they were inpu to the A2D.

After reading certain value from the A/D, compare your value to the values listed in the look up table untill you reac to a value equal or a bit greater than A2D output.

Use address of this location to get your displayed psi value.

Welcome any time
 

    ray21

    Points: 2
    Helpful Answer Positive Rating
lookup table using pic16f877a

thank you all so much. i'll give it a try asap.

btw, since after the adc, the voltage values are converted into 8 bit binary digits

therefore , if i am reading from a range of 1 to 10volts with a 0.1 increment, does it mean i have 100 values on the look up table

and yes. add a psi string at the end tag is a gd idea. :D

be working on it tmr. hopefully i get it right.
thanks again for the prompt replies.
 

rlf pic16f8

Hi
As you have range from 0 to 10 V with step of 0.1, you have 100 different value.
But take care, PIC IC A2D is not capable of handling Vref > 5 Volt. Either you will need to make a signal conditioning to your input or you may need to use external A2D.

Thanks
 

pic16f877, table, example

Here's some sample code which supports tables larger than 256 bytes and tables that might straddle a 256 byte boundary by correctly manipulating PCLATH. Replace the Put232 calls with your PutLCD calls.

Have fun. Kind regards, Mike

Code:
;******************************************************************
;
;  example table lookup - 4 character table entries
;
Print_PSI
;
;  multiply index [000..100] times 4
;
        clrc                    ; clear carry before rotate       |B0
        clrf    PTRH            ; clear pointer hi                |B0
        rlf     INDEX,W         ; put index (0-100) x 2 in W      |B0
        movwf   PTRL            ; index * 2 in PTRL:PTRH          |B0
        rlf     PTRH,f          ;                                 |B0
        clrc                    ; clear carry before rotate       |B0
        rlf     PTRL,f          ; index * 4 in PTRL:PTRH          |B0
        rlf     PTRH,f          ;                                 |B0
;
;  now add index*4 to table address
;
        movlw   low  PSI_table  ; table address lo                |B0
        addwf   PTRL,f          ; add to index x 4 lo byte        |B0
        btfsc   STATUS,C        ; overflow?                       |B0
        incf    PTRH,f          ; increment hi byte               |B0
        movlw   high PSI_table  ; table address hi                |B0
        addwf   PTRH,f          ; add to index x 4 hi byte        |B0
        call    PutString       ; lookup & print 4-char string    |B0
        Print   " psi"          ; using the "Print" macro         |B0
        return                  ;                                 |B0

;******************************************************************
;
;  PutStr - setup PTRL and PTRH to string address before entry
;         - string must be terminated with a 00 byte
;
PutString
        call    GetTable        ; get a table character           |B0
        andlw   b'11111111'     ;                                 |B0
        btfsc   STATUS,Z        ; a 00 byte, last character?      |B0
        return                  ; yes, return                     |B0
        call    Put232          ; output char                     |B0
        incfsz  PTRL,F          ; increment pointer               |B0
        goto    PutString       ;                                 |B0
        incf    PTRH,F          ;                                 |B0
        goto    PutString       ;                                 |B0
;
GetTable
        movf    PTRH,W          ;                                 |B0
        movwf   PCLATH          ;                                 |B0
        movf    PTRL,W          ;                                 |B0
        movwf   PCL             ;                                 |B0

;******************************************************************
;
;  PSI String Table (00 terminated strings)
;
PSI_table
        dt      "001/0"         ; entry 000
        dt      "002/0"         ; entry 001
        dt      "003/0"         ; entry 002

;******************************************************************

The Print macro referenced above provides a simple method for printing strings (in tables) that are in-line with your code. Some people think it looks more natural, perhaps a little more like a higher level language.
Code:
;
;  sample usage
;
        Print   " Controller Menu/r/n/n"
        Print   "<1> Adjust Azimuth/r/n"
        Print   "<2> Adjust Elevation/r/n"
        Print   "<3> Save New Settings/r/n"
;
Code:
;******************************************************************
;
;  Print macro - print a string to the RS-232 port
;
Print   macro   str             ;
        local   String,Prt
        movlw   low String      ;
        movwf   PTRL            ;
        movlw   high String     ;
        movwf   PTRH            ;
        goto    Prt             ;
String  dt      str,0
Prt     call    PutString       ; print string
        endm
;******************************************************************
 

pic16f8 length of the signal

Hi ray21

If you use a simple look-up table you must ensure that it does not cross a 256 word page boundary.

Simply set the ORG 0x???? of the ADDWF PC,F instruction to the start of a page.

eg.
org 0x0500
lookup table addwf PC,F
retlw value1
retlw value2
retlw value3
etc. etc.

put required element no. in W
call lookup ...... returns with value in W
call must be in the same 2k page as look-up table

Polymath
 

lookup table pic16f877

polymath said:
Hi ray21

If you use a simple look-up table you must ensure that it does not cross a 256 word page boundary.

Simply set the ORG 0x???? of the ADDWF PC,F instruction to the start of a page.

eg.
org 0x0500
lookup table addwf PC,F
retlw value1
retlw value2
retlw value3
etc. etc.

put required element no. in W
call lookup ...... returns with value in W
call must be in the same 2k page as look-up table

Polymath
May I suggest that you can easily overcome the limitations of your "simple" table example by simply adjusting PCLATH along with PCL?

Code:
;
;  code can be anywhere (within 2kword memory 'page' boundary)
;  table can be anywhere (within 2kword memory 'page' boundary)
;  table can cross a 256 word boundary
;
Lookup  movwf   TEMP            ; save index value
        movlw   high Table      ; table address hi
        movwf   PCLATH          ; setup PCLATH
        movlw   low  Table      ; table address lo
        addwf   TEMP,W          ; add index
        skpnc                   ; overflow? no, skip, else
        incf    PCLATH,f        ; increment address hi
        movwf   PCL             ; perform the branch
Code:
;
Table   dt      value1
        dt      value2
        dt      value3
;
There are other important considerations for saving, adjusting, and restoring PCLATH when code and/or tables cross PIC 2-kword "page" boundaries (Microchip uses the "page" term to describe 2-kword memory boundaries).

Kind regards, Mike
 

using look up table in a pic

To start off, I took the reading of 0.1V to 1V with step increments of 0.1V

If 0.1v = 1 psi,

am i on the right track below?



; MOVLW 0x00
; MOVWF PCLATH
; MOVF index,W
; ADDWF PCL,F


******************************************************

INIT CLRF STATUS
BSF STATUS,RP0
MOVLW H'07' ;SET TO DIGITAL
MOVWF ADCON1
MOVLW B'00100000' ;USING PORTA,5
MOVWF TRISA
RETURN


VOLTINPUT ADDWF PC,F
RETLW H'05' ;0.1V
RETLW H'0A' ;0.2V
RETLW H'0F' ;0.3V
RETLW H'14' ;0.4V
RETLW H'19' ;0.5V
RETLW H'1E' ;0.6V
RETLW H'23' ;0.7V
RETLW H'28' ;0.8V
RETLW H'2E' ;0.9V
RETLW H'33' ;1.0V
RETLW 0


PSI ADDWF PC,F
RETLW D'1' ;1PSI
RETLW D'2'
RETLW D'3'
RETLW D'4'
RETLW D'5'
RETLW D'6'
RETLW D'7'
RETLW D'8'
RETLW D'9'
RETLW D'10' ;10PSI
RETLW 0

Is this basic table correct and if so, how to i get the subroutine to read
the amount of voltage that is being input?

Just for more info, i'm acutally fabricating a pressure sensor(MEMS) and then
connecting it to an instrumental differential amplifier which is in turn connected
to my MCT circuit. With changes in the pressure, the voltage output from the
sensor will change and the readings - in psi (which is why i need a lookup table)
will be displayed on my LCD.

Any further help will be much appreciated.
Thanks & have a nice day.

Ray
 

pic16f877 and lookup table

Mike said:
polymath said:
Hi ray21

If you use a simple look-up table you must ensure that it does not cross a 256 word page boundary.

Simply set the ORG 0x???? of the ADDWF PC,F instruction to the start of a page.

eg.
org 0x0500
lookup table addwf PC,F
retlw value1
retlw value2
retlw value3
etc. etc.

put required element no. in W
call lookup ...... returns with value in W
call must be in the same 2k page as look-up table

Polymath
May I suggest that you can easily overcome the limitations of your "simple" table example by simply adjusting PCLATH along with PCL?

Code:
;
;  code can be anywhere (within 2kword memory 'page' boundary)
;  table can be anywhere (within 2kword memory 'page' boundary)
;  table can cross a 256 word boundary
;
Lookup  movwf   TEMP            ; save index value
        movlw   high Table      ; table address hi
        movwf   PCLATH          ; setup PCLATH
        movlw   low  Table      ; table address lo
        addwf   TEMP,W          ; add index
        skpnc                   ; overflow? no, skip, else
        incf    PCLATH,f        ; increment address hi
        movwf   PCL             ; perform the branch
Code:
;
Table   dt      value1
        dt      value2
        dt      value3
;
There are other important considerations for saving, adjusting, and restoring PCLATH when code and/or tables cross PIC 2-kword "page" boundaries (Microchip uses the "page" term to describe 2-kword memory boundaries).

Kind regards, Mike

Indeed Mike, k8lh
My table is simple, yes, designed that way - to help ray21 learn the technique in easy stages. KISS.
It uses only one active code line whereas your submission uses 8 code lines and complicates by using PCLATH - many have fallen using PCLATH when learning PICs.
I try to write uncomplicated code that is fit-for-purpose, no-more, no-less.
I would hazard a guess that most people started with the simple form then expanded on their success.

MChip use the word "page" as a DIRECTIVE for MPASM ref: Help File
Your example appears to be very similar to the example in the MChip Help File.

I currently use a 'complex' version which covers multiple selectable tables of variable length and both code and tables are in fully relocatable code - not recommended as an introduction to look-up tables. I don't use it as a teaching aid or to impress others.
kindest regards ... Polymath
*******************************************************

Hi ray21
These MChip app. notes may be helpful:

Implementing a Table Read
https://ww1.microchip.com/downloads/en/AppNotes/00556e.pdf

On Chip Memory
https://ww1.microchip.com/downloads/en/DeviceDoc/ramrom.pdf

hope this helps ... Polymath
 

pic16f877 using dt tables

That helps alot. I think i'll manage from here :D

A very big thank you to everyone who posted & helped
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top