How to setup 38kHz PWM with PIC18F1330


Closed Thread
Results 1 to 15 of 15

Hybrid View

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

    Default How to setup 38kHz PWM with PIC18F1330

    Hi there,

    I'm trying to setup a 38kHz PWM with a PIC18F1330.

    My circuit is as simple as possible for tests: a blinking "I'm alive" LED and a 8MHz crystal.

    Don't get it...

    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)
    PTPERL  = %11111111
    PTPERH  = %11111111
    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
    
    
    DEFINE OSC 8
    
    LED var PORTA.2
    'PORTB.1 is PWM1
    
    MAIN:
        TOGGLE LED
        PAUSE 500
        GOTO MAIN
    
    END
    What is actually the highest PWM frequency I can get with the 8MHz crystal?

    The DS is confusing me a lot :-(
    Roger

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    Quote Originally Posted by flotulopex View Post

    What is actually the highest PWM frequency I can get with the 8MHz crystal?

    The DS is confusing me a lot :-(
    Hi

    If you look on page 129 of the data sheet. There is a chart of Fosc vs. PWM frequency.
    They do not list 8mhz, but the highest 4mhz frequency is 3.9khz. So I guess you can expect
    it to be double at 8mhz or 7.8Khz

    If you configure the oscillator for 4x HSPLL you could get closer.

    8mhz crystal 4x HSPLL - PWM freq = 31.2khz max

    Did the PWM output anything, you didn't say?
    Last edited by mark_s; - 5th March 2015 at 17:54.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,610


    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.

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    Henrik,

    Thanks for correcting my wrong answer.

  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

    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

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


    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.

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