pulse width modulation


+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    cheznez's Avatar
    cheznez Guest

    Default 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 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

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    cheznez's Avatar
    cheznez Guest


    Did you find this post helpful? Yes | No

    Default

    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. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    cheznez's Avatar
    cheznez Guest


    Did you find this post helpful? Yes | No

    Default

    thanks to both of you, it is working

  6. #6
    cheznez's Avatar
    cheznez Guest


    Did you find this post helpful? Yes | No

    Default

    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. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8
    cheznez's Avatar
    cheznez Guest


    Did you find this post helpful? Yes | No

    Default

    yes, using the 4 MHz worked.

    Thanks Steve

  9. #9
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Great to know it's working. NEXT!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Pulse Capture and byte building
    By boroko in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 21st July 2009, 01:59
  2. measure pulse width 0-150Hz
    By kewetman in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 15th November 2007, 21:38
  3. Pulse width modulation
    By manxman in forum General
    Replies: 2
    Last Post: - 2nd March 2006, 12:06
  4. PIC12f675 timer1 pulse width
    By ck1223 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 15th February 2006, 04:05
  5. Pulse Width Measurement
    By atomski in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 29th January 2005, 00:53

Posting Permissions

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