PIC VI DataLoggerProj
PIC VI DataLoggerProj
PIC VI DataLoggerProj
Mark Panzer
Gang Feng
5-09-2005
1
Abstract
A data acquisition system was developed as a final project for the microcontroller design course at
UW-Platteville. This system was developed to monitor power supplies and aid as a development tool for the
design of a 200 W switch-mode power supply. A PIC microcontroller from Microchip was used as the heart
of the data collection system. Internal analog to digital converters acquired data from an analog interface.
The analog subsystem gathered data from temperature, voltage, and current sensors. Data was recorded
through HyperTerminal in Windows. Once the data was gathered MS Excel was used to convert, scale, and
plot the data.
Introduction
Measurement of the three basic quantities: voltage, temperature, and current can
provide enough information to allow for debugging of almost any electrical circuit. During the
development of a switch-mode power supply it was determined that some sort of data logging
was necessary to protect the supply and determine if the supply was operating properly. Out of
this need a data acquisition system was developed. The data acquisition system measures one
channel of voltage from 0 to 20 volts, one channel of current from –50 to +50 amps, and two
channels of temperature (one ambient and one load). This amount of data is enough to
determine supply efficiency and temperature rise. With additional channels or faster sampling
rates it would be possible to measure and calculate inrush current, supply stability, and transient
response.
Implementation options
Two options were initially considered to implement the data logger. First, adding an
outboard analog to digital converter (ADC) to the 8052 board used for development in class was
considered. This approach had the advantage of allowing me to use a known good development
system, software toolset, and the ability to get support from teachers and other students.
Unfortunately, I had a difficult time finding multi-channel ADC converters featuring parallel
interfaces to 8-bit busses that could resolve more than 8 bits. Most modern ADCs found used a
serial interface. This was a problem on the 8052 board as the serial port was tied up for
communication to the PC. Also fully developing a software-based serial protocol on a relatively
slow processor like the 8052 could be difficult. Figure 1 shows the proposed block diagram for
the 8052 based data acquisition system.
The second option considered the use of an altogether different processor, the Microchip
PIC controller. PICs are self-contained microcontrollers often including clock, I/O, and a host of
peripherals on-chip. The great advantage seen by adopting the PIC was a chip with onboard
2
analog to digital converters was available in a small 14-pin DIP package. In addition a serial port,
and multiple timer/counters were available. A low-cost ($35) development kit is available from
Microchip to try out any of the 14-pin series of micrcontrollers. Additionally, for the intended
application a low-power small form factor device was a plus. Essentially all the PIC needs to
create the system is the analog interface and a voltage regulator. Just the 8052 board requires 2
to 3 times the space of the board designed for the PIC controller and analog board. The
downside of this approach hinged around learning a new assembly language for the PIC
microcontroller and learning a new development environment and device programmer. Figure 2
shows the block diagram for the PIC data acquisition system.
Microcontroller Firmware
After configuration of peripherals the microcontroller firmware consisted of a simple loop
that acquired samples, converted them to a format acceptable to HyperTerminal, and echoed
them to the serial port. Code is attached to the end of this document in listing 1. Peripherals
were first initialized as described in the previous section. Following this a timer set to
approximately one second would overflow triggering a capture event. The capture event consists
of setting ADC registers, then waiting for the conversion to be completed. These events are
repeated four times to cover all the input channels. The results are then converted to a three
digit octal number via shifts and bit masks. Only 8 bits of the 10-bit result are converted to octal,
as it appeared the lower value bits only added noise to the acquired signal. Finally, the converted
values were output on the serial port to HyperTerminal in ASCII format delimited with commas.
Figure 3 shows the block diagram for the microcontroller firmware.
Hardware
The high integration of the PIC controller leads to a very simple hardware solution. On
the digital side the PIC controller is connected to a MAX232, RS-232 to logic-level converter. A 5
V power supply and some supply decoupling capacitors round out the digital section of the
hardware. Figure 4 shows the implementation of the digital board that was constructed on the
PICKit-1 development board.
4
The physical hardware is shown in photo 1. Two LM35 temperature sensors are attached
via twisted cable, enabling them to be clamped onto heatsinks to measure power supply
temperatures. A set of binding posts is provided for voltage measurement and current
measurement input. The 9 pin serial port hangs off the left side of the development board (black
PCB on top). Unregulated (9 to 37 VDC) DC input is supplied via two wires exiting the back of the
board.
5
Results
To test the data acquisition system an unregulated power supply was used. Real power
supplies have internal resistance that can be demonstrated if the load of the supply is varied. The
circuit in figure 6 was built to show the effect of loading the supply under various conditions.
The output voltage, current, ambient temperature, and temperature of the load resistor were all
monitored via the data acquisition system. After logging the octal numbers to a text file via
HyperTerminal, the data was scaled and plotted in MS Excel.
6
Plot 1, result of power supply testing, showing voltage, current, and two temperatures
Plot 1 shows the results of loading the supply with three different values of load resistors. As
expected the supply voltage sags with decreasing resistance. The output current changes from
400mA to about 1A over the three different resistors. Finally, the temperature of the load resistor
peaks at about 55 degrees Celsius. This simple application example shows the utility and some of
the capabilities of the data acquisition system.
major improvements are warranted. First, the capability to display real-time values of the
captured data would greatly increase the utility of the data acquisition system. Next, an
integrated capture and display program would greatly simplify data capture. The current system
requires capturing data via HyperTerminal to a text file. This text file then is updated in an excel
spreadsheet. Excel’s update feature works most of the time, however, trouble was occasionally
experienced resulting in Excel crashing. The current solution is far from a one-click data capture.
With dedicated software it is possible that a single click would enable a user to view live data and
concurrently save the trend data to a file.
8
__CONFIG _CP_OFF & _CPD_OFF & _BOD_OFF & _PWRTE_ON & _WDT_OFF &
_INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _FCMEN_OFF & _IESO_OFF
ADCH0 EQU B'00000001' ;setup ch 0, Vdd for Vref, and left justified result
ADCH1 EQU B'00000101' ;set to Ch1 same settings as above
ADCH2 EQU B'00001001' ;set to Ch2 same settings as above
ADCH3 EQU B'00001101' ;set to Ch3 same settings as above
COUNTER EQU 0x60 ;counter variable
TXPREBUF EQU 0x61 ;A buffer for the serial transmit buffer (a prebuf)
OCTTOVERT EQU 0x20 ;stores the a/d value when it's being converted to
octal
;**********************************************************************
ORG 0x0 ; processor reset vector
goto main ; go to beginning of program
main
;***********************************************************
;Setup For 9600 bps tx serial interface
;***********************************************************
bsf STATUS, RP0 ;bank 1
movlw B'00111111' ;setup RC0-5 as inputs for serial i/o and A/D
movwf TRISC
bcf STATUS, RP0 ;bank 0
bsf RCSTA, SPEN ;serial port enable
bsf BAUDCTL, BRG16 ;use 16 bit baudrate generator
bcf TXSTA, BRGH ;use low baudrate speed
movlw d'25'
movwf SPBRG ;setup baudrate generator
bcf TXSTA, SYNC ;asynchronous serial i/o
bsf TXSTA, TXEN ;enable transmission
;***********************************************************
;Setup A/D converter
;***********************************************************
bsf STATUS, RP0 ;bank 1
movlw B'00011111' ;Setup all of port A as inputs for A/D
movwf TRISA
movlw 0xFF
movwf ANSEL ;Enable all available Analog channels
movlw B'01010000'
movwf ADCON1 ;Set 4uS conversion time w/ 4MHz internal clk
bcf STATUS, RP0 ;bank 0
movlw ADCH0
movwf ADCON0 ;AD: right justified result, ch 0, VDD ref. voltage
call WaitForAD
;***********************************************************
9
GoAgain:
movlw 0x01 ;set to max for approx 1 sec delay
movwf TMR1L
movlw 0x00
movwf TMR1H
;bsf T1CON, TMR1ON ;start timer
bcf PIR1, TMR1IF ;clear overflow flag
;This is the delay between sample periods from the analog inputs
Wait:
btfss PIR1, TMR1IF ;check if timer has overflowed
goto Wait
call GetADValues
OutLoop:
movf INDF, W ;getting indirect data
movwf OCTTOVERT ;store a/d value we need to convert
movf 0x31, W
movwf TXPREBUF
call SENDBYTE
movf 0x32, W
movwf TXPREBUF
call SENDBYTE
movlw ',' ;do checking on this one to see if we're at the last byte
movwf TXPREBUF
call SENDBYTE
;***********************************************************
;Subroutines
;***********************************************************
FourFourWait:
decf COUNTER
bnz FourFourWait ;keep decrementing until we reach 0
return
call WaitForAD
call WaitForAD
call WaitForAD
return
return
return