Continue to Site

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.

  • 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.

Simple software PWM assembly code

Status
Not open for further replies.

Spadez

New Member
Hi,

Im a little bit stuck here. Im trying to do a PWM in assembly in software (not using the hardware PWM).

I also need this code to be simple so I can experiment with different frequencies. It is designed to turn a pump on and off. I will experiment with around with different frequencies but the pump has it own onboard electronics so I want to keep the frequency low to ensure I dont mess with that. I would think ranging it from 10HZ to 1HZ.

How would I code something like this? This is my preliminary mockup:

Code:
;=====================
;0.1S on - 0.1S off
; 5 cycles per second

10HZ PWM
	BSF PORTB,1
	CALL DELAY 0.1S_DELAY
	BCF PORTB,1
        CALL DELAY 0.1S_DELAY

;=====================

;=====================
;0.1S on - 0.1S off
; 5 cycles per second

1HZ_PWM
	BSF PORTB,1
	CALL DELAY 0.1S_DELAY
	movlw	.10
	mowf		COUNT1	
	decfsz	COUNT1,F
	goto 1HZ_PWM        
	BCF PORTB,1
        CALL DELAY 0.1S_DELAY
	movlw	.10
	mowf		COUNT1	
	decfsz	COUNT1,F
	goto 1HZ_PWM
	movlw	.10
	mowf		COUNT1	
	decfsz	COUNT1,F
	goto 1HZ_PWM  
	movlw	.10
	mowf		COUNT1	
	decfsz	COUNT1,F
	goto 1HZ_PWM  
	movlw	.10
	mowf		COUNT1	
	decfsz	COUNT1,F
	goto 1HZ_PWM 
	return  

;=====================


0.1S_DELAY
	.....
	return

Sorry if this is the ugliest code you have ever seen, but I thought I would be better off showing you my attempt at it.
 
Also my current clock speed is 4Mhz. I understand that this kind of delay is wasteful, and that I would be better using hardware PWM or a slower clock, but for this particual application, neither of those are suitable.
 
Hi,

Well your 10hz loop looks simple and straight forwards but your 1hz routine is confusing, party because there is no notation explaining the loop and why it has the three extra subloops after its done a 1hz pulse.
Code:
	movlw	.10
	mowf		COUNT1	
	decfsz	COUNT1,F
	goto 1HZ_PWM  
	movlw	.10
	mowf		COUNT1	
	decfsz	COUNT1,F
	goto 1HZ_PWM  
	movlw	.10
	mowf		COUNT1	
	decfsz	COUNT1,F
	goto 1HZ_PWM 
	return
If you simply did a 1hz delay routine then you 1hz loop will be the same as your 10hz routine apart from the CALL DELAY 1.0S_DELAY

You can use this delay calculator site Delay Code Generator

Also your delays as they stand will produce a 5hz and 0.5hz signal - a high and low pulse ..


The problem with loop delays is that you cannot really do anything else when they are running, but if you use the Timers 0, 1, 2 and ISR as the basis of your delays then you will find it easier to change the delay periods and allow you to do other processing.
 
Hi,

Thank you for the helpful generator. I have developed this code now:



Code:
;=====================
;Delay Routine for 0.1S
0.1S_DELAY
			;99993 cycles
	movlw	0x1E
	movwf	d1
	movlw	0x4F
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return

This delay routine is then called by the main code. This turns on the mosfet for 0.1S and then off for 0.1S.

Code:
;=====================
;0.1S on - 0.1S off
; 5 cycles per second

0.1S_PWM
	BSF PORTB,1
	CALL 0.1S_DELAY
	BCF PORTB,1
	CALL 0.1S_DELAY
	return

;=====================

If I wanted a 0.2S delay, should I just call the 0.1S delay twice?

Code:
;=====================
;0.2S on - 0.2S off
; 5 cycles per second

0.2S_PWM
	BSF PORTB,1
	CALL 0.1S_DELAY
	CALL 0.1S_DELAY
	BCF PORTB,1
	CALL 0.1S_DELAY
	CALL 0.1S_DELAY
	return

;=====================
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top