|
|||||||||||
| 技術(shù)交流 | 電路欣賞 | 工控天地 | 數(shù)字廣電 | 通信技術(shù) | 電源技術(shù) | 測控之家 | EMC技術(shù) | ARM技術(shù) | EDA技術(shù) | PCB技術(shù) | 嵌入式系統(tǒng) 驅(qū)動編程 | 集成電路 | 器件替換 | 模擬技術(shù) | 新手園地 | 單 片 機 | DSP技術(shù) | MCU技術(shù) | IC 設(shè)計 | IC 產(chǎn)業(yè) | CAN-bus/DeviceNe |
請問張工:軟件模擬PWM? |
| 作者:fineasy 欄目:單片機 |
產(chǎn)品選型時,不能用貴的MCU,但要有PWM功能(不很好也行),請問PIC是否有軟件模擬的PWM例程.多謝! |
| 2樓: | >>參與討論 |
| 作者: martin 于 2003/1/24 17:58:00 發(fā)布:
兩個軟件PWM例程 以前在網(wǎng)上找到的兩個軟件PWM的例程,32級和256級的,我沒有測試過,可參考其原理。 list p=16C64,t=ON,c=132,n=80 title "An all SOFTWARE PWM with 32 states" radix dec ;******************************************************************************** ; ; T. Scott Dattalo ; ; 25APR97 - Initial version ; ; The purpose of this program is to generate a PULSE Width Modulated ;square wave with SINGLE instruction cycle resolution. This version ONLY ;supports 32 PWM levels. 14 cycles are available every iteration for ;other tasks. ; ; The algorithm is a state machine based on indirect goto's and tables. ;The 32 PWM levels are divided into 4 GROUPs of 8. For each GROUP, ;there are three states through which the state machine will pass. The ;states contain the addresses to where the goto's branch. The tables ;provide a convenient way to create a variable delay. ; ; As an example, consider the execution sequence required to generate a ;10-cycle wide PULSE. The state initialization is: ; ;PW10 MOVLW PULSE_on2off+7 ; MOVWF state_3 ; MOVLW variable_delay+1 ; MOVWF state_1 ; MOVLW pw9_16 ; MOVWF state_2 ; nop ; goto pw9_16 ; ;This puts the addresses of the start of the three tasks into the three ;state variables. Then code like this: ; ; MOVF state_n,W ; MOVWF PCL ; ; will branch to that state. The offsets added to the program lables ;provide the variable delay needed to adjust the PULSE width. For ;example, when the contents of state_3 are placed into PCL, execution ;will begin 7 instructions after the label 'PULSE_on2off'. ; ; The sequence of execution for the PW10 PULSE is this: ; ; PULSE_on2off+7 -> variable_delay+1 -> pw9_16 ---+ ; ^-------<-----------------<---------------------------| ; ;This sequence executes in exactly 32 cycles. On the 0'th the PWM ;OUTPUT is driven high and on the 10'th cycle it's driven low. ;The "0'th" cycle is 'BSF' instruction in the pw9_16 code segment. ;the 10'th cycle is the 'BCF' instruction in the PULSE_on2off code ;segment. ; include "P16C64.INC" __CONFIG _CP_OFF & _PWRTE_ON & _WDT_OFF& _HS_OSC START_OF_RAM_LO equ 0x20 END_OF_RAM_LO equ 0x7f START_OF_RAM_HI equ 0xa0 END_OF_RAM_HI equ 0xbf ;*********************************************************************** ;PWM Equates PWM_PORT EQU PORTA PWM_BIT EQU 0 cblock START_OF_RAM_LO state_1,state_2,state_3 endc ;*********************************************************************** ORG 0 ;Reset Vector GOTO Main ;*********************************************************************** ;This segment will drive the PWM OUTPUT high (or KEEP it high if it ;was driven earlier) and then 1 to 8 cycles later drive it low. It then ;branches to the address contained in state_1. PULSE_on2off BSF PWM_PORT,PWM_BIT BSF PWM_PORT,PWM_BIT BSF PWM_PORT,PWM_BIT BSF PWM_PORT,PWM_BIT BSF PWM_PORT,PWM_BIT BSF PWM_PORT,PWM_BIT BSF PWM_PORT,PWM_BIT BSF PWM_PORT,PWM_BIT BCF PWM_PORT,PWM_BIT MOVF state_1,W MOVWF PCL ;This segment will drive the PWM OUTPUT low (or KEEP it low if it ;was driven earlier) and then 1 to 8 cycles later drive it high. It then ;branches to the address contained in state_1. PULSE_off2on BCF PWM_PORT,PWM_BIT BCF PWM_PORT,PWM_BIT BCF PWM_PORT,PWM_BIT BCF PWM_PORT,PWM_BIT BCF PWM_PORT,PWM_BIT BCF PWM_PORT,PWM_BIT BCF PWM_PORT,PWM_BIT BCF PWM_PORT,PWM_BIT BSF PWM_PORT,PWM_BIT NOP MOVF state_1,W MOVWF PCL variable_delay NOP NOP NOP NOP NOP NOP NOP MOVF state_2,W MOVWF PCL delay16 ;(actually, a delay of 17 cycles...) goto $+1 ;These 14 free cycles will be executed goto&nb |
|
| 3樓: | >>參與討論 |
| 作者: fineasy 于 2003/1/25 9:08:00 發(fā)布:
謝謝了!martin! |
|
|
|
| 免費注冊為維庫電子開發(fā)網(wǎng)會員,參與電子工程師社區(qū)討論,點此進(jìn)入 |
Copyright © 1998-2006 m.58mhw.cn 浙ICP證030469號 |