Autor Tema: Subrutina Conversor A/D 12 Bits MAX187-9  (Leído 1294 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado ORE

  • PIC10
  • *
  • Mensajes: 43
Subrutina Conversor A/D 12 Bits MAX187-9
« en: 09 de Mayo de 2006, 14:41:44 »
Hola muy buenas,

Coloco la subrutina del conversor MAX187, que he adaptado para poder obtener el valor digital de una señal analógica procedente del exterior.

Un saludo,

//////////////// Driver for MAX187 A/D Converter ////////////////////////
////                                                                                           ////
////  init_ext_adc()                      Call after power up                   ////
////                                                                                          ////
////  value = read_ext_adc()              Converts to digital number  ////
////                                      and sends to MCU                          ////
////                                                                                          ////
/////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2003 Custom Computer Services            ////
//// This source code may only be used by licensed users of the CCS  ////
//// C compiler.  This source code may only be distributed to other     ////
//// licensed users of the CCS C compiler.  No other use,                   ////
//// reproduction or distribution is permitted without written                ////
//// permission.  Derivative programs created using this software       ////
//// in object code form are not restricted in any way.                       ////
/////////////////////////////////////////////////////////////////////////

//Definición de los puertos, esto se puede cambiar, en un principio es para un PIC16F877

#ifndef MAX187_CS

   #define MAX187_DOUT  PIN_C4
   #define MAX187_SCLK  PIN_C3
   #define MAX187_CS    PIN_C2

#endif

//Función de inicialización de los puertos

void init_ext_adc() {
                     output_low(MAX187_CS);
                     output_low(MAX187_SCLK);
                    }

long read_ext_adc() {
                     int i;
                     long data;
                     data=0;

                     //Espero 85 us, para iniciar la conversion (tCONV)
                     
                     delay_us(85);               
                     
                     //PIN CS a nivel bajo para iniciar conversión

                     output_low(MAX187_CS);
                     for(i=0;i<12;++i) {         
                                       output_low(MAX187_SCLK);
                                       delay_us(1);

                                       //Leo en dos bytes el valor de salida del A/D

                                       shift_left(&data,2,input(MAX187_DOUT));
                                       output_high(MAX187_SCLK);
                                       delay_us(1);

                                       }
                     delay_us(10);
                     output_high(MAX187_CS);
                     return(data);
                    }

//Convierto a voltios el valor de data

float convert_to_volts(long data) {
                                   return ((float)data*2.5/0xFFFF);
                                  }