• AVR Freaks

Hot!Operating a buzzer using PWM in PIC18F26K83

Author
M.Ali
Starting Member
  • Total Posts : 64
  • Reward points : 0
  • Joined: 2018/11/12 00:36:07
  • Location: 0
  • Status: offline
2019/05/28 00:55:43 (permalink)
0

Operating a buzzer using PWM in PIC18F26K83

Hi,
I have connected a buzzer to the RC2 pin. i am trying to operate it through PWM . I am trying to generate the PWM using CCP1 module. I have followed the steps mentioned in the reference manual. But the buzzer doesnt work. Here are the buzzer details:
Buzzer Operaring Frequency - 2048 Hz
Buzzer Period                      - 0.48828125 ms
Fosc                                   - 64 MHz
PWM Duty Cycle                  - 50 %
 
Here is the code snippet :

// CONFIG1L
#pragma config FEXTOSC = OFF // External Oscillator Selection (Oscillator not enabled)
#pragma config RSTOSC = HFINTOSC_64MHZ// Reset Oscillator Selection (HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1)
// CONFIG1H
#pragma config CLKOUTEN = ON // Clock out Enable bit (CLKOUT function is enabled)
#pragma config PR1WAY = OFF // PRLOCKED One-Way Set Enable bit (PRLOCK bit can be set and cleared repeatedly)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
// CONFIG2L
#pragma config MCLRE = INTMCLR // MCLR Enable bit (If LVP = 0, MCLR pin function is port defined function; If LVP =1, RE3 pin fuction is MCLR)
#pragma config PWRTS = PWRT_64 // Power-up timer selection bits (PWRT set at 64ms)
#pragma config MVECEN = ON // Multi-vector enable bit (Multi-vector enabled, Vector table used for interrupts)
#pragma config IVT1WAY = ON // IVTLOCK bit One-way set enable bit (IVTLOCK bit can be cleared and set only once)
#pragma config LPBOREN = ON // Low Power BOR Enable bit (ULPBOR enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bits (Brown-out Reset enabled according to SBOREN)
// CONFIG2H
#pragma config BORV = VBOR_2P85 // Brown-out Reset Voltage Selection bits (Brown-out Reset Voltage (VBOR) set to 2.8V)
#pragma config ZCD = OFF // ZCD Disable bit (ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)
#pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config DEBUG = OFF // Debugger Enable bit (Background debugger disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Extended Instruction Set and Indexed Addressing Mode disabled)
// CONFIG3L
#pragma config WDTCPS = WDTCPS_7// WDT Period selection bits (Divider ratio 1:4096)
#pragma config WDTE = SWDTEN // WDT operating mode (WDT enabled/disabled by SWDTEN bit)
// CONFIG3H
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = LFINTOSC// WDT input clock selector (WDT reference clock is the 31.0 kHz LFINTOSC)
// CONFIG4L
#pragma config BBSIZE = BBSIZE_512// Boot Block Size selection bits (Boot Block size is 512 words)
#pragma config BBEN = OFF // Boot Block enable bit (Boot block disabled)
#pragma config SAFEN = OFF // Storage Area Flash enable bit (SAF disabled)
#pragma config WRTAPP = OFF // Application Block write protection bit (Application Block not write protected)
// CONFIG4H
#pragma config WRTB = OFF // Configuration Register Write Protection bit (Configuration registers (300000-30000Bh) not write-protected)
#pragma config WRTC = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
#pragma config WRTSAF = OFF // SAF Write protection bit (SAF not Write Protected)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (HV on MCLR/VPP must be used for programming)
// CONFIG5L
#pragma config CP = OFF // PFM and Data EEPROM Code Protection bit (PFM and Data EEPROM code protection disabled)
#include <xc.h>
void PWM();
void OSCILLATOR_Initialize();

void main()
{
OSCILLATOR_Initialize();
PWM();
}
void OSCILLATOR_Initialize(void)
{
// NOSC EXTOSC; NDIV 1;
OSCCON1 = 0x60;
// CSWHOLD may proceed; SOSCPWR Low power;
OSCCON3 = 0x00;
// MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
OSCEN = 0x40;
// HFFRQ 4_MHz;
OSCFRQ = 0x08;
// TUN 0;
OSCTUNE = 0x40;
}

void PWM()
{

RC2PPS = 0x09; /* Buzzer connected to RC2 */

TRISCbits.TRISC2 = 1;

T2PR = 977;

CCP1CON = 0x8C;

CCPR1L = 0x7A;

CCPR1H = 0x00;

CCPTMRS0 = 0x01;

CCPTMRS1 = 0x00;



PIR4bits.TMR2IF = 0;

T2CLK = 0x01;

T2CON = 0xB0;

T2RST = 0x01;

T2TMR = 977 ;

T2CONbits.ON = 1;

if(PIR4bits.TMR2IF == 1)
{
TRISCbits.TRISC2 = 0;
}
}

 
Are there any settings which I have missed ?
What might be the issue ?
 
 
 
Regards
Ali
post edited by M.Ali - 2019/05/28 00:59:08
#1

14 Replies Related Threads

    mpgmike
    Super Member
    • Total Posts : 159
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: online
    Re: Operating a buzzer using PWM in PIC18F26K83 2019/05/28 01:52:09 (permalink)
    0
    Do you have an o-scope to look at your output to the buzzer?  

    I don't need the world to know my name, but I want to live a life so all my great-grandchildren proudly remember me.
    #2
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Operating a buzzer using PWM in PIC18F26K83 2019/05/28 01:54:56 (permalink)
    0
    mpgmike
    Do you have an o-scope to look at your output to the buzzer?  




    Hi mpgmike,
    Yes. I have .
     
     
    Regards
    Ali
    #3
    mpgmike
    Super Member
    • Total Posts : 159
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: online
    Re: Operating a buzzer using PWM in PIC18F26K83 2019/05/28 02:02:52 (permalink)
    0
    Why would you want TRISC2 = 0 only if you trip a TMR2 flag?  Also, are you clearing the ANSELC bit?

     if(PIR4bits.TMR2IF == 1)
    {
    TRISCbits.TRISC2 = 0;
    }
    [/quote]

    I don't need the world to know my name, but I want to live a life so all my great-grandchildren proudly remember me.
    #4
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Operating a buzzer using PWM in PIC18F26K83 2019/05/28 02:18:17 (permalink)
    0
    mpgmike
    Why would you want TRISC2 = 0 only if you trip a TMR2 flag?  Also, are you clearing the ANSELC bit?

     
    Hi mpgmike,
    I am following as per the steps mentioned in the reference manual :
    6. Enable PWM output pin:
    • Wait until the Timer overflows and the
    TMR2IF bit of the PIR4 register is set.
    • Enable the CCPx pin output driver by
    clearing the associated TRIS bit.
     
    I haven't cleared the ANSELC bit.
     
    Regards
    Ali
    #5
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Operating a buzzer using PWM in PIC18F26K83 2019/05/28 02:20:14 (permalink)
    0
    mpgmike
    Do you have an o-scope to look at your output to the buzzer?  



    Hi mpgmike,
    I cannot see any wave at the output to the buzzer.
     
    Regards
    Ali
    post edited by M.Ali - 2019/05/28 02:23:24
    #6
    mpgmike
    Super Member
    • Total Posts : 159
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: online
    Re: Operating a buzzer using PWM in PIC18F26K83 2019/05/28 04:11:53 (permalink)
    0
    For the Peripheral Pin Select (PPS) there is an Unlock Sequence you haven't included.  You may not have PWM connected.

    RC2PPS = 0x09; /* Buzzer connected to RC2 */
    [/quote]

    I don't need the world to know my name, but I want to live a life so all my great-grandchildren proudly remember me.
    #7
    Aussie Susan
    Super Member
    • Total Posts : 3565
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: Operating a buzzer using PWM in PIC18F26K83 2019/05/28 19:21:26 (permalink)
    0
    The default reset value for the PPSLOCK bit is 0 which means the PPS registers are unlocked. Therefore you don't need to unlock (or lock) the PPS registers until you are sure you know what you are doing.
    This also applies to some of your CONFIG settings:
    - set PPS1WAY to OFF - if this is left to ON and you do end up locking the PP registers, then you can't unlock them again. Leave it off so you protect yourself against doing the wrong thin in this area. (Once the project is nearly complete, then you can look into locking the PPS registers to prevent accidental changes but you can also cause yourself a lot of headaches by prematurely locking them.)
    - never set the DEBUG option to anything - the IDE will set this bit as it is needed (release or debug build) and you setting might interfere.
    Susan
    #8
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Operating a buzzer using PWM in PIC18F26K83 2019/05/28 20:46:49 (permalink)
    0
    mpgmike
    For the Peripheral Pin Select (PPS) there is an Unlock Sequence you haven't included.  You may not have PWM connected.
     



    Hi mpgmike,
    I have included the PPS . But also I couldn't find any signal on the output pin to the buzzer.
    Here is the updated code :

    // CONFIG1L
    #pragma config FEXTOSC = OFF // External Oscillator Selection (Oscillator not enabled)
    #pragma config RSTOSC = HFINTOSC_64MHZ// Reset Oscillator Selection (HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1)
    // CONFIG1H
    #pragma config CLKOUTEN = ON // Clock out Enable bit (CLKOUT function is enabled)
    #pragma config PR1WAY = OFF // PRLOCKED One-Way Set Enable bit (PRLOCK bit can be set and cleared repeatedly)
    #pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
    #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
    // CONFIG2L
    #pragma config MCLRE = INTMCLR // MCLR Enable bit (If LVP = 0, MCLR pin function is port defined function; If LVP =1, RE3 pin fuction is MCLR)
    #pragma config PWRTS = PWRT_64 // Power-up timer selection bits (PWRT set at 64ms)
    #pragma config MVECEN = ON // Multi-vector enable bit (Multi-vector enabled, Vector table used for interrupts)
    #pragma config IVT1WAY = ON // IVTLOCK bit One-way set enable bit (IVTLOCK bit can be cleared and set only once)
    #pragma config LPBOREN = ON // Low Power BOR Enable bit (ULPBOR enabled)
    #pragma config BOREN = ON // Brown-out Reset Enable bits (Brown-out Reset enabled according to SBOREN)
    // CONFIG2H
    #pragma config BORV = VBOR_2P85 // Brown-out Reset Voltage Selection bits (Brown-out Reset Voltage (VBOR) set to 2.8V)
    #pragma config ZCD = OFF // ZCD Disable bit (ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)
    #pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
    #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
    #pragma config DEBUG = OFF // Debugger Enable bit (Background debugger disabled)
    #pragma config XINST = OFF // Extended Instruction Set Enable bit (Extended Instruction Set and Indexed Addressing Mode disabled)
    // CONFIG3L
    #pragma config WDTCPS = WDTCPS_7// WDT Period selection bits (Divider ratio 1:4096)
    #pragma config WDTE = SWDTEN // WDT operating mode (WDT enabled/disabled by SWDTEN bit)
    // CONFIG3H
    #pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
    #pragma config WDTCCS = LFINTOSC// WDT input clock selector (WDT reference clock is the 31.0 kHz LFINTOSC)
    // CONFIG4L
    #pragma config BBSIZE = BBSIZE_512// Boot Block Size selection bits (Boot Block size is 512 words)
    #pragma config BBEN = OFF // Boot Block enable bit (Boot block disabled)
    #pragma config SAFEN = OFF // Storage Area Flash enable bit (SAF disabled)
    #pragma config WRTAPP = OFF // Application Block write protection bit (Application Block not write protected)
    // CONFIG4H
    #pragma config WRTB = OFF // Configuration Register Write Protection bit (Configuration registers (300000-30000Bh) not write-protected)
    #pragma config WRTC = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
    #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
    #pragma config WRTSAF = OFF // SAF Write protection bit (SAF not Write Protected)
    #pragma config LVP = OFF // Low Voltage Programming Enable bit (HV on MCLR/VPP must be used for programming)
    // CONFIG5L
    #pragma config CP = OFF // PFM and Data EEPROM Code Protection bit (PFM and Data EEPROM code protection disabled)
    #include <xc.h>
    void PWM();
    void OSCILLATOR_Initialize();
    void PPS_Config();

    void main()
    {
    OSCILLATOR_Initialize();
    PWM();
    }
    void OSCILLATOR_Initialize(void)
    {
    // NOSC EXTOSC; NDIV 1;
    OSCCON1 = 0x60;
    // CSWHOLD may proceed; SOSCPWR Low power;
    OSCCON3 = 0x00;
    // MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
    OSCEN = 0x40;
    // HFFRQ 4_MHz;
    OSCFRQ = 0x08;
    // TUN 0;
    OSCTUNE = 0x40;
    }

    void PWM()
    {

    PPS_Config();


    TRISCbits.TRISC2 = 1;

    T2PR = 977;

    CCP1CON = 0x8C;

    CCPR1L = 0x7A;

    CCPR1H = 0x00;

    CCPTMRS0 = 0x01;

    CCPTMRS1 = 0x00;



    PIR4bits.TMR2IF = 0;

    T2CLK = 0x01;

    T2CON = 0xB0;

    T2RST = 0x01;

    T2TMR = 977 ;

    T2CONbits.ON = 1;

    if(PIR4bits.TMR2IF == 1)
    {
    TRISCbits.TRISC2 = 0;
    }
    }
    void PPS_Config()
    {

    INTCON0bits.GIE = 0;
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS


    /* PPS Configuration code*/

    RC2PPS = 0x09; /* Buzzer connected to RC2 */

    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS

    INTCON0bits.GIE = 1;
    }

    Regards
    Ali
    #9
    PStechPaul
    Super Member
    • Total Posts : 2264
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Operating a buzzer using PWM in PIC18F26K83 2019/05/28 21:33:26 (permalink)
    0
    You need an endless loop in your main() function - else the program will just keep resetting.
    void main(void){

    OSCILLATOR_Initialize();
    PWM();

    while(1); }


    However, that may not work either. You need to initialize the PPS and CCP just once, and then possibly sense a switch or something to turn the buzzer on and off.
     
    Also, if the buzzer is specified as 2048 Hz, it might be operated by DC. And if it is an AC device, you might need capacitive coupling to avoid DC bias. Please provide the specs for the buzzer.

     
    #10
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Operating a buzzer using PWM in PIC18F26K83 2019/05/28 22:47:24 (permalink)
    0
    PStechPaul
    You need an endless loop in your main() function - else the program will just keep resetting.
    void main(void){
     
     
     

    OSCILLATOR_Initialize();
    PWM();
     
     
     

    while(1); }


     However, that may not work either. You need to initialize the PPS and CCP just once, and then possibly sense a switch or something to turn the buzzer on and off. Also, if the buzzer is specified as 2048 Hz, it might be operated by DC. And if it is an AC device, you might need capacitive coupling to avoid DC bias. Please provide the specs for the buzzer.
     Here is the updated code :

    // CONFIG1L
    #pragma config FEXTOSC = OFF // External Oscillator Selection (Oscillator not enabled)
    #pragma config RSTOSC = HFINTOSC_64MHZ// Reset Oscillator Selection (HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1)
    // CONFIG1H
    #pragma config CLKOUTEN = ON // Clock out Enable bit (CLKOUT function is enabled)
    #pragma config PR1WAY = OFF // PRLOCKED One-Way Set Enable bit (PRLOCK bit can be set and cleared repeatedly)
    #pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
    #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
    // CONFIG2L
    #pragma config MCLRE = INTMCLR // MCLR Enable bit (If LVP = 0, MCLR pin function is port defined function; If LVP =1, RE3 pin fuction is MCLR)
    #pragma config PWRTS = PWRT_64 // Power-up timer selection bits (PWRT set at 64ms)
    #pragma config MVECEN = ON // Multi-vector enable bit (Multi-vector enabled, Vector table used for interrupts)
    #pragma config IVT1WAY = ON // IVTLOCK bit One-way set enable bit (IVTLOCK bit can be cleared and set only once)
    #pragma config LPBOREN = ON // Low Power BOR Enable bit (ULPBOR enabled)
    #pragma config BOREN = ON // Brown-out Reset Enable bits (Brown-out Reset enabled according to SBOREN)
    // CONFIG2H
    #pragma config BORV = VBOR_2P85 // Brown-out Reset Voltage Selection bits (Brown-out Reset Voltage (VBOR) set to 2.8V)
    #pragma config ZCD = OFF // ZCD Disable bit (ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)
    #pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
    #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
    #pragma config DEBUG = OFF // Debugger Enable bit (Background debugger disabled)
    #pragma config XINST = OFF // Extended Instruction Set Enable bit (Extended Instruction Set and Indexed Addressing Mode disabled)
    // CONFIG3L
    #pragma config WDTCPS = WDTCPS_7// WDT Period selection bits (Divider ratio 1:4096)
    #pragma config WDTE = SWDTEN // WDT operating mode (WDT enabled/disabled by SWDTEN bit)
    // CONFIG3H
    #pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
    #pragma config WDTCCS = LFINTOSC// WDT input clock selector (WDT reference clock is the 31.0 kHz LFINTOSC)
    // CONFIG4L
    #pragma config BBSIZE = BBSIZE_512// Boot Block Size selection bits (Boot Block size is 512 words)
    #pragma config BBEN = OFF // Boot Block enable bit (Boot block disabled)
    #pragma config SAFEN = OFF // Storage Area Flash enable bit (SAF disabled)
    #pragma config WRTAPP = OFF // Application Block write protection bit (Application Block not write protected)
    // CONFIG4H
    #pragma config WRTB = OFF // Configuration Register Write Protection bit (Configuration registers (300000-30000Bh) not write-protected)
    #pragma config WRTC = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
    #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
    #pragma config WRTSAF = OFF // SAF Write protection bit (SAF not Write Protected)
    #pragma config LVP = OFF // Low Voltage Programming Enable bit (HV on MCLR/VPP must be used for programming)
    // CONFIG5L
    #pragma config CP = OFF // PFM and Data EEPROM Code Protection bit (PFM and Data EEPROM code protection disabled)
     
    #include <xc.h>
     
    void PWM();
    void OSCILLATOR_Initialize();
    void PPS_Config();
    void CCP_Initialize();
     

    void main()
    {
    OSCILLATOR_Initialize();
    ANSELCbits.ANSELC2 = 0;
    PPS_Config();
    CCP_Initialize();
    while(1)
    {
    PWM();
    }
    }
     
    void OSCILLATOR_Initialize(void)
    {
    // NOSC EXTOSC; NDIV 1;
    OSCCON1 = 0x60;
    // CSWHOLD may proceed; SOSCPWR Low power;
    OSCCON3 = 0x00;
    // MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
    OSCEN = 0x40;
    // HFFRQ 4_MHz;
    OSCFRQ = 0x08;
    // TUN 0;
    OSCTUNE = 0x40;
    }
    void CCP_Initialize()
    {
    CCP1CON = 0x8C;

    CCPR1L = 0x7A;

    CCPR1H = 0x00;

    CCPTMRS0 = 0x01;

    CCPTMRS1 = 0x00;
     
    }
     
    void PWM()
    {

    TRISCbits.TRISC2 = 1;

    T2PR = 977;

    PIR4bits.TMR2IF = 0;

    T2CLK = 0x01;

    T2CON = 0xB0;

    T2RST = 0x01;

    T2TMR = 977 ;

    T2CONbits.ON = 1;

    if(PIR4bits.TMR2IF == 1)
    {
    TRISCbits.TRISC2 = 0;
    }
    }
     
    void PPS_Config()
    {

    INTCON0bits.GIE = 0;
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS


    /* PPS Configuration code*/

    RC2PPS = 0x09; /* Buzzer connected to RC2 */
     

    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS

    INTCON0bits.GIE = 1;
     
    }
     


     
    Here are the details:
    Rated voltage  (DC)     - 5V 
    Operating Voltage        - 3V to 6V
     
    However I cannot see any wave on the pin connected to the buzzer. I can only see a shift of 5V.
     
    Regards
    Ali
    post edited by M.Ali - 2019/05/28 22:50:03
    #11
    mpgmike
    Super Member
    • Total Posts : 159
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: online
    Re: Operating a buzzer using PWM in PIC18F26K83 2019/05/29 01:12:49 (permalink)
    +2 (2)
    1- "#pragma config MVECEN = ON" combined with
    2- "INTCON0bits.GIE = 1;"
    3- but no interrupt service routine.  Ditch GIE=1.
     
    4- 
     
    while(1)
    {
    PWM();
    }

     
    Every time you loop you are turning everything off, then on, but never getting to the output state.  Visit PWM() one time only, then just while(1);.
     
    I didn't dissect the individual registers, but this may just work.
    post edited by mpgmike - 2019/05/29 01:14:29

    I don't need the world to know my name, but I want to live a life so all my great-grandchildren proudly remember me.
    #12
    M.Ali
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2018/11/12 00:36:07
    • Location: 0
    • Status: offline
    Re: Operating a buzzer using PWM in PIC18F26K83 2019/06/12 22:56:00 (permalink)
    0
    Hi,
    I can see the PWM wave on the oscilloscope and also ON the buzzer.I want to ON the buzzer for just three seconds. I tried by using __delay_ms(3000) and then T2CONbits.TMR2ON = 0; but also the buzzer remains ON continuously. How to stop the PWM wave ?
     
    Here is the code:

    // CONFIG1L
    #pragma config FEXTOSC = OFF // External Oscillator Selection (Oscillator not enabled)
    #pragma config RSTOSC = HFINTOSC_64MHZ// Reset Oscillator Selection (HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1)
    // CONFIG1H
    #pragma config CLKOUTEN = ON // Clock out Enable bit (CLKOUT function is enabled)
    #pragma config PR1WAY = OFF // PRLOCKED One-Way Set Enable bit (PRLOCK bit can be set and cleared repeatedly)
    #pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
    #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
    // CONFIG2L
    #pragma config MCLRE = INTMCLR // MCLR Enable bit (If LVP = 0, MCLR pin function is port defined function; If LVP =1, RE3 pin fuction is MCLR)
    #pragma config PWRTS = PWRT_64 // Power-up timer selection bits (PWRT set at 64ms)
    #pragma config MVECEN = ON // Multi-vector enable bit (Multi-vector enabled, Vector table used for interrupts)
    #pragma config IVT1WAY = ON // IVTLOCK bit One-way set enable bit (IVTLOCK bit can be cleared and set only once)
    #pragma config LPBOREN = ON // Low Power BOR Enable bit (ULPBOR enabled)
    #pragma config BOREN = ON // Brown-out Reset Enable bits (Brown-out Reset enabled according to SBOREN)
    // CONFIG2H
    #pragma config BORV = VBOR_2P85 // Brown-out Reset Voltage Selection bits (Brown-out Reset Voltage (VBOR) set to 2.8V)
    #pragma config ZCD = OFF // ZCD Disable bit (ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)
    #pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
    #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
    #pragma config DEBUG = OFF // Debugger Enable bit (Background debugger disabled)
    #pragma config XINST = OFF // Extended Instruction Set Enable bit (Extended Instruction Set and Indexed Addressing Mode disabled)
    // CONFIG3L
    #pragma config WDTCPS = WDTCPS_7// WDT Period selection bits (Divider ratio 1:4096)
    #pragma config WDTE = SWDTEN // WDT operating mode (WDT enabled/disabled by SWDTEN bit)
    // CONFIG3H
    #pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
    #pragma config WDTCCS = LFINTOSC// WDT input clock selector (WDT reference clock is the 31.0 kHz LFINTOSC)
    // CONFIG4L
    #pragma config BBSIZE = BBSIZE_512// Boot Block Size selection bits (Boot Block size is 512 words)
    #pragma config BBEN = OFF // Boot Block enable bit (Boot block disabled)
    #pragma config SAFEN = OFF // Storage Area Flash enable bit (SAF disabled)
    #pragma config WRTAPP = OFF // Application Block write protection bit (Application Block not write protected)
    // CONFIG4H
    #pragma config WRTB = OFF // Configuration Register Write Protection bit (Configuration registers (300000-30000Bh) not write-protected)
    #pragma config WRTC = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
    #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
    #pragma config WRTSAF = OFF // SAF Write protection bit (SAF not Write Protected)
    #pragma config LVP = OFF // Low Voltage Programming Enable bit (HV on MCLR/VPP must be used for programming)
    // CONFIG5L
    #pragma config CP = OFF // PFM and Data EEPROM Code Protection bit (PFM and Data EEPROM code protection disabled)
    #include <xc.h>
    #define _XTAL_FREQ 64000000
    void PWM();
    void OSCILLATOR_Initialize();
    void PPS_Config_PWM();

    void main()
    {
    OSCILLATOR_Initialize();
    ANSELCbits.ANSELC2 = 0;
    PWM();
    __delay_ms(3000);
    T2CONbits.TMR2ON = 0;
    //while(1);

    }
    void OSCILLATOR_Initialize(void)
    {
    // NOSC EXTOSC; NDIV 1;
    OSCCON1 = 0x60;
    // CSWHOLD may proceed; SOSCPWR Low power;
    OSCCON3 = 0x00;
    // MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
    OSCEN = 0x40;
    // HFFRQ 4_MHz;
    OSCFRQ = 0x08;
    // TUN 0;
    OSCTUNE = 0x40;
    }


    void PPS_Config_PWM()
    {
    INTCON0bits.GIE = 0;
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS


    /* PPS Configuration code*/

    RC2PPS = 0x09; /* Buzzer connected to RC2 */

    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS

    INTCON0bits.GIE = 1;
    }
     
    void PWM()
    {
    PPS_Config_PWM();

    T2PR = 0xFF;

    CCP1CON = 0x8C;

    CCPR1L = 0xF0;

    CCPR1H = 0x00;

    CCPTMRS0 = 0x01;

    CCPTMRS1 = 0x00;

    PIR4bits.TMR2IF = 0;

    T2CLK = 0x01;

    T2CON = 0xB0;

    T2CONbits.TMR2ON = 1;

    while(!PIR4bits.TMR2IF);

    TRISCbits.TRISC2 = 0;
    }
     

     
     
    Regards
    Ali
    #13
    PStechPaul
    Super Member
    • Total Posts : 2264
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Operating a buzzer using PWM in PIC18F26K83 2019/06/12 23:09:04 (permalink)
    +1 (1)
    Read my post #10 above. You have commented out the endless loop (again?)
     
    You need an endless loop in your main() function - else the program will just keep resetting.
    void main(void){

    OSCILLATOR_Initialize();
    PWM();

    __delay_ms(5000);

    TRISCbits.TRISC2 = 1; //This is one way to turn off PWM after 5 seconds

    while(1); // Buzzer will stop and will not start again until program is reset

    }




     
    #14
    pcbbc
    Super Member
    • Total Posts : 1011
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: Operating a buzzer using PWM in PIC18F26K83 2019/06/13 03:37:33 (permalink)
    0
    PStechPaul
    Read my post #10 above. You have commented out the endless loop (again?)
     
    You need an endless loop in your main() function - else the program will just keep resetting.

     
    So, although you can see "something" on the PWM pin, it is not the output from the PWM module.  Just the result of the PIC constantly resetting, and disabling/enabling to output pin as it does so.
     
    As PStechPaul says, your main function must never exit in an embedded application.  Because if it does, where does it go?
    #15
    Jump to:
    © 2019 APG vNext Commercial Version 4.5