Forum: Mikrocontroller und Digitale Elektronik Veständnissfragen zum PWM_Assemlercodebespiel


von Rudi R. (microwitsch)


Lesenswert?

1
     include      "P16F887.INC"      ; Device specifics include file
2
      errorlevel -302
3
;
4
  __CONFIG  _CONFIG1, _LVP_OFF & _FCMEN_ON & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON  
5
                      & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT       
6
  __CONFIG  _CONFIG2, _WRT_OFF & _BOR21V 
7
;
8
 
9
;**********************Header**********************************************
10
;************************DEFINING VARIABLES********************************
11
      cblock       0x70            ; Block of interrupt-context-save-variables placed into shared memory
12
      w_temp                       ; Variable at address 70h
13
      pclath_temp                  ; Variable at address 71h
14
      status_temp                  ; Variable at address 72h
15
      endc
16
      
17
      #define      DARK PORTB,0    ; Push-button "DARK" is connected
18
                                   ; to PORTB,0 pin
19
      #define      BRIGHT PORTB,1  ; Push-button "BRIGHT" is connected
20
                                   ; to PORTB,1 pin
21
;************************PROGRAM START*************************************
22
23
      org          0x0000          ; First program instruction address
24
      goto         main            ; Jump to label "main"
25
26
;************************INTERRUPT ROUTINE*********************************
27
28
      org          0x0004          ; Interrupt vector
29
      
30
      movwf        w_temp          ; Save register W
31
      
32
      swapf        STATUS,W          ; Save register STATUS
33
      movwf        status_temp
34
      
35
      movf         PCLATH,W          ; Save register PCLATH
36
      movwf        pclath_temp
37
      
38
      banksel      CCPR1L
39
      btfss        DARK            ; Tests push-button "DARK"
40
      decf         CCPR1L          ; Push-button is pressed - decrement CCP1L by 1
41
      btfss        BRIGHT          ; Testing push-button "BRIGHT"
42
      incf         CCPR1L          ; Push-button is pressed - increment CCP1L by 1
43
44
      banksel      PIR1            ; Selects bank containing PIR1
45
      bcf          PIR1,TMR1IF     ; Clears interrupt flag TMR1IF
46
      
47
      bsf          TMR1H,7         ; Accelerates timer TMR0 counting
48
      bsf          TMR1H,6         ;
49
      
50
      movf         pclath_temp,w   ; PCLATH is given its original content
51
      movwf        PCLATH
52
      swapf        status_temp,w   ; STATUS is given its original content
53
      movwf        STATUS
54
      swapf        w_temp,f        ; W is given its original content
55
      swapf        w_temp,w
56
      
57
      retfie                       ; Return from interrupt routine
58
      
59
;************************ MAIN PROGRAM **************************************
60
61
main                              ; Start of the main program
62
      banksel      ANSEL          ; Selects bank containing register ANSEL
63
      clrf         ANSEL          ; Clears registers ANSEL and ANSELH
64
      clrf         ANSELH         ; All pins are digital
65
      
66
      banksel      OPTION_REG     ; Selects bank containing register ANSEL
67
      bcf          OPTION_REG,7   ; Pull-up resistors enabled      
68
      bsf          WPUB,0         ; Pull-up resistors enabled
69
      bsf          WPUB,1         ; on port B pins 0 and 1
70
      
71
      banksel      TRISC          ; Selects bank containing register TRISC
72
      clrf         TRISC          ; All port C pins are configured as outputs
73
      
74
      banksel      T1CON          ; Selects bank containing register T1CON
75
      bcf          T1CON,TMR1CS   ; TMR1 operates as a timer
76
      bcf          T1CON,T1CKPS0  ; Prescaler rate is 1:8
77
      bcf          T1CON,T1CKPS1
78
      bsf          T1CON,TMR1ON   ; Activates timer TMR1
79
      
80
      banksel      PIE1           ; Selects bank containing register PIE1
81
      bsf          PIE1,TMR1IE    ; Interrupt TMR1 is enabled
82
      bsf          INTCON,PEIE    ; Peripheral modules interrupts are
83
                                  ; enabled
84
      bsf          INTCON,GIE     ; Global interrupt enabled
85
      
86
      banksel      T2CON      
87
      movlw        B'11111101'    ; Prescaler TMR2 = 1:4 
88
      movwf        T2CON
89
      banksel      PR2
90
      movlw        B'11111111'    ; Number in register PR2
91
      movwf        PR2
92
      
93
      banksel      CCP1CON
94
      movlw        B'00001100'    ; Bits to configure CCP1 module_Bit 0-3=1100 = PWM mode; P1A, P1C active-high; P1B, P1D active-high
95
      movwf        CCP1CON
96
loop
97
      goto         loop           ; Remain here
98
      end                         ; End of program

Da ich noch neu bin in Sachen PIC-Programmierung hier mal eine Frage 
bezüglich meines Verständnisses des hier angehägtem PWM-Codes:

Die Taster, die hier die hier die Pulsdauer steuern sollen, werden doch 
mit dem Interrupthandler des Timers 1 verbunden oder?

In der Interruptroutine werden die Zustände per Scleife überprüft und 
die Pulsweitenregister dekrementiert oder inkrementiert,so das sich der 
PWM-Output bzw.eine Ausgangsleistung anpassen lässt.

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.