|
This application expands on our earlier projects using the PIC12C671 series A/D converter. If you missed the first articles covering the PIC12C671, you may want to review them to gain a full understanding of how to configure the onboard 4MHz oscillator of the PIC12C671. The first articles also explain how to configure the A/D control registers as well. Several folks have expressed interest in using the newer flash-based PIC12F683, so I have included code for this one here as a bonus. We now ship the PIC12F683 as the pre-programmed, 4-channel A/D for this project due to the increased price of the 12C67x series. We won't go into great detail here on how to configure the A/D control registers, or how to setup the internal oscillator since we covered these areas in past articles. the primary focus of this article is to show you how to use all 4 channels of the A/D converter on the PIC12C671. The code sample first sets up a 4-byte array as storage variables to hold the four different A/D readings that are extracted from the PIC12C671. One unique feature of this code is how the array works, and how the variable J is used to select each of the individual 4-channels. INCLUDE "MODEDEFS.BAS"
And here's the new version using the PIC12F683 flash-based 8-pin PIC. @ DEVICE PIC12F683,MCLR_OFF,INTRC_OSC_NOCLKOUT,WDT_OFF,BOD_OFF
@ DEVICE PWRT_ON,FCMEN_OFF,IESO_OFF,PROTECT_ON
DEFINE DEBUG_REG GPIO
DEFINE DEBUG_BIT 5 ' GPIO.5
DEFINE DEBUG_BAUD 2400 ' 2400 bps
DEFINE DEBUG_MODE 1 ' 1 = inverted, 0 = true
DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
'-------------------------------------------------------------------
' PIC12F683 SAMPLE ROUTINE FOR 4-CHANNEL, SERIAL, A/D
'-------------------------------------------------------------------
' PIN NAME USE/CONNECTION
' 1 Vdd +5VDC
' 2 GPIO.5 SERIAL OUT TO PC
' 3 GPIO.4 (CHANNEL 3)
' 4 GPIO.3 N/C
' 5 GPIO.2 (CHANNEL 2)
' 6 GPIO.1 (CHANNEL 1)
' 7 GPIO.0 (CHANNEL 0)
' 8 VSS GROUND
'-------------------------------------------------------------------
DEFINE OSC 8
ANSEL = %00111111 ' Set all to A/D, A/D clock = Frc
TRISIO = %0011111 ' GP.5 = serial out, rest inputs
OSCCON = %01110000 ' Internal 8MHz osc
RESULT VAR BYTE[4] ' 4-BYTE ARRAY STORAGE FOR 4 A/D CONVERSIONS
J VAR BYTE ' A/D CHANNEL NUMBER BYTE VARIABLE
MAIN:
FOR J = 0 TO 3 ' 4-CHANNEL COUNTER LOOP
ADCIN J, RESULT[J] ' Read all 4 channels
PAUSE 10
NEXT J
PAUSE 250 ' PAUSE 250mS
DEBUG 1,RESULT[0],2,RESULT[1],3,RESULT[2],4,RESULT[3]
GOTO MAIN ' DO IT AGAIN
END ' END PROGRAM
The code is pretty simple once you're familiar with how to configure the A/D options of the PIC12C671 or PIC12F683. Using the 4-byte array saves us a lot of typing by not requiring extra lines of code to handle four separate A/D reading/storage routines. There are plenty of different ways to do the exact same thing, but using arrays is pretty handy once you understand how they work. The Circuit: The simple circuit shown in figure 1 shows the connections for this application. Notice the lack of an external oscillator..? This is why it's important to learn how to configure the internal precision 4MHz oscillator when using the PIC12C67X series micro. Using an external oscillator with the PIC12C671 eats up two valuable I/O-pins, [GP5 and GP4]. Note that the circuit stays the same. Even if you're using the newer 12F683. The code has changed a bit since the two are slightly different, and we're using ADCIN in the new version.
Figure 1: Connecting the PIC12C671 for A/D Applications You do NOT need the potentiometers. These are shown only as a test circuit allowing you to test the serial A/D converter for operation. In a real-world application, each A/D input would be connected to the analog voltage "0 to 5V" being sampled. The A/D Connections: Connecting each channel of the PIC12C671 analog port-pins is done through a series 1K resistor attached to the wiper of a 5K potentiometer. The maximum recommended impedance for analog sources is 10K, so this keeps us well within the input impedance tolerances, and gives us plenty of current limiting to boot. Figure 1 shows the hookup for the A/D inputs. The PC Connection: Connecting the PIC12C671 to the PC serial port is simple. For this example, connect the PIC12C671 pin #2, GP5 to a DB-9 serial cable through the 1K resistor. Next connect pin #5 from the serial cable to the PIC circuit ground. Many people will want to use this application for sending A/D readings to another microcontroller. To use this circuit with another microcontroller such as the BASIC Stamp, or another PIC, just use a 1K series resistor from GP5 to the serial input-pin of the second microcontroller. This series resistor will help avoid problems if you have one pin HIGH while the other pin is LOW. Don't rely on software alone. Always plan for the unexpected, and add hardware protection in the beginning of your designs. It pays...! The Software: Figure 2 shows the actual results of our 4-channel A/D conversions. Using our custom ActiveX control, you can capture all 4-channels and display each channels analog input value with your PC. You can also LOG each reading with the time & date stamp.. Figure 2: 4-Channel Analog Readings As shown in figure 2, we can see the results of all 4-channels A/D readings. We adjusted each of the four potentiometers for this example to give you an idea of the range of the 4-channel ADC software. Notice that readings can be in millivolts as shown in channel-4. The software expects to receive two-bytes of data per channel:
To send a value to the PC to be displayed in the channel 1 window, first send the number 1 followed by the analog value as shown below. Serout GPIO.5, N2400, [ 1,result ]. this example assumes you have stored the analog reading for channel 1 in the byte variable result. To address the remaining 3 channels, simply precede the analog value with the channel number. Note: For reliable operation at 9600 baud, you may need to use an external oscillator. The internal oscillator of this PIC series does well for up to 2400 baud, but may not always work for higher baud rates. If you prefer the higher baud rate, and three A/D channels with be sufficient, I recommend using the external oscillator. The Log File: Figure 3: Screen Capture of Data Log File. Figure 3 shows a screen capture of the actual log file created by the 4-channel data-logging software. Each time data is sent to the PC serial port from the PIC, or other microcontroller, the log file records the channel number, voltage readings, and the date & time of each reading. The log file is an important part of any serious data-logging application, and makes the application stand-alone while carrying out the unattended task of capturing & recording analog variables. When you exit the application and re-open it, the log will record the access time as shown above. By controlling the time each reading is sent to the PC, you can control the time each reading is logged to file. A/D Accuracy/Error: The overall accuracy of the PIC12C671 A/D is less than + or - 1 LSb for Vdd = 5V + or - 10%, and the analog Vref = Vdd. The resolution & accuracy may be less when either the analog reference (Vdd) is less than 5.0V or when the analog reference (Vref) is less than Vdd. If the input voltage exceeds the rail values (Vss or Vdd) by greater than 0.2V, then the accuracy of the conversion will be out-of -tolerance. Make sure you have a nice stable power-supply with a good solid 5.0V for Vref to make the PIC12C671 A/D conversions as accurate as possible. Refer to the PIC12C671 and PIC12F683 datasheets for further details on the operation of the A/D converters and other functions. Our 4-channel analog data-logging software is available in two options.
When we receive your order, we will email you complete instructions to download the software from our secure server. You will receive a username and password that will allow you to download the software immediately. Note: We send download locations & passwords to you manually once we receive your order. Please be patient. It may take us a few hours to email you download instructions. Orders placed on weekends, or after normal business hours (9 to 5 Mon-Fri), will normally be processed on the following day, or Monday if placed during the weekend. If you don't own PicBasic Pro, we have these PICs pre-programmed, and ready to insert in your applications. Add "Serial" 8-bit ADC to your next design --- Use the order button below to purchase a pre-programmed PIC12F683 serial A/D IC. Item: 12F-A/D Note: These are programmed for operation at 2400 baud. Applications: The ability to monitor and take action based on analog inputs has quite a few applications well beyond simply measuring a voltage and recording it. By the time you finish this article, chances are, you've already come up with several excellent ideas for using the serial A/D converter. Many robotics applications can make use of a small 8-pin microcontroller that has these abilities. Using the same setup as shown above in figure 2, but replacing the 1K series resistors with photo-resistors let's us use the analog input values to determine the amount of light falling on (up to 4) individual photo-resistors. It would be easy to construct a robotic guidance system using the PIC12C671 and a hand-full of photo-resistors that could avoid, or follow light. Using this same setup we could design a servo driven plant holder that would continually re-position the plant to face the brightest sunlight from up to 4 different directions. The applications are virtually endless, but it just takes a little imagination. Have fun with this one. It has some serious potential....:o] Using the 4-Channel A/D ActiveX Control:
You should have a new form similar to the one shown below.
Add the simple code shown below ; Code for Exit button Private Sub cmdExit_Click() ; Code for form load to assign MSComm control
to voltmeter control ; Code for "toggle" activation by clicking
the voltmeter LED Set control properties: Right click on the voltmeter control, select properties to view & adjust the voltmeter control properties window shown below.
Customize the form as required for your own applications.
| | ||||||||||||||||||||||||||