Hi all
I am trying to set a PWM of 1KHz and 50% duty cycle for a buzzer but no luck and i cannot figure out why. The TRIS is on RB0 and i use the CCP4 and timer 2. If someone can help. The code as follows:
I am trying to set a PWM of 1KHz and 50% duty cycle for a buzzer but no luck and i cannot figure out why. The TRIS is on RB0 and i use the CCP4 and timer 2. If someone can help. The code as follows:
Code:
/******************************************************************************/
/* Files to Include */
/******************************************************************************/
#if defined(__XC)
#include <xc.h> /* XC8 General Include File */
#elif defined(HI_TECH_C)
#include <htc.h> /* HiTech General Include File */
#elif defined(__18CXX)
#include <p18cxxx.h> /* C18 General Include File */
#endif
#if defined(__XC) || defined(HI_TECH_C)
#include <stdint.h> /* For uint8_t definition */
#include <stdbool.h> /* For true/false definition */
#endif
#include "system.h" /* System funct/params, like osc/peripheral config */
#include "user.h" /* User funct/params, such as InitApp */
/******************************************************************************/
/* User Global Variable Declaration */
/******************************************************************************/
#define _XTAL_FREQ 4000000
void PWM1_Set_Duty(uint16_t DC)
{
// Check The DC Value To Make Sure it's Within 10-Bit Range
if(DC<1024)
{
CCP4CONbits.DC4B1 = (DC & 1)>>1;
CCP4CONbits.DC4B0 = (DC & 2);
CCPR4L = DC >> 2;
}
}
void main(void) {
/* Configure the oscillator for the device */
ConfigureOscillator();
OSCCONbits.IRCF = 0x05; // @4 MHz MCU
OSCCONbits.SCS = 0x00; // Internal oscillator block
/* Initialise I/O and Peripherals for application */
InitApp();
//--[ Configure The CCP Module For PWM Mode ]--
CCP4CONbits.CCP4M3 = 1;
CCP4CONbits.CCP4M2 = 1;
TRISB0 = 0; // The CCP1 Output Pin (PWM) is RB0
// Set The PWM Frequency (1kHz)
PR2 = 249;
// Set The PS For Timer2 (1:4 Ratio)
T2CONbits.T2CKPS0 = 1;
T2CONbits.T2CKPS1 = 0;
// Start CCP1 PWM !
T2CONbits.TMR2ON = 1;
while (1) {
PWM1_Set_Duty(500);
}
}