MPLAB simulating PWM on 16F628A

Status
Not open for further replies.

Lost Tourist

New Member
I have been poking around 16F84's for a while and have previously done pwm in software successfully, I am now experimenting with the 16F628A CCP module using MPLAB's MPLAB SIM simulator.
I have trawled the internet and have a fair idea of what it takes to get the CCP module running however after sometime (2 days) i cannot get MPLAB to show me the output from CCP1 on the logic analyzer.

Attached is the code i have been using.

Any hints would be greatly appreciated.



Thanks
 

Attachments

  • Step01.txt
    2.9 KB · Views: 541
For some reason the simulator doesn't like the bottom 2 bits of CCP1CON being set.

You have a couple of mistakes, TMR2ON = bit 2 not 3, so you has a postscaler on timer2.
Your on time was longer than your period so it never toggled.

See below,

Mike.
Code:
init
		clrf	CCP1CON			; Clear CCP Control register to reset CCP module

		movlw	b'001011[COLOR="Red"]00[/COLOR]'		; control word to switch CCP module to PWM mode (and period LSB's)
				; b:7-6 = not used
				; b:5-4 = LSB duty cycle
				; b:3-0 = CCP module configuration  11xx = PWM mode
		movwf	CCP1CON



		bsf		status,rp0	; access bank 1
		movlw	.25			; Set duty cycle
		movwf	PR2
		bcf		status,rp0	; return to bank 0

		movlw	[COLOR="red"].12[/COLOR]			; set period of PWM
		movwf	CCPR1L
		bcf 	CCP1CON,5  ; set bit 1
   		bcf 	CCP1CON,4  ; set bit 0
		
		movlw 	b'0000[COLOR="red"]0[/COLOR]110'   ; 		Turn on TMR2 and set post scaler to 1 and prescaler to 4
				; b:7 = not used
				; b:6-4 = post scale 0000 = 1:1
				; b:3 = 1 TMR2 ON
				; b:2-1= 10 prescale = 1:4
   		movwf 	T2CON

	
		retlw	0
 
Thanks

Thanks for that Mike, everything is working properly now.

I currently have some small remaining issues with my circuit in that the frequency out of CCP1 is not exactly the predicted value but i think that's probably due to using the internal oscillator being temperature sensitive, either way i'm pretty sure i can work through that.

Thanks again.

David
 
Set the prescaler to 1:1 and it will be much faster. About 4 times.

Mike.
 
BTW, your frequency will be 1,000,000/26/4 = 9615Hz. Is this what you are seeing. If you expect it to be 10KHz then change PR2 to 24.

It also makes no sense to have a prescaler of 4 on timer2. Change to a 1:1 prescaler, change the PR2 value from 25 to 100 and your PWM value will go from 0 to 400 instead of 0 to 100 (25 in CPR1L + 2 bits in CCP1CON).

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…