Dispara sonido de alaram con PIC16F886

0

Estoy codificando en un PIC16F886 IC. Estoy tratando de generar una alarma de incendio con una señal PWM. Tengo algunas preguntas como se indica a continuación.

Copié el código de Código de ejemplo Mi circuito de parlantes se parece a este video, estoy enviando pulsos PWM en lugar de entrada de audio. circuito de mosfet

Podría producir en base a la señal PWM.

El siguiente código produce un ciclo de trabajo del 50% con una frecuencia de 2 KHz. Ahora estoy tratando de producir un sonido de fuego alaram. Para eso estoy planeando usar Timer1 interrupt 16 bit. Durante el tiempo de Tonelada del temporizador, envíe la señal PWM para el ciclo de trabajo 50% y el tiempo de despegue del temporizador1. Apagaré el temporizador PWM. Alguien puede sugerir cómo puede hacer los cambios a continuación.

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

0 respuestas

Lea otras preguntas en las etiquetas