📄 notch_60.c
字号:
/*****************************************************************************
* 60 Hertz Notch Filter
*
* Written for "Digital Signal Processing with the PIC16C74" Application Note.
*
* This example program use the filter() function to implement a 60Hz notch
* filter. T0 is used to generate a 1kHz sample clock. The program samples the
* input signal x(n) on A-D channel 1, calls the filter routine signal, and
* outputs y(n) to PWM channel 1.
*
* If FILTER set to 0, performs straight talkthru from A-D to PWM output.
* T0 period can be changed to cary the sample rate.
*
* D. Mostowfi 4/95
******************************************************************************/
#include <16c74.h> /* c74 header file */
#include "analogio.c" /* analog I/O module */
#include "iir_filt.c" /* iir filter module */
#define FILTER 1
/* Function Prototypes */
void main_isr();
void timer0_isr();
/*****************************************************************************
* main isr - 16C74 vectors to 0004h (MPC __INT() function) on any interrupt *
* assembly language routine saves W and Status registers then tests flags in
* INTCON to determine source of interrupt. Routine then calls appropriate isr.
* Restores W and status registers when done.
*****************************************************************************/
void __INT(void)
{
if(INTCON.T0IF){ /* timer 0 interrupt ? */
INTCON.T0IF=0; /* clear interrupt flag */
timer0_isr(); /* and call timer 0 isr */
}
/* Restore W, WImage, and STATUS registers */
#asm
BCF STATUS,RP0 ;Bank 0
MOVF temp_PCLATH, W
MOVWF PCLATH ;PCLATH restored
MOVF temp_WImage, W
MOVWF __WImage ;__WImage restored
MOVF temp_FSR, W
MOVWF FSR ;FSR restored
SWAPF temp_STATUS,W
MOVWF STATUS ;RP0 restored
SWAPF temp_WREG,F
SWAPF temp_WREG,W ;W restored
#endasm
}
/*****************************************************************************
* timer 0 interrupt service routine
***************************************************************************/
void timer0_isr(void)
{
TMR0=100; /* reload value for 1ms period */
PORTB.0=!PORTB.0; /* toggle PORTB.0 */
sample_flag=active; /* set sample flag */
}
void main()
{
/* initialize OPTION register */
OPTION=0b00000011; /* assign prescaler to T0 */
/* initialize INTCON register (keep GIE inactive!) */
INTCON=0b00000000; /* disable all interrupts */
/* initialize PIE1 and PIE2 registers (periphreal interrupts) */
PIE1=0b00000000; /* disable all peripheral interrupts */
PIE2=0b00000000;
/* initialize T1CON and T2CON registers */
T1CON=0b00000000; /* T1 not used */
T2CON=0b00000000; /* T2 not used */
/* initialize CCPxCON registers */
CCP1CON=0b00001100; /* set CCP1CON for PWM mode */
CCP2CON=0b00000000; /* CCP2CON=0 (PWM 2 not used) */
/* initialize SSPCON register */
SSPCON=0b00000000; /* serial port - not used */
/* initialize ADCONx registers */
ADCON0=0b00000000; /* a-d converter */
ADCON1=0b00000010;
/* initialize TRISx register (port pins as inputs or outputs) */
TRISA=0b00001111;
TRISB=0b00000000;
TRISC=0b11111011;
TRISD=0b11111111;
TRISE=0b11111111;
/* clear watchdog timer (not used) */
CLRWDT();
/* initialize program bit variables */
FLAGS=0b00000000;
/* intialize output port pins */
PORTB=0;
/* enable interrupts... */
INTCON.T0IE=1; /* peripheral interrupt enable */
INTCON.GIE=1; /* global interrupt enable */
init_PWM(0x40); /* init PWM port */
init_filter(); /* init filter */
while(1){
while(!sample_flag){} /* wait for sample clock flag to be set */
sample_flag=0; /* clear sample clock flag */
x_n=get_sample(1); /* read ADC channel 1 into x(n) */
if(FILTER==1){ /* if filter enabled */
filter(); /* call filter routine */
}
else{ /* or else write x(n) to y(n) (talkthru) */
y_n=x_n;
}
write_PWM((char)y_n,0); /* write y_n to PWM port 1 */
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -