. .
.
Expt-7:DF-Part7 Implementation of Digital FIR Filter on 8051 Microcontroller
.
.

# STEP BY STEP PROCEDURE TO IMPLEMENT THE FILTER ON 8051

## STEP1:-

REQUIRED COMPONENTS

## STEP3:-

BLOCK DIAGRAM OF 8051-DAC INTERFACING

ALGORITHM FOR INTERFACING 8051 WITH DAC

## STEP4:-

PROCEDURE TO WRITE ASSEMBLY PROGRAMMING

ASSEMBLY TO HEX FLOW CHART

INSTRUCTIONS FOR 8051 MICROCONTROLLER

## STEP5:-

PROCEDURE TO USE MATLAB FDATOOL

•  Open up Matlab and type fdatool into the workspace and hit enter.

•  We can specify the type of filter and the design type as well as the pass and stop band frequencies. In addition we can specify the attenuation in the pass and stop band to give us the desired filter response (by changing the values on the bottom right corner of the window). After choosing the attenuation we want, fdatool will design a filter of the lowest order.

•  For the programs developed in this course, (Implementation of DSP) however, we normally want a filter of a specific order (31,  63, 127, etc…), so instead we will specify the order of the filter as has been done in figure 3 below.

•  Specify the order of the filter, the sampling frequency of the system for which the filter is to be used, the upper frequency (where the   pass band ends), and the stop frequency (where the stop band starts), as shown in figure 3 below:

Note: It may be useful to specify the order as N-1 where N is the number of   coefficients that the filter will have. Chosen below is a filter of order :
128 -1 = 127, with 128 coefficients.

•  Hit the design filter button to get the filter. As seen in figure 4 below, the filter has been generated and the response has been plotted.

•  Notice below (figure 5) that the filter has been altered to give a sharper response. The sharper response, however, sacrifices the amount of attenuation in the stop band and causes ripple in the passband.

• Notice now (figure 6 below) that the filter order has been changed to 63, resulting in about 45 dB less attenuation in the stop band as compared to a filter of order 127 (figure 4). This is just an example, the filter of order 127 will be used for the rest of this tutorial.

• Rule of Thumb: The higher the order of the filter, the greater the stop band  attenuation. The downside, however, is that a higher order filter means more computations.

• You can view many of the filters aspects using fdatool. For example, the amount of ripple in the pass band or the 3 dB point can be seen by placing markers on the plot (just click on the plot to place a marker; see figure 7 below).

• We may also view the magnitude and phase response by using the buttons on the toolbar at the top of the page (see figure 8).

• We may also view the filter coefficients by clicking on the "Filter Coefficients" button on the top toolbar (see figure 9).

•     To export these coefficients to the Matlab workspace, go to File --> Export (see figure 10).

•  The window shown in figure 11 will pop-up. Make sure workspace and coefficients are selected. Enter the name of the vector to save the coefficients ("coeff" chosen in the example of figure 11) and hit export.

• As seen in figure 12 and 13 below, the coefficients have been placed in the vector coeff and there are 128 coefficients for the filter of order 128 – 1 = 127.

• From here the coefficients can be used for design of the FIR filter.

Calculation of Filter Coefficients:-

• Filter coefficients can be calculated using the FDA tool in MATLAB. There are many other simulators on internet which will calculate filter coefficients according to our requirements. The coefficients I have used for 2nd order FIR filter are:
h0 = h2 = 0.256267154.....;    h0*256 = 65.60;
h1 = 0.487465691......;         h1*256 = 124.7912

Specifications I used to calculate these coefficients in MATLAB:

Filter                    : Low pass FIR filter
Design Method : Window
Order                  : 2
Window             : Kaiser
Beta                   : 0.5
Fs                       : 7200 HZ
Fc                       : 2000 HZ

The sampling frequency Fs, used here cannot be taken arbitrary. The sampling frequency is the frequency at which samples are taken from ADC to 8051, and that is nothing but the frequency of the clk signal given to the SC of ADC. This clk signal is given to SC from 8051. Hence, first this frequency has to be calculated by running the code on 8051 alone. And cut-off frequency Fc can be any value less than or equal to half of the sampling frequency. The coefficients obtained using the MATLAB are decimal values less than 1 and sum of these coefficients equals to 1. Maximum value of any input sample taken from ADC is 256. Hence, according to the equation y(n)=h0*x(n)+h1*x(n-1)+h2*x(n-2), maximum value of any output sample y(n) will be 256. But, as the coefficients h0, h1 and h2 are decimal values less than 1, these cannot be stored directly in 8051’s registers. Hence we use the technique of scaling the coefficients and descaling the output result. We multiply the coefficients by 28=256, hence the product of coefficients and input samples x(n) will become of 16 bit length. Now we descale this 16 bit result by 256, simply by removing the lower 8 bits of the result.

## STEP6:-

• After getting the Coefficients form MATLAB FDA Tool the implementation of FIR filter was done by writing assembly program using   8051 instruction set architecture.

•  The implementation of filter had some constraints.

•  The filter coefficients had to be scaled to 8-bit values as 8051 has 8 bit registers.

•  Also multiplication of filter coefficient with each 8-bit sample would yield a 16-bit output, which had to be interfaced with an 8-bit DAC.

• Code run time had to be taken into account as the multiplication and shifting operations would take more time than the ADC conversion time.

•  To tackle the afore mentioned constraints, following approach has been implemented for programming the FIR filter:

Each register in AT89c51/52 provides 8-bit storage space, of which the MSB is used for sign bit. Therefore, effective space for storage reduces to 7-bit. To accommodate the filter coefficients in this format, the highest filter coefficient was scaled.

The interfacing to ADC0809 was done previously. However to handle the difference in code runtime in 8051 and conversion   time of ADC, the EOC(End-Of-Conversion) signal was used as an interrupt to microcontroller.

The code algorithm and the corresponding labels as used in assembly programs are given below:

MAIN (Initialization): ALE(Address Latch Enable), SC(Start Conversion) and OE(Output Enable) bits of ADC were initially set to 0. Registers 30H to 34H of the microcontroller were cleared for future use as input storage. EOC interrupt port and input port were configured for input. EOC interrupt was set to edge triggered.

HCOEFF: The filter coefficients were recursively stored in registers 40h-44h for further use. Interrupts INT0 connected to EOC was enabled.

BACK: The input channel IN1 of ADC was selected by providing the required control signal (001) is given. Address was latched and ADC conversion was started.

HERE: The program counter (PC) waited here till the 1st interrupt provided by the EOC.

ISR for INT0 (Interrupt Service Subroutine): On receiving the interrupt  (high to low transition of EOC), the PC jumped to INT0 (External interrupt 0) service subroutine which is called the STORE label, starting the filtering operation. When PC returns after handling the interrupt, ‘reti’ (Return from interrupt) is called which takes the program  counter (PC) back to the label HERE, where the microcontroller again waits for the next interrupt.

FULL CIRCUIT DIAGRAM

Cite this Simulator:

.....
..... .....