## Digital Capacitance Meter with keyboard and display.

Introduction.

You will produce a digital capacitance meter (DCM) which is controlled by a keyboard and which shows the capacitance on a LCD display. The DCM will measure capacitances from 1 nf to 100 µfarad in several ranges. The ranges will be controlled from the keyboard.

Note that you will be using the AT90S4414, for this lab. Read the AT90S4414 data sheet.

Procedure:

1. Measure the capacitance (as in Lab 4).
2. Format the capacitance as a string.
3. Poll a keyboard for keystrokes.
4. Write the results to a LCD.
As in lab exercise 4, the approach we will use is to measure the time for a RC circuit to charge to a given level. The requirement for a large capacitance range in the measurement means that you will have to modify the circuit from Lab 4 (also shown below), or perhaps the software, to keep the time-measuring counter from over/under flowing. There are at least two ways to do this:
• The resistor R2 could be replaced by several resistors, with values chosen to that the time to charge C remains within a factor of five or so. Each resistor will need to go to a different port pin. Each port pin will need to be switched from high-Z to output, depending on the range as set on the keyboard.

• The mcu time-measuring counter could count different rates depending on the desired range. This requires less hardware, but might run into problems of response time and capacitor leakage.
You will have to choose a scheme.

The Keyboard:

You will need to get user input from a keypad with the following configuration. Demonstration keyboard scanning code is here.

```Pin 1  -- row 1 2 3 A
Pin 2  -- row 4 5 6 B
Pin 3  -- row 7 8 9 C
Pin 4  -- row * 0 # D

Pin 5  -- col 1 4 7 *
Pin 6  -- col 2 5 8 0
Pin 7  -- col 3 6 9 #
Pin 8  -- col A B C D

(a) Each switch shorts one row to one column.
(b) Each pin should be connected to one
bit of an i/o port.
(c) The i/o port pins will be used both
as inputs and outputs. When they are inputs,
they should have the pullup resistors
activated.
```
The Liquid Crystal Display (LCD):

A 16 character, one line (16x1), LCD display will be the output for your capacitance meter. The display we are using has an industry-standard interface. A data sheet for a similar display shows the connections. There are several aspects of the display you should note:

• Connecting the power backwards will destroy it instantly.
Always check the polarity before connecting the display.
• The 14 pin header is on the upper-left corner of the display. Pin 1 is closest to the left edge of the pc-board.
• The power supply should rise very quickly from 0 to 5 volts. Since the display only draws about 1-2 mA, use a port pin for power. You will need a 10k or 20k trimpot to adjust display contrast.
• The LCD setup code is long but using it is straightforward. You can also use the code to test your LCD connections. The code assumes a 4-bit interface. Lines 7,8,9 and 10 on the display are not used and should be left unconnected. The code shows how to initialize the LCD, issue commands, and write characters.
```LCDpin 	Connection
------  ----------
1	gnd
2	+5 volts - PortA7 (under program control)
3 	trimpot wiper (the ends of the trimpot go to gnd and +5)
4	PortD6
5	PortD5
6	PortD4
7-10	no connection
11	PortD0
12	PortD1
13	PortD2
14	PortD3
```
• The character representation is identical to ASCII codes for numbers, letters and most symbols. Refer to the data sheet above for a complete listing.

Assignment

Write a program which will:

• Scan the keyboard for one of four commands (which could be single keystrokes):
• Range=1-100 nf
• Range=100-1000 nf
• Range=1-100 µf
• Autorange
• Continuously measures the capacitance from 1 nf to 100 µf. The autorange command must detect a capacitance which is too small or too big and change the measurement.
• Formats the internal capacitance representation as an ASCII number and sends the message ``` C = xxxx yy ``` to the LCD. The `xxxx` represents a four digit ascii number. The ` yy ` represents one of two strings: "nf" or "µf".

When you demonstrate the program to a staff member, you should exercise the keyboard commands and show that the capcitance displayed on the LCD is correct.

Your written lab report should include:

• The scheme you used to decode the keyboard.
• The scheme you used to convert the result to formatted ASCII.
• The scheme you used to implement auto-ranging.
• A schematic of the curcuit you built.
• A summary of the accuracy of your measurements over the range of capacitances.
• A heavily commented listing of your code.