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.
But first you need to set up a board, arrange jumpers, learn how to connect peripheral devices and use the compiler. You will first do a quick exercise to compile an existing program and watch the blinking lights. The LEDs on the board are the embedded programmers low-level, fast, debug tool.
Hardware
The hardware you will be using to support the MEGA-series microcontroller is a small board providing:
For the initial testing phase of this lab, connect the LEDs and pushbuttons with jumpers as shown at the right above. Note that the buttons are active low. You will need a USB cable with a type-B connector for serial communication. Another USB cable attaches the programmer to the PC. The left image shows programming, LED and serial jumpers. The programming jumper is partly hidden to the left of the programmer cable plug. The two serial jumpers are to the left of the SOIC device. The LED jumper is below and to the right of the green LED.
Images of the target board from different angles without jumpers mounted are shown below.
The six-pin programming header is visible, as well as the 36 pin white-board header.
Software
Software you will use is freely downloadable and consists of:
More information
General hardware Procedures
Control Panel...System...Hardware Tab...Device Manager Button...+Ports
to find out which serial port is connected to the USB dongle.
!
Be sure to put all your files there and to back up daily!.
You can also use a flash drive.
Do not creat a project on the local PC disk drives!
These drives are wiped when you logout or if the system crashes.
AVR
.
. This program uses the circuitry shown above (two buttons, two LEDs).Project
menu choose Project Wizard
. When the dialog box
appears, choose New Project
. AVR simulator
and choose ATmega1284p,
then press Finish
Project-> Configuration Options -> General -> Frequency
enter 16000000 because the crystal frequency is 16 MHz. #define F_CPU 16000000UL
as the first line of your program. Project-> Configuration Options -> Libraries
click on libm.a
, then click on Add Library
, and press OK
.source files
item in the left panel. Also add uart.c.Build
from the Build
menu. A message window will open to tell you if
there are errors in the code. Tools>Program AVR>Autoconnect
menu item. One of two dialog boxes will open. You may be asked to choose a programming device. If so, choose AVRISPmkII
. Otherwise the main programmer dialog box will open. In the main programmer dialog box:
Main tab
, set the ISP frequency (Settings
... dropdown) to 125 Khz, then press Write
then Close
. Mega1284p
. Fuses
tab in the programmer window and set the SUT_CLKSEL
fuse to:Ext crystal osc 8- MHz; startup time: 16k clk+65 mSec.
CLKDIV8
box.JTAGEN
box and answer YES in the confirming dialog. (If you leave the box checked, PORTC does not work correctly). Program
Program Tab, Flash box
, select the hex
file generated by the compiler. project folder>default folder
. Press the Program
button. When the program is running and blinking lights, you should be able to press button 7 to change the blinking rate. You should have serial communication to the PC and the program should be printing sequential numbers at about 1 Hz. At this point you can move on to the actual lab exercise below.
Capacitance Procedure:
_delay_ms(1)
was replaced with
_delay_ms(2).
Also,
you may need to disconnect the programmer if the LCD scrolls uncontrollably.]
delay_ms
. Using delay_us
will be acceptable when you need to produce delays on the order of a few microseconds.
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. There are several aspects of the display you should note:
LCD pin | Mega1284 pin |
---|---|
1 gnd | gnd |
2 Vcc | Vcc |
3 contrast ** | look to the right >>> |
4 rs | port C0 |
5 rd | port C1 |
6 en | port C2 |
7 n.c. | -- |
8 n.c. | -- |
9 n.c. | -- |
10 n.c. | -- |
11 | port C4 |
12 | port C5 |
13 | port C6 |
14 | port C7 |
Capacitance measurement
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 v(t1/2)=Vcc/2
. Specifically,
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 v(t)=Vcc(1-exp(-t/τ))
with τ=(R2)*C
.
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.
If you decide to print floating point numbers to the LCD you need to follow the directions in Using sprintf function for float numbers in AVR-GCC the AVRstudio directions are at the bottom of the page, but NOTE that one library is left out. The correct summary is:
Open the Project>Project Configuration
dialog box, then go to the custom options
, then select linker options
,
and then add the
-Wl,-u,vfprintf -lprintf_flt -lm
and -Wl,-u,vfscanf -lscanf_flt -lm
switches.
Your program will have to (in time order):
R2>100*(100ohms).
I suggest that you organize the program as follows:
You can connect the oscilloscope to the computer with a USB cable (type-B connector) attached to the back of the oscilloscope (not the type-A connector on the front).
To use the Tektronix software on the PC:
OpenChoice Desktop
in the start menu, and start the program. Select Instrument
.USB
device, and click OK
.Get Screen
.Timing of all functions in this lab, and every exercise in this course will be handled by interrupt-driven counters, not by software wait-loops. This will be enforced because wait-loops are hard to debug and tend to limit multitasking.
Write a C program which will:
C = xx.x nf
to the LCD. There should be exactly one digit shown after the decimal point.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 program should not need to be reset during the demo.
Your written lab report should include the sections mentioned in the policy page, and :