• AVR Freaks

Hot!PWM signal in PIC18F24K40

Author
AMPS
Super Member
  • Total Posts : 443
  • Reward points : 0
  • Status: offline
2019/09/19 21:09:50 (permalink)
0

PWM signal in PIC18F24K40

I am using MPLAB X ide V5 .15 using Xc8 compiler driver 2 .0.
I wanted to used PWM signal for driving Buzzer . I need some help to create pattern in XC8 compiler. Let me know how it can be done.
CCP1 out is waveform i got from after setting 1 ms  700ms on time and 200ms off time.
 
void main(void)
{
    // Initialize the device
    SYSTEM_Initialize();

    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
    // Use the following macros to:

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    while (1)
    {
        PWM1_LoadDutyValue(100);
    }
}

Attached Image(s)


Amps
*.*.*.*.*.*.*.*.*.*.*.*.*
#1

11 Replies Related Threads

    ric
    Super Member
    • Total Posts : 23893
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PWM signal in PIC18F24K40 2019/09/19 21:33:23 (permalink)
    0
    If you want 700ms on and 200ms off, then your period is 900ms, not 1ms.
    I suspect you really want the PWM to generate an audible frequency, in which case you need much shorter on and off times.
    Do you just want to output a square wave (which can be done directly by a peripheral), or do you want a sine wave?
    A sine wave requires code to actively feed data to the PWM peripheral to output the sine data.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    AMPS
    Super Member
    • Total Posts : 443
    • Reward points : 0
    • Status: offline
    Re: PWM signal in PIC18F24K40 2019/09/19 23:18:02 (permalink)
    0
    I have one model . In that model creating waveform like below.
    Can you tell me what i need to do generate waveform
    https://www.murata.com/en-sg/support/faqs/products/sound/sounder/char/sch0001
     
    Ckt reference as above.
     

    Attached Image(s)


    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #3
    ric
    Super Member
    • Total Posts : 23893
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PWM signal in PIC18F24K40 2019/09/19 23:28:03 (permalink)
    0
    You picture is too blurred to see what is happening during the active time.
    If it's just a square wave, then you need to setup a timer to generrate the required frequency, then write code to turn it off and on at the required intervals.
    The peripheral is not going to do this by itself, you have to write code!
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    AMPS
    Super Member
    • Total Posts : 443
    • Reward points : 0
    • Status: offline
    Re: PWM signal in PIC18F24K40 2019/09/20 02:04:59 (permalink)
    0
    In above waveform .
    I have on time 2ms and off time 1 ms 
    During on time I would like to send burst of on signal and off time it should be idle.
    Can someone guide me how it can be done
     

    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #5
    pcbbc
    Super Member
    • Total Posts : 1332
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: PWM signal in PIC18F24K40 2019/09/20 02:42:08 (permalink)
    0
    So you want carrier 2ms ON and 1ms OFF = 333Hz
    And then you want to modulate that carrier 700ms ON and 200ms OFF?
     
    Assuming you do not need an absolutely exact number of the carrier cycles to make up your 700 and 200ms, set up a timer interrupt...
     
    When the interrupt occurs:
    1. turn the carrier PWM on at 333Hz
    2. Set the next timer interrupt for 700ms
     
    When the next interrupt occurs:
    1. Turn the carrier PWM off
    2. Set the next timer interrupt for 200ms
     
    Alternatively set a recurring interrupt for 100ms and count 7 and 2 timer ticks between turning on and off.
    post edited by pcbbc - 2019/09/20 02:45:00
    #6
    AMPS
    Super Member
    • Total Posts : 443
    • Reward points : 0
    • Status: offline
    Re: PWM signal in PIC18F24K40 2019/09/20 02:54:05 (permalink)
    0
    As said It require 2 timer Interrupt and 1 CCP PWm module.
     
    examples:
    Timer1_Interrupt :Could able to generate 2ms ON/off time
    Timer2_Interrupt : could able to generate 1ms on/off time
    How can i send PWM carrier in code to 2ms On time.
     
    This how look in time diagram. Is there other simple way to generate using this.

    Attached Image(s)


    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #7
    ric
    Super Member
    • Total Posts : 23893
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PWM signal in PIC18F24K40 2019/09/20 03:30:50 (permalink)
    0
    You use the PWM to output the  2ms ON, 1ms OFF, then code to enable this and disable it for the required times.
    pcbbc already just explained how to do that.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    AMPS
    Super Member
    • Total Posts : 443
    • Reward points : 0
    • Status: offline
    Re: PWM signal in PIC18F24K40 2019/09/20 04:16:00 (permalink)
    0
    my question here. The setting to made PWM work is correct  As explained in Thread 1 or not.

    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #9
    ric
    Super Member
    • Total Posts : 23893
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PWM signal in PIC18F24K40 2019/09/20 04:25:32 (permalink)
    0
    No it's not correct.
    If you want 1ms on, 2ms off, then your period is 3ms, not 1ms. Set the TIMER for a 3ms rollover.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    AMPS
    Super Member
    • Total Posts : 443
    • Reward points : 0
    • Status: offline
    Re: PWM signal in PIC18F24K40 2019/09/20 20:35:56 (permalink)
    0
    I have made some changes . Can someone guide me how i can fit exact no of pulse.
    I could able to generate burst pulse of particular length. Is there any setting to change 

    #include "mcc_generated_files/mcc.h"

    unsigned char Counter=0;


    void Buzzer_tone()
    {
     if(Counter>1)
        {
            PWM1_LoadDutyValue(100);
          Counter=0;
            
        }else
        {
       PWM1_LoadDutyValue(0);
            
        }
        
        
    }


    void Timer_Routine()
    {
        if(PIR0bits.TMR0IF==1)
        {
        PIR0bits.TMR0IF = 0;
        Counter++;
        Buzzer_tone();
        }
    }



    void main(void)
    {
        // Initialize the device
        SYSTEM_Initialize();

        // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
        // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
        // Use the following macros to:

        // Enable the Global Interrupts
        INTERRUPT_GlobalInterruptEnable();

        // Disable the Global Interrupts
        //INTERRUPT_GlobalInterruptDisable();

        // Enable the Peripheral Interrupts
        INTERRUPT_PeripheralInterruptEnable();

        // Disable the Peripheral Interrupts
        //INTERRUPT_PeripheralInterruptDisable();

        while (1)
        {
                  
        }
    }


    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #11
    ric
    Super Member
    • Total Posts : 23893
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PWM signal in PIC18F24K40 2019/09/20 23:30:43 (permalink)
    0
    What do you expect that code to do?
    As I read it, you're setting the PWM to maximum duty cycle for one timer interval, then zero duty cycle for ever more.
    That hardly seems to meet your previously declared requirements.
    Surely you want about 33% duty cycle for 200ms, then stop the PWM for 700ms, then loop and repeat.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    Jump to:
    © 2019 APG vNext Commercial Version 4.5