Tuesday, September 20, 2005

Serial ADC for BS2

Connect National's Semiconductors serial analog to digital converter ADC0831 to Basic Stamp 2 and digitize your analog data. The required program couldn't be simpler (literally only 3 commands).

Details

There are many implementations of analog to digital converters. However none of them can be compared to simplicity with this. We need only three available ports from Basic stamp 2.


Components

  • One ADC0831 chip (8bit serial A/D Converter).
  • One 1MΩ trimmer.
  • One Basic stamp 2.
  • Basic's stamp programming board (You can buy it or make it by your own: DIY BS2 board).
  • Wires for the connections.

Also you will need a stabilized 9 Volt external power supply (a 9Volt alkaline battery is the best choice).


Schematic Diagram

There is no schematic. Just connect the components on the BS2 board's breadboard like the following picture.

Attention: The 1MΩ trimmer you see in the picture is actually not necessary. We only use it for demonstration purposes (to generate the analog voltage we want to convert). Instead of the trimmer you can apply the analog voltage, you want to convert, directly to pin 2 of the ADC0831 IC (referenced to the ground Vss).


The program

The program is the definition of simplicity. We reed the converted data (meaning the digitized analog voltage) with only one SHIFTIN command.  Before that we enable the chip bys setting the appropriate port to low (in our case port P2). After the SHIFTIN command we set P2 high again.

LOW 2 ' Enable ADC0831
SHIFTIN 0,1,MSBPOST,[ADResult\9] ' Read the data
HIGH 2 ' Disable ADC0831

ADResult is the byte where the digitized voltage will be saved.

I wrote a simple program to demonstrate this.

'{$STAMP BS2}
'{$PBASIC 2.5}


CS CON 2 ' Chip Select
D0 CON 0 ' Data Output
CLK CON 1 ' Clock

' Variable to store the result of the ADC (8 bit)
ADResult VAR Byte

HIGH CS ' Disable ADC0831
DEBUG CLS

Main:
  DEBUG HOME," "
  GOSUB ADC0831
  DEBUG HOME,DEC ADResult
  GOTO Main


'---- [ Subroutine ] -------------------------
ADC0831:
  LOW CS ' Enable ADC0831
  SHIFTIN D0,CLK,MSBPOST,[ADResult\9] ' Read reasult
  HIGH CS ' Disable ADC0831
  RETURN

No comments: