Voltmeter with AT89C2051
-
Electrical sheet:
The hardware is a simple integrator followed by a trigger.
Both is build with a dual opamp with rail to rail output.
The trigger output must be connected to the INT0 input.
-
Initialization:
Since I use T1 as baud rate generator, T0 is divided in TL0 and TH0 to measure
period time and high time. Both timers are cascaded to 3 Byte with software
in the timer interrupt routines.
Measuring is finished with the high to low changing of the trigger output
and the next measuring is started.
The external interrupt must be executed with the highest priority.
The resolution error is given by the response time of the external interrupt
and can be up to 5 cycles (or 3, if no MUL or DIV instruction is used).
-
Interrupt Routines
Both Timer interrupts are simple cascaded with DJNZ instructions. On Overflow
of the period time measuring an error flag was set and measuring was
finished.
The external interrupt handler is very tricky !
Both timer registers are read and cleared with XCH instruction, so must not
be stopped.
But the high values can not be read out at this time, since it can be, that
an overflow is pending or in progress on one or both timers. To solve this
problem, the interrupt priority was inverted and a second external interrupt
is forced by setting the IE0 bit. So after the first RETI the timer overflows
are handled or completed with highest priority and only after this the external
interrupt is executed again. Then the higher values are read and cleared.
Since the timers are cleared, a timer interrupt of the next measuring is
not executed until the next 256 cycles. This is the maximum time to handle
all interrupts (include SIO interrupt).
-
Get Counting values
The counting values can be read out if the conversion done flag was
set. If it was detect as set after reading, a new measuring was finished
and the reading must be repeat (it can contain values mixed from the older
and the new measuring).
The higher bytes are 2's complement, since the DJNZ instruction was used.
The result is expand to 4 Bytes. Then you can calculate the input voltage.