A/D converter for the PC's serial interface with a PIC microcontroller
------------------------------------------------------------------------

  Author: Wolfgang Buescher ( DL4YHF@qsl.net )                     
  Revision date:  10/2002                                          
  !! OLD !! THIS DOCUMENT HAS RECENTLY BEEN REPLACED WITH AN HTML FILE !!



This file describes the function of a firmware for the PIC12F675 microcontroller.
The firmware converts two analog input channels (called "I" and "Q" for special reasons)
into serial data frames which are sent to the PC via serial interface ("COM1" or "COM2").

A utility program for the PC is available from the author (a windoze prog),
which reads the serial data stream from the COM port and dumps the samples
into an ordinary disk file, which can be "consumed" by any other application
(including simple QBasic progs running in a DOS box !). Look for "SerInput.exe".
You may find this utility program at www.qsl.net/dl4yhf/snd_utl1.html .


COM port settings
----------------------------------------------------------------------
  - 115200 bits/second, 
  - 1 startbit, 8 databits, 1 stopbit
  - no parity, no handshake (neither hard nor soft)


Format of serial data
----------------------------------------------------------------------
The analog values are sent to the PC in a 4-byte frame.
The first byte (=byte[0]) is mainly used for synchronisation, 
the receiver can easily detect the begin of a 4-byte frame.
Note: The PC's async serial interface transmits the LSB
      in a BYTE ("bit 0") first, the MSB ("bit 7") last.


 Byte[0] = Sync/Status
           Usually 0xFF which looks like a "STOP BIT" to the receiver.
           Used for frame sync, but also for BYTE sync if receiver
           turned on too late.

 Byte[1] = Least significant bits of I-channel (I7..I0).

 Byte[2] = Least significant bits of Q-channel (Q7..Q0).

 Byte[3] : Bits 3..0 = Most significant bits of I-channel (I11..I8),
           Bits 7..4 = Most significant bits of Q-channel (Q11..Q8),


Timing considerations for A/D CONVERSIONS
----------------------------------------------------------------------
 Most desirable: A very accurate sampling rate, which is not
          too exotic, but as high as possible.
          The serial interface would be able to transfer about
          2880 four-byte-frames per second, but a sampling rate
          of 2880 samples/second is not a nice fraction of 10MHz.
          So 2500 samples/second were chosen.
 To increase the ADC's resolution (10 bit) a little,
 as many samples as possible are taken (synchronously) 
 and averaged for both channels.
 The conversion time for a single sample is at least  
    Tconv = 11 * 1.6us = 17.6 us ,
 plus an additional settling time for the analog multiplexer
 (which depends on the source impedance; must be kept below 2.5 kOhm),
 so a total acquisition+conversion time for A SINGLE VALUE
 should be Tacq+Tconv = 8us + 17.6us = ~ 25us.
 During one complete frame transmission cycle of 400us (see below),
 400us/25us = 16 single 10-bit-values can be acquired.
 The value range of 8 added 10-bit-samples is 0..8192,
 so the sum of 8 added samples must be divided by two
 before packing it in the transmitted data frame.
 

 


Timing considerations for SERIAL INTERFACE
----------------------------------------------------------------------
 At 115.2 kBit/sec, a single bit time (T_ser_bit) is
          1 / 115200 = 8.6805555 us. 
          The PIC's instruction cycle (with 10MHz crystal)
          is 4 / 10MHz = 400 ns. To achieve an "average"
          bit time of 8.68 us, the number of instruction cycles
          between two transmitted bits must be 21.7 ,
          so it will be mostly 22 instructions but somtimes
          only 21 instructions (otherwise the receivers clock
          will be too far off when receiving the last data bit).

 Total count of bit-times in the 4-byte frame:
          4 * (1+8+1) = 40 bit.
 Total time to transmit the frame:
          Tframe = 40 * 8.6805us = 347.22 us
 Frame repetition rate:
          Tcycle = 1/(2500Hz) = 400 us
 "Gap" between two frames
          Tgap = Tcycle - Tframe = 400us - 347.22us = 52.78
          (during this time, "less important" tasks 
           may be performed in the PIC firmware).



   
              
          




