1 Continuous MCPWM at a time


Closed Thread
Results 1 to 40 of 47

Hybrid View

  1. #1
    Join Date
    Jul 2011
    Posts
    29


    Did you find this post helpful? Yes | No

    Default Re: 1 Continuous MCPWM at a time

    Hello mark_s,

    Thank you for your quick reply, it is exactly what I was looking for.

    I Know need to adjust my pwm duty cycle with a potentiometer 1K on AN0.
    Is 1K enough ?

    Can someone help me to understand the process with that A/D module ?
    I'm not sure i'm understanding quite well the process.

  2. #2
    Join Date
    Jul 2011
    Posts
    29


    Did you find this post helpful? Yes | No

    Question

    Hello, it's me again.
    I went through the registers in the datasheet and used the example of Bruce here : http://www.picbasic.co.uk/forum/showthread.php?t=6768
    I tried to understand and apply it to my case, but it's not doing anything my duty cycle ?

    Thank you in advance for your help, it'd be really appreciated

    Code:
    Duty VAR Word
    
    TRISA  = %11111111
    TRISB  = %00000000
    TRISC  = %00000010
    TRISD  = %00000000
    TRISE  = %00000000 
    
     ' Analog setup
        ' Single shot, single channel, Group A is taken and converted, A/D converter enabled 
        ADCON0 = %00000001
        
        ' 
        ADCON1 = %00010000
        
        ADCON2 = %11001111   ' Right justified, 24 Tad, Frc
        ADCON3 = 0           ' Disable all triggers
        ADCHS = 0            ' Channel select for AN0
        ANSEL0 = %00000001   ' AN0 analog input, rest digital
        
        
    
    PORTB = 0
    PORTD = 0 ' clear port
    
    
    PTCON0=$00 'timebase
    PTPERL=$A0 ' $01A0 = 12kHz
    PTPERH=$01
    
    PWMCON0 = %01001111
    PWMCON1=%00000001
    DTCON=$00
    OVDCOND = %00000000
    OVDCONS = %00000000 'PORT PINS
    
    PDC0L=$00
    PDC0H=$00
    PDC1L=$00
    PDC1H=$00
    PDC2L=$00
    PDC2H=$00
    
    PDC0L = Duty.LowByte
    PDC0H = Duty.HighByte
    
    PDC1L = Duty.LowByte
    PDC1H = Duty.HighByte
    
    PDC2L = Duty.LowByte
    PDC2H = Duty.HighByte
    
    
    PTCON1=%10000000
    
    
     
    GoTo Main
    
    Main:
        ADCON0.1 = 1         ' Start the conversion
        While ADCON0.1=1     ' Wait for it to complete
        Wend
    
        Duty.HighByte = ADRESH ' get result from AN0
        Duty.LowByte = ADRESL  ' Increment buffer pointer
    
        OVDCOND = %00000010 'PWM on PWM1

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default Re: 1 Continuous MCPWM at a time

    Is this all of your code? If it is, can you explain what you "think" should be happening, and how you know it's not?

    Folks here love to help, but what you're showing here doesn't indicate even a modest amount of homework..;o)
    Regards,

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

  4. #4
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: 1 Continuous MCPWM at a time

    If it is the entire code, maybe looping back to main would help? Otherwise I agree with Bruce.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default Re: 1 Continuous MCPWM at a time

    Something like this might help so it doesn't drop-off the end and restart from the beginning. It also loads the new duty-cycle values.

    Code:
    PTCON1=%10000000
    OVDCOND = %00000010 'PWM on PWM1
     
    Main:
       ADCON0.1 = 1         ' Start the conversion
       While ADCON0.1=1     ' Wait for it to complete
       Wend
       Duty.HighByte = ADRESH ' get result from AN0
       Duty.LowByte = ADRESL  ' Increment buffer pointer
       PDC0L = Duty.LowByte
       PDC0H = Duty.HighByte
       PDC1L = Duty.LowByte
       PDC1H = Duty.HighByte
       PDC2L = Duty.LowByte
       PDC2H = Duty.HighByte
       GoTo Main
    It's untested, but should work. I didn't check the A/D part, but it looks ok at a glance.

    You might want to insert a PAUSE before the GOTO Main?
    Last edited by Bruce; - 13th July 2011 at 12:46.
    Regards,

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

  6. #6
    Join Date
    Jul 2011
    Posts
    29


    Did you find this post helpful? Yes | No

    Default Re: 1 Continuous MCPWM at a time

    Hello,

    Thank you very much for your answers guys ! For you that code is nothing but for me it is what I wrote after spending hours on the forum and on the datasheet... trying to understand what i'm writing. So I am sorry if you feel that way..
    Bruce, thank you for putting in the right order my code, it is working now but somehow when my potentiometer is at its maximum i'm only able to have 50% duty cycle ? Where does this come from ?

  7. #7
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default Re: 1 Continuous MCPWM at a time

    We're just messing with you when you miss something simple. Don't take any of it personal..

    When you forget to loop back to Main it runs off the end and loops back to the start. But that wasn't the only problem.

    PCPWM on these can be a bit intimidating. Give this a shot.
    Code:
    DEFINE OSC 20
     
    Duty VAR Word
    TRISA  = %11111111
    TRISB  = %00000000
    TRISC  = %00000010
    TRISD  = %00000000
    TRISE  = %00000000 
     
     ' Analog setup
     ' Single shot, single channel, Group A is taken and converted, A/D converter enabled 
     
    ADCON0 = %00000001    
    ADCON1 = %00010000  
    ADCON2 = %11001111   ' Right justified, 24 Tad, Frc
    ADCON3 = 0           ' Disable all triggers
    ADCHS = 0            ' Channel select for AN0
    ANSEL0 = %00000001   ' AN0 analog input, rest digital
     
    PORTB = 0
    PORTD = 0  ' clear port
    PTCON0 = 0 ' timebase
    PTPERL = 0 ' ~19.4kHz at 20MHz
    PTPERH = 1
    PWMCON0 = %01001111
    PWMCON1 = %00000001
    DTCON = 0
    OVDCOND = 0
    OVDCONS = 0 
    PTCON0 = 0
    PTCON1 = %10000000
    OVDCOND = %00000010 'PWM on PWM1
     
    Main:
       ADCON0.1 = 1         ' Start the conversion
       While ADCON0.1=1     ' Wait for it to complete
       Wend
       Duty.HighByte = ADRESH ' get result from AN0
       Duty.LowByte = ADRESL  ' Increment buffer pointer
       PDC0L = Duty.LowByte
       PDC0H = Duty.HighByte
       PDC1L = Duty.LowByte
       PDC1H = Duty.HighByte
       PDC2L = Duty.LowByte
       PDC2H = Duty.HighByte
       PAUSE 500
       GoTo Main
    All-in-all I would say you're doing pretty darn good just getting started with one of the hardest to use/understand PIC types in this range....
    Regards,

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

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