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.

Hardware PWM 16F684 HELP

Status
Not open for further replies.

erospawn

New Member
Well.. I fuzzled around with this the last two days, reading and re-reading the instructions on the data sheet.. and Im afraid I just don't know what Im doing..or something.. Can someone pls help me understand what Im doing wrong here? Thanks in advance!

Code:
;PWM test using a lookup table.


list P=16f684
#include <p16f684.inc>
	__config _INTRC_OSC_CLKOUT & _BOD_OFF & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF 
	
	cblock		20h
	swcnt
	endc
	t equ      0x02
RESET_VECTOR CODE	0X000

start call Init


Init
    bsf	    STATUS,RP0		;bank 1
    movlw	0x00          ; SET ALL TRISC 
    movwf	TRISC         ;PORTS TO OUTPUT
    movlw	b'01110000'
    movwf	OSCCON
    movlw   0x65	
    MOVWF	PR2           ; set PR2
    movlw	0x00
    movwf   ANSEL         ; Turn off A/D
    MOVLW	0X07          ; and CMCON0
    MOVWF	CMCON0       ; to make all PORTC pins I/O
    BCF     STATUS,RP0   ; Switch back to bank0
    clrf   CCP1CON
    movlw	b'00000111'
    movwf	T2CON	;set prescaler to 16
    movlw	d'50'
    movwf	CCPR1L
    ;movlw	b'10000011'
    ;movwf	PWM1CON
    clrf	TMR2IF	; Clear the interrupt flag
    BSF		TMR2,TMR2ON    
Toflow	
    btfsc   TMR2,TMR2IF
	goto    Toflow
    movlw   b'11001100'
    clrf   CCP1CON
	RETURN
    END
 
Last edited:
I don't understand what you are trying to do, your code calls init which returns and then it falls through and runs init again before returning to oblivion.
Code:
start		call	Init
[COLOR="red"];you should have code here.
;ending with,
hang		goto	hang[/COLOR]

Init
		bsf	STATUS,RP0	;bank 1
		movlw	0x00		; SET ALL TRISC 
		movwf	TRISC		;PORTS TO OUTPUT
		movlw	b'01110000'
		movwf	OSCCON
		movlw	0x65
		movwf	PR2		; set PR2
		movlw	0x00
		movwf	ANSEL		; Turn off A/D
		movlw	0X07		; and CMCON0
		movwf	CMCON0		; to make all PORTC pins I/O
		bcf	STATUS,RP0	; Switch back to bank0
		clrf	CCP1CON
		movlw	b'00000111'
		movwf	T2CON		;set prescaler to 16
		movlw	d'50'
		movwf	CCPR1L
					;movlw	b'10000011' 
					;movwf	PWM1CON 
		clrf	[COLOR="red"]PIR1,[/COLOR]TMR2IF	; Clear the interrupt flag
		bsf	TMR2,TMR2ON    
Toflow
		btfsc	TMR2,TMR2IF
		goto	Toflow
		movlw	b'11001100'
		[COLOR="red"]clrf	CCP1CON		;should this be movwf[/COLOR]
		return
		end

What are you trying to do?

Mike.
 
I wanted to do a simple 2 LED fader .. So, I was trying to set up a fullwave bridge PWM...just making all the PWM outputs active. Using a PR2 value of 65 with a clock freq. of 8Mhz and prescaler of 16... I was trying to follow the steps in the data sheet to initialize the PWM.. but after that, Im not sure what code should come after that.. after it's init'd.. does there have to code that loops continuously? Basically, Im just trying to get it to work.. then play around with to understand it better... PWM for dummies is somewhat scarce.
 
Last edited:
This will modulate bits 3 and 4 of PortC. If you change the duty cycle then one will go brighter and the other dimmer.

Code:
		list	P=16f684
#include	<p16f684.inc> 
		__config _INTRC_OSC_CLKOUT & _BOD_OFF & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF 

RESET_VECTOR	code	0X000

start		call	Init
hang		goto	hang

Init
		bsf	STATUS,RP0	;bank 1
		movlw	0x00		; SET ALL TRISC 
		movwf	TRISC		;PORTS TO OUTPUT
		movlw	b'01110000'
		movwf	OSCCON
		movlw	0x65
		movwf	PR2		; set PR2
		clrf	ANSEL		; Turn off A/D
		movlw	0X07		; and CMCON0
		movwf	CMCON0		; to make all PORTC pins I/O
		bcf	STATUS,RP0	; Switch back to bank0
		clrf	CCP1CON
		movlw	b'00000100'
		movwf	T2CON		;set prescaler to 1
		movlw	0x65/2		;50% duty cycle
		movwf	CCPR1L
		movlw	b'10001100'
		movwf	CCP1CON
		return
		end

Mike.
 
Thanks! Nice to see I had it somewhat close. I wasn't sure what I was supposed to do with the PWM after it was initialized. Anyway, I did manage to get working ..except, with the "hang goto hang", I loaded a value into CCPR1L and decremented or incremented that value as necessary..works fairly well.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top