ajit_nayak87
Member level 5
I am using PIC16F886 . I am trying to generate fire alarm sound using PWM signal.
Currently i can able to create duty cycle with 2Khz at 50% duty
Now inorder to create siren sound using Timer1 16 bit timer I am trying to create 10ms on time and 10ms OFF time.
PWM signal should be always ON on 10ms Ton time remaining Toff time it should be OFF . can someone suggest how can do it here.
Currently i can able to create duty cycle with 2Khz at 50% duty
Now inorder to create siren sound using Timer1 16 bit timer I am trying to create 10ms on time and 10ms OFF time.
PWM signal should be always ON on 10ms Ton time remaining Toff time it should be OFF . can someone suggest how can do it here.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 #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); } }
Last edited by a moderator: