While loop on pwm module


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2012
    Posts
    24

    Default While loop on pwm module

    I want to incease the duty cycle until the volts on R4 is equal to three ( feedback voltage to the pic greater than 153) then at this volt i want to stop the duty cycle.
    Problems
    the duty cycle is increased but it didnt stop when R4 it reach volts, duty cycle it seems to go up to 100% and i dont want this to occur
    Please.! I need some one to help me on this task

    Code:
    clear
    DEFINE ADC_BITS 8
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50
    Feedback var word
    ADCON1=%00000010
    duty VAR WORD ' Duty cycle value (CCPR1L:CCP1CON<5:4>)
    TRISA = %11111111
    TRISB = %11111111
    TRISC = %11111011 ' Set PORTC.2 (CCP1) to output
    CCP1CON = %00001100 ' Set CCP1 to PWM 
    T2CON = %00000101 ' Turn on Timer2, Prescale=4
    LED1   VAR  PORTB.1
     adcin 0, feedback
     
    ' Use formula to determine PR2 value for a 1KHz signal, 
    ' 4MHz clock, and prescale=4. (4E6/(4*4*1E3))-1=249
       PR2 = 249      ' Set PR2 to get 1KHz out
            
    ' Use formula to determine CCPR1L:CCP1CON<5:4> value for
    ' ends of range 0% to 100%.  (249+1)*4*0=0 (0% value)
    ' (249+1)*4*1=1000 (100% value)
       duty =0      ' Set duty cycle to 20%
       
         
    while feedback>153        
       CCP1CON.4 = duty.0   ' Store duty to registers as
       CCP1CON.5 = duty.1   ' a 10-bit word
       CCPR1L = DUTY >> 2
       duty = duty + 1    ' Increase duty cycle
    ' Since the total sweep of duty is 1000 (1000-0) and 
    ' we are adding 1 for each loop, that results in 60
    ' steps min to max. 1 second divided by 60 = 1mS            
       Pause  1             ' Pause 1/60 of second
       
        wend
       
        End
    Doc1.pdf

  2. #2
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: While loop on pwm module

    You don't read ADC (feedback) whitin the while loop.

    Cheers

    Al.
    All progress began with an idea

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: While loop on pwm module

    Also if you're increasing the PWM duty cycle & expecting the ADC'ed voltage to rise until a target is met, then I think you'd want it arranged like this...

    while feedback<153 (in other words, while 'feedback' is less than 153 increase PWM duty cycle)

  4. #4
    Join Date
    Jan 2012
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: While loop on pwm module

    Thanx alot HankMSpank and arrati, i did what you said and my problems have been solved.
    I want to add something in this code, i want after the duty cycle being constant to giveout this duty cycle for 3ms and then to put off duty cycle for 1.5ms
    Please do me a favour for this too

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

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