Forum: Mikrocontroller und Digitale Elektronik A/D conversion-scalling with look-up-table and frequency generation with PIC16F877a


von Stephan L. (nickgomez)


Lesenswert?

Hallo Leute,

ich bin neu hier und hab da ein problem.
mein projekt ist folgendes:
Analog Neigungssensor mit ausgangsspannung zwischen 1,8V und 3,2V
mit PIC 16F877A 8Bit A/D Wandlung
dann ne direkte skallierung des wertes über eine Look-Up-Table(mit excel 
erzeugt)
und diesen wert gleich als Timer-Vorladung benutzt um eine gewisse 
pulsbreite des ausgangssignals zu erreichen.
mit dem Timer dann nen High-Puls erzeugt und nochmal mit dem timer ne 
ungefähre low-periode von 15ms
das signal dann auf einen RC-Servomotor gegeben der sich der neigung des 
sensors nach positioniert.

programmiert mit MPLAB und die simulation mit registerinjection in 
register ADRESSL funktionert (also look-up-table und frequenz erzeugung) 
auf dem PIC und auf der platine passiert leider garnichts. könnte mir 
bitte jemand helfen, sich mein programm anschauen und mal ne "kleine 
fehlersuche" betreiben?!?! VIELEN VIELEN DANK!

nun, ich bin absoluter anfänger in sachen programmierung und brauche die 
programmierung nur für ein projekt im studium.
das es wahrscheinlich 100 bessere, elegantere Lösungen gibt ist mir 
klar.
Ich würde es aber gern, der verständnis halber, so lösen.
die kommentare sind meist fehlerhaft weil ein großer teil aus einem, von 
mir, älteren projekt stammt.
PS:(Programmcode muss in Assembler sein)


__CONFIG h'3F39'  ;command for compiler

;+++ DECLARATIONS 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
++++++++++++

TMR0   equ 01h    ;TMR0 is register 1
STATUS   equ 03h    ;Status register is register 3
OPT_REG  equ 81h    ;OPTION_REG is register 1 (Bank 1)
PCL  equ 02h
PORTA  equ 05h    ;Port A is register 5 (Bank 0)
PORTB   equ 06h          ;Port B is register 6 (Bank 0)
PORTD  equ 08h    ;Port D is register 8 (Bank 0)
PORTE  equ 09h    ;Port E is register 9 (Bank 0)
TRISA  equ 85h    ;TRIS A is register 5 (Bank 1)
TRISB  equ 86h    ;TRIS B is register 6 (Bank 1)
TRISD  equ 88h    ;TRIS D is register 8 (Bank 1)
TRISE  equ 89h    ;TRIS E is register 9 (Bank 1)
ADCON0  equ 1Fh    ;ADCON0 is register 31 (Bank 0)
ADCON1  equ 9Fh    ;ADCON1 is register 159 (Bank 1)
ADRESH  equ 1Eh    ;ADRESH is register 30 (Bank 0)
GODONE  equ 2    ;GODONE is Bit 2 of ADCON0 register
CARYFL  equ 0    ;CARYFL is Bit 0 of Status register
ZEROFL  equ 2    ;ZEROFL is Bit 2 of Status register
ANAREG   equ 20h    ;ANAREG recieved analog value register
VALUE  equ 30h    ;VALUE is an register for ...
RP0       EQU 5
OPTION_REG    EQU 1

;+++ A/D Configuration 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
++++++

 MOVLW  B'11000001'  ;Move value of 0xC1 into working register
 MOVWF  ADCON0    ;Move content of working register into the ADCON0
 BSF  STATUS,5  ;Jump to Bank1
 MOVLW  B'00001110'  ;Move value of 0x0E into working register
 MOVWF  ADCON1    ;Move content of working register into the ADCON1


;+++ Configuration Ports and Timer 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 MOVLW  B'11111111'  ;Move value of 0xFF into working register
 MOVWF  TRISA    ;Move content of working register into TRISA
      ;to Config. PortA: all bits are input
 MOVWF  TRISB    ;Move content of working register into TRISB
      ;to Config. PortB: all bits are input
 CLRF  TRISD    ;Clear TRISD register to Config. PortD: all bits
 CLRF  TRISE    ;Clear TRISE register to Config. PortE: all bits

 BCF  STATUS,5  ;Jump back to Bank0


;+++ MAIN Program 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
+++++++++++

START

;+++ A/D conversion +++
    BSF    ADCON0,GODONE  ;start A/D conversion
READ
    BTFSC  ADCON0,GODONE  ;wait for A/D conversion to

    GOTO  READ      ;still waiting for A/D
    MOVF  ADRESH,W    ;Move content of ADRESH
    MOVWF  ANAREG      ;Move content of working
    MOVF  ANAREG,F    ;Move content of register

    BTFSS  STATUS,ZEROFL  ;If Zero-flag is clear skip the
    GOTO  EXIT      ;Jump to marker EXIT

    Call  LOOK_UP_TABLE
    MOVWF  VALUE

    BSF  STATUS,RP0           ;Jump to Bank 1
    MOVLW B'00000010'      ;Configure Option register, TMR0
    MOVWF OPTION_REG    ;Set Option re
    BCF STATUS,RP0           ;Jump to Bank 0

    BSF PORTD,0        ;Set Bit 0 of
    MOVF  VALUE,W
    MOVWF TMR0         ;Summon the TMR0
C4_High
    BTFSS TMR0,7      ;Test Bit 7 of register
    GOTO C4_High

    BCF PORTD,0        ;(start Low level)
    BSF  STATUS,RP0           ;Jump to Bank 1
    MOVLW B'00000110'      ;Configure Option
    MOVWF OPTION_REG    ;Set Option re
    BCF STATUS,RP0

    MOVLW .10        ;load the decimal
    MOVWF TMR0        ;Summon the TMR0
C4_Low
    BTFSS TMR0,7      ;Test Bit 7 of register
    GOTO C4_Low           ;Loop

EXIT
    GOTO  START      ;Jump back to marker Start



;+++ Subroutine: LOOK up table 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

LOOK_UP_TABLE
 ADDWF  PCL,f    ;Add the value of the ANAREG Register to the
 RETLW  B'00100000'  ;0    Value  32
 RETLW  B'00100000'  ;1    Value  32
 RETLW  B'00100000'  ;2    Value  32
 RETLW  B'00100000'  ;3    Value  32
 RETLW  B'00100000'  ;4    Value  32
 RETLW  B'00100000'  ;5    Value  32
 RETLW  B'00100000'  ;6    Value  32
 RETLW  B'00100000'  ;7    Value  32
 RETLW  B'00100000'  ;8    Value  32
 RETLW  B'00100000'  ;9    Value  32
 RETLW  B'00100000'  ;10    Value  32
 RETLW  B'00100000'  ;11    Value  32
 RETLW  B'00100000'  ;12    Value  32
 RETLW  B'00100000'  ;13    Value  32
 RETLW  B'00100000'  ;14    Value  32
 RETLW  B'00100000'  ;15    Value  32
 RETLW  B'00100000'  ;16    Value  32
 RETLW  B'00100000'  ;17    Value  32
 RETLW  B'00100000'  ;18    Value  32
 RETLW  B'00100000'  ;19    Value  32
 RETLW  B'00100000'  ;20    Value  32
 RETLW  B'00100000'  ;21    Value  32
 RETLW  B'00100000'  ;22    Value  32
 RETLW  B'00100000'  ;23    Value  32
 RETLW  B'00100000'  ;24    Value  32
 RETLW  B'00100000'  ;25    Value  32
 RETLW  B'00100000'  ;26    Value  32
 RETLW  B'00100000'  ;27    Value  32
 RETLW  B'00100000'  ;28    Value  32
 RETLW  B'00100000'  ;29    Value  32
 RETLW  B'00100000'  ;30    Value  32
 RETLW  B'00100000'  ;31    Value  32
 RETLW  B'00100000'  ;32    Value  32
 RETLW  B'00100000'  ;33    Value  32
 RETLW  B'00100000'  ;34    Value  32
 RETLW  B'00100000'  ;35    Value  32
 RETLW  B'00100000'  ;36    Value  32
 RETLW  B'00100000'  ;37    Value  32
 RETLW  B'00100000'  ;38    Value  32
 RETLW  B'00100000'  ;39    Value  32
 RETLW  B'00100000'  ;40    Value  32
 RETLW  B'00100000'  ;41    Value  32
 RETLW  B'00100000'  ;42    Value  32
 RETLW  B'00100000'  ;43    Value  32
 RETLW  B'00100000'  ;44    Value  32
 RETLW  B'00100000'  ;45    Value  32
 RETLW  B'00100000'  ;46    Value  32
 RETLW  B'00100000'  ;47    Value  32
 RETLW  B'00100000'  ;48    Value  32
 RETLW  B'00100000'  ;49    Value  32
 RETLW  B'00100000'  ;50    Value  32
 RETLW  B'00100000'  ;51    Value  32
 RETLW  B'00100000'  ;52    Value  32
 RETLW  B'00100000'  ;53    Value  32
 RETLW  B'00100000'  ;54    Value  32
 RETLW  B'00100000'  ;55    Value  32
 RETLW  B'00100000'  ;56    Value  32
 RETLW  B'00100000'  ;57    Value  32
 RETLW  B'00100000'  ;58    Value  32
 RETLW  B'00100000'  ;59    Value  32
 RETLW  B'00100000'  ;60    Value  32
 RETLW  B'00100000'  ;61    Value  32
 RETLW  B'00100000'  ;62    Value  32
 RETLW  B'00100000'  ;63    Value  32
 RETLW  B'00100000'  ;64    Value  32
 RETLW  B'00100000'  ;65    Value  32
 RETLW  B'00100000'  ;66    Value  32
 RETLW  B'00100000'  ;67    Value  32
 RETLW  B'00100000'  ;68    Value  32
 RETLW  B'00100000'  ;69    Value  32
 RETLW  B'00100000'  ;70    Value  32
 RETLW  B'00100000'  ;71    Value  32
 RETLW  B'00100000'  ;72    Value  32
 RETLW  B'00100000'  ;73    Value  32
 RETLW  B'00100000'  ;74    Value  32
 RETLW  B'00100000'  ;75    Value  32
 RETLW  B'00100000'  ;76    Value  32
 RETLW  B'00100000'  ;77    Value  32
 RETLW  B'00100000'  ;78    Value  32
 RETLW  B'00100000'  ;79    Value  32
 RETLW  B'00100000'  ;80    Value  32
 RETLW  B'00100000'  ;81    Value  32
 RETLW  B'00100000'  ;82    Value  32
 RETLW  B'00100000'  ;83    Value  32
 RETLW  B'00100000'  ;84    Value  32
 RETLW  B'00100000'  ;85    Value  32
 RETLW  B'00100000'  ;86    Value  32
 RETLW  B'00000111'  ;87    Value  7
 RETLW  B'00001000'  ;88    Value  8
 RETLW  B'00001000'  ;89    Value  9
 RETLW  B'00001001'  ;90    Value  9
 RETLW  B'00001001'  ;91    Value  10
 RETLW  B'00001010'  ;92    Value  11
 RETLW  B'00001011'  ;93    Value  11
 RETLW  B'00001011'  ;94    Value  12
 RETLW  B'00001100'  ;95    Value  12
 RETLW  B'00001101'  ;96    Value  13
 RETLW  B'00001101'  ;97    Value  14
 RETLW  B'00001110'  ;98    Value  14
 RETLW  B'00001110'  ;99    Value  15
 RETLW  B'00001111'  ;100  Value  16
 RETLW  B'00010000'  ;101  Value  16
 RETLW  B'00010000'  ;102  Value  17
 RETLW  B'00010001'  ;103  Value  17
 RETLW  B'00010010'  ;104  Value  18
 RETLW  B'00010010'  ;105  Value  19
 RETLW  B'00010011'  ;106  Value  19
 RETLW  B'00010011'  ;107  Value  20
 RETLW  B'00010100'  ;108  Value  21
 RETLW  B'00010101'  ;109  Value  21
 RETLW  B'00010101'  ;110  Value  22
 RETLW  B'00010110'  ;111  Value  22
 RETLW  B'00010111'  ;112  Value  23
 RETLW  B'00010111'  ;113  Value  24
 RETLW  B'00011000'  ;114  Value  24
 RETLW  B'00011000'  ;115  Value  25
 RETLW  B'00011001'  ;116  Value  26
 RETLW  B'00011010'  ;117  Value  26
 RETLW  B'00011010'  ;118  Value  27
 RETLW  B'00011011'  ;119  Value  27
 RETLW  B'00011100'  ;120  Value  28
 RETLW  B'00011100'  ;121  Value  29
 RETLW  B'00011101'  ;122  Value  29
.
.
.
.
.
.
 und so weiter

END

von Anja (Gast)


Lesenswert?

Stephan L. schrieb:
> (also look-up-table und frequenz erzeugung)

Glaube ich nicht:
Eine Lookup-Table mit 256 Einträgen muß zwingend an einer 256 Byte 
Page-Grenze beginnen. Das PCLATH-Register muß dann vor ADDWF PCL,f mit 
der Page-Nummer geladen sein.

Ich verstehe auch nicht warum bei ADC-Wert = 0 die Impulsausgabe 
übersprungen wird.

Gruß Anja

von Stephan L. (nickgomez)


Lesenswert?

das überspringen der pulsausgabe bei ADC wert =0 kann vielleicht aus dem 
programm davor stammen. Wie gesagt ich kenn mich leider nicht großartig 
aus mit programmierung. Sich da rein zu denken mit hilfe des 
datenblattes und ein wenig SPS erfahrung geht noch, aber jetzt ganz neue 
überlegungen anzustellen und ein projekt bei NULL zu beginnen, würd ich 
nicht hin bekommen. sorry

die look-up-table funktioniert jedenfalls, in der MPLAB-simulation inkl. 
register injection (ADRESSL)

was könnte denn noch unstimmig sein?

von Anja (Gast)


Lesenswert?

Stephan L. schrieb:
> die look-up-table funktioniert jedenfalls,

Nein dann hast Du nicht alle 256 Werte getestet. bei Überschreiten der 
Page-Grenze springt der Prozessor in den Wald.

Gruß Anja

von Stephan L. (nickgomez)


Lesenswert?

Anja schrieb:
> Eine Lookup-Table mit 256 Einträgen muß zwingend an einer 256 Byte
> Page-Grenze beginnen. Das PCLATH-Register muß dann vor ADDWF PCL,f mit
> der Page-Nummer geladen sein.

 hallo,

also auf meiner platine läuft das jetzt eigentlich ganz gut, die 
wichtigsten werte 87-167 digital nimmt er gut aus der tabelle mit auch 
bis schätzungsweise 200... nur dann hängt er sich meistens auf.
ich mein in natura würde sollch ein wert bei meiner anwendung nicht 
auftreten aber wenn der supervisor das testet...
mmmh.

wie komm ich denn dahin das die look up table an so einer grenze 
beginnt.

Vielen Dank! und danke anja

von Anja (Gast)


Lesenswert?

Stephan L. schrieb:
> wie komm ich denn dahin das die look up table an so einer grenze
> beginnt.

Dafür gibt es die "org" direktive.

am einfachsten mit
1
 org 0x00FC
2
3
LOOK_UP_TABLE
4
 movlw high (LOOK_UP_TABLE_00)
5
 movwf PCLATH
6
 MOVF  ANAREG,W   ;Move content of register
7
 ADDWF  PCL,f    ;Add the value of the ANAREG Register to the
8
LOOK_UP_TABLE_00
9
 RETLW  B'00100000'  ;0    Value  32 (table begins at 0x0100)
10
 ....

Du kannst auch die Prozessor-Register mit "include" einbinden
 include "P16F877.INC"

Gruß Anja

von Stephan L. (nickgomez)


Lesenswert?

Anja schrieb:
> org 0x00FC
>
>
>
> LOOK_UP_TABLE
>
>  movlw high (LOOK_UP_TABLE_00)
>
>  movwf PCLATH
>
>  MOVF  ANAREG,W   ;Move content of register
>
>  ADDWF  PCL,f    ;Add the value of the ANAREG Register to the
>
> LOOK_UP_TABLE_00
>
>  RETLW  B'00100000'  ;0    Value  32 (table begins at 0x0100)
>
>  ....

also der org befehl ist für den compiler, verstehe.

  MOVF  ANAREG,W

  ADDWF  PCL,f

ist mir auch klar was da passiert, aber die drei zeilen davor, die 
verstehe ich nicht so ganz, leider denn ich würde es gern verstehen.

  LOOK_UP_TABLE

  movlw high (LOOK_UP_TABLE_00)   ??? high ins working register?

  movwf PCLATH     ???

eingebunden und ausprobiert hab ich es jetzt noch nicht, denn zum 
programmieren des pic´s muss ich in die uni.
wärst du noch so lieb und würdest mir den vorgang erklären?
Vielen Dank anja!

meine unwissenheit tut mir leid, ich muss zu meiner verteidigung sagen 
das ich ein wenig ausgepowert bin vom studium

von Stephan L. (nickgomez)


Lesenswert?

hilfe!

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.