PDA

View Full Version : pot controlled pwm -- help



docwisdom
- 25th January 2006, 07:09
greetings, I am a novice at the whole pic thing. I hope to change that with many experiments and networking with people like you guys.

anyway, I am trying to control a PWM with a 10k pot. It is for controlling the dimming of an LED driver. I have the pot hooked up correctly and am getting proper readings. I am using the pot command to read. Given that this gives me a number from 0-255, it seemed perfect to set a variable and plug it right into the hpwm command. I then let it loop, looking for changes in the pot. The drivers frequency is 10khz and outputs current to the led when the pin goes low.
Unfortunately what I get is a very erratic blink that really has no continuity with the pot being adjusted. Any ideas?

here is the code, remember, Im an extreme beginner.

----------------------------------
define OSC 8
v1 var byte 'Variable v1 holds pot information

high portc.2
pause 100
High portd.2 'power indicator

main:
pot portd.1,23,v1 'Read resistance of pot
Hpwm 1, v1, 10000 'hardware pwm on RC2
pause 100
goto main


End
-------------------------------

as for hardware, I am using a buckpuck made by luxdrive. It is a 1 amp LED driver that gives a regulated 5v reference for microcontroller power, it also has a control return which I hooked the pwm out pin (RC2/CCP1) to. I have an led indicating the chip is on & running. I also have a 10k pot with capacitor for the dimming adjustment. MCLR is pulled up with a 4.7k. I am also running an 8mhz crystal with two caps.

thanks in advance for your feedback.

maus
- 25th January 2006, 09:38
what pic do you use ?

sample for 16f876:

TRISA = 255 ' Set PORTA als input
ADCON1 = 0
define OSC 8
DEFINE ADC_BITS 8 ' adc = 8 bits

v1 var byte 'Variable v1 holds pot information

high portc.2
pause 100
High portd.2 'power indicator

main:

adcin 0,v1 'Read resistance of pot
pause 10
Hpwm 1, v1, 10000 'hardware pwm on RC2

goto main


End

Acetronics2
- 25th January 2006, 15:20
Hi,DOC

Forget the POT command ... it gives more headaches than anything Else. @ 4 Mhz only, you can hope for something

the RC Time command ... ( see manual ) works perfectly for such input readings.
you'll just have do divide the result by 256.

Alain

sayzer
- 25th January 2006, 17:21
Also, don't forget that your V1 variable as "byte" holds values upto 255 just as "pot" command does.

However, when you use a scale less then 255, which is 23 in your code, after 23 you still get V1 as a value bigger then 23.

Use an LCD just to see how the values in your scale do not match with the variable you are using to hold the pulse value.

To avoid this problem, you should use a check module like an IF statement.
Ex: IF V1 > 23 then V1=23

You should have already realized that after you get the value of your scale (23), your 10K pot does not respond accurately since the reading value now is much bigger then 23.

docwisdom
- 25th January 2006, 17:38
thank you all for your help so far. to answer the first question, I am using the 16F877A. It wont be the chip that I will actually use for this item when I finally have a finished board, but it is feature rich and good for prototyping.

maus: I will give your code changes a shot. my main reason for using pot was I get a value from 0-255, the same as the input for hpwm, so no conversion necessary. what kind of values do you get from the adcin? I havent found any documentation that has answered this question thus far.


Acetronics: thanks for the input as well, it looks like the pot command is going out the window. Ill read up on the RC command

sayzer: I thought the scale was an adjustment for setting the pot's upper limit? I used a counter program that read the pot with an if then statement and blinked an LED equal to the scale amount, this is where 23 came from.

thanks again for all your help. I will probably be able to test some of these things tonight after work.


included is a schematic, it also may be crude, its my first time using the schematic software.

mister_e
- 26th January 2006, 04:07
As you use a 16F877, why spending time and extra capacitor when life can be sooo much easier by using internal ADC on PORTA?

Look for ADCIN in the PBP manual and Section 11 of the datasheet.

docwisdom
- 26th January 2006, 05:34
what pic do you use ?

sample for 16f876:

TRISA = 255 ' Set PORTA als input
ADCON1 = 0
define OSC 8
DEFINE ADC_BITS 8 ' adc = 8 bits

v1 var byte 'Variable v1 holds pot information

high portc.2
pause 100
High portd.2 'power indicator

main:

adcin 0,v1 'Read resistance of pot
pause 10
Hpwm 1, v1, 10000 'hardware pwm on RC2

goto main


End


thanks for this code, but doesnt this base itself on the ADCIN producing a number from 0-255 to feed to the hpwm? I have made these changes and the LED's just stay on, even with adjustment of the pot. I dont think the variable output from the adcin is properly formatted for the hpwm

does anyone know how this is formatted? I cant find any documentation on it.

docwisdom
- 26th January 2006, 05:55
So I gave up on the pot for now and just set a pwm to go to the driver.

hpwm 1, 127, 5000

I get 50% duty with a regular LED, but it doesnt work properly with the LED driver

sayzer
- 26th January 2006, 10:58
Try the attached PWM filter.
Play with the driver resistor and the load resistor to catch a nice signal out.

If you already have a signal amplifier, the NPN transistor may not be necessary.

Acetronics2
- 26th January 2006, 12:22
Hi, Sayzer

PWM is intended to know what you apply to your load, your design is very very very very far from giving a known output ... it's more an ON/OFF output than anything else.

Alain

milestag
- 26th January 2006, 14:54
I don't really care for the HPWM command. I think it's easier to manually set the registers. This is a conversion from part of my code that may work for you to at least get the PWM working.

This is for an 8MHz oscillator (on a 18F2525, so double check register names):

T2CON = 4 'Timer2 = ON; Prescale = 1:1
PR2 = 200 '(period) this should give you should give you about 10KHz
CCPR1L = 100 '(duty cycle) 100 should be close to 50%, 20 is about 10%, etc

To turn PWM ON:
CCP1CON = 12

To turn PWM OFF:
CCP1CON = 0

sayzer
- 29th January 2006, 11:53
Hi Acetronics,

PWM filter is not something I created through my output port; it is what picbasic pro recommends.
If you check the manual or help files, you will find this filter as a solution to scarry pwm out. Still, it is optional.

If you know something else, pls share it with us so that we can learn. Also, PWM is already an on/off state with different intervals on ON mode, on OFF mode etc. and this is called "modulation".

Acetronics2
- 29th January 2006, 12:29
Hi, sayzer

As you're asking, it's a pleasure ...

Just insert a resistor between your Emitter and ground, and the transistor collector current will simply follow linearly ( w. a little offset due to Vbe ...) the PWM value ...
An U/I converter w/ OpAmp could wipe off this offset, if needed ...

No, no, don't send flowers ....

Alain