## Serially controlled, Digital Capacitance Meter.

Introduction.

You will produce a digital capacitance meter (DCM) which is controlled and read over an RS-232 serial line connected to a PC window. The DCM will measure capacitances from 1 to 100 nF and display the capacitance in the window on the PC.

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

Procedure:

The approach we will use is to measure the time for a RC circuit to charge to a given level. Since R will be known, we can get C. Specifically, we will use the internal analog comparator as shown in the following diagram. Power for the circuit, Vcc, will come from PortB0. The bit will be set to output and logical 1.

The capacitor shown is the device you are trying to measure. The resistor going to PortB2 should be around 100 ohms. The other three you must choose. If you choose R2, R3, R4 correctly, you can save lots of arithmetic and program logic. Considerations might include choosing R3 and R4 to produce a voltage at PortB3 of (Vcc)(1-e-1) and choosing R2 so that the number of timer0 ticks which occur during the time when the voltage at PortB2 is less than the voltage at PortB3 is easily related to the capacitance. We will supply 10k potentiometers so that you can easily set values for R3 and/or R4.

Your program will have to (in time order):

1. Drive PB2 to zero by making it an output and wait long enough to discharge the capacitor.
2. Convert PB2 to an input and start a timer. The capacitor will start to charge.
3. Detect when the voltage at PortB2 is greater than than the voltage at PortB3. That is, you will have to record when the comparator changes state. You could do this by counting timer0 interrupts, but a much better way to do it is to use the timer1 capture function (triggered by the comparator). See the 4414 data sheet.
4. Convert the raw time to capacitance.
5. Format the the result into a 3 digit, ASCII formatted number representing the capacitance.
6. Transmit the ascii number to the PC.
7. repeat
At any time during this loop, your program may get an interrupt to handle input from the PC.

One way of handling the serial i/o is to have interrupt routines for transmit and receive functions. This scheme allows the fast cpu to perfrom other functions while waiting for the slow serial interface. A demo program shows basic UART transmit and receive interrupt routines for the AT90S4414 mcu. You can use this fragment as a test that the RS232 connnections and terminal program on the PC are correct. The serial port (connector J131) should be connected to COM2 of the PC. You should use a simple terminal program, such as Hyperterminal, on the PC connected to COM2. The terminal program should be set to 9600 baud, no parity, one stop bit, and no flow control.

The UART receive is on PortD0, which should be connected to J132, pin1 (the left pin when the buttons are near you). The UART transmit is on PortD1, which should be connected to J132, pin2. These connections assume a RS232 cable to the PC with straight-through connection.

Note that the 'transmit buffer empty" interrupt is set when the mcu is reset. This means that you must initialize the message pointer before you enable interrupts, or you will get garbage sent to the terminal at each reset.

Assignment

Write a program which will repeatedly:

• Accept the ASCII command 'g' typed from a terminal window on the PC to make a single capacitance measurement.
• Measures the capacitance, as described above.
• Formats the internal capacitance representation as an ASCII number and sends the message
``` Capacitance = xxx nf(CR)(LF) ``` over the J131 serial port to a terminal window on the PC. The `(CR)(LF)` represents carriage-return and line-feed characters. The `xxx` represents a three digit ascii number.
• Loops back and waits for another go command.

When you demonstrate the program to a staff member, you should exercise the keyboard command typed from the PC and show that the capcitance displayed in the PC window is correct.

Your written lab report should include:

• The scheme you used to decode commands.
• The scheme you used to convert the result to formatted ASCII.
• A heavily commented listing of your code.