fire alaram sound using PIC16F886

fire alaram sound using PIC16F886

Postby ajitnayak87 » Sat Nov 25, 2017 2:54 pm

I am coding on pIC16F886 IC. I am trying to generate Fire alarm siren with PWM signal . I have few questions as below.

Below code produce 50% duty cycle with 2Khz frequency.Now i am trying to produce fire alarm 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.
CODE:
https://electrosome.com/pwm-pic-microcontroller-hi-tech-c/

https://www.youtube.com/watch?v=8gXyh0jnwt0


Code: Select all


#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);
 
 }

}

ajitnayak87
 
Posts: 3
Joined: Tue Aug 01, 2017 1:37 pm

Re: fire alaram sound using PIC16F886

Postby PStechPaul » Sun Nov 26, 2017 4:18 am

As others said in your YouTube comments, your MOSFET amplifier is not a good design. Here is a simulation of a reasonably good simple MOSFET amplifier:

MOSFET_Amp.png
MOSFET_Amp.png (52.96 KiB) Viewed 39 times


The 3.7 volt lithium battery makes it difficult to apply sufficient voltage to bias a standard MOSFET, so I chose one with about 1.1 volt gate threshold. This circuit applies about 46 mV P-P into an 8 ohm speaker load, with an applied signal of 20 mV P-P.

As for generating the fire alarm sound, you may need to change the frequency of the 50% PWM every 100 mSec or so.
PStechPaul
 
Posts: 4
Joined: Tue Sep 23, 2014 7:37 am
PIC experience: Professional 5+ years with MCHP products


Return to CCP, ECCP and PWM

Who is online

Users browsing this forum: No registered users and 1 guest