• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PWM mania

Status
Not open for further replies.

AtomSoft

Well-Known Member
Ok i found this awesome video on PWM:
PWM tutorial video in High-def

They made it seem so simple. Are you telling me i can set up a Timer to toggle a bit at specific times and effectively make a pwm? if so then why is hardware PWM such a pain?

bit meaning Port(x) pin
 
Last edited:

Nigel Goodwin

Super Moderator
Most Helpful Member
Ok i found this awesome video on PWM:
PWM tutorial video in High-def

They made it seem so simple. Are you telling me i can set up a Timer to toggle a bit at specific times and effectively make a pwm? if so then why is hardware PWM such a pain?
Hardware PWM isn't a pain, it's dead simple and easy to use.

Software PWM is easy as well, but has the BIG disadvantage that it uses too much processor time - hardware PWM takes no processor time.
 

Sceadwian

Banned
The only thing I find difficult about using the hardware (PWM or otherwise) on micro controllers is the setup registers, it's easy if every time you get a new type of chip that you print a cheat sheet of all of the hardware control registers for each peripheral so that you have a quick easy reference to them.
 

AtomSoft

Well-Known Member
Thats what im doing now printing the registers to some important stuff for my 18F2525. Gonna try a software PWM and then if all works ill look into the hardware version.
 

Sceadwian

Banned
Atom. After the initial setup (which isn't bad) hardware PWM is way better for most applications. It's completely hands off with a lot less code.
 

AtomSoft

Well-Known Member
Ok i know i mess up alot lol but im stuck here and dont see why the below doesnt goto a interrupt:

Code:
	LIST P=18F2525	;directive to define processor
	#include <P18F2525.INC>	;processor specific variable definitions


    CONFIG	OSC = INTIO67, WDT = OFF, LVP = OFF, XINST = OFF
	
#define pwmt TRISC
#define pwm 2


		CBLOCK	0x000
		EXAMPLE		;example of a variable in access RAM
		ENDC

		ORG	0x0000
		bra Init
		ORG	0x0008
		bra	HighInt	

Init:
	movlw	0x72
	movwf	OSCCON
	movlw	0x0F
	movwf	ADCON1
	bcf		pwmt,pwm
InitTmrInt:
	movlw 	b'00000100'
	movlw	INTCON2
	movlw 	b'11000000'
	movwf	T0CON
	bcf		RCON, IPEN
	movlw	b'11100000'
	movlw	INTCON
	bsf		LATC, pwm
Main:

	goto 	Main

HighInt:
	bcf		INTCON,TMR0IF
	btg		LATC, pwm
	retfie	FAST
		END
 

AtomSoft

Well-Known Member
I want to figure out Software PWM tho so i can then have the option to use more PWMs then the PIC includes.

Just checked and timer is running nice but the interrupt isnt on for some reason

EDIT
OOPS

i had:
Code:
	movlw	b'11100000'
	movlw	INTCON
needed to be
Code:
	movlw	b'11100000'
	movwf	INTCON
EDIT:
Not really working out so ima try hardware PWM now. It doesnt seem hard now that i really read the datasheet. ima comment it nicely too lol
 
Last edited:

AtomSoft

Well-Known Member
wow it was way easier using hardware you guys were right lol
Code:
	LIST P=18F2525	;directive to define processor
	#include <P18F2525.INC>	;processor specific variable definitions


    CONFIG	OSC = INTIO67, WDT = OFF, LVP = OFF, XINST = OFF
	
#define pwmt TRISC
#define pwm 2


		CBLOCK	0x000
		dir
		ENDC

		ORG	0x0000
		bra Init
		ORG	0x0008
		bra HighInt	

Init:
	movlw	0x72
	movwf	OSCCON			;8 Mhz

	movlw	0x0F
	movwf	ADCON1			;All Digital

	clrf	dir

InitPWMTmr:
	movlw	0xFF
	movwf	PR2				;Do a full 255 count
	
	movlw	b'00000000'		
	movwf	CCPR1L			;0% remember CCP1CON B1:B0 must be 00

	clrf	TRISC			;Set the pins to output
	
	movlw	b'00010011'
	movwf	T2CON			;0, 0000 = 1:1 Postscale/0001 = 1:2 Postscale/•••/1111 = 1:16 Postscale, Timer = OFF(0), 00 = Prescaler is 1/01 = Prescaler is 4/1x = Prescaler is 16

	bsf		T2CON,TMR2ON

	movlw 	b'00001100'		
	movwf	CCP1CON			;00, DC-B1:B0(00), PWM Mode (1100)

InitInter:
	bsf		RCON, IPEN		;Enable Interrupt Priority Levels (1)

	movlw	b'11000000'		;To Enable interrupts GIEH(7 = 1), GIEL(6 = 1)
	movwf	INTCON

	movlw	b'00000010'		;Enable Timer 2 Interrupt(PIE1,2 = 1)
	movwf	PIE1

	movlw	b'00000010'		;Timer 2 is High Priority INT (IPR1,1 = 1)
	movwf	IPR1

	bcf		PIR1, TMR2IF	;Clear Interrupt Flag (Timer 2)

Main:

	bra		Main

HighInt:
	bcf		PIR1, TMR2IF	;Clear Interrupt Flag (Timer 2)

	movlw	0xFF
	cpfseq	CCPR1L
	bra		$+6
	bcf		dir,0			
	bra		donea

	movlw	0x00
	cpfseq	CCPR1L
	bra		$+4
	bsf		dir,0		

donea:
	btfsc	dir,0
	bra		$+4				;If dir = 00 then INCF
	bra		$+6				;If dir = 01 then DECF
	incf	CCPR1L
	bra		$+4
	decf	CCPR1L
	retfie	FAST

		END
 
Last edited:

AtomSoft

Well-Known Member
Look at this its so pretty lol I just did it lol. I put it on my youtube its there in better quality if ya wanna see it in better quality click the HQ button.
[embed]http://www.youtube.com/v/EMSB5s26e0Y[/embed]
 
Last edited:

Nigel Goodwin

Super Moderator
Most Helpful Member
If you are using timer with interrupt mode negligible processor time is wasted in software PWM.
It depends what resolution you want, but in all cases it's not negligible - particularly compared with the zero processor time of the hardware PWM.

A LONG time ago I did two channel software PWM using a 16C84, plus receiving radio commands via a licence free radio module - it was a difficult balancing act between getting enough resolution for the PWM, and as fast as possible radio link. In the end I had to drop the radio speed down considerably, as the timer interrupts disrupted the radio signals.
 

AtomSoft

Well-Known Member
i will try to use 2 hardware PWMs today.. I have a 18F1330 and 18F448. Is it possible to have 3 PWM on at same time with different rates for each?

If so ill try to figure it out but would like to make sure with those PIC micros its viable.
 

Nigel Goodwin

Super Moderator
Most Helpful Member
i will try to use 2 hardware PWMs today.. I have a 18F1330 and 18F448. Is it possible to have 3 PWM on at same time with different rates for each?

If so ill try to figure it out but would like to make sure with those PIC micros its viable.
Assuming the PIC supports three hardware PWM channels then it's no more difficult than just using one. Check my PWM tutorial which uses a 16F876 for two channel PWM.

You can set the output width totally independently, it wouldn't be much good otherwise - but the frequency is fixed because they all run off the same timer (at least on two channel ones).
 

AtomSoft

Well-Known Member
oh ok cool. I think i get it. So i can set different duty cycles but using the same timer so i will interrupt only once since its only timer2. Right?

The 18F1330 is said to have:

• Up to 6 PWM Channel Outputs
- Complementary or independent outputs
The 18F448:
Enhanced CCP module which has all the features
of the standard CCP module, but also has the
following features for advanced motor control:
- 1, 2 or 4 PWM outputs
- Selectable PWM polarity
- Programmable PWM dead time
so im assuming the 1330 would be best for separate operation
 

Odin

Member
so im assuming the 1330 would be best for separate operation
I think you're right. The 18F448, and many other pics, are specialised for motor control. Half bridge or full bridge. It modulates only one output at a time. The other outputs just switches the elements in the bridge on or off.
Example: To make the motor run in one direction, upper left switch on, lower right modulated (pwm) Even mid range pics like 16F684 can do this.

The 1330 seems to be more specialised for power control. SMPS etc. Looks interesting. I was once searching for a pic to control a push-pull power supply. As a SG3525 used in car amps etc. Maybe the 1330 would fit the task.
 

Mike - K8LH

Well-Known Member
If you are using timer with interrupt mode negligible processor time is wasted in software PWM.
I have to agree with Nigel on this. An interrupt driven software PWM driver requires processor "overhead" which becomes significant as you add channels and/or increase the interrupt frequency to increase the refresh rate. Take the following 8 channel 256 step "counter method" PWM interrupt driver excerpt for example. It uses 26 instruction cycles during each 50 usec interrupt which would be 52% "overhead" if you were using a 4 MHz clock. Using a faster clock improves "overhead" but in any case there's 0% "overhead" with hardware PWM.

Regards, Mike

Code:
;
;  soft PWM "counter method", 256 levels, 16 bit core
;  50 usec interrupts, 12.8 msec "frame", 78.125 Hz refresh
;
;  void isr_hi()
;  { pir1.TMR2IF = 0;                   //
;    if(dcy >= Led[0]) shadow.0 = 0;    //
;    if(dcy >= Led[1]) shadow.1 = 0;    //
;    if(dcy >= Led[2]) shadow.2 = 0;    //
;    if(dcy >= Led[3]) shadow.3 = 0;    //
;    if(dcy >= Led[4]) shadow.4 = 0;    //
;    if(dcy >= Led[5]) shadow.5 = 0;    //
;    if(dcy >= Led[6]) shadow.6 = 0;    //
;    if(dcy >= Led[7]) shadow.7 = 0;    //
;    latb = shadow;                     //
;    dcy++                              // inc duty cycle, 0..255
;    if(dcy == 0)                       // if end-of-period
;      shadow = 255;                    // reset shadow
;  }
;
;  23 words, 26 cycles (isochronous), 10 bytes RAM
;
isr_hi
        bcf     PIR1,TMR2IF     ; clear timer 2 interrupt flag    
        movf    dcy,W           ; duty cycle counter, 0..255      
        cpfsgt  Led+0           ; if(Led[0] >= dcy)               
        bcf     Shadow,0        ;   Shadow.0 = 0                  
        cpfsgt  Led+1           ; if(Led[1] >= dcy)               
        bcf     Shadow,1        ;   Shadow.1 = 0                  
        cpfsgt  Led+2           ;                                 
        bcf     Shadow,2        ;                                 
        cpfsgt  Led+3           ;                                 
        bcf     Shadow,3        ;                                 
        cpfsgt  Led+4           ;                                 
        bcf     Shadow,4        ;                                 
        cpfsgt  Led+5           ;                                 
        bcf     Shadow,5        ;                                 
        cpfsgt  Led+6           ;                                 
        bcf     Shadow,6        ;                                 
        cpfsgt  Led+7           ;                                 
        bcf     Shadow,8        ;                                 
        movff   Shadow,LATB     ;                                 
        infsnz  dcy,F           ; if end-of-period                
        setf    Shadow          ;   reset shadow                  
        retfie  FAST            ;                                 
;
 

Speakerguy

Active Member
Hardware PWM is wonderful. Try software for four PWM channels driving an H-bridge with variable dead time configuration, all of the characteristics you can change on the fly, vs. the same thing done by a hardware peripheral on a PIC.

Now figure the cost of all the MOSFETs and diodes you just blew trying to do it in software and not getting it quite right :)
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top