a microchip pic based tilt measurement device

29
A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady Page 1 of 29 A Microchip PIC Based Tilt Measurement Device Gaurav S Konchady* Department of Electrical and Computer Engineering Cleveland State University Cleveland, Ohio 44115 May 1, 2008 Abstract Tilt monitoring is an important exercise in many industrial applications as well as in our daily lives. An accelerometer is a device which measures the acceleration in terms of m/s². There are many MEMS-based (Micro-electro-mechanical systems) Accelerometers available in the market. MEMS-based Accelerometers convert the tilt signal to an electrical signal which can be used for measurement. The output electrical signal generated is a digital signal in most cases. The MEMS Accelerometer contains on-board signal processing units which convert the mechanical signal to a digital format. This digital signal will further require processing so that we can extract the measurement information from the signal. The objective of this project is to use this signal from the accelerometer to present the measured degree of tilt on an electronic display. 1. Introduction In any application where movement is involved, there will be a need at some point to monitor this movement. It is always helpful to have a visual representation of this movement. Using a visual readout, we can easily determine whether the amount of tilt is tolerable in the application. Thus, we need a sensitive, fast device which responds to the slightest movement. The Microchip-based Tilt Measurement Device aims to design a prototype version of a tilt display device. MEMS-based Accelerometers consists of a movable mass kept in position with springs that is suspended over a static mass which forms a capacitor type of arrangement. The movable mass is constructed from polysilicon, while the static mass is constructed from silicon wafers. The ADXL202EB [5] Accelerometer Evaluation Board from the analog devices was used as the accelerometer and forms the core of this project. The Microchip PIC Microcontroller interfaced the ADXL202EB Device and a 7-segment LED Display Device giving us a visual readout of the angle of tilt. We limit our application to a single axis operation. In this report, we discuss the construction process of the project. In section 2, we discuss the prototype system block diagram and a description of each block. Section 3 gives a description of the circuit diagram. Section 4 gives a flow chart representation of the assembly language code used to program the microcontroller. Section 5 discusses the conclusion and further modifications. *Email address: [email protected]

Upload: others

Post on 21-May-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 1 of 29

A Microchip PIC Based Tilt Measurement Device

Gaurav S Konchady* Department of Electrical and Computer Engineering

Cleveland State University Cleveland, Ohio 44115

May 1, 2008

Abstract Tilt monitoring is an important exercise in many industrial applications as well as in our daily lives. An accelerometer is a device which measures the acceleration in terms of m/s². There are many MEMS-based (Micro-electro-mechanical systems) Accelerometers available in the market. MEMS-based Accelerometers convert the tilt signal to an electrical signal which can be used for measurement. The output electrical signal generated is a digital signal in most cases. The MEMS Accelerometer contains on-board signal processing units which convert the mechanical signal to a digital format. This digital signal will further require processing so that we can extract the measurement information from the signal. The objective of this project is to use this signal from the accelerometer to present the measured degree of tilt on an electronic display. 1. Introduction In any application where movement is involved, there will be a need at some point to monitor this movement. It is always helpful to have a visual representation of this movement. Using a visual readout, we can easily determine whether the amount of tilt is tolerable in the application. Thus, we need a sensitive, fast device which responds to the slightest movement. The Microchip-based Tilt Measurement Device aims to design a prototype version of a tilt display device. MEMS-based Accelerometers consists of a movable mass kept in position with springs that is suspended over a static mass which forms a capacitor type of arrangement. The movable mass is constructed from polysilicon, while the static mass is constructed from silicon wafers. The ADXL202EB [5] Accelerometer Evaluation Board from the analog devices was used as the accelerometer and forms the core of this project. The Microchip PIC Microcontroller interfaced the ADXL202EB Device and a 7-segment LED Display Device giving us a visual readout of the angle of tilt. We limit our application to a single axis operation. In this report, we discuss the construction process of the project. In section 2, we discuss the prototype system block diagram and a description of each block. Section 3 gives a description of the circuit diagram. Section 4 gives a flow chart representation of the assembly language code used to program the microcontroller. Section 5 discusses the conclusion and further modifications. *Email address: [email protected]

Page 2: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 2 of 29

2. System Block Diagram The MEMS Accelerometer forms the core of the design. The output of the accelerometer will be in the form of digital pulses. These digital pulses are counted by using the microcontroller. We utilize the timer functionality of the microcontroller to perform this operation. The counted pulses are then analyzed by the microcontroller and a series of mathematical operations are performed to get the final measurement readings with appropriate polarity. The final readings are displayed on a 7-segment 2-digit LED Display driven by one of the ports of the microcontroller. An LED driven by another port pin will display the direction of tilt of the device. A push button attached to the microcontroller will configure the device when in the equilibrium position. Figure 2.1 shows the simplified block diagram of the system.

Figure 2.1 Simplified block diagram of system

2.1 MEMS Accelerometer The ADXL202 [2] Device is a dual-axis MEMS Accelerometer which measures static as well as dynamic acceleration. The acceleration measured by these devices is represented in terms of ‘g’. According to the Dictionary of Technical Terms for Aerospace Use published by NASA, a ‘g’ is defined as “An acceleration equal to the acceleration of gravity, 980.665 centimeter per second-squared, approximately 32.2 feet per second per second at sea level. It is used as a unit of stress measurement for bodies undergoing acceleration.” In terms of ‘g’ the measurement range of the ADXL202 is +/- 2g or +/- 10g. The block diagram if the ADXL202 Device is shown in Figure 2.2 As described in the introduction section, we have a differential capacitor arrangement of the static and dynamic masses of the sensor. When the dynamic mass moves due to the tilting of the device, we will get a square wave output from the capacitor arrangement. The demodulator block processes the square wave signal and sends it to the duty cycle modulator block. We can configure the output of this block i.e. duty cycle of the square wave by selecting appropriate values of Cx, Cy and Rset seen in Figure 2.2. Xout and Yout represent the acceleration experienced by the device in the X and Y directions.

Page 3: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 3 of 29

Figure 2.2 Block Diagram of ADXL202 taken from [2]

The duty cycle modulator block will generate a square wave of variable duty cycle. When at rest or 0 g, the duty cycle modulator block outputs a square wave of 50 % duty cycle. This duty cycle varies by 12.5 % per ‘g’ of acceleration experienced by the device. While configuring the device, there are some issues to be resolved. We have to first determine the measurement resolution required or the accelerometer bandwidth which will also influence the selection of the microcontroller. The typical duty cycle output generated by the device by the Xout and Yout pins is shown in the Figure 2.3.

Figure 2.3 Typical duty cycle output of ADXL202 taken from [3]

We have to measure the values of T1 for the X and Y axes while T2 is to be measured only once for both axes since its duration is equal. T2 is the period of the square wave and is decided by the Rset resistor as we will see later in the report.

Page 4: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 4 of 29

The ADXL202 [2] Device is available in an 8-lead CERPAK Surface Mount Device. Using the device on the breadboard is going to be difficult as it requires good soldering skills. So, I decided to use the ADXL202EB [5] Evaluation Board distributed by Jameco Electronics. The ADXL202EB consists of an ADXL202 Device soldered on to a P.C. Board. The P.C. Board provides space for connecting the user programmable capacitors and resistor which decide the bandwidth and resolution of the device. The device contains a 5-Pin Header which makes it convenient to connect to the breadboard. The datasheet contains pre-calculated values for Rset, Cx and Cy. Table 1 illustrates the values selected for the design and the effect of the values of the bandwidth, noise and the period of T2.

Component Value Bandwidth (Hz)

Noise (mg) Period of T2 (ms)

Cx and Cy 0.1 µF 50 4.2 - Rset 1.24 MΩ - - 10

Table 1- Component value selection for ADXL202EB taken from [5]

In the table above, we illustrate the values to be selected for building the circuit. Blank entries in the table indicate that the selection of the component does not affect the parameter under consideration. Since 1.24 MΩ is not a standard, we have chosen 1.5 MΩ for the design. This results in a period of 12.4 ms. I calculated this using the “XL202 Interactive Datasheet” from analog devices. The desired acquisition rate from these component values is calculated at 18 samples per second per channel provided the part is powered up 100 % of the time. Thus, the maximum time available to acquire the two channels was approximately 27.8 ms with 3 ms for calculations. Thus, the time remaining for signal acquisition is 24.8 ms. The value of T2 will vary with temperature changes and has jitter so we will need some compensation. 2.2 Verification of Operation of Accelerometer To ensure proper operation of the evaluation board, I measured the Xout and Yout duty cycles on the Oscilloscope and verified that it matched the T2 value specified in Table 1. Connecting Pin B (Self Test) of the 5-Pin Header to VDD will change the duty cycle by 10%. This test also verifies correct operation of the device. 2.3 Method of Calculation of Acceleration from the Duty Cycle Output The duty cycle output derived from the ADXL202EB [5] needs to be converted to the acceleration output ‘g’. We have used the method described in [3] to calculate the acceleration from the duty cycle output. The formula for calculating the acceleration taken from [3] is given below: Acceleration (in g) = (T1/T2) -50 % (1) 12.5 % T1 and T2 are the time periods as observed in the figure 2.3. We will use fixed point math to calculate the results since we are using an 8-bit microcontroller in the design, although a floating point math would give more precise results.

Page 5: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 5 of 29

2.4 PIC Microcontroller From the previous calculations in section 2.1, we have got the value of acquisition time as 24.8 ms. Since we are using a 1 MHz timer frequency for the microcontroller, a counter that counts up to 24800 counts, or a 15-bit counter is required. We have selected PIC16F877 [4] for the design since it runs on a 4 MHz frequency and a 1 MHz timer frequency. In addition, we will require some memory to store the T1 and T2 values corresponding to the 50 % duty cycle. These values are subtracted from the values of T1 and T2 which would change when the device tilts to get the acceleration result. Thus, we will utilize the data memory EEPROM of the PIC Microcontroller to store the values corresponding to the 50 % duty cycle. I have used the Timer0 port to count the duty cycle output. Although Timer0 is an 8-bit counter and a 15-bit counter is required, we can utilize another 8-bit register that will function as a counter incrementing the overflow. After performing arithmetic operations on the PIC, we get the final acceleration result. The final signed result of the X-axis tilt is displayed on the 7-segment LED Display. The output result would be between +/- 90. Hence, we will require a 2 digits output display. We have used a multiplexed output scheme (a single port output for the 2 digits.) for displaying the digits on the 7-segment LED Display. The Port C drives the 7-segment display and uses 2 pins of Port D to select which display will be on. We get the impression of a continuously updating display due to persistence of vision. The push button S1 will calibrate the device and store the readings corresponding to the 50 % duty cycle. Before calibration, we ensure that the device is on level ground. After calibration, we calculate the actual values of the duration of T1 in the X and Y direction from the calibrated values and temperature compensated values of T2. ZXactual = (ZXcal × T2actual) / T2cal (2) ZYactual = (ZYcal × T2actual) / T2cal (3) ZXactual and ZYactual in (2) and (3) represent the actual values of the duration of T1 in the X and Y direction. T2actual and T2cal represent the actual and calibrated values of T2. ZXcal and ZYcal represent the duration of T1 when the device is calibrated or the duty cycle is 50 %. Note that ‘actual’ values are values that are calculated due to temperature variation of T2 as discussed earlier. XAccel = [K × (T1x – ZXActual)] / T2actual (4) YAccel = [K × (T1y – ZYActual)] / T2actual (5) XAccel and YAccel is the final measured value of tilt. K is the scale factor using which we scale the output to value to the range of +/- 90. Note that (2), (3), (4) and (5) are taken from [3].

Page 6: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 6 of 29

3. Hardware Schematic

Figure 3.1 Hardware schematic of design

Page 7: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 7 of 29

3.1 Description of Schematic We start with the ADXL202EB [5] block. C1 and C2 in the schematic represent the Cx and Cy capacitors described in the table 1. The ADXL202EB has a 5-Pin Header as discussed in section 2. They are marked as A, B, C, D and E. Pin A (Vdd) is connected to a 5 V supply. Pin E (COM) is connected to ground. Pin B (Self Test) is left unconnected. The Pins C (Xout) and D (Yout) are the duty cycle output pins of the ADXL202EB. Finally, the Rset pin is connected to the Rset resistor which we discussed in section 2. We connect the Xout and Yout pins to the RA0 and RA1 pins of the microcontroller. They are configured as digital inputs since Xout and Yout are in digital format. Port C Pins RC0-RC6 drive the 7-segment LED Display. Since the decimal point is not needed for our design, we can drive the LED Display with 7 pins instead of the standard 8 pins. As discussed earlier in section 2.3, we are multiplexing the pins of Port C to display two digits. The Port D Pins RD0 and RD1 are used to enable the 7-segment display through a PNP transistor 2N4403 [9]. We require these PNP Transistors because the 7-segment display unit LDD-A814RI [10] is a common anode display. The microcontroller uses 3.6864 MHz oscillator whose output is connected to the pin 13 (CLKin) of the PIC. We connect a push button switch S1 to pin 37 (RB4). This button as discussed in section 2.3 stores the calibration values to the data EEPROM. The polarity LED is connected to pin 34 (RB1). Table 2 lists the parts used for the project.

Part Quantity Cost ($ per unit) 1N4003 Diodes 1 0.1

Crystal Oscillator 1 3 LED 1 0.2

10 kΩ resistors 2 0.05 1 kΩ resistors 2 0.05 100 Ω resistor 1 0.05

1.5 MΩ resistor 1 0.05 470 Ω network of 8 resistors 1 1

0.1 µF Capacitor 6 0.2 7805 Voltage Regulator 1 1

LDD-A814RI LED Display 1 4 Push button switch 1 0.5

2N4403 PNP Transistor 1 0.5 ADXL202EB Evaluation Board 1 68 PIC Microcontroller 16F877 [4] 1 10

Table 2-Parts List

Total project cost is - $89.80

Page 8: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 8 of 29

4. Flowchart Representation of Assembly Code

Start

Initialize all registers

Read previously storedvalues from EEPROM.

IsPush Button

pressed?

Read timer count values B

Save these values in the calibration constant

variables.

Write constants to data EEPROM

Is push button released?

A

A B

Find Zactual from calibrated values

Calculate Tilt

Check polarity of Tilt

Record T1X and T1Y

Calculate T2

Return

Is tilt negative?

Take 2's complement and glow LED

Display Tilt on 7 segment LED display

End

N

Y

Y

N

Y

N

Figure 4.1 Software Flowchart

Page 9: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 9 of 29

4.1 Explanation of Flowchart As in all assembly language codes, we initialize the values of each register used in the design. This step will setup the appropriate port pins used in the design in input or output modes. When power is applied to the device, it will first restore the previously stored values in the data EEPROM. These values are used in the calculation of tilt. If the readings don’t seem to be accurate, the device will need calibration. We press the calibration push button and record the T1x, T1y and T2 values from the Timer0 unit. These values are then stored in ZXcal, ZYcal and T2cal variables as we had discussed earlier in section 2.4. These values are then written to the data EEPROM which can be retrieved later when we read the data EEPROM. After the calibration step, we wait for the push button to be released. The next step is to read the duty cycle outputs again which will read the current value. The next step is calculating the weighted values of the value currently measured using the calibration values. The results are stored in ZXActual and ZYActual. Using ZXActual and ZYActual, we calculate the final tilt and store it in XAccel and YAccel. The XAccel and YAccel are signed representations of the tilt values. Hence, we test the MSB of these variables to detect whether the tilt value is negative or positive. Depending on the state of the MSB, we will turn on the polarity LED and take the 2’s complement of the signed negative value of XAccel. If the MSB is 0, then the XAccel value is positive and the polarity LED remains off. The final step is to display the XAccel value on the 2 digit 7-segment LED Display. All of the above routines are illustrated in the code listing in the Appendix. The RestoreCal routine is used to recall the previously stored values of the calibration constants. The CheckCal routine will check if the push button is pressed and store the new calibration constants. The FindZActual routine calibrates the readings and returns the weighted version of the present measured values. CalculateAccel calculates the signed value of the tilt reading. CheckPol will check the sign of the results while taking appropriate action. Finally, DisplayAccel will display the results on the 7-segment display. 4.2 Software Used As mentioned earlier the code is written in assembly language. The compilation and simulation of the code is done in Microchip’s MPLAB software v5.70.40. The MPLAB software converts the assembly code to a hex file which is then burned on to the program memory of the PIC16F877 [4] using the in-circuit debugger (ICD). 5. Conclusions and Further Modifications The tilt measurement device can find many applications where we have to maintain stability. Coupling the application with our device will provide the application user with the tilt information on the display. Hence, he can manually work towards achieving

Page 10: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 10 of 29

stability, or design a separate control system with the same purpose. The user can confirm that the control system is functioning correctly by monitoring the visual readout provided by our device. The typical applications of the device would be:

a. Robotics b. Machine monitoring c. Construction industry d. Defense industry e. Consumer Electronics

The project has plenty of scope for improvements. The 7-segment display on the demo board did not show a stable zero reading when at rest. This is an extremely important as the user may get confused about the digits being displayed. This may be due to ambiguity in the digits to be displayed at that particular instant. I had tried to use another push button to switch between the X-Axis and the Y-Axis displays, but this made the display unstable i.e. the readings were displayed only momentarily and seemed to be correct. This may be due to the debouncing of the push button switch. Using an electronic switch instead would solve this problem. Many applications require storage of the tilt values to a database. We can incorporate RS-232 Communication between the PIC and the PC. The tilt values will be serially transmitted using this protocol. Visual Basic is the preferred software for creating a GUI to view the transmitted information on the PC. We can store these readings to a database created in Microsoft Access or Excel. Wireless transmission is another possibility. Another application would be to store threshold data for the tilt on the data EEPROM and actuate a control system or indicator, if the application exceeds the threshold limits. Recent MEMS Accelerometers produce direct angle output over the SPI Port and do not require any external calculation circuitry. Interfacing this device with the PIC and an LCD or an LED Display would be a challenge. 6. References:

[1] Rodger Richey, “AN715 - Measure Tilt Using PIC16F84A and ADXL202,” www.microchip.com, 2002.

[2] Analog Devices Inc., “ADXL202 Datasheet,” www.analog.com, 2000. [3] Harvey Weinberg, “AN604-Using the ADXL202 duty cycle output,” www.analog.com, 2002. [4] Microchip Inc., “PIC16F87X Datasheet,” www.microchip.com, 2001. [5]Analog Devices Inc., “Dual Axis Accelerometer Evaluation Board ADXL202EB,” www.analog.com, 2003. [6] Nebojsa Matic, “PIC microcontrollers, for beginners too.” [7] Tim Green, “Embedded System Programming with the PIC16F877.” [8] Myke Predko, “Programming and Customizing the PIC Microcontroller.” [9] Fairchild Semiconductor Corp., “2N4403 general purpose amplifier datasheet,” www.fairchildsemi.com, 2001. [10] Lumex Inc., “LDD-A814RI datasheet,” www.lumex.com, 1999.

Page 11: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 11 of 29

APPENDIX: Code Listing ;********************************************************************** 00002 ; 00003 ; Tilt Measurement device 00004 ; Adapted from Application Note AN715 by Microchip Technology Inc 00005 ; Written by Rodger Richey for the PIC16F84A 00006 ; Modified by Gaurav Konchady for the PIC16F877 00007 ; 00008 ;*********************************************************************** 00009 list p=16f877 00010 include "p16f877.inc" 00001 LIST 00002 ; P16F877.INC Standard Header File, Version 1.00 Microchip Technology, Inc. 00373 LIST 00011 errorlevel -302 00012 errorlevel -203 00013 errorlevel -205 00014 00015 ;PORTA defines 00016 #define XOUT 0 00017 #define YOUT 1 00018 00019 ;PORTB defines 00020 #define CAL 4 00021 00000002 00022 KHi equ 0x02 000000D0 00023 KLo equ 0xd0 00024 00025 ;==================================================================== 00026 ; User defined variables 00027 ;==================================================================== 00028 cblock 0x20 ;bank 0 00000020 00029 T1XHi 00000021 00030 T1XLo 00000022 00031 ArgL 00000023 00032 ArgH 00000024 00033 AccHi 00000025 00034 AccLo 00000026 00035 DivCnt 00000027 00036 PRODW3 00000028 00037 PRODW2 00000029 00038 PRODW1 0000002A 00039 PRODW0 0000002B 00040 DIV0 0000002C 00041 DIV1 0000002D 00042 ANS0 0000002E 00043 ANS1 0000002F 00044 T2Hi

Page 12: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 12 of 29

00000030 00045 T2Lo 00000031 00046 T1YStartLo 00000032 00047 T1YStartHi 00000033 00048 T1YEndLo 00000034 00049 T1YEndHi 00000035 00050 T1YHi 00000036 00051 T1YLo 00000037 00052 ZXcalHi 00000038 00053 ZXcalLo 00000039 00054 ZYcalHi 0000003A 00055 ZYcalLo 0000003B 00056 T2calHi 0000003C 00057 T2calLo 0000003D 00058 ZXActualHi 0000003E 00059 ZXActualLo 0000003F 00060 ZYActualHi 00000040 00061 ZYActualLo 00000041 00062 XAccel 00000042 00063 YAccel 00000043 00064 Temp0 00000044 00065 Temp1 00000045 00066 Temp2 00000046 00067 Temp3 00000047 00068 Timer0H 00000048 00069 Dig1 00000049 00070 Dig2 0000004A 00071 Dig3 0000004B 00072 Digtemp 0000004C 00073 G 0000004D 00074 Count1 0000004E 00075 Count2 0000004F 00076 Tp 00077 endc 00078 00079 cblock 0x120 ;bank 2 00000120 00080 EADR 00000121 00081 EDATA 00082 endc 00083 00084 ;********************************************************************

00085 ; Macro definitions 00086 ; This macro takes a 3 digit number and 00087 ; separates into 3 single digit numbers

00088 ; Adapted from http://www.mikroe.com/en/books/picbook/7_08chapter.htm 00089 ;******************************************************************** 00090 00091 digbyte macro par0 00092 local Pon0 00093 local Exit1 00094 local Exit2 00095 local Pozitiv 00096 00097 clrf Dig1 00098 clrf Dig2

Page 13: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 13 of 29

00099 clrf Dig3 00100 Pozitiv 00101 movf par0,w 00102 movwf Digtemp 00103 movlw .100 00104 Pon0 00105 incf Dig1,F 00106 subwf Digtemp,f 00107 btfsc STATUS,C 00108 goto Pon0 00109 decf Dig1,f 00110 addwf Digtemp,f 00111 Exit1 00112 movlw .10 00113 incf Dig2,f 00114 subwf Digtemp,f 00115 btfsc STATUS,C 00116 goto Exit1 00117 decf Dig2,f 00118 addwf Digtemp,f 00119 Exit2 00120 movf Digtemp,w 00121 movwf Dig3 00122 endm 00123 00124 ;****************************************************************** 00125 00126 ;Start of executable code 00127 0000 00128 org 0x00 0000 0000 00129 nop 0001 2808 00130 goto Start ;Go to start of program 00131 00132 ;****************************************************************** 00133 00134 ;Interrupt vector 00135 0004 00136 org 0x04 0004 0AC7 00137 incf Timer0H,F 0005 110B 00138 bcf INTCON,T0IF 0006 118B 00139 bcf INTCON,RBIE 0007 0009 00140 retfie 00141 00142 ;****************************************************************** 00143 ;Start of program 00144 0008 00145 Start 0008 1283 1303 00146 banksel PORTA 000A 0185 00147 clrf PORTA 000B 0186 00148 clrf PORTB 000C 0187 00149 clrf PORTC 000D 1683 1303 00150 banksel TRISA 000F 300F 00151 movlw B'00001111' ;Set up the I/O ports 0010 0085 00152 movwf TRISA

Page 14: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 14 of 29

0011 3006 00153 movlw B'00000110' 0012 009F 00154 movwf ADCON1 0013 3010 00155 movlw B'00010000' 0014 0086 00156 movwf TRISB 0015 0187 00157 clrf TRISC 0016 0188 00158 clrf TRISD 0017 300F 00159 movlw B'00001111' 0018 0081 00160 movwf OPTION_REG 0019 1283 1303 00161 banksel PORTA ;Bank0 00162 001B 21FC 00163 call RestoreCal ;Restore calibration constants 001C 178B 00164 bsf INTCON,GIE 00165 001D 00166 Main 001D 20F5 00167 call CheckCal ;Check if need to calibrate 001E 2024 00168 call ReadAccel ;Read the acceleration 001F 2064 00169 call FindZActual ;Calibrate readings 0020 2089 00170 call CalculateAccel ;Calculate tilt 0021 2195 00171 call CheckPol; Check sign of result 0022 219D 00172 call DisplayAccel ;Display results 0023 281D 00173 goto Main;Do it again 00174 00175 ;==================================================================== 00176 ;=========== Acceleration Measurement 00177 00178 ;******************************************************************** 00179 ;ReadAccel 00180 ; This subroutine acquires and calculates T1X, T1Y, and T2. 00181 ; T1X is in registers T1XHi,T1XLo 00182 ; T1Y is in registers T1YHi,T1YLo 00183 ; T2 is in registers T2Hi,T2Lo 00184 ;******************************************************************** 0024 00185 ReadAccel 0024 00186 EDGE1 0024 1805 00187 btfsc PORTA,XOUT ;Wait for low on XOUT 0025 2824 00188 goto EDGE1 0026 00189 EDGE2 0026 1C05 00190 btfss PORTA,XOUT ;Wait for high on XOUT 0027 2826 00191 goto EDGE2 0028 0181 00192 clrf TMR0 ;Clear Timer 0029 01C7 00193 clrf Timer0H 002A 110B 00194 bcf INTCON,T0IF ;Enable Timer0 overflow interrupt 002B 168B 00195 bsf INTCON,T0IE 002C 00196 EDGE3 002C 1805 00197 btfsc PORTA,XOUT ;Look for falling edge on XOUT 002D 282C 00198 goto EDGE3 002E 0801 00199 movf TMR0,W ;Save Timer0H:TMR0 as T1X 002F 00A1 00200 movwf T1XLo 0030 0847 00201 movf Timer0H,W 0031 00A0 00202 movwf T1XHi

Page 15: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 15 of 29

0032 00203 EDGE4 0032 1885 00204 btfsc PORTA,YOUT ;Look a low level on YOUT 0033 2832 00205 goto EDGE4 0034 00206 EDGE5 0034 1C85 00207 btfss PORTA,YOUT ;Look for rising edge on YOUT 0035 2834 00208 goto EDGE5 0036 0801 00209 movf TMR0,W ;Save Timer0H:TMR0 for start 0037 00B1 00210 movwf T1YStartLo ;of T1Y 0038 0847 00211 movf Timer0H,W 0039 00B2 00212 movwf T1YStartHi 003A 00213 EDGE6 003A 1885 00214 btfsc PORTA,YOUT ;Look for falling edge on YOUT 003B 283A 00215 goto EDGE6 003C 0801 00216 movf TMR0,W ;Save Timer0H:TMR0 as the end 003D 00B3 00217 movwf T1YEndLo ;of T1Y 003E 0847 00218 movf Timer0H,W 003F 00B4 00219 movwf T1YEndHi 0040 128B 00220 bcf INTCON,T0IE 00221 0041 0834 00222 movf T1YEndHi,W ;Calculate T1Y 0042 00A4 00223 movwf AccHi 0043 0833 00224 movf T1YEndLo,W 0044 00A5 00225 movwf AccLo 0045 0832 00226 movf T1YStartHi,W 0046 00A3 00227 movwf ArgH 0047 0831 00228 movf T1YStartLo,W 0048 00A2 00229 movwf ArgL 0049 210F 00230 call Sub16x16 004A 0824 00231 movf AccHi,W 004B 00B5 00232 movwf T1YHi 004C 0825 00233 movf AccLo,W 004D 00B6 00234 movwf T1YLo 004E 0834 00235 movf T1YEndHi,W ;CALCULATE T2 004F 00A4 00236 movwf AccHi ;2*(T2Hi,T2Lo) = (T1YEndHi:T1YEndLo)+ 0050 0833 00237 movf T1YEndLo,W ;(T1YStartHi:T1YStartLo)- (T1XHi:T1XLo) 0051 00A5 00238 movwf AccLo 0052 0832 00239 movf T1YStartHi,W 0053 00A3 00240 movwf ArgH 0054 0831 00241 movf T1YStartLo,W 0055 00A2 00242 movwf ArgL 0056 2108 00243 call Add16x16 ;ACCHI,ACCLO=(T1YEndHi:T1YEendLo)+ 0057 0820 00244 movf T1XHi,W ; (T1YStartHi:T1YStartLo) 0058 00A3 00245 movwf ArgH 0059 0821 00246 movf T1XLo,W 005A 00A2 00247 movwf ArgL 005B 210F 00248 call Sub16x16 ;ACCHI,ACCLO = 2*T2 005C 1003 00249 bcf STATUS,C 005D 0CA4 00250 rrf AccHi,F 005E 0CA5 00251 rrf AccLo,F 005F 0824 00252 movf AccHi,W 0060 00AF 00253 movwf T2Hi 0061 0825 00254 movf AccLo,W

Page 16: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 16 of 29

0062 00B0 00255 movwf T2Lo 0063 0008 00256 return 00257 00258 ;******************************************************************** 0064 00259 FindZActual 00260 ; This subroutine finds the value of 00261 ; ZActual for X and Y axes. 00262 ; Output is ZXActualHi & ZXActualLo for the X-axis and 00263 ; ZYActualHi & ZXActualLo for the Y-axis. 00264 ;******************************************************************** 00265 0064 00266 FindZActual 0064 0837 00267 movf ZXcalHi,W ;First the X-axis 0065 00A4 00268 movwf AccHi 0066 0838 00269 movf ZXcalLo,W 0067 00A5 00270 movwf AccLo 0068 082F 00271 movf T2Hi,W 0069 00A3 00272 movwf ArgH 006A 0830 00273 movf T2Lo,W 006B 00A2 00274 movwf ArgL ;PRODW3,PRODW2,PRODW1,PRODW0 = 006C 211B 00275 call Mul16x16 ;(ZXCAL_HI,ZXCAL_LO)*(T2HI,T2LO) 006D 083B 00276 movf T2calHi,W 006E 00AC 00277 movwf DIV1 006F 083C 00278 movf T2calLo,W 0070 00AB 00279 movwf DIV0 0071 2166 00280 call Div32x16 ;ANS1,ANS0=(ZXcal * T2)/T2cal 0072 082E 00281 movf ANS1,W 0073 00BD 00282 movwf ZXActualHi 0074 082D 00283 movf ANS0,W 0075 00BE 00284 movwf ZXActualLo 0076 0839 00285 movf ZYcalHi,W ;Same thing for the Y-axis 0077 00A4 00286 movwf AccHi 0078 083A 00287 movf ZYcalLo,W 0079 00A5 00288 movwf AccLo 007A 082F 00289 movf T2Hi,W 007B 00A3 00290 movwf ArgH 007C 0830 00291 movf T2Lo,W 007D 00A2 00292 movwf ArgL ;PRODW3,PRODW2,PRODW1,PRODW0 = 007E 211B 00293 call Mul16x16 ;(ZYCAL_HI,ZYCAL_LO)*(T2HI,T2LO) 007F 083B 00294 movf T2calHi,W 0080 00AC 00295 movwf DIV1 0081 083C 00296 movf T2calLo,W 0082 00AB 00297 movwf DIV0 0083 2166 00298 call Div32x16 ;ANS1,ANS0=(ZYcal*T2)/T2cal 0084 082E 00299 movf ANS1,W 0085 00BF 00300 movwf ZYActualHi 0086 082D 00301 movf ANS0,W 0087 00C0 00302 movwf ZYActualLo 0088 0008 00303 return 00304 00305 ;********************************************************************

Page 17: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 17 of 29

00306 ;CalculateAccel 00307 ; This subroutine performs the acceleration 00308 ; calculation for each axis. The formula 00309 ; ACCEL = [K * (T1-Zactual) / T2actual] 00310 ; Output is XAccel and YAccel 00311 ;******************************************************************** 0089 00312 CalculateAccel 0089 083D 00313 movf ZXActualHi,W ;Check if acceleration is positive 008A 0220 00314 subwf T1XHi,W ;or negative by comparing 008B 1C03 00315 btfss STATUS,C ;T1X and ZXactual 008C 28A9 00316 goto CA1 ;Jump if T1X < ZXactual 008D 1D03 00317 btfss STATUS,Z ;Test if T1XHI=ZX_ACTUAL_HI 008E 2893 00318 goto CA2 ;Jump if T1X > ZXactual 008F 083E 00319 movf ZXActualLo,W 0090 0221 00320 subwf T1XLo,W 0091 1C03 00321 btfss STATUS,C 0092 28A9 00322 goto CA1 ;Jump if TX1 < ZXactual 0093 00323 CA2 0093 0820 00324 movf T1XHi,W ;T1X - ZXactual 0094 00A4 00325 movwf AccHi 0095 0821 00326 movf T1XLo,W 0096 00A5 00327 movwf AccLo 0097 083D 00328 movf ZXActualHi,W 0098 00A3 00329 movwf ArgH 0099 083E 00330 movf ZXActualLo,W 009A 00A2 00331 movwf ArgL 009B 210F 00332 call Sub16x16 009C 3002 00333 movlw KHi 009D 00A3 00334 movwf ArgH 009E 30D0 00335 movlw KLo 009F 00A2 00336 movwf ArgL 00A0 211B 00337 call Mul16x16 ;PRODW3,PRODW2,PRODW1,PRODW0 00338 ;K * (T1X - ZXactual) 00A1 082F 00339 movf T2Hi,W 00A2 00AC 00340 movwf DIV1 00A3 0830 00341 movf T2Lo,W 00A4 00AB 00342 movwf DIV0 00A5 2166 00343 call Div32x16 ;ANS1:ANS0= 00A6 082D 00344 movf ANS0,W ; [K*(T1X-ZXactual)]/T2actual 00A7 00C1 00345 movwf XAccel ;The result will be a signed 8-bit # 00A8 28BF 00346 goto DoYCalc 00A9 00347 CA1 00A9 083D 00348 movf ZXActualHi,W ;ZXactual - T1X 00AA 00A4 00349 movwf AccHi 00AB 083E 00350 movf ZXActualLo,W 00AC 00A5 00351 movwf AccLo 00AD 0820 00352 movf T1XHi,W 00AE 00A3 00353 movwf ArgH 00AF 0821 00354 movf T1XLo,W 00B0 00A2 00355 movwf ArgL 00B1 210F 00356 call Sub16x16 00B2 3002 00357 movlw KHi 00B3 00A3 00358 movwf ArgH 00B4 30D0 00359 movlw KLo

Page 18: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 18 of 29

00B5 00A2 00360 movwf ArgL 00B6 211B 00361 call Mul16x16 ;PRODW3,PRODW2,PRODW1,PRODW0 00362 ;K * (ZXactual - T1X) 00B7 082F 00363 movf T2Hi,W 00B8 00AC 00364 movwf DIV1 00B9 0830 00365 movf T2Lo,W 00BA 00AB 00366 movwf DIV0 00BB 2166 00367 call Div32x16 ;ANS1,ANS0 = 00BC 092D 00368 comf ANS0,W ; [K*(ZXactual-T1X)]/T2actual 00BD 3E01 00369 addlw 0x01 ;The result will be a signed 8- bit # 00BE 00C1 00370 movwf XAccel 00BF 00371 DoYCalc 00BF 083F 00372 movf ZYActualHi,W ;Check if acceleration is positive 00C0 0235 00373 subwf T1YHi,W ;or negative by comparing 00C1 1C03 00374 btfss STATUS,C ;T1Y and ZYactual 00C2 28DF 00375 goto CA3 ;Jump if T1Y < ZYactual 00C3 1D03 00376 btfss STATUS,Z ;Test if T1YHI=ZY_ACTUAL_HI 00C4 28C9 00377 goto CA4 ;Jump if T1Y > ZYactual 00C5 0840 00378 movf ZYActualLo,W 00C6 0236 00379 subwf T1YLo,W 00C7 1C03 00380 btfss STATUS,C 00C8 28DF 00381 goto CA3 ;Jump if TY1 < ZYactual 00C9 00382 CA4 00C9 0835 00383 movf T1YHi,W ;T1Y - ZYactual 00CA 00A4 00384 movwf AccHi 00CB 0836 00385 movf T1YLo,W 00CC 00A5 00386 movwf AccLo 00CD 083F 00387 movf ZYActualHi,W 00CE 00A3 00388 movwf ArgH 00CF 0840 00389 movf ZYActualLo,W 00D0 00A2 00390 movwf ArgL 00D1 210F 00391 call Sub16x16 00D2 3002 00392 movlw KHi 00D3 00A3 00393 movwf ArgH 00D4 30D0 00394 movlw KLo 00D5 00A2 00395 movwf ArgL 00D6 211B 00396 call Mul16x16 ;PRODW3,PRODW2,PRODW1,PRODW0 00397 ;K * (T1Y - ZYactual) 00D7 082F 00398 movf T2Hi,W 00D8 00AC 00399 movwf DIV1 00D9 0830 00400 movf T2Lo,W 00DA 00AB 00401 movwf DIV0 00DB 2166 00402 call Div32x16 ;ANS1,ANS0 = 00DC 082D 00403 movf ANS0,W ; [K*(T1Y-ZYactual)]/T2actual 00DD 00C2 00404 movwf YAccel ;The result will be a signed 8-bit # 00DE 0008 00405 return 00DF 00406 CA3 00DF 083F 00407 movf ZYActualHi,W ;ZYactual - T1Y 00E0 00A4 00408 movwf AccHi 00E1 0840 00409 movf ZYActualLo,W 00E2 00A5 00410 movwf AccLo 00E3 0835 00411 movf T1YHi,W 00E4 00A3 00412 movwf ArgH 00E5 0836 00413 movf T1YLo,W

Page 19: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 19 of 29

00E6 00A2 00414 movwf ArgL 00E7 210F 00415 call Sub16x16 00E8 3002 00416 movlw KHi 00E9 00A3 00417 movwf ArgH 00EA 30D0 00418 movlw KLo 00EB 00A2 00419 movwf ArgL 00EC 211B 00420 call Mul16x16 ;PRODW3,PRODW2,PRODW1,PRODW0 00421 ;K * (ZYactual - T1Y) 00ED 082F 00422 movf T2Hi,W 00EE 00AC 00423 movwf DIV1 00EF 0830 00424 movf T2Lo,W 00F0 00AB 00425 movwf DIV0 00F1 2166 00426 call Div32x16 ;ANS1,ANS0 = 00F2 092D 00427 comf ANS0,W ;[K*(ZYactual-T1Y)]/T2actual 00F3 3E01 00428 addlw 0x01 ;The result will be a signed 8- bit # 00F4 00C2 00429 movwf YAccel 00430 00431 ;******************************************************************** 00432 ;CheckCal 00433 ; This subroutine reads the CAL pushbutton switch (RB4) and if 00434 ; it is low, performs a simple calibration routine. 00435 ;******************************************************************** 00F5 00436 CheckCal 00F5 1A06 00437 btfsc PORTB,CAL ;Is RB4 low? 00F6 0008 00438 return ;If not then exit routine 00F7 2024 00439 call ReadAccel ;If yes then perform a read cycle 00F8 082F 00440 movf T2Hi,W ;Save the measured values in 00F9 00BB 00441 movwf T2calHi ; the calibration registers 00FA 0830 00442 movf T2Lo,W 00FB 00BC 00443 movwf T2calLo 00FC 0820 00444 movf T1XHi,W 00FD 00B7 00445 movwf ZXcalHi 00FE 0821 00446 movf T1XLo,W 00FF 00B8 00447 movwf ZXcalLo 0100 0835 00448 movf T1YHi,W 0101 00B9 00449 movwf ZYcalHi 0102 0836 00450 movf T1YLo,W 0103 00BA 00451 movwf ZYcalLo 0104 21E3 00452 call WriteCal ;Write the calibration data to EEPROM 0105 00453 CCLoop 0105 1E06 00454 btfss PORTB,CAL ;Wait for pushbutton switch 0106 2905 00455 goto CCLoop ; to be released 0107 0008 00456 return 00457 00458 ;******************************************************************** 00459 00460 ;==================================================================== 00461 ;Mathematical Operations

Page 20: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 20 of 29

00462 ;==================================================================== 00463 ;******************************************************************** 00464 ;Add16x16 00465 ; This subroutine performs a 16-bit by 16- bit addition. 00466 ; Note that this routine does not check for possible overflow 00467 ; results i.e., 17-bit sum. 00468 ; Inputs are AccHi:AccLo and ArgH:ArgL 00469 ; Result is in AccHi:AccLo 00470 ; (AccHi:AccLo) = (AccHi:AccLo)+(ArgH:ArgL) 00471 ;******************************************************************** 0108 00472 Add16x16 0108 0822 00473 movf ArgL,W ;Add low bytes together 0109 07A5 00474 addwf AccLo,F 010A 1803 00475 btfsc STATUS,C ;Check for carry out of addtion 010B 0AA4 00476 incf AccHi,F ;If yes, increment AccHi 010C 0823 00477 movf ArgH,W ;Add high bytes together 010D 07A4 00478 addwf AccHi,F 010E 0008 00479 return 00480 00481 ;******************************************************************** 00482 ;Sub16x16 00483 ; This subroutine performs a 16-bit by 16- bit subtraction. 00484 ; Inputs are AccHi:AccLo and ArgH:ArgL 00485 ; Result is in AccHi:AccLo 00486 ; (AccHi:AccLo) = (AccHi:AccLo)-(ArgH:ArgL) 00487 ;******************************************************************** 010F 00488 Sub16x16 010F 09A2 00489 comf ArgL,F ;2’s complement ArgH:ArgL 0110 0AA2 00490 incf ArgL,F 0111 1903 00491 btfsc STATUS,Z 0112 03A3 00492 decf ArgH,F 0113 09A3 00493 comf ArgH,F 0114 0822 00494 movf ArgL,W ;Now perform a 16-bit addition 0115 07A5 00495 addwf AccLo,F 0116 1803 00496 btfsc STATUS,C 0117 0AA4 00497 incf AccHi,F 0118 0823 00498 movf ArgH,W 0119 07A4 00499 addwf AccHi,F 011A 0008 00500 return 00501 00502 ;******************************************************************** 00503 ;Mul16x16 00504 ; This subroutine performs a 16-bit by 16- bit multiplication. 00505 ; It produces a 32-bit number. Multiplication by 0 is checked 00506 ; and performed correctly, ie, A * 0 = 0. 00507 ; Inputs are (AccHi:AccLo) and (ArgH:ArgL)

Page 21: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 21 of 29

00508 ; Output is (PRODW3:PRODW2:PRODW1:PRODW0) 00509 ; (PRODW3:PRODW2:PRODW1:PRODW0) = (AccHi:AccLo) * (Argh:ArgL) 00510 ;******************************************************************** 011B 00511 Mul16x16 011B 01C3 00512 clrf Temp0 ;Clear the temporary variables used 011C 01C4 00513 clrf Temp1 ;in this routine 011D 01C5 00514 clrf Temp2 011E 01C6 00515 clrf Temp3 011F 01AA 00516 clrf PRODW0 0120 01A9 00517 clrf PRODW1 0121 01A8 00518 clrf PRODW2 0122 01A7 00519 clrf PRODW3 0123 0825 00520 movf AccLo,W 0124 00C3 00521 movwf Temp0 ;Move contents of AccHi:AccLo 0125 0824 00522 movf AccHi,W ;into Temp1:Temp0 0126 00C4 00523 movwf Temp1 0127 08A4 00524 movf AccHi,F ;Test if AccHi:AccLo = 0000 0128 1D03 00525 btfss STATUS,Z 0129 292D 00526 goto CheckNext ;AccHi:AccLo not zero 012A 08A5 00527 movf AccLo,F 012B 1903 00528 btfsc STATUS,Z 012C 2961 00529 goto Equal0 ;AccHi:AccLo = 0000 012D 00530 CheckNext 012D 08A3 00531 movf ArgH,F ;Test if ArgH:ArgL = 0000 012E 1D03 00532 btfss STATUS,Z 012F 2933 00533 goto DoMultiply ;ArgH:ArgL not zero 0130 08A2 00534 movf ArgL,F 0131 1903 00535 btfsc STATUS,Z 0132 2961 00536 goto Equal0 ;ArgH:ArgL = 0000 0133 00537 DoMultiply 0133 08A3 00538 movf ArgH,F ;Test if ArgH:ArgL has been reduced 0134 1D03 00539 btfss STATUS,Z ;to 0 0135 2939 00540 goto TestLSB ;ArgH:ArgL has not been reduced to 0 0136 08A2 00541 movf ArgL,F 0137 1903 00542 btfsc STATUS,Z 0138 0008 00543 return ;ArgH:ArgL has been reduced to zero 00544 ;so multiplication is done 0139 00545 TestLSB 0139 1003 00546 bcf STATUS,C ;Shift ArgH:ArgL right 013A 0CA3 00547 rrf ArgH,F 013B 0CA2 00548 rrf ArgL,F 013C 1C03 00549 btfss STATUS,C ;Is LSb of ArgH:ArgL = 1 013D 295B 00550 goto DoShift ;Jump if LSb = 0 013E 0843 00551 movf Temp0,W ;If LSb = 1 then 013F 07AA 00552 addwf PRODW0,F ;PRODW3:PRODW2:PRODW1:PRODW0 0140 1C03 00553 btfss STATUS,C ;PRODW3:PRODW2:PRODW1:PRODW0 + 0141 294B 00554 goto ADD2 ;Temp3:Temp2:Temp1:Temp0 0142 3001 00555 movlw 0x01 ;Add carry bit if necessary 0143 07A9 00556 addwf PRODW1,F 0144 1C03 00557 btfss STATUS,C 0145 294B 00558 goto ADD2

Page 22: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 22 of 29

0146 3001 00559 movlw 0x01 ;Add carry bit if PRODW1 overflows 0147 07A8 00560 addwf PRODW2,F ;as a result of the addition of the 0148 1C03 00561 btfss STATUS,C ;previous carry 0149 294B 00562 goto ADD2 014A 0AA7 00563 incf PRODW3,F 014B 00564 ADD2 014B 0844 00565 movf Temp1,W 014C 07A9 00566 addwf PRODW1,F 014D 1C03 00567 btfss STATUS,C 014E 2954 00568 goto ADD3 014F 3001 00569 movlw 0x01 0150 07A8 00570 addwf PRODW2,F 0151 1C03 00571 btfss STATUS,C 0152 2954 00572 goto ADD3 0153 0AA7 00573 incf PRODW3,F 0154 00574 ADD3 0154 0845 00575 movf Temp2,W 0155 07A8 00576 addwf PRODW2,F 0156 1C03 00577 btfss STATUS,C 0157 2959 00578 goto ADD4 0158 0AA7 00579 incf PRODW3,F 0159 00580 ADD4 0159 0846 00581 movf Temp3,W 015A 07A7 00582 addwf PRODW3,F 015B 00583 DoShift 015B 1003 00584 bcf STATUS,C ;Shift temp registers left 015C 0DC3 00585 rlf Temp0,F 015D 0DC4 00586 rlf Temp1,F 015E 0DC5 00587 rlf Temp2,F 015F 0DC6 00588 rlf Temp3,F 0160 2933 00589 goto DoMultiply 0161 00590 Equal0 0161 01AA 00591 clrf PRODW0 ;Since one arguement equals zero 0162 01A9 00592 clrf PRODW1 ;PRODW3,PRODW2,PRODW1,PRODW0 = 0 0163 01A8 00593 clrf PRODW2 0164 01A7 00594 clrf PRODW3 0165 0008 00595 return 00596 00597 ;******************************************************************** 00598 ;Div32x16 00599 ; This subroutine performs a 32-bit x 16- bit division. 00600 ; Division is performed by binary long division. 00601 ; Inputs are (PRODW3:PRODW2:PRODW1:PRODW0) and (DIV1:DIV0). 00602 ; Output is (ANS1:ANS0) 00603 ; (ANS1:ANS0) = (PRODW3:PRODW2:PRODW1:PRODW0) / (DIV1:DIV0) 00604 ;******************************************************************** 0166 00605 Div32x16 0166 01AE 00606 clrf ANS1 ;Clear the result registers

Page 23: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 23 of 29

0167 01AD 00607 clrf ANS0 0168 3011 00608 movlw 0x11 ;DivCnt = 17d 0169 00A6 00609 movwf DivCnt 016A 00610 DA1 016A 082C 00611 movf DIV1,W 016B 0227 00612 subwf PRODW3,W ;Is DIV1 > PRODW3 016C 1C03 00613 btfss STATUS,C 016D 2974 00614 goto NoSub ;Jump if DIV1 > PRODW3 016E 1D03 00615 btfss STATUS,2 ;Is DIV1 = PRODW3 016F 297D 00616 goto DoSubs ;Jump if DIV1 < PRODW3 0170 082B 00617 movf DIV0,W ;Is DIV0 > PRODW2 0171 0228 00618 subwf PRODW2,W 0172 1803 00619 btfsc STATUS,C 0173 297D 00620 goto DoSubs ;Jump if DIV0 < PRODW2 0174 00621 NoSub 0174 1003 00622 bcf STATUS,C ;Clear the carry bit 0175 0DAD 00623 rlf ANS0,F ;Add 0 to LSb of ANS1,ANS0 0176 0DAE 00624 rlf ANS1,F 0177 1003 00625 bcf STATUS,C ;Clear the carry bit 0178 0DAA 00626 rlf PRODW0,F ;Shift PRODW3,2,1,0 left 0179 0DA9 00627 rlf PRODW1,F 017A 0DA8 00628 rlf PRODW2,F 017B 0DA7 00629 rlf PRODW3,F 017C 2992 00630 goto ChkCnt 017D 00631 DoSubs 017D 0827 00632 movf PRODW3,W 017E 00A4 00633 movwf AccHi 017F 0828 00634 movf PRODW2,W 0180 00A5 00635 movwf AccLo 0181 082C 00636 movf DIV1,W 0182 00A3 00637 movwf ArgH 0183 082B 00638 movf DIV0,W 0184 00A2 00639 movwf ArgL 0185 210F 00640 call Sub16x16 ;(PRODW3:2) = (PRODW3:2)- (DIV1:0) 0186 0824 00641 movf AccHi,W 0187 00A7 00642 movwf PRODW3 0188 0825 00643 movf AccLo,W 0189 00A8 00644 movwf PRODW2 018A 1403 00645 bsf STATUS,C 018B 0DAD 00646 rlf ANS0,F 018C 0DAE 00647 rlf ANS1,F ;Add 1 to LSb of ANS1:ANS0 018D 1003 00648 bcf STATUS,C 018E 0DAA 00649 rlf PRODW0,F ;Shift PRODW3,2,1,0, left 018F 0DA9 00650 rlf PRODW1,F 0190 0DA8 00651 rlf PRODW2,F 0191 0DA7 00652 rlf PRODW3,F 0192 00653 ChkCnt 0192 0BA6 00654 decfsz DivCnt,F ;Check for 17 operations 0193 296A 00655 goto DA1 ;If not then loop 0194 0008 00656 return 00657 00658 ;******************************************************************** 00659 ; CheckPol checks the polarity of the result and 00660 ; takes twos complement if negative

Page 24: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 24 of 29

00661 ; Also used to glow the polarity LED 00662 ;******************************************************************** 0195 00663 CheckPol 0195 0186 00664 clrf PORTB ;clear polarity LED 0196 1FC1 00665 btfss XAccel,7 ;check if Xaccel is negative 0197 299C 00666 goto Xpos 0198 1486 00667 bsf PORTB,1 ;set polarity LED since XAccel is negative 0199 0941 00668 comf XAccel, W 019A 3E01 00669 addlw 0x01 ;two's complement 019B 00C1 00670 movwf XAccel 019C 00671 Xpos 019C 0008 00672 return 00673 ;******************************************************************** 00674 00675 ;******************************************************************** 00676 ;==================================================================== 00677 ; Display Routines for 7 segment LED display 00678 ;==================================================================== 00679 ;******************************************************************** 019D 00680 DisplayAccel 019D 0841 00681 movf XAccel,W 00683 digbyte G 0000 M local Pon0 0000 M local Exit1 0000 M local Exit2 0000 M local Pozitiv M 019F 01C8 M clrf Dig1 01A0 01C9 M clrf Dig2 01A1 01CA M clrf Dig3 01A2 M Pozitiv 01A2 084C M movf G,w 01A3 00CB M movwf Digtemp 01A4 3064 M movlw .100 01A5 M Pon0 01A5 0AC8 M incf Dig1,F 01A6 02CB M subwf Digtemp,f 01A7 1803 M btfsc STATUS,C 01A8 29A5 M goto Pon0 01A9 03C8 M decf Dig1,f 01AA 07CB M addwf Digtemp,f 01AB M Exit1 01AB 300A M movlw .10 01AC 0AC9 M incf Dig2,f 01AD 02CB M subwf Digtemp,f 01AE 1803 M btfsc STATUS,C 01AF 29AB M goto Exit1 01B0 03C9 M decf Dig2,f 01B1 07CB M addwf Digtemp,f

Page 25: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 25 of 29

01B2 M Exit2 01B2 084B M movf Digtemp,w 01B3 00CA M movwf Dig3 01B4 0188 00684 clrf PORTD 01B5 0187 00685 clrf PORTC 01B6 084A 00686 movf Dig3,w 01B7 21C5 00687 call GET_DISPLAY_CODE 01B8 0087 00688 movwf PORTC 01B9 1408 00689 bsf PORTD,0 01BA 300A 00690 movlw 0x0a ;wait for 5 ms 01BB 21D8 00691 call Delay_Ms_4MHz 01BC 0187 00692 clrf PORTC 01BD 0188 00693 clrf PORTD 01BE 0849 00694 movf Dig2,w 01BF 21C5 00695 call GET_DISPLAY_CODE 01C0 0087 00696 movwf PORTC 01C1 1488 00697 bsf PORTD,1 01C2 300A 00698 movlw 0x0a ;wait for 5 ms 01C3 21D8 00699 call Delay_Ms_4MHz 01C4 0008 00700 return 00701 00702 ;************************************************************************** 00703 ;========================================================================== 00704 ; Look up table for LED display 00705 ; Adapted from Myke Predko's book “Programming and Customizing 00706 ; the PIC microcontroller” 00707 ; Modified by Gaurav Konchady 00708 ;************************************************************************** 00709 ; Look up table 01C5 00710 GET_DISPLAY_CODE: 01C5 00CF 00711 movwf Tp 01C6 3001 00712 movlw HIGH TableEntries ;get the 256 Instruction Block 01C7 008A 00713 movwf PCLATH ;store it so next jump is correct 01C8 084F 00714 movf Tp,w ;compute next offset within 256 instruction block 01C9 3ECD 00715 addlw LOW TableEntries 01CA 1803 00716 btfsc STATUS,C 01CB 0A8A 00717 incf PCLATH,f ;If in next increment PCLATH 01CC 0082 00718 movwf PCL 01CD 00719 TableEntries 01CD 0782 00720 addwf PCL,F ; Add (W) to PCL, With Result in PCL 01CE 34C0 00721 retlw 0xc0 ; “0” 01CF 34EB 00722 retlw 0xeb ; “1” 01D0 348C 00723 retlw 0x8c ; “2”

Page 26: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 26 of 29

01D1 3489 00724 retlw 0x89 ; “3” 01D2 34A3 00725 retlw 0xa3 ; “4” 01D3 3491 00726 retlw 0x91 ; “5” 01D4 3490 00727 retlw 0x90 ; “6” 01D5 34CB 00728 retlw 0xcb ; “7” 01D6 3480 00729 retlw 0x80 ; “8” 01D7 3483 00730 retlw 0x83 ; “9” 00731 ;********************************************************** 00732 00733 ;******************************************************************** 00734 ;Delay_Ms_4MHz 00735 ; Generic delay routine. Delay length in ms is loaded 00736 ; into WREG before calling. 00737 ;******************************************************************** 01D8 00738 Delay_Ms_4MHz 01D8 1283 1303 00739 banksel PORTA 01DA 00CD 00740 movwf Count1 01DB 00741 DLMS2M1 01DB 307C 00742 movlw 0x7c 01DC 00CE 00743 movwf Count2 01DD 00744 DLMS2M2 01DD 0000 00745 nop 01DE 0BCE 00746 decfsz Count2,F 01DF 29DD 00747 goto DLMS2M2 01E0 0BCD 00748 decfsz Count1,F 01E1 29DB 00749 goto DLMS2M1 01E2 0008 00750 return 00751 00752 ;******************************************************************** 00753 ;==================================================================== 00754 ; Data EEPROM Routines 00755 ;==================================================================== 00756 ;******************************************************************** 00757 ;WriteCal 00758 ; This subroutine takes 6 bytes starting with address 00759 ; ZXcalHi and writes them to the internal Data EEPROM. 00760 ; Calls to WriteEE perform the actual write sequence. 00761 ;******************************************************************** 01E3 00762 WriteCal 01E3 1283 1303 00763 banksel PORTA 01E5 3006 00764 movlw 0x06 ;Load byte counter with 6 01E6 00CD 00765 movwf Count1 01E7 3037 00766 movlw ZXcalHi ;Load the starting address into FSR 01E8 0084 00767 movwf FSR

Page 27: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 27 of 29

01E9 1283 1703 00768 banksel EEDATA 01EB 01A0 00769 clrf EADR ;Start writing data to EE address 0 01EC 00770 WCLoop 01EC 1283 1303 00771 banksel PORTA 01EE 0800 00772 movf INDF,W ;Load data 01EF 1283 1703 00773 banksel EEDATA 01F1 00A1 00774 movwf EDATA 01F2 2212 00775 call WriteEE ;Call routine to write data 01F3 1283 1703 00776 banksel EEDATA 01F5 0AA0 00777 incf EADR,F ;Increment EE address 01F6 1283 1303 00778 banksel PORTA 01F8 0A84 00779 incf FSR,F ;Increment FSR 01F9 0BCD 00780 decfsz Count1,F ;Decrement count 01FA 29EC 00781 goto WCLoop 01FB 0008 00782 return 00783 00784 ;******************************************************************** 00785 ;RestoreCal 00786 ; This subroutine reads 6 bytes from the Data EE starting 00787 ; with address 0 and saves them starting 00788 ; Calls to ReadEE perform the actual read sequence. 00789 ;******************************************************************** 01FC 00790 RestoreCal 01FC 1283 1303 00791 banksel PORTA 01FE 3006 00792 movlw 0x06 ;Load byte counter 01FF 00CD 00793 movwf Count1 0200 3037 00794 movlw ZXcalHi ;Load starting address into FSR 0201 0084 00795 movwf FSR 0202 1283 1703 00796 banksel EEDATA 0204 01A0 00797 clrf EADR ;Load starting EE address with 0 0205 00798 RCLoop 0205 2227 00799 call ReadEE ;Read data from EE 0206 1283 1303 00800 banksel PORTA 0208 0080 00801 movwf INDF ;Save in register 0209 1283 1703 00802 banksel EEDATA 020B 0AA0 00803 incf EADR,F ;Increment EE address 020C 1283 1303 00804 banksel PORTA 020E 0A84 00805 incf FSR,F ;Increment FSR 020F 0BCD 00806 decfsz Count1,F ;Decrement count 0210 2A05 00807 goto RCLoop 0211 0008 00808 return 00810 ;******************************************************************** 00811 ;WriteEE 00812 ; This is the subroutine to load the address and data into 00813 ; the special EE access registers and perform the EE write 00814 ; sequence. 00815 ;******************************************************************** 0212 00816 WriteEE 0212 1703 00817 bsf STATUS,RP1

Page 28: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 28 of 29

0213 1683 00818 bsf STATUS,RP0 0214 188C 00819 btfsc EECON1,WR 0215 2A14 00820 goto $-1 0216 1283 00821 bcf STATUS,RP0 0217 0820 00822 movf EADR,W ;Load EE address 0218 008D 00823 movwf EEADR 0219 0821 00824 movf EDATA,W ;Load EE data 021A 008C 00825 movwf EEDATA 021B 1683 00826 bsf STATUS,RP0 021C 138C 00827 bcf EECON1,EEPGD 021D 150C 00828 bsf EECON1,WREN 021E 138B 00829 bcf INTCON,GIE 021F 3055 00830 movlw 0x55 ;must be performed 0220 008D 00831 movwf EECON2 ;in this order 0221 30AA 00832 movlw 0xaa ;otherwise write 0222 008D 00833 movwf EECON2 ;does not take 0223 148C 00834 bsf EECON1,WR ;place correctly 0224 178B 00835 bsf INTCON,GIE 0225 110C 00836 bcf EECON1,WREN ;Disable writes 0226 0008 00837 return 00838 00839 ;******************************************************************** 00840 ; ReadEE 00841 ; This is the subroutine to read from the data EE using 00842 ; special EE access registers. 00843 ;******************************************************************** 0227 00844 ReadEE 0227 1703 00845 bsf STATUS,RP1 0228 1283 00846 bcf STATUS,RP0 0229 0820 00847 movf EADR,W ;Load EE address 022A 008D 00848 movwf EEADR 022B 1683 00849 bsf STATUS,RP0 022C 138C 00850 bcf EECON1,EEPGD 022D 140C 00851 bsf EECON1,RD ;Perform the EE write sequence 022E 1283 00852 bcf STATUS,RP0 022F 080C 00853 movf EEDATA,W ;Move data into WREG 0230 0008 00854 return 00855 00856 end

Page 29: A Microchip PIC Based Tilt Measurement Device

A Microchip PIC Based Tilt Measurement Device Gaurav S. Konchady

Page 29 of 29

MEMORY USAGE MAP ('X' = Used, '-' = Unused) 0000 : XX--XXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0040 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0080 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 00C0 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0100 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0140 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0180 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 01C0 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0200 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX X--------------- All other memory blocks unused. Program Memory Words Used: 559 Program Memory Words Free: 7633 Errors : 0 Warnings : 6 reported, 512 suppressed Messages : 0 reported, 27 suppressed