PDA

View Full Version : PWM instruction not working as I expect



peu
- 18th January 2006, 21:16
Hi,

I have this code for the 12F683 pic (4mhz int clock):




TRISIO = %00000000

conta var byte
ciclos var byte
pasos var byte

red var gpio.1
green var gpio.2
blue var gpio.4


ciclos=10
pasos=16


high red 'this goes to low unexpectedly when the following for-next is executed
low blue
low green

for conta=0 to 255 step pasos 'blue rise
pwm blue, conta, ciclos
next conta
high blue 'this goes to low unexpectedly when the following for-next is executed

for conta=0 to 255 step pasos 'red fall
pwm red, 255-conta, ciclos
next conta
low red

'rest of the program removed

end


I expect:

1st loop
red remain high while 1st for-next is executed
blue increase till high

2nd loop
blue remains high from last loop
red decrease till low

what happens

no matter the previous state of the port, when it enters a loop only the port being incremented/decremented is high, the other is low.

what can be wrong?


Thanks in advance

Bruce
- 18th January 2006, 21:42
Are you disabling A/D & comparators?

peu
- 18th January 2006, 22:05
Are you disabling A/D & comparators?

bingo :)

Thanks

peu
- 19th January 2006, 00:00
bingo :)

Thanks

not that bingo after all... :(

Here is the full code:



trisio =%00000000
ansel =%00000000

conta var byte
ciclos var byte
pasos var byte

red var gpio.1
green var gpio.2
blue var gpio.4
'pote var gpio.0

ciclos =10
low red
high blue
high green

pasos =16
loop:

'ADCIN pote, pasos

for conta=0 to 255 step pasos 'blue fall
pwm blue, 255-conta, ciclos
next conta
low blue

for conta=0 to 255 step pasos 'red rise
pwm red, conta, ciclos
next conta
high red

for conta=0 to 255 step pasos 'green fall
pwm green, 255-conta, ciclos
next conta
low green

for conta=0 to 255 step pasos 'blue rise
pwm blue, conta, ciclos
next conta
high blue

for conta=0 to 255 step pasos 'red fall
pwm red, 255-conta, ciclos
next conta
low red

for conta=0 to 255 step pasos 'green rise
pwm green, conta, ciclos
next conta
high green

goto loop
end


red is supposed to stay high until the for-next that put it back to low, but as soon the rise for-next is finished it goes off.

any idea?


Pablo

mister_e
- 19th January 2006, 00:05
CMCON0=7


about now?

Bruce
- 19th January 2006, 00:15
Hi Pablo,

Look in the data sheet under Comparator Module. Look at the "default" POR (power on reset) value in CMCON0.

Now look at the drawing in FIGURE 8-3: COMPARATOR I/O OPERATING MODES.

Place CMCON0 = X in the initialization section of your code.

Replace X with the value shown in this section that will turn "Comparator Off".

peu
- 19th January 2006, 15:45
Hi Pablo,

Look in the data sheet under Comparator Module. Look at the "default" POR (power on reset) value in CMCON0.

Now look at the drawing in FIGURE 8-3: COMPARATOR I/O OPERATING MODES.

Place CMCON0 = X in the initialization section of your code.

Replace X with the value shown in this section that will turn "Comparator Off".

Thanks Bruce, your answer helped me two ways, to solve the problem and to understand a little more how to read datasheets, I have the 12F683 printed&binded datasheet next to me, but since I didn't know where to look exactly I wast lost in the sea of trial&error.

Thanks again, and thanks Steve who also gave me the right answer!


Pablo

peu
- 20th January 2006, 19:17
Hi Bruce,

I won't ask howto but where to look for the answer :)

I'm trying to use gpio.3 as an input, but I dont know how to set it as such.

Setting trisio=%00001000 does not help me

Thanks in advance


Hi Pablo,

Look in the data sheet under Comparator Module. Look at the "default" POR (power on reset) value in CMCON0.

Now look at the drawing in FIGURE 8-3: COMPARATOR I/O OPERATING MODES.

Place CMCON0 = X in the initialization section of your code.

Replace X with the value shown in this section that will turn "Comparator Off".

Bruce
- 20th January 2006, 19:36
Hi Pablo,

PBP has default config fuse settings in device specific header files. Look in your PBP directory for a file named 12F683.INC. Open this file.

You'll see the PBP default config fuse settings for this target PIC. It looks somthing like this;

This is used only if you're using the default PM asembler that ships with PBP.

device pic12F683, intrc_osc_noclkout, wdt_on, mclr_on, protect_off

And this one is used when you're using the Microchip MPASM assembler.

__config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF

Notice how the default settings for /MCLR (GPIO.3) all have the reset function enabled (mclr_on, _MCLRE_ON)?

You can over-ride these default config fuse settings, and make GPIO.3 available as an input by editing the file, and changing it to mclr_off or _MCLRE_OFF, save the file, and re-compile.

Or simply add this line to the beginning of your code (if using the PM asembler);

@ device pic12F683, intrc_osc_noclkout, wdt_on, mclr_off, protect_off

Now you just need to make the pin an input by setting the appropriate TRIS bit.

Melanie posted a nice tutorial on config settings here:
http://www.picbasic.co.uk/forum/showthread.php?t=543 so I won't elaborate.

peu
- 20th January 2006, 19:39
Thanks again Bruce, just added that line, and now I can use this extra pin !!

I trully appreciate your answers.


Pablo