pulse width modulation

1. ## pulse width modulation

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 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
reset:
x=0
green_loop:

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

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

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

Thanks, David

2. Did you find this post helpful? |
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.
Last edited by mister_e; - 10th February 2005 at 19:06.

3. Did you find this post helpful? |
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.

4. Did you find this post helpful? |
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.

5. Did you find this post helpful? |
thanks to both of you, it is working

6. Did you find this post helpful? |
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?

7. Did you find this post helpful? |
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.
Last edited by mister_e; - 15th February 2005 at 22:17.

8. Did you find this post helpful? |
yes, using the 4 MHz worked.

Thanks Steve

9. Did you find this post helpful? |
Great to know it's working. NEXT!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts