PDA

View Full Version : Software PWM Problems



surfer0815
- 27th November 2005, 16:24
Hi! want to read the 8 Bit AD of the 12F675 an put it out as PWM with duty cyle between 0-100%.
My Programm is working well, but when AD gets 5V, I get Pulses out with 400ĩS Pause. But at AD 5 V it should be permanently on, without Pulses
I donīt know whats the problem.
Here Is my Programm, I attached the Scope Picture.

Define OSCCAL_1K 1 ' Calibrate internal oscillator

' Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS


LED Var GPIO.0 ' LED is bit 0, PIN 7

adval var word ' Create adval to store result
i var word

ANSEL = %00111000 ' Set AN3 analog, rest digital
CMCON = 7 ' Analog comparators off

loop:
ADCIN 3, adval ' Read channel 3 to adval
i = 256 - adval
I = i * 10
Pulsout LED,adval ' Puls is 2,56 ms
Pauseus i 'cycle = 5,12ms = 195 Hz

Goto loop ' Do it forever

End

Hope, Someone could help me!

Bruce
- 27th November 2005, 18:13
With an A/D input > 0V you will always have a pulseout "period" > 0. Grounding the A/D input should stop the output pulse.

surfer0815
- 27th November 2005, 19:06
I think you missunderstood something, the 12f675 hasnīt got a Hardware PWM, so I want to emulate it in Software! And my Problem is that I could not put the output permanently on, between 0 an 100% der must be pulses, but on 0% the output should have ground signal (thats working) and on 100% the output should have high signal, and thats the problem, it isnīt a countinously line it should be!

mister_e
- 27th November 2005, 19:20
loop:
ADCIN 3, adval ' Read channel 3 to adval
if adval<250 then
i = 256 - adval
I = i * 10
Pulsout LED,adval ' Puls is 2,56 ms
Pauseus i 'cycle = 5,12ms = 195 Hz
else
high led
endif

Goto loop ' Do it forever


If you can change your PIC, use the 12F683. It have an internal PWM module.

Charles Linquis
- 28th November 2005, 00:27
If you *REALLY* need smooth PWM out of your chip, it is possible.

One approach would be to set up a hardware timer to give you a constant period (say 100uSec), and then use an interleave-type A/D routine. You could do the conversions (and the math) on consecutive timer rollovers.

Using this technique you could get 8 bits of resolution and 0 to 100% duty cycle at 40Hz. Not great, but possibly usable.

HenrikOlsson
- 28th November 2005, 13:53
Hi,
(OK, just an idea here - worth a try)
With the AD result set to 8 bits your result will vary from 0 to 255. Try changing the line:


i = 256 - adval

to


i = 255 - adval

and see what happends. Even at full scale (adval=255) your i variable will be 10 if you subtract adval from 256. You will probably still get short glitches but hopefully you will end up closer to what you want.

/Henrik Olsson.