Version:0.9 StartHTML:0000000105 EndHTML:0000147668 StartFragment:0000001210 EndFragment:0000147652 mikroIDE
/* 12 juin 2010

 version speciale avec CS AD7715  maintenu à ZERO
 et choix Frequence sample rate=200 au lieu de 20  FS1=1 FS0=1
 Usage d'une reference 4,096V MCP1541   +-50 ppm/°C  out 2mA
 test avec 115200 bds => bad 4,8% erreur sur vitesse
 => laissé UART à 57600 bds   1,36% erreur sur la vitesse = OK
 
  Interface to TinyBootLoader, v1.9.5
  http://www.etc.ugal.ro/cchiculita/software/picbootloader.htm
  Connected to \\.\COM2 at 19200
  HEX: 1 min old, INHX32,18Fcode+cfg, total=4402 bytes.
  Searching for PIC ...
  Found:18F 252o/452o
  WRITE OK  at 17:03,   time:4.427 sec
*/
// avec PIC18F252  sous MikroC 8.2.0.0
// version 1 entree Ana 10 bits + 16 entrees ANA 16 bits , sortie LCD & RS232
// gestion ADC 16 bits AD7715  connecté sur port B
// gestion CD4051 demultiplexeur 8-> 1 via le port A
// port C    Rc6 Rc7 = UART1

// Librairies utilisees   :
//   ADC ,  C_String , Conversions , UART , EEPROM


/*--- Hardware ------------
PIC 18F252 DIP20 small
ADC 16 bits  AD7715  DIP16
2 x CD4051 démultiplexeurs 8-> 1 analogique
Q=10Mhz
 -----------------------
 PIC    Pin   fonction  pin
RA0     2      EA0
RA1     3      A        11 CD4051 N°1 select 1
RA2     4      B        10 CD4051 N°1 select 2
RA3     5      C        9 CD4051 N°1 select 4
RA4     6     INH1      6 CD4051 N°1 validation   Valid=0
                        13,14,15,12,1,5,2,4  8 inputs
                        3  sortie
                        16 Vdd  7  VEE    8 VSS
RA5     7    INH2        6 CD4051 N°2 validation   Valid=0
 -----------------------
      PIC      pin      pin LCD Nokia 3310  version 8x16 cars
RC0  SCE        11--R->  5   CS    Chips select
RC1  D_C        12--R->  4   D/C   Data ou Cde
RC2  RES        13--R->  8   RES   reset
RC3  SCK        14--R->  2   SCK   Serial Clock
RC4  SDI        15--R->  3   DIN   Data Input
                         6   GND    0V
                         7   Capacite 1µF ( 8,2V accross)
                         1   VDD  alim 3,3V not connected !!!
RC5  --->       not used
     Liaison serie           RS232 driver
RC6  Rs_TX      17 ----> 1   DS275 ... ecran
RC7  Rs_Rx      18 <---  3   DS275 ... clavier
---------------------
     PIC      pin      pin  AD7715
RB0  ADC_DI     21 ---> 14   DIN
RB1  ADC_DRDYI  22 <--- 12   /DRDY
RB2   .....
RB3  ADC_RESET  24 --->  5   /RESET
RB4  ADC_DO     25 ---> 13   DOUT
RB5  ADC_CLK    26 --->  1   SCLK
RB6  --->        not used
RB7  ---Led rouge -- 560 ohms --< +5V
...................Other pins AD7715
      7 +AIN input
      8 -AIN input =0V
     11 AGND  =0V
     16 DGND  =0V
     15 AVDD  =+5V
      6 DVDD  =+5V
      2 Quartz 1MHz
      3 Quartz 1MHz
      4 version avec /CS maintenu à Zero en permanence !
      10 Reference MCP1541 4,096V (Sans ajustement!)
-----------------------
ref MCP1541 en boier T092
      1 +0V
      2 sortie 4.096V
      3 +5V
---------------------------
*/
//-- Selection des voies sur les 2 multiplexeur CD4051 8->1
#define Poids1 PORTA.F1
#define Poids2 PORTA.F2
#define Poids4 PORTA.F3
#define INH1   PORTA.F4
#define INH2   PORTA.F5
//--- COM RS232 USART ----
#define Rs_TX PORTC.F6
#define Rs_RX PORTC.F7
//--- ADC16bits AD7715 --------
#define ADC_DI    PORTB.F0
#define ADC_DRDY  PORTB.F1
#define ADC_CS    PORTB.F2
#define ADC_RESET PORTB.F3
#define ADC_DO    PORTB.F4
#define ADC_CLK   PORTB.F5
//#define notused PORTB.F6
#define LedRouge  PORTB.F7
//--- LCD nokia sur port C
#define SCE PORTC.F0
#define D_C PORTC.F1
#define RES PORTC.F2
#define SCK PORTC.F3
#define SDI PORTC.F4
//#define notused PORTC.F5
//-------------------------------------
//#define Tracage     // utilise pour debugging seulement
#define TAB 9
#define CR 13

#define LCD_PRESENT

const char table[480] = {
0x00,0x00,0x00,0x00,0x00, // 20 space ASCII table for NOKIA LCD: 96 rows * 5 shorts= 480 shorts
0x00,0x00,0x5f,0x00,0x00, // 21 ! Note that this is the same set of codes for character you
0x00,0x07,0x00,0x07,0x00, // 22 " would find on a HD44780 based character LCD.
0x14,0x7f,0x14,0x7f,0x14, // 23 # Also, given the size of the LCD (84 pixels by 48 pixels),
0x24,0x2a,0x7f,0x2a,0x12, // 24 $ the maximum number of characters per row is only 14.
0x23,0x13,0x08,0x64,0x62, // 25 %  A variant of Nokia 3310 can use 16 char per row and 8 rows
0x36,0x49,0x55,0x22,0x50, // 26 &
0x00,0x05,0x03,0x00,0x00, // 27 '
0x00,0x1c,0x22,0x41,0x00, // 28 (
0x00,0x41,0x22,0x1c,0x00, // 29 )
0x14,0x08,0x3e,0x08,0x14, // 2a *
0x08,0x08,0x3e,0x08,0x08, // 2b +
0x00,0x50,0x30,0x00,0x00, // 2c ,
0x08,0x08,0x08,0x08,0x08, // 2d -
0x00,0x60,0x60,0x00,0x00, // 2e .
0x20,0x10,0x08,0x04,0x02, // 2f /
0x3e,0x51,0x49,0x45,0x3e, // 30 0
0x00,0x42,0x7f,0x40,0x00, // 31 1
0x42,0x61,0x51,0x49,0x46, // 32 2
0x21,0x41,0x45,0x4b,0x31, // 33 3
0x18,0x14,0x12,0x7f,0x10, // 34 4
0x27,0x45,0x45,0x45,0x39, // 35 5
0x3c,0x4a,0x49,0x49,0x30, // 36 6
0x01,0x71,0x09,0x05,0x03, // 37 7
0x36,0x49,0x49,0x49,0x36, // 38 8
0x06,0x49,0x49,0x29,0x1e, // 39 9
0x00,0x36,0x36,0x00,0x00, // 3a :
0x00,0x56,0x36,0x00,0x00, // 3b ;
0x08,0x14,0x22,0x41,0x00, // 3c <
0x14,0x14,0x14,0x14,0x14, // 3d =
0x00,0x41,0x22,0x14,0x08, // 3e >
0x02,0x01,0x51,0x09,0x06, // 3f ?
0x32,0x49,0x79,0x41,0x3e, // 40 @
0x7e,0x11,0x11,0x11,0x7e, // 41 A
0x7f,0x49,0x49,0x49,0x36, // 42 B
0x3e,0x41,0x41,0x41,0x22, // 43 C
0x7f,0x41,0x41,0x22,0x1c, // 44 D
0x7f,0x49,0x49,0x49,0x41, // 45 E
0x7f,0x09,0x09,0x09,0x01, // 46 F
0x3e,0x41,0x49,0x49,0x7a, // 47 G
0x7f,0x08,0x08,0x08,0x7f, // 48 H
0x00,0x41,0x7f,0x41,0x00, // 49 I
0x20,0x40,0x41,0x3f,0x01, // 4a J
0x7f,0x08,0x14,0x22,0x41, // 4b K
0x7f,0x40,0x40,0x40,0x40, // 4c L
0x7f,0x02,0x0c,0x02,0x7f, // 4d M
0x7f,0x04,0x08,0x10,0x7f, // 4e N
0x3e,0x41,0x41,0x41,0x3e, // 4f O
0x7f,0x09,0x09,0x09,0x06, // 50 P
0x3e,0x41,0x51,0x21,0x5e, // 51 Q
0x7f,0x09,0x19,0x29,0x46, // 52 R
0x46,0x49,0x49,0x49,0x31, // 53 S
0x01,0x01,0x7f,0x01,0x01, // 54 T
0x3f,0x40,0x40,0x40,0x3f, // 55 U
0x1f,0x20,0x40,0x20,0x1f, // 56 V
0x3f,0x40,0x38,0x40,0x3f, // 57 W
0x63,0x14,0x08,0x14,0x63, // 58 X
0x07,0x08,0x70,0x08,0x07, // 59 Y
0x61,0x51,0x49,0x45,0x43, // 5a Z
0x00,0x7f,0x41,0x41,0x00, // 5b [
0x02,0x04,0x08,0x10,0x20, // 5c Yen Currency Sign
0x00,0x41,0x41,0x7f,0x00, // 5d ]
0x04,0x02,0x01,0x02,0x04, // 5e ^
0x40,0x40,0x40,0x40,0x40, // 5f _
0x00,0x01,0x02,0x04,0x00, // 60 `
0x20,0x54,0x54,0x54,0x78, // 61 a
0x7f,0x48,0x44,0x44,0x38, // 62 b
0x38,0x44,0x44,0x44,0x20, // 63 c
0x38,0x44,0x44,0x48,0x7f, // 64 d
0x38,0x54,0x54,0x54,0x18, // 65 e
0x08,0x7e,0x09,0x01,0x02, // 66 f
0x0c,0x52,0x52,0x52,0x3e, // 67 g
0x7f,0x08,0x04,0x04,0x78, // 68 h
0x00,0x44,0x7d,0x40,0x00, // 69 i
0x20,0x40,0x44,0x3d,0x00, // 6a j
0x7f,0x10,0x28,0x44,0x00, // 6b k
0x00,0x41,0x7f,0x40,0x00, // 6c l
0x7c,0x04,0x18,0x04,0x78, // 6d m
0x7c,0x08,0x04,0x04,0x78, // 6e n
0x38,0x44,0x44,0x44,0x38, // 6f o
0x7c,0x14,0x14,0x14,0x08, // 70 p
0x08,0x14,0x14,0x18,0x7c, // 71 q
0x7c,0x08,0x04,0x04,0x08, // 72 r
0x48,0x54,0x54,0x54,0x20, // 73 s
0x04,0x3f,0x44,0x40,0x20, // 74 t
0x3c,0x40,0x40,0x20,0x7c, // 75 u
0x1c,0x20,0x40,0x20,0x1c, // 76 v
0x3c,0x40,0x30,0x40,0x3c, // 77 w
0x44,0x28,0x10,0x28,0x44, // 78 x
0x0c,0x50,0x50,0x50,0x3c, // 79 y
0x44,0x64,0x54,0x4c,0x44, // 7a z
0x00,0x08,0x36,0x41,0x00, // 7b <
0x00,0x00,0x7f,0x00,0x00, // 7c |
0x00,0x41,0x36,0x08,0x00, // 7d >
0x10,0x08,0x08,0x10,0x08, // 7e Right Arrow ->
0x78,0x46,0x41,0x46,0x78}; // 7f Left Arrow <-
//
//.. gestion LCD .....
void initlcd(void);   // Initializes the LCD.
void sendcmd(char);   // Writes a command.
void senddata(char);  // Writes data to DDRAM to illuminate the pixels.
void cleanram(void);  // Erase the DDRAM contents.
void gotoxy(char,char); // Position cursor to x,y.
void sendchar(char);  // Write one character.
void sendpack(char);
//  gestion string
void PrintHandler(char c);
void Write_String(void);// envoie une chaine de caracteres
void CRLF(void) ;
// .. gestion ADC 16 bits ......
void write_adc_short(short);
void adc_7715_init(void);
long int read_adc_word(void) ;
long int read_adc_value(void);
//
void interrupt (void);   // not used now..for futur

//... declarations de variables communes
char *txt="1234567890123456789012345678901234567890";
char *valtxt="1234567";
char *mail ="paulfjujo@free.fr";
char  car_recu;
unsigned int M;
unsigned int N1;
unsigned int M16[16];
short masque;
short indice;
unsigned int counter;
unsigned short Drapeau;
char outmode;
char dummy;

void main()
{
unsigned int  Nb;
unsigned int i, i1,k1;
  PORTB = 0x00;
  TRISB = 0b00010010 ;  // portb pins 0 et 1 as inputs others as output
  INTCON2.RBPU=0;
  PORTA = 0xFF;
  TRISA = 0x01;          // PORTA is RA0=input  EA1..RA5=outputs
  ADCON0 =1 ;
 // 0x81  portA all analog avec  An3=+ref   reliée à 4,096V
 // 0x80  portA All Ra0..RA5 as analog inputs b10000000
 // 0x8E  portA Ch0 only as analog input, all other are digital
  ADCON1 = 0x8E ;       //b  1000 1110
  TRISC = 0;            // designate PORTC pins as output
  T0CON=0;
  UART1_Init(57600);    // init RS232
  if (UART1_Data_Ready() == 1) car_recu=UART1_Read();
  outmode=7 ;    // aiguillage -> RS232 +CR LF
  CRLF();
  txt= strcpy(txt," AD7715_16EA_4096mV.C ");
  Write_String();
  k1=strlen(mail);
  for(i1 = 0; i1 <k1; i1++) EEPROM_Write(0x00+i1,*(mail+i1));
  Delay_ms(100);
  txt= strcpy(txt," Lecture EEPROM ");
  Write_String();
  for(i1 = 0; i1 < k1; i1++) {   // Read xx bytes from address 0x00
   i = EEPROM_Read(0+i1);        //   and send to PC for displaying data
   Delay_ms(100);
   UART1_Write(i);
   }
  txt= strcpy(txt," Delai inter trame=30mS ");
  Write_String();
  #ifdef LCD_PRESENT
   // -------- PRESENTATION SUR LCD --------
  outmode=0;     // aiguillage -> LCD
  initlcd();
  Delay_ms(100);
  gotoxy(0,0);
  #else
  outmode=5 ;
  #endif
  //1234567890123456     <- gabarit pour lCD
  txt= strcpy(txt,"AD7715_nokia_18F");
  Write_String();
  LedRouge=0;
  #ifdef LCD_PRESENT
    gotoxy(0,0);
  #endif
  txt= strcpy(txt,"Version 12/06/10");
  Write_String();

  #ifdef LCD_PRESENT
    gotoxy(0,1);
  #endif
  txt= strcpy(txt,"16 Voies 16 bits");
  Write_String();
  #ifdef LCD_PRESENT
    gotoxy(0,2);
  #endif
  txt= strcpy(txt,"ADC CH0 10bits  ");
  Write_String();
  #ifdef LCD_PRESENT
    gotoxy(0,3);
  #endif
  txt= strcpy(txt,"Uses CD4051 8->1");
  Write_String();
  #ifdef LCD_PRESENT
    gotoxy(0,4);
  #endif
  txt= strcpy(txt,"--- MENU -------");
  Write_String();
  #ifdef LCD_PRESENT
      gotoxy(0,5);
  #endif
  txt= strcpy(txt,"1 affich N°trame");
  Write_String();
  #ifdef LCD_PRESENT
    gotoxy(0,6);
  #endif
  txt= strcpy(txt,"2 affich ADC CH0");
  Write_String();
  #ifdef LCD_PRESENT
    gotoxy(0,7);
  #endif
  txt= strcpy(txt,"3 Simul AD7715  ");
  Write_String();

  outmode=1;  // RS232
  CRLF();
  adc_7715_init();

  txt= strcpy(txt,"16 Valeurs 16bits");
  // PrintOut(PrintHandler," %s\n\r",txt) ; <-- BAD ????
  Write_String();
  CRLF();
  txt= strcpy(txt,"Menu affichage: 1=NB  2=CH0 3=Simul");
  Write_String();
  CRLF();
  // valeurs pour simulation AD7715
  outmode=1;
  for (indice=0;indice<16;indice++)
  {
   M16[indice]=indice*1750+525;
   WordToStr(M16[indice], txt);
   Write_String();
   }
  CRLF();
  Delay_ms(1000);
  indice=0;
  car_recu=0;
  Nb=0; // compteur de trame
  #ifdef LCD_PRESENT
  cleanram()  ;
  #endif
 // boucle principale
  while(1)
 {
  Nb=Nb+1;
  outmode=1;       // RS232 select
  if(Nb % 32==0)
  {
      adc_7715_init();     // init ADC 16 bits, Zero et echelle 4.096V
      CRLF();              // toute les 32 trames de mesures
      }
  if (Drapeau & 0x01)
  {
  txt= strcpy(txt,"Nb=");
  WordToStr(Nb, valtxt);
  strcat(txt,valtxt);
  Write_String();
  }
  if (Drapeau & 0x02)
  {
  strcpy(txt,"CH0=");
  M = ADC_Read(0) ;
  WordToStr(M, valtxt);
  strcat(txt,valtxt);
  Write_String();
  }
  outmode=0;
  indice=0;
  #ifdef LCD_PRESENT
  gotoxy(0,0);
  #endif
  
  do
  {
   //lecture ADC
   //Choix de la voie du multiplexeur 0 à 7  code sur A B C
   //multiplié par 2 car A B C sur RA1 RA2 RA3 respectivement
   //RA0 est conservé comme entree ANA 10 bits
   PORTA=(indice << 1 );
   if (indice<8)
    {
     INH1=0;   // selection du CD4051 N°1
     INH2=1;
      }
      else
      {
      INH1=1 ;
      INH2=0;   // selection du CD4051 N°2
     }
   Delay_ms(30);     // attente stabilisation apres commutation 120ms mini !
   if (Drapeau & 0x04)
       M = M16[indice];
        else
         M=read_adc_value() ;
   WordToStr(M, txt);   // mesure transformée binaire -> text ascii
  // formatage sortie numerique sur 5 cars avec "0" en tete
   for(i=0;i<5;i++)
   {
    if (*(txt+i)==' ') *(txt+i)='0';
   }
   Write_String();        // ecriture sur sortie RS232
   // rajout separateur , sauf sur derniere mesure
   if (indice<15)
   {
    UART1_Write(',');
   }
   #ifdef LCD_PRESENT
   if (indice<8) gotoxy(0,indice);
   if (indice>7) gotoxy(8*6,indice-8);
   outmode=0;
   strcpy(txt,"0 ");
   if (indice<10) *(Txt)=indice +48; else *(Txt)=indice +38;
   Write_String();
   WordToStr(M, txt);
   Write_String();
   outmode=1;
   #endif
   indice ++;
  } while (indice < 16);
  
  UART1_Write(10);  //    CRLF() ; //= 13 + 10
  if(LedRouge==0)    LedRouge=1;  else LedRouge=0;
  if (UART1_Data_Ready() == 1) // si caractere recu dans le buffer UART
   {
    car_recu=UART1_Read();
    //UART1_Write(car_recu);  // echo
    switch (car_recu)
     {
      case '1': Drapeau= Drapeau ^ 1; break;
      case '2': Drapeau = Drapeau ^ 2 ; break;
      case '3':Drapeau = Drapeau ^ 4 ;  break;
      default:   break;
      }
      car_recu=0;
    }
   }
}
// ****************  fin du MAIN *****************
void interrupt ()
{
 if (PIR1.TMR1IF) {
    counter++;
    TMR1H=0xFF;
    TMR1L=0xFF;
    PIR1.TMR1IF = 0;
  }
}

// -------  LCD ------------
void initlcd()
{
 RES =1;
 SCE =1;
 RES =0;
 Delay_ms(200);
 RES=1;            // come out of lcd state
 sendcmd(0x21);    //Function SET: 00100_PD_V_H
 sendcmd(0x90);    //VLCD=;3.06+162*0.06=4.02
 sendcmd(0x05);    //; TC0=1 TC1=0
 sendcmd(0x13);    //bias System 1:48 -> n=4
 sendcmd(0x20);    //Function SET: 00100_PD_V_H
 Delay_ms(10);
 sendcmd(0x0C);
 cleanram();
 Delay_ms(20);
 gotoxy(0,0);
}

void sendcmd(char x)
{
 D_C= 0;
 SCE=0;
 sendpack(x);
 Delay_ms(2);
 SCE=1; //deselect the display
 return;
}

void senddata(char x)
{
 D_C= 1;
 SCE=0;
 sendpack(x);
 SCE=1; //deselect the display
}

void cleanram()
{int i;
 gotoxy(0,0);
 for(i=0;i<=896;I++)
 {
 senddata(0x00);
 Delay_ms(1);
 }
}

void gotoxy( char x, char y)
{
 sendcmd(0x40|(y&0x07));
 sendcmd(0x80|(x&0x7f));
}

void sendchar(char x)
{ int a,b;
 char c;
 if((x<0x20)||(x>0x7f)) return;
 a=((5*x)-160);
 for(b=5;b>0;b--)
  {  c=table[a];
    senddata(c);
    a++;
   }
 senddata(0x00);
}

void sendpack(char x)
{ int i;
 for(i=0;i<8;i++)
 {
  SCK=0;
  if ((x&0x80)==0x80) SDI=1; else SDI=0;
  SCK=1;
  x=x<<1; }
}
//-----------------

void Write_String()
{
 short int i1,j1,k;
 j1=strlen(txt);
   i1=0;
   do
   {
   k=txt[i1];
   if (outmode & 0x01)    // RS232 output
       UART1_Write(k);
   else
   sendchar(k);
    i1++;
   }while (i1<j1);
   switch (outmode)
   {
   case 0:   break;
   case 1:   UART1_Write(9); break;
   case 3:   UART1_Write(',');  break;
   case 5:   UART1_Write(13);  break;
   case 7:   UART1_Write(13); UART1_Write(10); break;
   default:  UART1_Write('*'); break;
   }
}

void CRLF()
{
 UART1_Write(10);
 UART1_Write(13);
}

//--------- ADC 16 bits --------------------

void adc_7715_init()
{  int i;
  ADC_RESET=0;
  delay_ms(10);
  ADC_CLK=1;
  //ADC_CS=1;          //Select AD7715
 // delay_ms(1);
  ADC_RESET=1;        //Reset AD7715
  ADC_CS=0;
  delay_ms(1);
  ADC_DI=1;
  delay_ms(1);
  ADC_CLK=1;
  delay_ms(1);
  for (i=1;i<=5;i++)
  {
    write_adc_short(0x10)  ;    // preparation acces au Setup register, pour mode ecriture
    write_adc_short(0xFF) ;     // ecrit 1111 1111 dans le registre setup
  }
  write_adc_short(0x10) ; delay_ms(1);
  write_adc_short(0x00) ; delay_ms(1);
  //Communications Register :  acces au setup , prochain acces en ecriture ,
  // pas de STBY,   GAIN=1
  write_adc_short(0x10);  //acces Setup register
  delay_ms(1);
  // write_adc_short(0x44);
  //Setup Register: Mode SelfCalibration automatique , Clk=1MHz,
  //filtre=20Hz ,Unipolar ,Buffer ,Pas de synchro
  //write_adc_short(0x54);
  //Setup Register: Mode SelfCalibration automatique , Clk=1MHz,
  //filtre=100Hz ,Unipolar ,Buffer ,Pas de synchro
   write_adc_short(0x54);
  //Setup Register: Mode SelfCalibration automatique , Clk=1MHz,
  //filtre=200Hz ,Unipolar ,Buffer ,Pas de synchro
   delay_ms(1);

}

void write_adc_short(short data1)
 {
  short i,k;
  //ADC_CS=0;
  //delay_ms(1);
  k=0;
  for(i=1;i<=8;++i)
  {
   ADC_CLK=0;
   if ((data1 & 0x80)==0x80)
   {
    ADC_DI=1;
    #ifdef Tracage
     UART1_Write(49);
    #endif
   }
    else
   {
    ADC_DI=0;
    #ifdef Tracage
     UART1_Write(48);
    #endif
   }
   data1 = data1 << 1;
   ADC_CLK=1;
  }
  //ADC_CS=1;
  ADC_CLK=1;

  #ifdef Tracage
    UART1_Write(13) ;
    UART1_Write(10) ;
  #endif
}

long int read_adc_word() {
   short i;
   long data2;
   //ADC_CS=0;
   //delay_ms(1);
   data2 = 0;
   for(i=15;i>=0;--i)
   {
     ADC_CLK=0;
     ADC_CLK=1;
     if (ADC_DO==1) data2 =data2 + (1 << (i-1)) ;
   }
   //ADC_CS=1;
   return data2;
}

long int read_adc_value()
{
  long int value;
  while ( ADC_DRDY==1) ;
  while ( ADC_DRDY==0) ;
 // while ( ADC_DRDY==1) ;
  // DRDY=0 ZERo=0 RS1=1 RS0=1 R/W=1  STBY=0  G1=0 G0=0
  //  operation  d'ecriture dans le  registre de communication
  // avec Gain=1
  //  le prochain acces sera en Lecture sur le Data Register
  write_adc_short(0x38); //  Read select   acces Data register 16 bits  Gain=00 soit 1
 // 32767 points pour 2,048V
 // write_adc_short(0x39); // Gain=2    32767 points pour 1,024V
  value=read_adc_word();
  return value;
}
/*      resultat sur terminal RS232
17:05:53.232> Yjþ Lecture EEPROM
17:05:53.232>
17:05:54.935> paulfjujo@free.fr
17:05:54.935>
17:05:56.387>
17:05:56.387>
17:05:56.437> 16 Valeurs 16bits
17:05:56.437>
17:05:56.437> Menu affichage: 1=NB  2=CH0 3=Simul
17:05:56.437>
17:05:56.487>   525         2275         4025         5775         7525         9275        11025        12775        14525        16275        18025        19775        21525        23275        25025        26775
17:05:56.487>
17:06:02.997> 27196040470000300005177730000400002191070000000000000000000000000000000000000000
17:06:06.562> 27203040520000300004177750000300002191070000000000000000000000000000000000000000
17:06:10.107> 27188040480000200004177730000400002191070000000000000000000000000000000000000000
17:06:13.662> 27213040520000300005177750000400002191070000000000000000000000000000000000000000


 mikroCPIC1618.exe -MSF -DBG -pP18F252 -DL -O11111114 -fo10 -N"C:\_MickroC\_MesProjets\_AD7715\AD7715_16EA_4096.mcppi" -SP"E:\_save2010\mikroC PRO for PIC\defs\" -SP"E:\_save2010\mikroC PRO for PIC\uses\P18\" -SP"C:\_MickroC\_MesProjets\_AD7715\" "AD7715_16EA_4096mV.c" "__Lib_Math.mcl" "__Lib_MathDouble.mcl" "__Lib_System.mcl" "__Lib_Delays.mcl" "__Lib_CString.mcl" "__Lib_Conversions.mcl" "__Lib_ADC_A_C.mcl" "__Lib_EEPROM_256.mcl" "__Lib_UART_c67.mcl"
 All files Preprocessed in 50 ms
 Compilation Started AD7715_16EA_4096mV.c
 Generated baud rate is 56818 bps (error = 1.36 percent) AD7715_16EA_4096mV.c
 Compiled Successfully AD7715_16EA_4096mV.c
 All files Compiled in 561 ms
 Used RAM (bytes): 392 (26%)  Free RAM (bytes): 1123 (74%) Used RAM (bytes): 392 (26%)  Free RAM (bytes): 1123 (74%)
 Used ROM (bytes): 4381 (13%)  Free ROM (bytes): 28387 (87%) Used ROM (bytes): 4381 (13%)  Free ROM (bytes): 28387 (87%)
 Project Linked Successfully AD7715_16EA_4096.mcppi
 Linked in 681 ms
Project 'AD7715_16EA_4096.mcppi' completed: 1422 ms
Finished successfully: 12 juin 2010, 20:06:34 AD7715_16EA_4096.mcppi
 */