Special Application Processor (SAP) Series of Microcontroller Modules 

SAPIN3 : 3 Input Serial Converter
Embedded Micro Software
8/8/00



Introduction

The SAPIN3 is a microcontroller based module that converts 2 analog inputs to an 8 bit value, reads a single digital input, and serially transmits the values at 9600 baud. This simple device requires only 1 integrated circuit and a handful of discrete components.

The software is written in C using the CCS PCM C Compiler which is a powerful and cost effective compiler.

Hardware

Hardware
Description
The SAPIN3 uses the PIC12C672 8 pin microcontroller (U1) from MicroChip (www.microchip.com). The 4 Mhz ceramic oscillator provides accurate timing for generating the 9600 baud rate. The serially transmitted data is connected from pin 7 (GP0) of the micro through a 220 ohm resistor to pin 2 of the DB9 female connector. This eliminates the need for a MAX232 type of device.

The 2 a/d inputs are applied to GP1 and GP2. The potentiometers shown are external and provide a 0 to 5 volt input. The single digital input is a pushbutton switch connected to pin 4 (GP3) and pulled up to VCC with a 10k resistor.

The 5 volts to power the module is provided by the 7805 type regulator.

Software

Software
Description
The software is written in C and is contained in the file sapin3.c. There are 2 sections to the file. The first section I consider the header section and contains information on the file such as file name, author, last update, and a description. Compiler information is included also as well as how to compile it.
The header portion of the file contains a comment section with file information. The #include <12c672.h> statement includes processor specific definitions. The #use delay statement sets the oscillator value and the #use rs232 statement sets the parameters for the serial port. Notice that only the transmit pin is used and the INVERT qualifier is used to get the level correct for the serial port on the PC to recognize. //------------------------------------------------
// file: sapin3.c
// author: Emicros
// update: 8/8/00
//------------------------------------------------
// Description: This file contains the C source
// code for a PIC12C672 to read 2 a/d inputs
// and 1 digital input and send the
// data at 9600 baud to a terminal (PC).
//
// Compiler: CCS PCM C Compiler
// Compiling: pcm -c sapin3.c
//------------------------------------------------
#include <12c672.h>
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_A0,INVERT)
//--------------------------------------
// Period of time between updates
//--------------------------------------
#define REPEAT_TIME 100

unsigned int rad1, rad2;
The main() function contains the application specific code and is executed from reset after the C startup code is executed. The program executes the infinite loop contained within the while statement;

while( 1 )
{
   execute program forever
}

The a/d channels are set as inputs in the set_tris_a statement followed by the statement that controls the delay between sends. The REPEAT_TIME is defined in the header portion of the file and is set to 100 milliseconds.

//-----------------------------------------------
// function: main()
//-----------------------------------------------
void main( void )
{
  while( 1 )
  {
    //----------------------------
    // setup the processor. Note
    // the delay statement.
    //----------------------------
    set_tris_a( 0x1f );
    delay_ms( REPEAT_TIME ); 
    setup_adc( ADC_CLOCK_DIV_8 );
    setup_adc_ports( ALL_ANALOG );

    //----------------------------
    // read the a/d channels
    //----------------------------
        set_adc_channel( 1 );
    delay_us( 10 );
        rad1 = read_adc();
    set_adc_channel( 2 );
    delay_us( 10 );
        rad2 = read_adc();

    setup_adc_ports( NO_ANALOGS );

    //----------------------------
    // display the a/d channels
    //----------------------------
    printf( "ad1=%u,", rad1 );
    printf( "ad2=%u,", rad2 );

    //----------------------------
    // display the digital input
    //----------------------------
    if( input( PIN_A3 ) )
      printf( "1" );
    else
      printf( "0" );
    printf( "\n\r" );
  }
}

The clock used for the a/d conversion is set to the divide by 8 configuration. This allows correct conversion using the 4 Mhz oscillator. Refer to the data sheet for more details. The a/d pin configuration is then set for all analog configuration.

A/D channels 1 and 2 on pins 6 (GP1) and 5 (GP2) are converted and storied in variables rad1 and rad2 respectively. These are 8 bit values ranging from 0 to 255 (0 to 0xff hex). 

The remaining portion of the while loop uses the printf statement to format the output as ascii text. The printf statement sends the text out the serial channel.


Software
Compiling

The software for the PIC is written in C for the CCS PCM C Compiler. The program is compiled from the DOS command prompt as;

pcm -c sapin3.c

Part
Programming
The compiler produces the hex file, sapin3.hex, that is used to program the PIC12C672 using the PICSTART Plus programmer. Set the configuration bits as follows;
  • Oscillator: HS
  • Power Up Timer: Off
  • Code Protect: Off
  • Master Clear: Internal
This page has been hit  times.