Newbie PIC PWM question

I posted this to comp.arch.embedded, but the only suggestion I got was that timer 2 was not enabled, but that's why I put in the : BSF T2CON,TMR2ON near the end. Does anyone here have any thoughts please? ================================================================== I'm having problems with the PWM module on a PIC 16F627, and I wondered if anyone could help please.

The PIC is clocked at 4MHz, and I've got 6 LEDs on port B, including RB3/CCP1.

I can write simple light-chaser programs, but I'm trying to dim the LED on CCP1, but it is either on, or its off. My program looks like this:

RESET MOVLW B'00000111' ;Disable Comparator modules MOVWF CMCON

BSF STATUS,RP0 ;Switch to bank 1

;Disable pull-ups ;INT on rising edge ;TMR0 to CLKOUT ;TMR0 Incr low2high trans. ;Prescaler assign to Timer0 ;Prescaler rate is 1:256 MOVLW B'11010111' ;Set PIC options MOVWF OPTION_REG ;Write the OPTION register.

CLRF INTCON ;Disable interrupts MOVLW B'11000000' ;RB7 & RB6 are inputs. MOVWF TRISB ;RB5...RB0 are outputs.

MOVLW 0xFF ; all RA are inputs MOVWF TRISA

MOVLW 100 ; set period for PWM MOVWF PR2

BCF STATUS,RP0 ; Switch Back to Bank 0

MOVLW 5 ; set duty cycle (very low) MOVWF CCPR1L

CLRF T2CON ; Reset prescale and postscale to 1:1

MOVLW B'00001100' ; Set PWM mode MOVWF CCP1CON

BSF T2CON,TMR2ON ; enable TMR2

LOOP GOTO LOOP

Just lights the LED on RB3

Any thoughts anyone please?

Reply to
Deep Reset
Loading thread data ...

"Deep Reset" wrote

wondered if

Have you looked at the output pin with a scope?

Reply to
Anthony Fremont

including

I don't have a scope, but I lit the adjacent LEDs and varied the duty cycle (CCPR1L) from something like 5 to 95 and could see no difference in brightness between the solid 'on' LEDs and the PWM one. I should've noticed a distinct difference at these sorts of values, I think. I'll see if I can borrow a scope.

Thanks

Deep.

Reply to
Deep Reset

"Deep Reset" wrote

Your code looks ok to me, I don't see any reason for it to not work. I wanted to burn it into a PIC and test it, but I just can't find the time right now. The code below definitely works for me on a 16F628. I get a

256uS period with a duty cycle of 12.5% (32uS) on RB3. The code below was used when I was doing some experimentation, but I ended up needing two PWM pins, so I did it all in software; that's why the ints are disabled, but the comments may indicate otherwise. I left it in the program anyway (for times like this ;-) and it drives an LED directly as a power on indicator and it's quite bright.

BTW, are you sure you aren't being bitten by the watchdog timer and that it's not browning out? You didn't specify what your CONFIG word settings are. You also didn't use any ORG directives to skip over the interrupt vector, so that's a potential issue for later. But it does appear that you have disabled all ints so that really shouldn't be the problem. You could try making PR2 bigger than 100.

; ; Initialize PWM Module ; PWMInit

; Set Period to 255 bsf STATUS, RP0 ; Select BANK 1 bcf PIE1 & 0x07F, TMR2IE ; Turn off TMR2 ints movlw d'255' movwf PR2 & 0x07F bcf STATUS, RP0 ; Select BANK 0

; Set Duty Cycle to 32 movlw DutyCycle movwf CCPR1L

; Setup of Prescaler and TMR2 for different PWM rates (Based on 4Mhz clock) ; (4 Mhz clock) movlw b'00000000' ; TMR2 timer stopped, prescale /1, no post scale ; |||||||+-------------- T2CKPS0 ; ||||||+--------------- T2CKPS1 ; |||||+---------------- TMR2ON ; ||||+----------------- TOUTPS0 ; |||+------------------ TOUTPS1 ; ||+------------------- TOUTPS2 ; |+-------------------- TOUTPS3 ; +--------------------- Unused movwf T2CON

; Set CCP1CON for PWM mode movlw b'00001100' ; PWM Mode ; |||||||+-------------- CCP1M0 ; ||||||+--------------- CCP1M1 ; |||||+---------------- CCP1M2 ; ||||+----------------- CCP1M3 ; |||+------------------ CCP1Y ; ||+------------------- CCP1X ; |+-------------------- Unused ; +--------------------- Unused movwf CCP1CON

; Clear the interrupt flags bcf PIR1, TMR2IF

; Enable Peripheral interrupts bcf INTCON, PEIE

; Enable the timer clrf TMR2 bsf T2CON, TMR2ON ; Start counting

return

Reply to
Anthony Fremont

Thanks to all who replied/emailed - the code was working all the time - I'd just forgotten two things:

1) Don't trust the eye to measure relative brightness (!) 2) The duty cycle register has an extra 2 bits in CCP1CON.

Thanks again

Deep.

Reply to
Deep Reset

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.