Infrared HPWM setup


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,666


    2 out of 2 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: Infrared HPWM setup

    prescaler is 4 , 16 is also a possibility
    (64Mhz / (4*TMR2 prescale value * 38Khz))-1 = PR2
    (64Mhz / (4*4*38Khz)) - 1 = PR2
    (64Mhz / (152000*4)) - 1 = PR2 <=>


    Code:
    64,000,000
    <=> PR2 = ------------ -1 <=>
    152,000*4
    <=> PR2 = 104

    (PR2 + 1) * TMR2 prescale * 50% Duty Cycle = value for CCPRL1, or
    (104+ 1) * 4 * 0.50 = 421 * 0.50 = 210

    T2CON = %00000101

    PR2 = 104 ' Set PWM Period for approximately 38KHz
    CCPR1L = 210>>2 ' Set PWM Duty-Cycle to 50%

    note
    PR2 is a 8bit reg it cannot ever be 420 or anything larger than 255
    CCPR1L are the upper 8 bits of the duty cycle word value
    CCP1CON bit 4 and 5 are the lower two bits

    Warning I'm not a teacher

  2. #2
    Join Date
    Oct 2010
    Posts
    413


    Did you find this post helpful? Yes | No

    Default Re: Infrared HPWM setup

    Hi Richard,

    thanks for the clarification and answer.

    I found in the manual what you said regarding the Resolution bits.

    Name:  Resolution.jpg
Views: 1976
Size:  90.0 KB

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,666


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: Infrared HPWM setup

    the duty cycle is a 10 bit value at best for that chip, not all frequencies can have the full 10bit range available
    it totally depends on choices made with regard to prescaler and period.
    no matter what bit resolution is possible the CPRxL value is the upper 8 bits of the duty cycle word valueCCP1CON bit 4 and 5 are the lower two bits
    Warning I'm not a teacher

  4. #4
    Join Date
    Oct 2010
    Posts
    413


    Did you find this post helpful? Yes | No

    Default Re: Infrared HPWM setup

    i've also noticed you've shifted 2 bits on the right the CCPR1L

    CCPR1L = 210>>2 ' Set PWM Duty-Cycle to 50%

    So the CCPR1L value will be 52.

    (11010010) 210>>2 = 00110100 = 52

    why have we shifted?
    Last edited by astanapane; - 1st June 2023 at 08:01.

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,666


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: Infrared HPWM setup

    why have we shifted?
    CPRxL value is the upper 8 bits of the duty cycle word valueCCP1CON bit 4 and 5 are the lower two bits



    (011010010) 210>>2 = 00110100 = 52 ;high 8bits
    (011010010) 210 & 3 = 00000010 = 2 ;low 2 bits

    00000010<<4 = 00100000
    CCP1CON = 00100000 |12 to set low bits in pwmmode

    C version from microchip
    Code:
    void PWM1_LoadDutyValue(uint16_t dutyValue)
    {
       // Writing to 8 MSBs of pwm duty cycle in CCPRL register
        CCPR1L = ((dutyValue & 0x03FC)>>2);
        
       // Writing to 2 LSBs of pwm duty cycle in CCPCON register
        CCP1CON = ((uint8_t)(CCP1CON & 0xCF) | ((dutyValue & 0x0003)<<4));
    }
    Last edited by richard; - 1st June 2023 at 08:07.
    Warning I'm not a teacher

  6. #6
    Join Date
    Oct 2010
    Posts
    413


    Did you find this post helpful? Yes | No

    Default Re: Infrared HPWM setup

    thanks Richard. now is clear.

    i will get back once i have a working code and establish an IR communication.

  7. #7
    Join Date
    Oct 2010
    Posts
    413


    Did you find this post helpful? Yes | No

    Default Re: Infrared HPWM setup

    with the following configuration i got this on the scope. So i guess we are in a good path.

    To mention that the main OSC is at 64Mhz using HPWM on the PIC18f26K22 on portc.2

    Code:
    TRISC.2 = 0          ' CCP1 (PortC.2 = Output)
    PR2 = 104            ' Set PWM Period for approximately 38KHz
    CCPR1L = 52          ' Set PWM Duty-Cycle to 50% (011010010) 210>>2 = 00110100 = 52
    T2CON = %00000101    ' Timer2 ON + 4 prescale
    CCP1CON = %00001100  ' Mode select %00001100 = PWM
    Name:  38khz frequency.jpg
Views: 1865
Size:  403.1 KB

    all thanks to Richard.

    now im going on the next step.

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,666


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: Infrared HPWM setup

    very good.
    you could set the lower two bits of the Duty-Cycle word [CCP1CON bits 4:5] to get the duty cycle
    even closer to exactly 50%, if you wanted to
    CCP1CON = %00101100
    Warning I'm not a teacher

Similar Threads

  1. Replies: 3
    Last Post: - 23rd October 2011, 12:53
  2. InfraRed Data Com
    By rayzrocket in forum Off Topic
    Replies: 5
    Last Post: - 29th March 2010, 15:42
  3. Infrared x PIC
    By ewandeur in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 29th December 2009, 18:30
  4. infrared help
    By griffin in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th December 2008, 05:34
  5. infrared
    By bmohnsen in forum General
    Replies: 1
    Last Post: - 2nd May 2007, 16:35

Members who have read this thread : 4

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