Designing Microprocessor Based Instrumentation

C PROGRAMMING for 8051SBC

After we finished schematic drawing, check all connections and create the NET LIST FILE. We will use net list file with file extension *.MNL to load into Orcad layout Plus!

Let's add the footprint library using Library Manager beforehand.Download below zip file and add to the footprint library.

The layout will be used with AT89C2051 for TINY model experiment. The first two weeks, we have learned how to use Orcad Capture and Layout. Now you can have your time practice at home for complicated schematic and PCB layout design. We will use them for product design on later.

Now we will learn how to use Micro-C with 8051SBC. Download the sample startup files for TINY and SMALL memory model and save them to LIB51 folder. Make the folder SMALL for 8051SBC c program under main folder MC. Download the sample code for onboard Analog-to-Digital Converter and save them to folder SMALL.

Exemplary Design: The Solar Integrator

We may draw the picture appeared in our mind on the paper, see how each block are wired together. For simplicity I usually put the quantity to be measured on the left hand and put the signal processing circuitry from left to right. We see that the photon intensity when hit pyranometer will produce heat which in turn are measured by internal thermopile. The signal output is 5 microvolts/Wm-2. The output resistance of pyranometer is quite high, this makes us to use the high input resistance of DC amplifier. We can use a chopperless DC amplifier, OP07, or OP27 with noninverting amplifier. What's the GAIN of DC amplifier will be? To answer this question, let's look at our ADC. The resolution of ADC is +5V/4096 or 1.22mV/LSB. To make the ADC be able to see 1W/m-2, we must have the GAIN of DC amplifier about 244! We may have adjustable GAIN for DC amplifier so we can calibrate the display unit to be 1W/m-2. The converted digital data now can be processed with c coding. We will have digital filtering for removing high frequency noise. The onboard LCD can display irradiance and insolation.

Pyranometer

Practice1:

# study how to use onboard LCD with lcd driver functions.
# write a program that prints counting interger from -32768 to 32767.
# write a program that displays digital data converted by ADC ch0 and ch1 on LCD (see sample code ADC.C)

DC Amplifier

Since the signal output from pyranometer is voltage source or thermoelectric EMF and the source resistance is quite high. So we do need high input resistance DC amplifier. There are four types of DC amplifier that suitable for amplification of DC signal.

1. Inverting Amplifier: single-end input, input resistance =Ri, invert polarity

2. Noninverting Amplifier: single-end input, high input resistance, same polarity output

3. Differential Amplifier: differential input, input resistance = Ri, reject common mode signal, e.g. AC power line coupling.

4. Instrumentation Amplifier: high input resistance, high CMRR

We must choose the proper amplifier for a given signal source chracteristics. After type selections, we must then choose the opertinal amplifer. Since DC amplification needs minimum DC offset voltage and lowest drift. The right one is ICL7650 chopper stabilized DC amplifier. Howvere we will use OP07, chopperless DC amplifer. Because the OP07 has such parameters closely to chopper amplifier. The ICL7650 has no offset adjustment POT. The OP07, instead, for high gian has the offset adjustment POT.

Let's build a noninverting DC amplifier with OP07. See schematic and BOM below.

 


Bill Of Materials for DC Amplifier

Item Quantity Reference Part
______________________________________________

1	 3	 C1,C3,C6			 0.1uF disceramic cap
2  3  C2,C4,C5         10uF electrolytic cap
3  1  C7					 4.7uF tantalum cap
4  2  D1,D2 				1N4148
5  1  J1					 CON2
6  1  R1					 20k
7  1  R2					 1k
8  1  R3					 24K
9  1  R4					 100
10 1  R5					 4.7k
11 1  U1					 OP-07
12 1  U2					 ICL7660
13 1  U3                    LM336B2.5 

Circuit Descriptions

R2 and C2 forms a simple low pass filer. D1, D2 and R2 provide signal limitter to +/-600mV. The voltage gain is 1+R3/R4 or approx. 240. We may change R3 with 25 turns POT for adjustable gain, but I prefer using fixed value resistor. We just provide enough gain for the ADC to resolve the desire LSB for a given physical quatity. Later we can use software method to scale the proper display value. R1 is for DC offset adjustment. To adjust the offset voltage, tie the signal input to GND and measure output voltage of U1. Adjust R1 unitl we get nearly 0mV. C1 and C3 are high freuqency bypassing capacitors. Sometime I added 4.7uF tantalum capacitor to both power rails. The output signal ties to Ch0 of ADC. Actually the signal of pyranometer will be only positive value with respect to GND signal, we can use single supply Operational Amplifier, e.g. LM358. Howvere the OP07 needs negative supply, we then need voltage converter, U2 ICL7660, to convert +5V to -5V. U3, LM336-2.5 provides reference voltage source, +2.5V for calibration.

Practice2:

# layout the PCB for DC amplifer with Orcad Layout Plus (download Orcad schematic and netlist files, op07.zip).
# build the circuit and test adjust offset voltage.
# tie the sensor and measure the output signal (we may test with thermocouple type K (+40uV/C) or type J(+50uV/C).

Analog-to-Digital Converter

The onboard ADC, LTC1298 or MP3202, is 12-bit sucessive approximation converter. VREF is tied to +5V supply, so the LSB is +5V/4096 or 1.2mV. The converter has serial interface, SPI (Serial Pheripheral Interface) port. We can use three bits from P1 with software to emulate the SPI signal. Let's study hardware of the onboard ADC.

R8 and C19 forms a low pass filter for channel0, R9 and C18 for channel1. The reference voltage of LTC1298 uses the same pin as VCC. L1 and C20, C21 filters high frequency noise from digital supply.

The SPI signal has three signals, SCK, MISO (Master In Slave Out), MOSI (Master Out Slave In). The sample circuit below shows the MASTER chip, 68HC11 interfaces LTC1298. The master generates shift clock signal to slave. MOSI generates command and MISO recives converted digital data. CS pin is special designed for LTC1298 for start conversion command and shutdown the power.

With 8051, we can use three signals with software control to emulates such SPI signals. P1.1 sends and receives data to LTC1298. P1.2 is clock for serial data bit shifting. P1.3 is chip slect signal. To start converison, CS must chnage from high to low. First four bits are command output to LTC1298. The data is shifting in at rising edge of clock signal. At the end of four bits command, P1.1 was reconfigured to input bit. The following 12-bit data willl be MSB to LSB, B11 to B0. Data is shifting out from LTC1298 on falling edge of clock signal.

The source code of sample program for interfacing LTC1298 was written for Micro-C compiler. The function read_ADC(char n) will return 12-bit data. If n=0 then the command will be 0x0d. If n=1 it will be 0x0f.

#define Data P1.1
#define CLK P1.2
#define CS P1.3          
int read_ADC(char n)
           {
           int k;
           char i,channel;
           k=0;
           clrbit(CS)
        if(n==0) channel= 0x0d;
           else channel= 0x0f;
// send command
           for(i=0; i<4; i++)
           {
           clrbit(CLK)
           if(channel&8) setbit(Data)
           else clrbit(Data)
           setbit(CLK)
           channel <<=1;
           }
           P1 |= 2; // change to input bit
           clrbit(CLK) 
// now read 12-bit data
           for(i=0; i<12; i++)
           {
           k<<=1;
           setbit(CLK)
           clrbit(CLK)
           if(P1&2) k |=1;
           else k &= ~1;
           }
           setbit(CS)
 return k&=0xfff;
           }
         

Here is main function.

void main(void)
{
InitLcd();
 while(1)
           {
           P1 ^=0x80; // blink onboard LED
           sprintf(buffer,"ADC0=%d ",read_ADC(0));
           // printf("\n%s",buffer);
           goto_xy(0,0);
           Puts(buffer);
           sprintf(buffer,"ADC1=%d ",read_ADC(1));
           // printf(" %s",buffer);
           goto_xy(0,1);
          Puts(buffer);
          }
} 

 

Download sample code of 8051SBC Based Solar Integrator v1.0, solar.c The example shows how to use structure variable for record storage.

Data Transmission


Copyright (C) 2005 by Wichit Sirichote, kswichit@kmitl.ac.th

HOME
NEXT