PDA

View Full Version : pulse width modulation



cheznez
- 10th February 2005, 18:47
Hey, thanks for taking the time to help.

here is what i am trying to do:
1. take the average of 10 voltage readings while the green light is on, then average it...then do the same with the red light.
2. I am then taking the ratio of the 2.

i am taking measurements infinitely

after i find my first ratio, i need to output the ratio as a pulse width modulation signal. the problem is, i want to continue the PWM while i am taking my next measurement. is there a way to send out the pwm while i am doing other things such as blinking lights and making calculations. here is what i have so far:

'Define ADCIN parameters
Define LOADER_USED 1
'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



G var PORTD.0
'CS var PORTA.1 'chip select sensors
R var PORTD.1
Green var byte
Red var byte
pH var byte
x var byte
out var byte
x=0
' TRISA = %00100001 'sets port A to inputs/outputs
' TRISC = %00000000 'sets port C to inputs/outputs
'ADCON1 =0
reset:
x=0
green_loop:

high g ' Turn green LED on/red off
low r
pause 50
' ADCin 0,green 'a/d converter
ph=ph+green 'voltage measured 10x and added into one number
x=x+1
if x<10 then green_loop
x=0
green=ph/10 'averages Green reading
ph=0


red_loop:
low g 'Turn red LED on/green off
high r
pause 50
' ADCin 0,red 'a/d converter
ph=ph+red 'voltage measured 10x and added into one number
x=x+1
if x<10 then red_loop
red=ph/10 'averages reading

out=green/red 'ration of green/red
ph=0
pwm PORTD.2,out,500 'outputs ratio as PWM signal
goto reset

Thanks, David

mister_e
- 10th February 2005, 19:03
the real solution is to have a PIC who have a internal PWM +adc module like PIC16F877 and how many other.. PIC12F683 have one PWM + ADC.

Using HPWM is the statement you need instead of PWM


OR using a pic who have only ADC an use Internal timer to generate your PWM.

cheznez
- 10th February 2005, 19:25
i am using the pic16f877, and i tried using the hpwm command. i am getting the error "twc not found in macro file". i cannot seem to find anything on the hpwm command.

Bruce
- 10th February 2005, 20:25
This error is returned when you try using the HPWM command on a port pin that's not associated with the internal hardware PWM module.

If you use something like this, it returns a similar error message.

HPWM PORTD.2,out,500 'outputs ratio as PWM signal

If you're using the PBP HPWM command, then you have to use the PIC's internal hardware PWM outputs.

For the 16F877;

TRISC.2 = 0 ' Make RC2 an output

HPWM 1,out,500 'outputs ratio as PWM signal on RC2.

or

TRISC.1 = 0 ' Make RC1 an output

HPWM 2,out,500 'outputs ratio as PWM signal on RC1.

cheznez
- 10th February 2005, 21:49
thanks to both of you, it is working

cheznez
- 15th February 2005, 21:53
I cant get the hpwm command to work with a pic16F684. It has capibility to do it. Here is my code.

option_16f684


TRISC=0

hpwm 1,127, 2000


Shouldnt this output a pulse?

mister_e
- 15th February 2005, 22:14
If you're using the internal oscillator, you must add this line at the top of your code

OSCCON = $70 'use 8MHZ

OR

OSCCON = $60 ' use internal 4MHZ

in case you don't set this register, you'll run @32khz... at this speed. i'm not sure PWM will work as you want

also be sure your RA.3 is correctly tie to vcc if you didn't set the MCLR OFF fuse.

cheznez
- 16th February 2005, 14:24
yes, using the 4 MHz worked.

Thanks Steve

mister_e
- 16th February 2005, 17:14
Great to know it's working. NEXT! ;)