ajit_nayak87
New Member
I am coding on pIC16F886 IC. I am trying to generate Fire alarm siren with PWMsignal . I have few questions as below. I copied code from **broken link removed** My spaker circuit look like this video , I am sending PWM pulses instead of audio Input mosfet circuit
I could able to produce based on PWM signal.
Below code produce 50% duty cycle with 2Khz frequency.Now i am trying to produce fire alaram sound for that I am plan to use Timer1 interrupt 16 bit. During Ton time of timer send PWM signal for duty cycle 50% and Toff time of timer1 i will turn of PWM timer. can someone suggest how can make below changes.
I could able to produce based on PWM signal.
Below code produce 50% duty cycle with 2Khz frequency.Now i am trying to produce fire alaram sound for that I am plan to use Timer1 interrupt 16 bit. During Ton time of timer send PWM signal for duty cycle 50% and Toff time of timer1 i will turn of PWM timer. can someone suggest how can make below changes.
C:
#include <htc.h>
#include <stdio.h>
#include<pic.h>
#include<stdint.h>
#define _XTAL_FREQ 20000000
unsigned int i=0;
#define TMR2PRESCALE 16
long freq;
unsigned int x;
#define LED RC7
#define LED1 RC6
#define LED2 RC2
unsigned short int cnt, num,Dgt=0;
unsigned int i;
unsigned int j;
void out_p(int l , int k);
int PWM_Max_Duty()
{
return(_XTAL_FREQ/(freq*TMR2PRESCALE);
}
PWM1_Init(long fre)
{
PR2 = (_XTAL_FREQ/(freq*4*TMR2PRESCALE)) - 1;
freq = fre;
}
PWM1_Duty(unsigned int duty)
{
if(duty<1024)
{
duty = ((float)duty/1023)*PWM_Max_Duty();
CCP1X = duty & 2;
CCP1Y = duty & 1;
CCPR1L = duty>>2;
}
}
PWM1_Start()
{
CCP1M3 = 1;
CCP1M2 = 1;
#if TMR2PRESCALE == 1
T2CKPS0 = 0;
T2CKPS1 = 0;
#elif TMR2PRESCALE == 4
T2CKPS0 = 1;
T2CKPS1 = 0;
#elif TMR2PRESCALE == 16
T2CKPS0 = 1;
T2CKPS1 = 1;
#endif
TMR2ON = 1;
TRISC2 = 0;
}
PWM1_Stop()
{
CCP1M3 = 0;
CCP1M2 = 0;
}
void SetPWMDutyCycle(unsigned int DutyCycle) // Give a value in between 0 and 1024 for DutyCycle
{
CCPR1L = DutyCycle>>2; // Put MSB 8 bits in CCPR1L
CCP1CON &= 0xCF; // Make bit4 and 5 zero
CCP1CON |= (0x30&(DutyCycle<<4)); // Assign Last 2 LSBs to CCP1CON
}
void InitPWM(void)
{
TRISC2 = 0; // Make CCP1 pin as output
CCP1CON = 0x0C; // Configure CCP1 module in PWM mode
PR2 = 0xFF; // Configure the Timer2 period
T2CON = 0x01; // Set Prescaler to be 4, hence PWM frequency is set to 4.88KHz.
SetPWMDutyCycle(0); // Intialize the PWM to 0 duty cycle
T2CON |= 0x04; // Enable the Timer2, hence enable the PWM.
}
void Delay(int k)
{
for(j=0;j<k;j++);
}
void tone(int Frequency,int duration)
{
SetPWMDutyCycle(Frequency);
Delay(duration);
}
void Timer1_Interrupt()
{
INTCON = 0b00000000;
PIE1=0b00000001;
PIR1=0x01;
TMR1H=0x0B;
TMR1L=0xDC;
T1CON=0x31;
// T1CON=0x01;
}
void Init_Controller()
{
cnt=100;
TRISC=0b00000000;
PORTC=0b00000000;
TRISB=0b10000000;
PORTB = 0b00000000;
TRISA=0b00000000;
PORTA=0X00;
ADCON0 = 0b00000000;
ANSEL = 0b00000000;
Timer1_Interrupt();
LED=0;
LED1=0;
LED2=0;
}
void interrupt isr(void)
{
if(TMR1IF==1)
{
TMR1H=0xEA; // Load the time value(0x0BDC) for 100ms delay
TMR1L=0x60; //Timer1 Interrupt for 65000
TMR1IF=0; // Clear timer interrupt flag
Dgt++;
LED=!LED;
LED1=!LED1;
}
}
void main(void)
{
Init_Controller();
Timer1_Interrupt();
GIE=1;
PEIE=1;
TMR1IE=1;
PWM1_Init(5000);
PWM1_Start();
while(1)
{
PWM1_Duty(500);
}
}