Infrared HPWM setup


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    Oct 2010
    Posts
    411

    Default Infrared HPWM setup

    Dear all,

    just a quick question. I didnt want to open a new thread but most of the Infrared threads are closed.

    I would like to set up the PIC18F26k22 in order to use HPWM for the 38khz IR.

    First of all i would like to ask if i can use 64Mhz as the main PIC frequency.

    i tried to use mister e's pic multi-calc and set 64Mhz. The following are the results i got.

    Name:  HPWM.jpg
Views: 831
Size:  47.6 KB

    but from the calculations i made:

    (64Mhz / (4*TMR2 prescale value * 38Khz))-1 = PR2
    (64Mhz / (4*1*38Khz)) - 1 = PR2
    (64Mhz / (152000)) - 1 = PR2 <=>

    Code:
                64,000,000
    <=> PR2 =  ------------ -1 <=>
                 152,000
    <=> PR2 = 420

    Also calculating the bits:

    Code:
                  log(FOSC / FPWM)
     Resolution = ------------------- bits
                      Log(2)
    log(64,000,000 / 38,000) = 3.226

    log(2) = 0.301

    so we have:
    3.226
    ------- = 10.17 ~ 10 bits
    0.301

    and calculating the CCPRL1:

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

    So i set in the code the following:

    TRISC.2 = 0 ' CCP1 (PortC.2 = Output)
    PR2 = 420 ' Set PWM Period for approximately 38KHz
    CCPR1L = 210 ' Set PWM Duty-Cycle to 50%
    T2CON = %00000100 ' Timer2 ON + 1:1 prescale --> i know this might not be right as at 64Mhz there is no 1:1 scale based on mister's e multi-calc.
    CCP1CON = %00001100 ' Mode select = PWM

    could you please help me to clear things up?

    I dont mind using 16Mhz as a base frequency, but still the calculations i get are not match with mister's e multi-calc.

    Thanks for your help in advance.
    Last edited by astanapane; - 31st May 2023 at 22:16.

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,397


    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

  3. #3
    Join Date
    Oct 2010
    Posts
    411


    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: 740
Size:  90.0 KB

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,397


    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

  5. #5
    Join Date
    Oct 2010
    Posts
    411


    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.

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,397


    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

  7. #7
    Join Date
    Oct 2010
    Posts
    411


    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.

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 : 10

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