![]() |
DT_INTS-14 (SPWM_INT - Multiple Software PWM) 10/1/2006
This program allows you to have MANY Pulse Width Modulation's being
generated as an interrupt driven Background process with PicBasic Pro.
It's a complete Re-Write of the original Multi-SPWM
with new features added. So be sure to read this article before trying to use
it.
This program will create 3 PWM channels on PORTB 0, 1, and 2. The PWM
frequency is 40Hz, with a resolution of 256 (8-bit). All PWM channels
automatically configure the pin as an output, and set the output to the LOW
state. It will IDLE there until the Dutycycle is changed.
Features:
As before: The downside to this program is that it uses a lot of processor time. This leaves very little for the rest of the program. Depending on how it is configured, it may use 75% or more of the available processing time. The reason it uses so much is due to the large number of interrupts required. For instance, with a Frequency of 100hz, and a resolution of 256, it takes 25,600 interrupts per second. With each output using around 11 instructions per interrupt, it adds up to about 281,600 instructions per second, per output. Yowsa. Needless to say, you probably won't want to run this on a 4Mhz processor, although it is possible. 3 new features of this version are:
Here is a sample program that shows the basics of how to use SPWM_INT. ; Initialize your hardware first DEFINE OSC 20 CLEAR INCLUDE "DT_INTS-14.bas" ; Base Interrupt System INCLUDE "SPWM_INT.bas" ; Software PWM module DEFINE SPWM_FREQ 40 ; SPWM Frequency DEFINE SPWM_RES 256 ; SPWM Resolution DutyVars VAR BYTE[3] ; DutyCycle Variables DutyVar1 VAR DutyVars[0] ; group them in an array for easy access DutyVar2 VAR DutyVars[1] ; with FOR loops etc. DutyVar3 VAR DutyVars[2] ASM SPWM_LIST macro ; Define Pin's to use for SPWM SPWM_PIN PORTB, 0, _DutyVar1 ; and the associated DutyCycle variables SPWM_PIN PORTB, 1, _DutyVar2 ; Notice the underscore before variables SPWM_PIN PORTB, 2, _DutyVar3 endm SPWM_INIT SPWM_LIST ; Initialize the Pins ENDASM ASM INT_LIST macro ; IntSource, Label, Type, ResetFlag? INT_Handler TMR1_INT, SPWMhandler, ASM, yes endm INT_CREATE ; Creates the interrupt processor ENDASM @ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts ;_____________________________________________________________________________ RandVar VAR WORD : RandVar = 12345 LoopCount VAR WORD Main: ; Simple Demo to fade/flash some LED's DutyVar1 = 5 ; Start with 3 LED's at different DutyVar2 = 50 ; brightness DutyVar3 = 150 PAUSE 3000 FOR LoopCount = 1 TO 4 ; Reapeat 4 times FOR DutyVar1 = 0 TO 150 ; Fade LED1 UP PAUSE 10 RANDOM RandVar DutyVar3 = RandVar & $3F ; Give LED3 a random dutycycle NEXT DutyVar1 FOR DutyVar1 = 150 TO 0 STEP -1 ; Fade LED1 Down PAUSE 10 RANDOM RandVar DutyVar2 = RandVar & $3F ; Give LED2 a random dutycycle NEXT DutyVar1 NEXT LoopCount GOTO Main
DutyVar1 = 5 DutyVar2 = 50 DutyVar3 = 150 That's all there is to it. Download the files here: (Right click and Save As) be sure to remove the .txt extension SPWM_INT.bas
; Multiple software PWM's Include file.
|