天堂草原最受欢迎的角色,天堂动漫,天堂在线,色天堂下载,天堂中文在线资源,亚洲男人天堂

技術熱線: 4007-888-234
設計開發

專注差異化嵌入式產品解決方案 給智能產品定制注入靈魂給予生命

開發工具

提供開發工具、應用測試 完善的開發代碼案例庫分享

技術支持

從全面的產品導入到強大技術支援服務 全程貼心伴隨服務,創造無限潛能!

新品推廣

提供新的芯片及解決方案,提升客戶產品競爭力

新聞中心

提供最新的單片機資訊,行業消息以及公司新聞動態

利用PIC的PWM實現數模轉換的方法

更新時間: 2019-03-25
閱讀量:1823

硬件圖:

 

3cb4317700b54fa3a81c287de7a81526.png

源程序:

; PWM port (in w), pin, duty
; Generates a bit stream of 256 1s and 0s with the specified
; duty cycle. If this bit stream is run through a simple RC
; integrator, the result is an analog voltage output of
; (duty/255) * (PIC supply voltage). For example, if duty is
; 100 and the supply is 5 volts, then the integrated output of
; pwm would be (100/255) * 5 = 1.96 volts. In many applications,
; pwm output should be buffered with a unity-gain op-amp
; circuit. In those cases, the pwm output pin should be set up
; as an output when Pwm is called, then immediately switched to
; input when Pwm is done. This prevents the steady-state output
; of the pin from affecting the voltage set by Pwm.

  P = pic16c55
        #include <16c55.inc>   ; processor assembler definitions
        _CONFIG _xt_osc & _wdt_off & _protect_off
        reset start

        org 8
duty Res d'1' ; Pwm duty cycle.
acc Res d'1' ; Pwm accumulator.
index Res d'1' ; Temporary counter for pwm.
pin Res d'1' ; Pin number to pulse (0-7).

; Device data and reset vector

 org 0

; Table to convert pin number (0-7) into bit mask (00000001b to 10000000b).
Pinz         ADDWF pcl                 
             RETLW d'1'                
             RETLW d'2'
             RETLW d'4'
             RETLW d'8'
             RETLW d'16'
             RETLW d'32'
             RETLW d'64'
             RETLW d'128'

start        MOVLW d'0'                 ; All outputs.
             TRIS 5h
             CLRF 5h                    ; Start with LED off.
             CLRF duty                  ; Initial brightness = 0.
             CLRF index                 ; Clear loop counter for pwm.
start_loop   MOVLW d'2'                 ; Pin 2.
             MOVWF pin

 MOVLW d'0'                 ; of port ra.
             CALL pwm                   ; Send pwm to LED.
             INCF duty                  ; Turn up brightness.
             GOTO start_loop            ; Endless loop
; Upon entry, the desired pin must already be set up as an output.
; Variable "pin" contains the pin number (0-7). The w register contains a
; number representing the output port (0-2) for RA through RC. The variable
; duty contains the desired duty cycle from 0 to 255.

Pwm          MOVWF fsr                  ; Point to the port number.
             MOVLW 5h                   ; Add offset for port RA.
             ADDWF fsr
             MOVF pin,w                
             CALL Pinz                  ; Get bit mask from the table.
             MOVWF pin                  ; Put the mask into pin.
Pwm_loop     MOVF duty,w                ; Let acc = acc + duty
             ADDWF acc

             MOVF pin,w                
             BTFSC status,c             ; IF carry THEN pin = 1
             IORWF indirect             ; ELSE pin = 0.
             COMF pin,w                
             BTFSS status,c            
             ANDWF indirect             

    DECFSZ index               ; Repeat 256 times.
             GOTO Pwm_loop
             RETLW 0h                  

            
            
             end


联系我们: 盘锦市| 历史| 玉树县| 潜山县| 甘德县| 德令哈市| 江永县| 图木舒克市| 开阳县| 屯门区| 鹿邑县| 石狮市| 迁西县| 章丘市| 多伦县| 鄂州市| 萨嘎县| 甘孜县| 新乡县| 农安县| 平乡县| 崇阳县| 子长县| 台南县| 泌阳县| 阳春市| 咸丰县| 昂仁县| 吉林市| 垫江县| 虎林市| 苗栗县| 贺兰县| 博兴县| 哈密市| 敖汉旗| 西平县| 乌兰察布市| 庆元县| 上蔡县| 六枝特区|