PDA

View Full Version : 16f876a Pwm



Hainkm
- 30th December 2007, 02:49
Ok, I've done PWM a bunch of times on other PICs, but for some reason I can't get the 16F876A do what I want...

I want to use software PWM, basically doing an RGBW LED Fade (4 channels)

Right now, using either PORTA or PORTB, I fade up an LED, then set it HIGH. I go to fade up the 2nd channel and the first channel (set high, not using PWM anymore) goes dead.

I have my pins set to outputs, a/d is off, etc.

Any ideas?

Thanks!
Hainkm

paul borgmeier
- 30th December 2007, 06:31
>> any ideas?

R-M-W problem? ... post your code and you will get more help

Jerson
- 30th December 2007, 07:29
Paul is right. If you are modifying the outputs like this

PORTB = PORTB + $2

You can be sure that bit corresponding to $2 will come on, but the bits which are supposed to stay where they are may not. This is the Read-Modify-Write issue Paul is talking about. Read more about it in the datasheets

JF

Hainkm
- 30th December 2007, 14:50
Here's some code. Thanks for the help!!

@ device PIC16F876A, hs_osc, wdt_off, pwrt_off, bod_off, lvp_on, cpd_off

TRISA=%00000000
TRISB=%00000000
ADCON0=7
ADCON1=7

W VAR BYTE

TheLoop:

FOR W=1 TO 255
PWM PORTB.0,W,1
NEXT W

HIGH PORTB.0

PAUSE 1000

' As soon as I start this PWM on PORTB.1, PORTB.0 goes LOW...this is what I need to fix.

FOR W=1 TO 255
PWM PORTB.1,W,1
NEXT W

HIGH PORTB.1

PAUSE 1000

GOTO TheLoop

Jerson
- 30th December 2007, 15:41
You don't seem to have the RBPU enabled in your code (OPTION_REG) and also you may not be having external pull ups on the PWM output pins. So, when the PWM code executes, it does the RMW routine and PortB.0 goes low. You could check on this.

JF

Hainkm
- 30th December 2007, 17:33
That did the trick! Thank you so much!

Hainkm