EE 476: Laboratory 1
Digital Capacitance Meter.
You will produce a digital capacitance meter (DCM) which displays the capacitance in the LCD. The DCM will
measure capacitances from 1 to 100 nF.
- After you define a new project, you can add a C source file and edit it.
- Save this code (from the Timer page) into your directory. This program measures the period of a square wave in two diffent ways. It is organized as main, one task subroutine and two ISRs. I suggest modifying this code in the assignment below so that the task updates the LCD and another task slowly blinks an LED so that you know your code is running. The example uses timer0 interrupt to maintain the time base for printing data. It uses timer1 capture ISR to measure intervals. You will need to change the ACSR definitions so that AIN1 (B.3) is compared to AIN0 (B.2), rather than the the bandgap reference.
- In the Codevision file menu choose to make a new file. When the prompt comes up, choose
Project. When asked if you wish to use
CodeWizard, decline the offer. At this point, you have made a new project, now you need to configure it. The configure dialog should be open but if it is not, under the
Project menu, choose
- In this dialog box, in the
Files tab, add the source code to the project you just defined.
- In the
Compiler tab, set the Chip type to
Set clock speed to 16 MHz.
(s)printf features to
- In the
After make tab, set the
Program the Chip checkbox.
You may need to uncheck the
Check Signature box.
Then close the configure dialog.
- Click on the compile icon. A message window will open to tell you if there are errors in the code. If there are errors, The open the listing file to see where they are. If the compile is successful, you should be able to download the program to the STK500 board and see some blinking LEDs.
- Connect the LCD (see below) and run the LCD test program. You should see a counter and a moving dot.
- Remember that a switch which is pushed reads back a logic zero..
The Liquid Crystal Display (LCD):
A 16 character, two line (16x2), LCD display be used as a numerical display. The display we are using has an industry-standard interface. A more detailed data sheet for a similar display shows the command set, but start by reading the demo code, not by reading the data sheet. There are several aspects of the display you should note:
The approach we will use is to measure the time it takes for a RC circuit to
charge a capacitor to a given level. If
R3=R4 in the schematic below then the level will be
we will use the internal analog comparator as shown in the following
diagram to trigger a timer1 event. Since R2 will be known, we can get C because the voltage on the capacitor
The capacitor shown is the device you are trying to measure.
The resistor going to PortB2 should be around 100 ohms to limit current. You must choose R2
so that the capacitor charging time is not too short or too long. If it is too short you will lose measurement accuracy. It if is too long, the timer will overflow.
Your program will have to (in time order):
- Set PortB3 to an input.
- Drive PortB2 to zero by making it an output and wait long enough
to discharge the capacitor through 100 ohms. Clearly, to dischage to zero volts with 1% accuracy,
- Convert PortB2 to an input and start a timer. The capacitor will start
to charge toward Vcc.
- 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 polling the ACO bit of the ACSR and stopping the clock when ACO changes state, but a
much better way to do it is to use the timer1 input capture function set up to be triggered
by the comparator. Using input capture gives better timing accuracy and more dynamic range.
I suggest that you organize the program as follows:
- Timer0 compare-match ISR updates a millisceond counter to time task1.
- Timer1 capture ISR simply copies ICR1 into a variable.
- Main schedules task1 to execute about every 200 mSec..
- Does steps 1 and 2 above
- Computes the capacitance and updates the LCD. This takes long enough that the capacitor is discharged.
- Zeros timer1 and does step 3 above.
- By the time that task1 executes again, the capture ISR will have executed and a new ICR1 value will be available.
All timing must be done with interrupt-driven hardware timers and not with software wait-loops. All programs must be in C.
Write a program which will:
- The LCD should be updated every 100 mSec or so.
- An LED should blink about 1/second.
- The capacitance should be measured as quickly as possible as described above.
- The program should detect whether a capacitance is present or not and display an appropriate message if no capacitor is present.
- Formats the capacitance as an ASCII number
and prints the message
C = xxx nf to the LCD
When you demonstrate the program to
a staff member, you should demonstrate that the capacitance is correct within the tolerance of the resistors you use.
Your written lab report should include:
- How you converted time to capacitance
- A heavily commented listing of your code.
Copyright Cornell University Dec 2007