1 Continuous MCPWM at a time


Closed Thread
Results 1 to 40 of 47

Hybrid View

  1. #1
    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

  2. #2
    Join Date
    Jul 2011
    Posts
    29


    Did you find this post helpful? Yes | No

    Default Re: 1 Continuous MCPWM at a time

    Hi bruce,

    Thank you for your quick reply, and yes apparently this chip is pretty complicated but well I do not mind it's challenging.
    I have tried your code, changing the register "PTPERH" seems to do changing, I now have around 66% duty cycle.

    Now i'm looking at the datasheet of the pic page 187, and I am wondering one thing and maybe it has to do something with my duty cycle problem.
    Figure 18-11 shows the duty cycle comparison. Now PTMR(H and L) has 14 bits with 2 reserved for a clock and PDC(H and L) register has 14 bits as well.
    However, tell me if I am wrong but the analog digital converter module only operate on 10 bit. Would this be one of the reason ?

    If I could figure this out, it'd be amazing.. Thank you again for your help, I really appreciate !

  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

    You're getting the picture..

    12kHz you tried before bumped it up over 10-bit resolution. If you're stuck with 10-bit A/D just figure out a frequency with 10-bit resolution.

    I think 12kHz worked out to around 10.7-bit, but it was enough to shift it up >10-bits.
    Regards,

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

  4. #4
    Join Date
    Jul 2011
    Posts
    29


    Did you find this post helpful? Yes | No

    Default Re: 1 Continuous MCPWM at a time

    My oscillator is 20MHz, and my PWM can't be over 15 KHz frequency.
    If I understand this right, (with the formula resolution page 185, equation 18-4), to have a 10 bit resolution to match the analogue/digital module, I probably need to change the value of my oscillator ? (as I don't want a PWM frequency above 14KHz).

  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

    You should be able to divide the frequency by setting it up for Center-Aligned mode. Try my example for 19.455kHz, change PTCON0 = 0 to PTCON0 = 2.

    That should give you roughly 9.7kHz, and let you keep the 10-bit resolution.

    At least that's my guess after reading the note just above table 18-2. Seems like it would work just fine, and you can keep the 20MHz osc. If this freq will work for you?
    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

    yes, that would work, thank you, I will try that first thing in the morning tomorrow I will, of course, let you know.
    Thanks again for the help, really appreciate !

  7. #7
    Join Date
    Jul 2011
    Posts
    29


    Did you find this post helpful? Yes | No

    Default Re: 1 Continuous MCPWM at a time

    I have tried this, and it is still at 68% duty cycle maximum... I also tried with a 4MHz oscillator and PTCON0 = 0, and it gives also 68%.
    What are we missing ?
    Getting desesperate as I thought this would work but it seems it's not really the problem.. is it ?

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