How to setup 38kHz PWM with PIC18F1330


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    Hi,
    If you set PTPER to 51 with the prescaler set to 1:1 and 8MHz clock you'll get a PWM period of 26us which 38461Hz, set it to 52 and you'll get 37736Hz.

    Just remember that the resolution won't be 14 or even 10 bits at that PWM frequency and oscillator frequency. Forumla says you'll get 7.7bits which would be a dutycycle value of 0 to 246 or there abouts.

    /Henrik.

    EDIT: Oh, and the highest frequency possible would be when you set PTPER to 1 which would give you a PWM frequency of 1MHz. The resolution will be 2 bits.
    Last edited by HenrikOlsson; - 5th March 2015 at 18:31.

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    Henrik,

    Thanks for correcting my wrong answer.

  3. #3
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    Thank you.

    I'll try this out when I'm back home.

    Did the PWM output anything, you didn't say?
    Yes, it does work. With the code in my first post, I get a PWM signal around 430Hz (not sure about that frequency right now).
    Roger

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    Hi,

    The PWM Period register has 12 bits of resolution and the PWM period is defined as (PTPER+1) * Prescaler / (Fosc/4)

    In your original code you have the prescaler set to 1:1 and you're setting PTPER to 65535 but again, only the low 12 bits are used which equals 4095. Plug that into the formula and you'll get 4096/2000000=0.002048. That's a PWM period of 2.048ms or a frequency of 488.28Hz.

    /Henrik.

  5. #5
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    I'm modified my code as follows (in bold) and unfortunately, I doesn't work at all now (now PWM signal).
    Code:
    ' Fuses PIC18F1330 for MPASM
    @ __CONFIG _CONFIG1H, _OSC_HS_1H     & _FCMEN_OFF_1H   & _IESO_OFF_1H
    @ __CONFIG _CONFIG2L, _PWRT_OFF_2L   & _BOR_OFF_2L     & _BORV_0_2L
    @ __CONFIG _CONFIG2H, _WDT_OFF_2H    & _WDTPS_512_2H
    @ __CONFIG _CONFIG3L, _HPOL_HIGH_3L  & _LPOL_HIGH_3L   & _PWMPIN_OFF_3L
    @ __CONFIG _CONFIG3H, _FLTAMX_RA7_3H & _T1OSCMX_LOW_3H & _MCLRE_OFF_3H
    @ __CONFIG _CONFIG4L, _STVREN_ON_4L  & _BBSIZ_BB256_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    @ __CONFIG _CONFIG5L, _CP0_OFF_5L    & _CP1_OFF_5L
    @ __CONFIG _CONFIG5H, _CPB_OFF_5H    & _CPD_OFF_5H
    @ __CONFIG _CONFIG6L, _WRT0_OFF_6L   & _WRT1_OFF_6L
    @ __CONFIG _CONFIG6H, _WRTC_OFF_6H   & _WRTB_OFF_6H    & _WRTD_OFF_6H
    @ __CONFIG _CONFIG7L, _EBTR0_OFF_7L  & _EBTR1_OFF_7L
    @ __CONFIG _CONFIG7H, _EBTRB_OFF_7H    
    
    '==========================================================================
    ' Main Registers
    '          76543210
    'OSCCON  = %00000000 'OSCILLATOR CONTROL REGISTER
    'OSCTUNE = %00000000 'OSCILLATOR TUNING REGISTER
    ADCON0  = %00000000 'A/D CONTROL REGISTER 0
    ADCON1  = %00000000 'A/D CONTROL REGISTER 1
    ADCON2  = %00000000 'A/D CONTROL REGISTER 2
    INTCON  = %00000000 'INTERRUPT CONTROL REGISTER
    INTCON2 = %00000000 'INTERRUPT CONTROL REGISTER 2
    INTCON3 = %00000000 'INTERRUPT CONTROL REGISTER 3
    TRISA   = %00000000 'Data Direction Control Register (Input/Output)
    PORTA   = %00000000 'State High (1) or Low (0)
    TRISB   = %00000000 'Data Direction Control Register (Input/Output)
    PORTB   = %00000000 'State High (1) or Low (0) 
    
    ' Set up PWM1 (PORTB.1)
    T0CON   = %00000000
    T1CON   = %01001010
    PTCON0  = %00000000 'Postscale 1:1, Fosc/4 Prescale 1:1, Free Running Mode
    PTCON1  = %10000000 'Time base in ON, Time base counts up
    PWMCON0 = %00010000 'PWM1 enabled
    PDC0H   = 30        'Duty Cycle control for PWM0/1
    PTMRL   = %00000000
    PTMRH   = %00000000
    PTPERL  = %00110011 ' = 51
    PTPERH  = %00000000
    
    DEFINE OSC 8
    
    LED var PORTA.2
    'PORTB.1 is PWM1
    
    MAIN:
        TOGGLE LED
        PAUSE 500
        GOTO MAIN
    
    END
    Roger

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    Hi,
    Way to high duty cycle value (7680) try writing 30 to PDC0L instead.

    /Henrik.

  7. #7
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    Thanks a lot Henrik.

    It works now

    To get a nice "visual" (= visible on scope) close to 50% duty-cycle, I set PDC0L to 100.

    I now have to start to "play" with the formula(s) and registers to change values - I might come back for more questions

    Code:
    ' Set up PWM1 (PORTB.1)
    T0CON   = %00000000
    T1CON   = %01001010
    PTCON0  = %00000000 'Postscale 1:1, Fosc/4 Prescale 1:1, Free Running Mode
    PTCON1  = %10000000 'Time base in ON, Time base counts up
    PWMCON0 = %00010000 'PWM1 enabled
    PDC0L   = 100        'Duty Cycle control for PWM0/1
    PTMRL   = %00000000
    PTMRH   = %00000000
    PTPERL  = %00110011 ' = 51
    PTPERH  = %00000000
    Sorry to ask maybe a stupid question but what is the interrest of a PWM "resolution" parameter? I can understand resolution for an incoming analog signal, but PWM is only output as far as I know...

    Another thing; look at the pulsed signal, especially at the high state where the signal is kind of rippled. What's wrong here?
    Name:  20150306_465914_1280x960.jpg
Views: 903
Size:  136.7 KB
    Roger

Similar Threads

  1. 38Khz Modulator
    By Gord11 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 28th September 2011, 08:14
  2. manual hardware PWM setup on 16F1824
    By comwarrior in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 31st January 2011, 18:34
  3. Use Button For setup
    By tump in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 21st November 2007, 19:43
  4. Generated square wave 19Khz and 38Khz
    By blinkstar88 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th August 2007, 16:40
  5. pic12f683 setup
    By erice1984 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 4th July 2007, 11:48

Members who have read this thread : 0

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