How to set HPWM to generate 38khz signal? (16F870)


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1,078

    Default How to set HPWM to generate 38khz signal? (16F870)

    Hello.

    I'm building custom IR control, I already wrote the code for ir command generation, but I want to use built in hardwre pwm of 16F870 chip to generate 38khz carrier. PBP supports up to 32khz. Also, I'd be very grateful, if some gives me ASM code insert which simply enables and disables hpwm with already set frequency and duty cycle. The PBP command HPWM does too many things together, like switching port and so on, so it is slow.

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: How to set HPWM to generate 38khz signal? (16F870)

    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: How to set HPWM to generate 38khz signal? (16F870)

    Thanks, so I need this part of code, right?

    Code:
    ASM
    _Pulse38
       bcf IRTX,PIN         ; 1uS, LED=on
       goto $+1             ; + 2uS = 3uS
       goto $+1             ; + 2uS = 5uS
       goto $+1             ; + 2uS = 7uS
       goto $+1             ; + 2uS = 9uS
       goto $+1             ; + 2uS = 11uS
       goto $+1             ; + 2uS = 13uS   
       bsf IRTX,PIN         ; 1uS, LED=on
       goto $+1             ; + 2uS = 3uS
       goto $+1             ; + 2uS = 5uS
       goto $+1             ; + 2uS = 7uS
       goto $+1             ; + 2uS = 9uS
       nop                  ; + 1uS = 10uS
       decfsz _Cycles,f     ; + 1uS = 11S    
       goto _Pulse38        ; + 2uS = 13uS
       return               ; Return to caller    
    ENDASM

  4. #4
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: How to set HPWM to generate 38khz signal? (16F870)

    and by the way, this code does nothing to built-in HPWM, right?

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: How to set HPWM to generate 38khz signal? (16F870)

    No need for assembly code.
    You want a PWM period of 1/38000 = 26.315us. Assuming 20MHz operating frequency your instruction cycle time is 200ns so you want a PWM period of 131-132 instruction cycles. Then you simply follow the instructions in the datasheet:
    8.4.3 SETUP FOR PWM OPERATION
    The following steps should be taken when configuring
    the CCP module for PWM operation:
    1. Set the PWM period by writing to the PR2 register.
    2. Set the PWM duty cycle by writing to the CCPR1L register and CCP1CON<5:4> bits.
    3. Make the CCP1 pin an output by clearing the TRISC<2> bit.
    4. Set the TMR2 prescale value and enable Timer2 by writing to T2CON.
    5. Configure the CCP1 module for PWM operation.
    Code:
    PR2 = 130                      ' 1) PWM period of 131 instruction cycles assuming 1:1 prescaler
    CCPR1L = 65                  ' 2) Dutycycle of ~50%, no need to write 2 LSB for this.
    TRISC.2 = 0                   ' 3) Pin is output
    T2CON = %00000100      ' 4) 1:1 Prescaler, TMR2 ON
    CCP1CON = %00001100    ' 5) PWM mode enabled
    This sets up the carrier and enables it. To disable the carrier:
    Code:
    CCP1CON = 0
    Enable it again:
    Code:
    CCP1CON = 15
    I'm not 100% sure what the note in the datasheet says
    Note: Clearing the CCP1CON register will force
    the CCP1 PWM output latch to the default
    low level. This is not the PORTC I/O data
    latch.
    It either says that as long as you set PortC.2 to 0 the output pin will revert to 0 as soon as the CCP module is disabled (which is good in this case) OR it says that actual output pin is left in the state it was at the time the CCP module was disabled which means that it can be high or low and that you need to force it back down by doing PortC.2 = 0 after turning off the CCP module.
    (Untested, please verify against datasheet)

    /Henrik.

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: How to set HPWM to generate 38khz signal? (16F870)

    Dave
    Always wear safety glasses while programming.

Similar Threads

  1. How can I stop HPWM and then manually set the pin level?
    By gavinthacker in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th March 2016, 16:55
  2. 6 HPWM signal generation with PIC18F4431
    By pxidr84 in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 6th March 2013, 05:00
  3. 38Khz Modulator
    By Gord11 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 28th September 2011, 08:14
  4. HPWM signal transfered to an other pin!
    By lilimike in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 22nd April 2010, 19:25
  5. Generate a 120Khz signal using the pic16F877a
    By texas5 in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 11th September 2008, 21:59

Members who have read this thread : 2

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