Version:0.9 StartHTML:0000000105 EndHTML:0000339306 StartFragment:0000001579 EndFragment:0000339290 C:\_MickroC\_MesProjets_MickroC\_16F877_Platine\16F877_Platine_Nokia_RS_24LC256_140628.c
// 29-06-2014    ... OK .....
// recupere Pic Downloader 1.08 
// test chargement ..plus d'erreur ! all OK
// rajout Cde RAZ Index eeprom
// modif Index relecture pour 1 à xxxx au lieu de 0 à xxx
// suite division par 2 bytes pour chaque Int mesure ADC
// modif SiplayDate   affiche 23 pour jour 29 sur UARTT mais OK sur LCD
// enlevé conversion BCcd2dec
// DateTime[1]=Mois;ByteToStr( Mois,CRam1); UART1_Write_Text( CRam1); UART1_Write('/'); 
//  was DateTime[1]=Mois;ByteToStr( Bcd2Dec(Mois),CRam1); UART1_Write_Text( CRam1); UART1_Write('/'); 


// Config touches de fonction terminal VBRAY
// 16F877_Platine_Nokia_RS.tmf      ac:Vbray_TF
// Hardware:
// Platine 16F877 8Ko equipee Nokia  + I2C + 2 x PCF8574 + 24LC256 + DS1307 + DS275
// LCD nokia 3310 8 lignes de 16 car
// Q=10Mhz
// config:
// 16F877_Platine_Nokia_10Mhz.cfgsch

// schema :      ac:Platine_Schema

// 28-06-2014
// problem avec donwloader version 1.09 ... error juste avant la fin de chargement
// corection BUG dans void strConstRamCpy(unsigned char *dest, const code char *source);
// car *dest etait tout le temp initialise sur table TEXTE!
// si rajout save DateTime sur eeprom => problemes avec IRP bit et aussi
// => derniere adresse Flash rom
// L_end_main:
// 0x1F27        0x2F27              GOTO       $+0
// 27-6-2014
// probleme avec Write String si usage d'un autre buffer que TEXTE => Modifs 
// passage buffer comme argument ...reste des bugs!! 
// Downloader  : writing error line number 992    avec free rom=8%   
// Downloader  : writing error line number 723 avec free rom= 33%  ??
// fichier LST donne:
//Address Opcode         ASM
//0x0000        0x160A              BSF        PCLATH, 4
// 0x0001        0x2800              GOTO       4096     // soit 1000H
//; 16F877_Platine_Nokia_RS_24LC256_140627.c,1089 ::                 void main()
// ;16F877_Platine_Nokia_RS_24LC256_140627.c,1091 ::                 PORTB = 0x00;           // set PORTB to FF
//0x1000        0x1283              BCF        STATUS, 5
//0x1001        0x1303              BCF        STATUS, 6
//0x1002        0x0186              CLRF       PORTB

// par contre Platine_16F877_nokia_Xport_Archos_120228_ok.HEX se charge OK ??
// en MPLAB 100% asm rev 28/02/2011  
//  Platine_16F877_nokia_Xport_Archos_120228.asm 

// 23-06-2014
// test3       16F877_Platine_Nokia_RS_24LC256_140623.c
// apres reloading bootloader 16F877 8Ko
// derniere adresse flash rom utilisable
// 1F20 3EFF 3EFF 3EFF 3EFF 3EFF A00A 2B8A A00A 
// .....
//1FF0 8C00 2503 FA83 23A0 1303 09FB 1003 279E 

// 17-06-2014
// rajout gestion 24LC256
// cdes terminal
// STA<CR> start stockage sur EEprom  Index razé!
// HAL<CR> HALte sauve sur eerprom  (Index NON razé!) 
// REL<CR> Stope stockage et Relit (liste) contenu Eeprom  
// DUM<CR> dump Flash du PIC
// probleme : insuffisance memoire ROM 
// => mis en commentaares mesg en ROM    ; OK masi , car usage 98,30% de ROM  
// last adress code 1F7C  ??   empiete le bootloader ?
// bootloader at 0x1FFF - taille code= 0xD6 soit    0x1F29 
// Flash programm memory protection 0x1F00 to 0x1FFF    
// compilation donne Error
//102 342 There is not enough ROM space __Lib_MathDouble.c
//1056 392 Address must be greater than 0 main 16F877_Platine_Nokia_RS_24LC256_140617.c

// rechargement du bootloader 16F877 8Ko
// en reduisant les messages et code au maxi
// 0x1EE2       [7]    ?lstr_9_16F877_Platine_Nokia_RS_24LC256_140617
// compil OK    mais tranfert Donloader avec msg error ligne number 1029 ?
// recompilation..
// "C:\_Mplab8\_Mesprojets_ASM\_BOOTLOADERs\bootldr_16F877_10_19200_140617.hex".
// Pickit2: Config 3FF3  checksum=5277
//Run ...   semble OK

// 16-06-2014
// PB resolu : j'avais inversé la definition des bits software I2C  SCL et SDA !
// mais reste un bug sur affichage time sur ecran seulement !
/*
U;16;06;14;1;19;35#
Mise a l'heure RTC 
Time:  13: 23:  0        ADC0=   442        ADC1=  1020
Time:  13: 23:  2        ADC0=   442        ADC1= 
*/
// 0x13= 19   0x22=35 ! => résolu .. ne pas reconvertir à nouveau Heure,minute en dec2bdc!!

// 14-06-2014
// CONFIG   :$2007 : 0x1B12  Flash programm over 0x10000 protegee
// 16F877_Platine_Nokia_RS.hex
// test DS1307 .. BAD
// des que j'initialise I2C ,plus d'affichage LCD !!!
// HOOREUR ! le piege : c'est en I2C Software que la platine est prevue (car au depart 100% Asm)
// et RC3 RC4 sont utilisés en SPI pour le nokia .. 
// I2C SOFTWARE sur RB7=SCL et RB6=SDA
//  pile HS, remplacee..test RTC ..BAD
// re-essai avec version asm     
// C:\_Mplab8\_Mesprojets_ASM\_16F877_Platine\Platine_16F877_nokia_Xport_Archos_120225_ok.asm
// fichier hex reloadé :   Platine_16F877_nokia_Xport_Archos_120228_ok.HEX
// apres remise à L'heure via cde U<CR>  AAMMJJWWHHMMSS => OK 
// led Seconde clignote


/*
Version  XPORT-Archos43it RFObasic 
 en MPLAB 100% asm rev 28/02/2011  
 Platine_16F877_nokia_Xport_Archos_120228.asm 
 ----
 PIC16F877 avec bootloader Q=10Mhz
 MPLAB version 8.63
 Platine 16F877 2012
 ----
<H>elp
<1>Consigne 1
<2>Consigne 2
<3>Consigne 3
<L>ecture PCF
<E>critur PCF
<U>pdate RTC
------
#14/06/14 18H51M32S;A1=0793 ;A2=0305 ;A3=0293 ;A4=0279 ;A5=0271 ;L=00000001=01h;R=00100010=22h;&
#14/06/14 18H51M35S;A1=0793 ;A2=0305 ;A3=0294 ;A4=0286 ;A5=0281 ;L=00000010=02h;R=00100010=22h;&
#14/06/14 18H51M37S;A1=0793 ;A2=0305 ;A3=0294 ;A4=0285 ;A5=0277 ;L=00000011=03h;R=00100010=22h;&
#14/06/14 18H51M39S;A1=0793 ;A2=0305 ;A3=0294 ;A4=0287 ;A5=0283 ;L=00000100=04h;R=00100010=22h;&
re-teste version MikroC..rine ne s'affiche apres mes 5 lignes de presentation LCD nokia
Version Ldd_Chr en conflit avec LCD biblio => desactivee

// 13-06-2014
// Ne pas utiliser la biblio ADC Mickroc
// qui ne gere pas le cas de Vref specifique 
// rajout bragraph nokia

// 12-06-2014
// hardware LCD connecté sur PORT C
// ATTENTION 
// 16f877 avec bootloader  !
// bootldr_16F877_10_19200_071217.asm
//#define LoaderSize        0xD6                             ; taille du bootloader
//#define LoaderMain        UserStart+5                     ; adresse de base du bootloader
//#define LoaderTop        ProgHI-(ICD_DEBUG*0x100)     ; adresse haute du bootloader
//#define LoaderStart        (LoaderTop)-LoaderSize+1     ; adresse de demarrage du bootloader
// mode d'emploi downloader
// terminal deconnecté
// lancer l'outils N°3 = Downloader
// fenetre Dowloader, cliquer sur WRITE 
// faire POWER OFF/POWER ON
// le telechargement commence ..
// a la fin, fermer Downloder et reactiver le terminal (connect)
// usage de Pic Downloader 1.09 
// C:\_MickroC\_MesProjets_MickroC\common\ICSP_downloader109.exe
//  pour transfert via bootloader du 16F877
 */

 //==== Parametrage pour RS232 ====
#define FOSC D'10000000'        ; <<< frequence du quartz[Hz]  ici= 10 MHz
#define BAUD D'19200'           ; <<< vitesse du port serie [bit/sec] ici=19200
#define BAUD_ERROR D'4'; <<< erreur de vitesse [%]
#define TIMEOUT  D'10'
#define BAUD_REAL   FOSC/((D'64'-(HIGH_SPEED*D'48'))*(DIVIDER+1))

#define RS232 1
#define Nokia 0

//#define Limite_Max_Flash 0x1F28


//-- compilations conditionnelles -----
//#define Test3
#define EEPROM_24LC256
//#define Test_Timer2
//#define Need_IRPBit
#define Software_I2C

#define EEPROM_32K_ADR 0xA0 // EEPROM 24LC256
#define DS1307 0xD0

//  Nokia LCD
#define D_C PORTC.B1
#define SCE PORTC.B0
#define RES PORTC.B2
#define SCK PORTC.B3
#define SDI PORTC.B4

 // pour RTC DS1307 + PCF8574
#ifdef Software_I2C       
sbit Soft_I2C_Scl           at RB6_bit;
sbit Soft_I2C_Sda           at RB7_bit;
sbit Soft_I2C_Scl_Direction at TRISB6_bit;
sbit Soft_I2C_Sda_Direction at TRISB7_bit;

// pour garder la syntaxe I2C Hardware
#define I2C1_Start()    Soft_I2C_Start()               // Issue start signal
#define I2C1_Wr        err=Soft_I2C_Write           // Address PCF8583, see PCF8583 datasheet
#define I2C1_Stop     Soft_I2C_Stop             // Start from address 2
#define I2C1_Rd      Soft_I2C_Read 
#endif
 
sbit INIT_RTC  at RB1_bit;  
 

#define LedVerte PORTE.F0
#define LedBlanche PORTE.F1
#define LedRouge PORTE.F2
#define pi 3.14159 // Constant pi.
#define num_of_points 64 // Number of points to plot in a graph.


#define CLS 12
#define CR 13
#define LF 10
#define BS 8
#define TAB 9



const char table[480] = {
0x00,0x00,0x00,0x00,0x00, // 20 space ASCII table for NOKIA LCD: 96 rows * 5 bytes= 480 bytes
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 %
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 <-


// ---- BECAUSE 98% ROM utilise , passé en commentaire les declarations suivantes
//const code char mesg0[]="MikroC 16F877 avec Bootloader Platine PF  17-06-2014\r\n";
//const code char mesg1[]="RTC+UART+I2C+PCF8574+24LC256+Nokia3310 Q=10Mhz\n\r";
//const code char * Messages[]={mesg0,mesg1};
//const code char * Messages[]={mesg0};
const code char JS[8][6] ={"Diman","Lundi","Mardi","Mercr","Jeudi","Vendr","Samed"};


#define MAXLEN 32
unsigned  char buffer[MAXLEN] ;

 
unsigned char TEXTE[32];
unsigned char *txt;

unsigned char CRam1[30];
unsigned char *CRam;

char CRam0[6];
volatile int Index1, CptErr,i1;
volatile int count;
int i,j,k;
unsigned int ir,k1;
unsigned int Index;
long L;
unsigned int M,M0,M1;

char UnCar,err;
unsigned char Seconde, Minute, Heure, week_day, day, Mois, Annee;
unsigned char octet;
unsigned char DateTime[9];

volatile struct chbits {   // 8 flags
                 unsigned FrameErr:1;
                 unsigned Store:1;
                 unsigned Elligible:1;
                 unsigned Togle:1;            
                 unsigned Second;
                 unsigned See:1;
                 unsigned Full:1;
                 unsigned Lcd:1;
               }Drapeaux ;   
float F1;  





// --------------------------------
 
#ifdef Test3 
unsigned int unite=0,dizaine=0,centaine=0,mille=0;
unsigned long compteur=10000;
unsigned long distance=0;
#endif


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.     x de 0 à 95   Y de 1 à 9
void sendchar(char); // Write one character.
void sendpack(char);
void redirige_RS232(int wcr,unsigned char *T);
void Write_String(unsigned char *T);
void CRLF(void);
void strConstRamCpy(unsigned char *dest, const code char *source);
unsigned char *Float2Ascii (float x, unsigned char *str,char precision);
void UART1_Write_CText(const char *txt); 
 
//---- EEPROM PIC  bytes  ================
unsigned char EEPROM_Lect(unsigned char adrE) ; // lecture a l'adresse adrL
void EEPROM_Ecr(unsigned char adrE,unsigned char car); // ecrit  a l'adresse adrE
void Save_Msg_To_EEPROM(unsigned char * adrx, unsigned char *ptr1);
void PrintOut_Eeprom_Msg(unsigned char PtrE);  //  for 16Fxxxxx

void  Init_DS1307(void);
void Write_Time(void) ;
void Read_Time(char *Seconde, char *min, char *Heure, char *week_day, char *day, char *Minute, char *Annee);
void Transform_Time(char  *Seconde, char *min, char *Heure, char *week_day, char *day, char *Minute, char *Annee);
void Display_Time(char Seconde, char Minute, char Heure); 
void Display_Date(char week_day,char day, char Mois, char Annee)  ;
void Write_Byte(unsigned char c2);


void Write_Byte_To_24LC256(unsigned int Adr, unsigned char cx);
unsigned char Read_Byte_from_24LC256(unsigned int Adr);
void Read_Msg_From_24LC256(unsigned char *t, unsigned int Adr, int Lg);
void Write_Msg_To_24LC256(unsigned char *t, unsigned int Adr, int lg);
void Save_Date_2_24LC256(void);
 
void interrupt()
 {
    unsigned char c1;
    if ((RCIE_bit) && (RCIF_bit))
    {
       if (RCSTA.OERR==1)    // voir parag 16.1.26 p273
      {
       RCSTA.CREN = 0 ;
       c1 = RCREG;
       RCSTA.CREN = 1 ;
        CptErr++;
       }
      if(RCSTA.FERR==1 )
      {
      RCSTA.SPEN = 0 ;
      RCSTA.SPEN= 1 ;
      CptErr++;
       c1 = RCREG;
      }
      c1 = RCREG;     // lecture registre => RAZ RCIF 
     if ((c1==CR) || (i1>=MAXLEN)) 
      {
      Drapeaux.Full=1;
      buffer[i1]=0;
      Index1=i1;
      i1=0;
      c1=0;
     }
     else
     { 
          buffer[i1]=c1;
        Index1=i1;
       i1++;
      }
    // RCIF_bit=0   ;
    }
  // ------ Timer0 --------------
    if ((T0IE_bit)&&(TMR0IF_bit))
     {  
         TMR0IF_bit    = 0;
         TMR0       = 60;         //with  Q=20Mhz  prescaler 1:256 =>   (255-60)*256*0.2   =10035 µS   ~10mS
         count++;                  //Increase counter by one
        if (count >= 100)         //Adjust this value for different time (10ms x XX)
        {
         Drapeaux.Elligible=1;  
         count = 0;
         Soft_I2C_Break();

         }
      
      }
   #ifdef Use_Timer1  
   // -- Timer1 --------------
    if ((TMR1IE_bit)&&(TMR1IF_bit))
      {  
         TMR1IF_bit    = 0;
        // TMR1H=Hi (Valeur);
        // TMR1L=Lo (Valeur);
         TMR1H= (3035 >> 8);
         TMR1L= (3035 & 0x00FF);
         //TMR1H=  0x0B; // 3035  init à 100mS at 20Mhz
         //TMR1L=  0xDB;
         count1++;                  //Increase counter by one
      if (count1 >= 10)         //Adjust this value for different time (10ms x 100 = 1000ms or 1sec)
        {
         Drapeaux.Second=1;  
         count1 = 0;
         }
      }
   #endif     
   
   #ifdef Test_Timer2
   if((TMR2IE_bit) && (TMR2IF_bit))
        {
        LedRouge=~LedRouge;
         //     TXREG='*';    to see tha interrupt has occured on terminal UART
         TMR2=0;
        TMR2IF_bit=0;
       }
   #endif    
 }

#ifdef Test_Timer2
 void Init_Timer2(void)
{        
        TMR2 = 0x00;
        //T2CON = 0x75;  // Prescaler 4, Postscaler 
        T2CON.TOUTPS0=0x0F; // 1:16 Postscale
        T2CON.T2CKPS0=3 ;  // Precaler= 16
        T2CON.TMR2ON=1; // Timer 2 ON
        
        T2CON=0b01111111; 
        TMR2IF_bit = 0;
        CCP1CON=0;
        TMR2IE_bit = 1;
        TMR2=0;
        PR2 = 0xFF;     
}
#endif


//------  EEPROM du PIC16Fxxx ---------------

unsigned char EEPROM_Lect(unsigned char AdrE) // lecture a l'adresse adr
{  
     EEADR =(unsigned char) AdrE & 0x00FF;
    //EEADRH =0;
        EECON1.EEPGD=0;     // 0= adressage sur EEPROM , 1=sur flash
      // EECON1.CFGS=0;      // 0= acces data EEPROM
        EECON1.RD=1;        // arme Lecture EEprom
       while( EECON1.RD=1);  // wait RD=0
        return(EEDATA);
}

//void EEPROM_Ecr(unsigned char AdrE,unsigned char car) // ecrit  a l'adresse adrE
void EEPROM_Ecr(unsigned char AdrE,unsigned char car) // ecrit  a l'adresse adrE
{int dr=0;
   if(INTCON.GIE ==1)
   {
   INTCON.GIE =0 ;
    dr=1;
   } 
    while( EECON1.WR==1); 
    EEADR =AdrE ;
    EEDATA=car;   
    EECON1.EEPGD=0;
    EECON1.WREN=1;  
   EECON1.WRERR=0;    
    EECON2=0x55;
    EECON2=0xAA; 
    EECON1.WR=1; 
    // test si fin d'ecriture
   while ( EECON1.WR ==1); 
    // ou rajout delay   
    // Delay_ms(20);
    PIR2.EEIF=0;    // to be sure
    EECON1.WREN=0;  
   if (dr==1)INTCON.GIE =1;
}

void Save_Msg_To_EEPROM(unsigned char adrx, unsigned char * ptr1)
{   while ((*ptr1>0) && (adrx<256))
        {
         EEPROM_Ecr(adrx++,*ptr1++);
         Delay_ms(20);        
         }
}

void PrintOut_Eeprom_Msg(unsigned char PtrE)
{ unsigned char c;
  do
  {
  c = EEPROM_Lect(PtrE);
  if ( c>0) UART1_Write(c);
  PtrE++;
   Delay_ms(20);
  }
  while ((c!=0) && (PtrE<255))  ;
}


#ifdef EEPROM_24LC256
// ******* EEPROM 32K  ******************

void Write_Byte_To_24LC256(unsigned int Adr, unsigned char cx)
{

I2C1_Start();
I2C1_Wr(EEPROM_32K_ADR );      // write
I2C1_Wr(Adr>>8);    // send data (data to be written)
I2C1_Wr(Adr& 0x00FF);
I2C1_Wr(cx);    // send data (data to be written)
I2C1_Stop();
Delay_ms(5);
 }

unsigned char Read_Byte_from_24LC256(unsigned int Adr)
{
unsigned char c3;
I2C1_Start();
I2C1_Wr(EEPROM_32K_ADR );  // write
I2C1_Wr(Adr>>8);    // send data (data to be written)
I2C1_Wr(Adr&0x00FF);
I2C1_Stop();
I2C1_Start();    // I2C1_Repeated_Start();
Delay_ms(1);

I2C1_Wr(EEPROM_32K_ADR+1 );  // Read
c3=I2C1_Rd(0u);    // data to be read)
I2C1_Stop();
return c3;   
}

void Read_Msg_From_24LC256(unsigned char *t, unsigned int Adr, int Lg)
{int i;
  for (i=0;i<Lg;i++)
  {
   *(t+i)=Read_Byte_from_24LC256(Adr+i);
    Delay_ms(1);
  }
 }
 
void Write_Msg_To_24LC256(unsigned char *t, unsigned int Adr, int lg)
{ int i;
  for (i=0;i<lg;i++)
  {
   Write_Byte_To_24LC256(Adr+i, *(t+i)) ;
   Delay_ms(5);   // delay minimum !!
  }
}

//void Save_Date_Time_2_24LC256()
void Save_Date_2_24LC256()
{
GIE_bit=0;
 Read_Time(&Seconde,&Minute,&Heure,&week_day,&day,&Mois,&Annee);      // read time from RTC(DS1307)
 Write_Byte_To_24LC256(Index,Mois); Index++; 
  Write_Byte_To_24LC256(Index,day); Index++;  
   // Write_Byte_To_24LC256(Index,Heure);  Index++;
   //   Write_Byte_To_24LC256(Index,Minute);   Index++;   
    //    Write_Byte_To_24LC256(Index,Seconde);   Index++; 
         Write_Byte_To_24LC256(Index,13);   Index++;    
        GIE_bit=1;
} 
       
#endif 
 // *******************************************

void UART1_Write_CText(const char *txt)
 {
   while (*txt)
      UART1_Write(*txt++);
}
 
 

 
 unsigned char *Float2Ascii (float x, unsigned char *str,char precision)
{
 /* converts a floating point number to an ascii string */
 /* x is stored into str, which should be at least 30 chars long */
 unsigned char *adpt;
 int ie, i, k, ndig;
 double y;
 adpt=str;
 ndig = ( precision<=0) ? 7 : (precision > 22 ? 23 : precision+1);
 ie = 0;
 /* if x negative, write minus and reverse */
 if ( x < 0)
 {
   *str++ = '-';
   x = -x;
 }
 /* put x in range 1 <= x < 10 */
 if (x > 0.0) while (x < 1.0)
 {
   x *= 10.0;                // a la place de =*
   ie--;
 }
 while (x >= 10.0)
 {
   x = x/10.0;
   ie++;
 }
 // in f format, number of digits is related to size
 ndig += ie;                                // a la place de =+
 //round. x is between 1 and 10 and ndig will be printed to
 // right of decimal point so rounding is ...
 for (y = i = 1; i < ndig; i++)
 y = y/10.;
 x += y/2.;
 if (x >= 10.0) {x = 1.0; ie++;}
 if (ie<0)
 {
   *str++ = '0'; *str++ = '.';
   if (ndig < 0) ie = ie-ndig;
   for (i = -1; i > ie; i--)  *str++ = '0';
 }
 for (i=0; i < ndig; i++)
 {
   k = x;
   *str++ = k + '0';
   if (i ==  ie ) *str++ = '.';
   x -= (y=k);
   x *= 10.0;
  }
 *str = '\0';
return (adpt);
} 
    
/*
 void Write_Byte(unsigned char c2)
 {
  ByteToHex(c2,CRam0);UART1_Write_Text(CRam0);   UART1_Write(':');
  }   
*/    

 #ifdef Software_I2C
void Init_DS1307() 
{  // 04 juin 2014 à 13H59
   Seconde=0x00;
   Minute=0x59;
   Heure=0x13 ;                             
   day=0x04;
   Mois=0x06;
   week_day=0x04;
   Annee=0x14;
   Write_Time();
}

void Write_Time()
 {
        I2C1_Start();       // start signal
        I2C1_Wr(DS1307);    // addresse DS1307
        I2C1_Wr(0);         // start from REG 0
        I2C1_Wr(0x80);      // write $80 to REG 0. (pause counter + 0 sec)
        I2C1_Wr(Minute);      // write  Minutes to REG 1
        I2C1_Wr(Heure);        // write  Heures (mode 24H) to REG 2
        I2C1_Wr(week_day);  // write Jour semaine to REG 3
        I2C1_Wr(day);       // write  date to REG 4
        I2C1_Wr(Mois);        // write  mois to REG 5
        I2C1_Wr(Annee);      // write annee to REG 6
        I2C1_Stop();        // issue stop signal
        //I2C1_Repeated_Start(void);      // issue start signal
        I2C1_Start();       // start signal
        Delay_ms(10);
        I2C1_Wr(DS1307);      // address DS1307
        I2C1_Wr(0);         // start from word at address 0
        I2C1_Wr(0);         // write 0 to REG0 (enable counting + 0 sec)
        I2C1_Stop();        // issue stop signal
}

void Read_Time(char *Seconde, char *Minute, char *Heure, char *week_day, char *day, char *Mois, char *Annee)
 {
        I2C1_Start();
        I2C1_Wr(DS1307);
        I2C1_Wr(0);
         I2C1_Stop();
        I2C1_Start();
        Delay_ms(5);
        I2C1_Wr(0xD1);
        Delay_ms(1);
        *Seconde =I2C1_Rd(1);
        *Minute =I2C1_Rd(1);
        *Heure =I2C1_Rd(1);
        *week_day =I2C1_Rd(1);
        *day =I2C1_Rd(1);
        *Mois =I2C1_Rd(1);
        *Annee =I2C1_Rd(0);
        I2C1_Stop();
}

void Transform_Time(char  *Seconde, char *Minute, char *Heure, char *week_day, char *day, char *Mois, char *Annee)
 {
        *Seconde  =  ((*Seconde & 0x7F) >> 4)*10 + (*Seconde & 0x0F);
        *Minute  =  ((*Minute & 0xF0) >> 4)*10 + (*Minute & 0x0F);
        *Heure   =  ((*Heure & 0x30) >> 4)*10 + (*Heure & 0x0F);
        *week_day =(*week_day & 0x07);
        *day  =  ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
        *Mois   =  ((*Mois & 0x10) >> 4)*10 + (*Mois & 0x0F);
        *Annee =  ((*Annee & 0xF0)>>4)*10+(*Annee & 0x0F);
}
#endif

 void Lcd_Car(int x,int y, unsigned char c)
 {
   gotoxy(y,x);      // attention Y varie de 1 à 8
   sendchar(c);
   } 

void Display_Time(char Seconde, char Minute, char Heure) 
{      Drapeaux.Lcd=1;  //outmode=Nokia;
       gotoxy(0, 1)  ; // FIRST_ROW  
       strConstRamCpy(txt,"Time: ");
       Write_String(txt);
       redirige_RS232(0,txt);
       gotoxy(1, 1)  ; // FIRST_ROW  
       Lcd_Car(1,7,(Heure / 10)  + 48);
       Lcd_Car(1,8,(Heure % 10)  + 48);
       Lcd_Car(1,9,':');
       Lcd_Car(1,10,(Minute / 10) + 48);
       Lcd_Car(1,11,(Minute % 10) + 48);
       Lcd_Car(1,12,':');
       Lcd_Car(1,13,(Seconde / 10) + 48);
       Lcd_Car(1,14,(Seconde % 10) + 48);
       Lcd_Car(1,15,' ');
    // disply on terminal
       Drapeaux.Lcd=0; 
       DateTime[4]=Heure; ByteToStr( Heure,CRam0); UART1_Write_Text( CRam0);UART1_Write(':'); 
       DateTime[5]=Minute; ByteToStr(  Minute,CRam0); UART1_Write_Text( CRam0); UART1_Write(':');
       DateTime[6]=Seconde; ByteToStr( Seconde,CRam0); UART1_Write_Text( CRam0); 
       UART1_Write(';');
      
  }

 void Display_Date(char week_day,char day, char Mois, char Annee)  
 {    
       Drapeaux.Lcd=1; //outmode=Nokia;
       strConstRamCpy(txt,JS[week_day]);
       Write_String(txt);
       redirige_RS232(0,txt);
       Drapeaux.Lcd=1;
       gotoxy(0, 2)  ;  
       Lcd_Car(2,5,32);
       Lcd_Car(2,6,(day / 10)   + 48);    // Print tens digit of day variable
       Lcd_Car(2,7, (day % 10)   + 48);    // Print oness digit of day variable
       Lcd_Car(2,8,'/');
       Lcd_Car(2,9,(Mois / 10) + 48);
       Lcd_Car(2,10,(Mois % 10) + 48);
       Lcd_Car(2,11,'/');
       Lcd_Car(2,12,'2');
       Lcd_Car(2,13,'0');
       Lcd_Car(2,14, (Annee / 10)  + 48);          // Print Annee vaiable + 8 (start from Annee 2008)
       Lcd_Car(2,15, (Annee % 10)  + 48);
       Drapeaux.Lcd=0; 
       DateTime[0]=day;ByteToStr(day,CRam1); UART1_Write_Text( CRam1);  UART1_Write('/');
       DateTime[1]=Mois;ByteToStr( Mois,CRam1); UART1_Write_Text( CRam1); UART1_Write('/'); 
      //  DateTime[1]=Mois;ByteToStr( Bcd2Dec(Mois),CRam1); UART1_Write_Text( CRam1); UART1_Write('/'); 
       DateTime[2]=Annee;ByteToStr(Annee,CRam1); UART1_Write_Text( CRam1);    
       DateTime[3]=week_day;//IntToStr( Bcd2Dec(week_day),CRam1); UART1_Write_Text( CRam1);  // jour de la semaine sur 1 seul digit 
       UART1_Write(';');
 
}


void MAJ_RTC()
{ int m,l,j,i;
  PIE1.RCIE = 0;
  m=2; // passe par dessus "U;"
  j=0;
   
   CRLF();
   l=strlen(buffer);
   WordToStr(l,CRam0);
   UART1_Write_Text(CRam0);
   CRLF();
  // U;04;06;14;04;19;59#
    if ((buffer[0]=='U')&& (buffer[l-1]=='#') && (l==20))
     { 
         UART1_Write_CText("\n\rMise a l'heure RTC \r\n"); 
         for (i=2;i<l;i++)
          { 
            if(buffer[i]==';')
              { 
                buffer[i]=0;
                DateTime[j]=atoi(buffer+m);
                m=i+1;
                j++;
              } 
          }// for i 
       // ne pas oublier le dernier! 
        buffer[l-1]=0; // elimine # en fin de chaine
        DateTime[j]=atoi(buffer+m);
        day=Dec2Bcd(DateTime[0]);
        Mois=Dec2Bcd(DateTime[1]);
        Annee=Dec2Bcd(DateTime[2]);
        week_day=Dec2Bcd(DateTime[3]); // jour de la semaine sur 1 seul digit
       Heure=Dec2Bcd(DateTime[4]);
       Minute=Dec2Bcd(DateTime[5]);
       Write_Time();
       buffer[0]=0;
           
       j=0;
       CptErr=0;
       Drapeaux.Full=0;
   
     }// if buff1[0]='U'
    PIE1.RCIE = 1;
 }   


 
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(100);
gotoxy(0,0);
}

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

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

void Erase_Line(int L)
{
 int i,j;
 gotoxy(0,L);
 Delay_ms(10);
 i=(L-1)*96;;
 j=i+96;
 for(L=i;L<j;L++)
 {
 senddata(0x00);
 Delay_ms(1);
 }
  gotoxy(0,L);
   Delay_ms(2);
} 


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

void Bargraph (int V , int ligne)
{
  unsigned int i,j,m;
   F1= (float)V *96.0/1024.0;
   m=(unsigned int)F1;   
  if ((ligne>0) && (ligne<9))    //   1 à 8 
  {
    sendcmd(0x40|((ligne-1) & 0x07));
   for (i=0;i<94;i++)
   {
     sendcmd(0x80|i);
     if (i<=m)  senddata(0x3C); else  senddata(0x00);
     Delay_us(5);
   }
    sendcmd(0x40|((ligne-1) & 0x07));
  }
 }


void gotoxy( char x, char y)
{ if ((y>0) && (y<9))    //   1 à 8 
 {
  sendcmd(0x40|((y-1) & 0x07));
  sendcmd(0x80|((x*6) & 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 redirige_RS232(int wcr, unsigned char *T)
 {
   Drapeaux.Lcd=0; //outmode=0;
  Write_String(T);
  if (wcr==1)
   {
   UART1_Write(10);
  UART1_Write(13);
   }
   Drapeaux.Lcd=1; //outmode=1;
 }

void Write_String(unsigned char *T)
{
short int i1,j1,k;
 j1=strlen(T);
   i1=0;
   do
   {
   k=T[i1];
   if (Drapeaux.Lcd==0)
      UART1_Write(k);
   else  
      sendchar(k);
   i1++;
   }while (i1<j1);
}
 void CRLF()
 {
 UART1_Write(13);
 UART1_Write(10);
 }

// --- Copie le texte pointé en ROM  vers RAM
void strConstRamCpy(unsigned char *dest, const code char *source) {
  while (*source)
  {*dest = *source ;
    dest ++  ;
    source ++;
  }  
  *dest = 0 ;    // terminateur
}


 void ADC_Init_()
{
   ADCON0 = 0x81;  // ADON = 1, Channel A.0 select, Fosc/32 select
   ADCON1 = 0x8E;        // 0x8E,0xC0 port a as analog, right justified output
}        //original 0x8E


unsigned int ADC_Read_(unsigned char channel)
{
  int i; 
  unsigned int RawM, avg_output=0,temp=0 ;
  if(channel > 7) return 0; 
  ADCON0 &= 0xC7;        // AND= RAZ channel selection
  ADCON0 |= channel<<3; // OR = Set  chanel selection
  Delay_ms(3);
 // for(i=0;i<32;i++)
 // {   
     GO_bit = 1;
     _asm NOP  //  allways one instruction after setting bit GO  (MicroChip recommandation)
      while(GO_bit);  // Wait until conversion gets over
     RawM=(unsigned int) (ADRESH<<8) + ADRESL ;   // Store 10-bit output into a 16-bit variable
 //    temp=temp+RawM;
//     WordToStr(RawM,CRam0); 
//     if (Drapeaux.See==1)
 //    {
 //      Drapeaux.Lcd=0 ;
 //     UART1_Write_Text(CRam0);    UART1_Write(TAB);
//      if ((i % 8)==0) CRLF();
 //       Drapeaux.Lcd=1;
  //   }
     Delay_ms(1);    
 // }
 // avg_output=temp>>5;    // use SHIFT to divide by 32 to avoid use of maths.h
//return(avg_output);
return(RawM);
}


#ifdef Use_Timer1   
void Init_Timer1(void)    
 {
  T1CON=0;
 // T1CON=0b00110000; 
  TMR1CS_bit=0;   // FOSC /4 internal clock .. page 52
  T1CKPS1_bit=1;  //prescal select=11 => 1/8
  T1CKPS0_bit=1;
  //TMR1H= 0x0B; // Hi (3035);  // init à 100mS at 20Mhz
  //TMR1L= 0xDB; // Lo (3035);
   TMR1H= (3035 >> 8);
  TMR1L= (3035 & 0x00FF); 
  PIR1.TMR1IF=0;     // Reset Timer1 Flag
  PIE1.TMR1IE=0;  //Enable interrupt TMR1
  TMR1ON_bit=0;
} 
#endif  



void main()
{
 PORTB = 0x00;           // set PORTB to FF
 TRISB = 0xFD;        // designate portb pins as input except RB2 as output
 PORTA = 0xFF;
 TRISA = 0xFF;          // PORTA is RA0,1,2,3,4,5=input
 TRISD=0x00; // all outputs
  
 PORTC = 0xFE;       // set PORTC to $FF
 TRISC = 0xC0;          // designate PORTC pins as output
 TRISE=0;
 PORTE=0xFF;
 LedBlanche=1;
   
   // portA A0 A1 analog AN2=-Vref=3,0V An3=+ref=4.096V   RA5,RE0,RE1,RE2 digitals,
 ADC_Init_(); // my Adc init !
  
 Drapeaux.Lcd=0; //outmode=RS232;
 Delay_ms(1000);
 LedBlanche=0;
   
 UART1_Init(19200);   // RS232
 Delay_ms(100);
 UART1_Write(CLS);
 Delay_ms(1000);
  
 LedBlanche=1;
 txt=&TEXTE[0];
 CRam=&CRam1[0];
 
  
 //  ---- OK ci dessous mais inhibée because --------
 //0 342 There is not enough ROM space ?lstr_16_16F877_Platine_Nokia_RS_24LC256_140617 
 //  16F877_Platine_Nokia_RS_24LC256_140617.c
   
 // UART1_Write_CText( "Mikroc Pro 6.01\r\n");
 // UART1_Write_CText( "Nokia_LCD_16F877\r\n");
 //  UART1_Write_CText("With Bootloader.\r\n");
  UART1_Write_CText( "Platine PIC16F Nokia  140629 \r\n");

/*
  L=Get_Fosc_kHz();
  UART1_Write_CText("Freq kHz= ");
  LongToStr(L, CRam1);
  UART1_Write_Text(CRam1);
  CRLF();
 */

 initlcd();       // Nokia LCD 3310
 Delay_ms(500);
 Drapeaux.Lcd=1; //outmode=Nokia;

/*

 // ------- Presentation inhibée because --------
 //0 342 There is not enough ROM space ?lstr_16_16F877_Platine_Nokia_RS_24LC256_140617 
 //  16F877_Platine_Nokia_RS_24LC256_140623.c
 gotoxy(0,1);
 strConstRamCpy(txt, (const code char *)"Mikroc Pro 6.01");
 Write_String();        // sur LCD
 redirige_RS232(1);
 gotoxy(0,2);
 strConstRamCpy(txt,(const code char *)"Platine 16F877  "); 
 Write_String();
 redirige_RS232(1);
  gotoxy(0,3);
 strConstRamCpy(txt,(const code char *) "With Bootloader.");
 Write_String();
 redirige_RS232(1);
 gotoxy(0,4);
 strConstRamCpy (txt,(const code char *)"RTC DS1307 I2C ");
 Write_String();
 redirige_RS232(1);
 gotoxy(0,5);
 strConstRamCpy (txt,(const code char *)  "Paulfjujo@freefr");
 //  txt= strcpy(txt,"Paulfjujo@freefr");
 Write_String();
 redirige_RS232(1);
*/  
 Delay_ms(5000);
  
 #ifdef Test_Timer2
 UART1_Write_CText("\n\r Init Timer2\r\n "); 
 Init_Timer2() ;
 #endif
 
 #ifdef Use_Timer1
 Init_Timer1();
 #endif
  
 //UART1_Write_CText("\n\r ATTENTION : SOFTWARE I2C Init :  ");     
// UART1_Write_CText(" SCL =RB6   SDA=RB7\n\r ");   
  
 #ifdef Software_I2C
 Soft_I2C_Init ();  // car RC3 et RC4 deja utilisé en SPI pour Nokia
 #else
 I2C1_Init(400000);
 #endif
  
 
  if (INIT_RTC ==1) 
   {
  //  UART1_Write_CText("\n\r Init DS1307 par defaut \r\n "); 
    Init_DS1307(); 
   }
  // else
  // {
  //  UART1_Write_CText("\n\r re-Lecture DS1307 \r\n ");
  // }  
 Drapeaux.Lcd=0;
 UART1_Write_CText(" Read RTC TIME\r\n "); 
  
 Read_Time(&Seconde,&Minute,&Heure,&week_day,&day,&Mois,&Annee);      // read time from RTC(DS1307)
 Transform_Time(&Seconde,&Minute,&Heure,&week_day,&day,&Mois,&Annee); // format date and time
 Display_Time(Seconde, Minute, Heure);
 Display_Date(week_day, day, Mois, Annee);
 Delay_ms(3000);

 Drapeaux.See=0; 
 Drapeaux.Lcd=1; //outmode=Nokia;
 Erase_Line(4);
 gotoxy(0,4);
 strConstRamCpy( txt,(const code char *)"ADC0= ");
 Write_String(txt);
 Erase_Line(6);
 gotoxy(0,6);
 strConstRamCpy( txt,(const code char *) "ADC1= ");
 Write_String(txt);
 
  // ******************************************************************
 #ifdef Test1
  // 1ere chose , verifier que l'UART fonctionne (au moins en transmission)
 UART1_Write_CText("\n\r  TEST UART \r\n ");
 Delay_ms(100);
// ADC_Init();  //  prealable à l'usage de ADC..read
 UART1_Write_CText(" Init ADC \r\n ");
  j=0;
 PORTB = 0;
 do
   {
   temp_res = ADC_Read_(0); // Get results of AD conversion lecture des données du port A0
   IntToStr(temp_res,buffer);
   UART1_Write_Text(buffer);
   UART1_Write(13); // change de ligne
   UART1_Write(10);    // retour à la ligne
   Delay_ms(500);
   j++;
 
   }while(j<20);
 #endif
 // *************************************************************
  
  
   CRLF();
   UART1_Write_CText( "Keyb Cdes : \r\n"
   "- STArt sauve -> 24LC256\r\n"
   "- HALt  sauve x 24LC256\r\n"
   "- RELit <-  from 24LC256\r\n"
   "- DUMp all Flash area\r\n"); 
   
   
   UART1_Write_CText( "\r\nInit Interrupts UART \r\n ");
//INTCON =>  GIE PEIE T0IE INTE RBIE T0IF INTF RBIF
  INTCON=0;
  RCIE_bit=1;
  UnCar=RCREG; // like RCIF_bit=0;
  PEIE_bit=1;
  GIE_bit=1;
  
  j=0;
  Drapeaux.Store=0;
  Index=0;
  i1=0;
  Index1=0;
  buffer[0]=0;
  txt=TEXTE;
  
  while(1)
  {
 
   Read_Time(&Seconde,&Minute,&Heure,&week_day,&day,&Mois,&Annee);
   Transform_Time(&Seconde,&Minute,&Heure,&week_day,&day,&Mois,&Annee);
   Drapeaux.Lcd=1;
   //Display_Date( week_day, day,  Mois, Annee) ; 
   // affiche time sur ligne 1 du Nokia et sur UART1
   Display_Time(Seconde,Minute, Heure); 

   Drapeaux.Lcd=0;
   UART1_Write_CText( " ADC0= ");
   txt=TEXTE;
   M0 = ADC_Read_(0) ;
   WordToStr(M0, txt);
   Drapeaux.Lcd=1; 
   gotoxy(6,4);    // X = 0 à 15   Y=1 à 8
   Write_String(txt);
   redirige_RS232(0,txt); 
   Drapeaux.Lcd=0;  
   UART1_Write (';');
   Drapeaux.Lcd=1;  
   Bargraph (M0 ,5) ;

   LedBlanche=! LedBlanche;
   
   txt=TEXTE;
  // gotoxy(0,7);
  // Drapeaux.Lcd=0;
  // txt= strcpy(txt,"ADC1= ");
  //  strConstRamCpy txt,(const code char *) "ADC1= ");
  // Write_String();
  // redirige_RS232(0);
 
   UART1_Write_CText( " ADC1= ");
   txt=TEXTE;
   M1 = ADC_Read_(1) ;
   WordToStr(M1, txt);
   Drapeaux.Lcd=1; 
   gotoxy(6,6);
   Write_String(txt);
   redirige_RS232(0,txt);
   Bargraph (M1 ,7) ;
   if (Drapeaux.Store==0) CRLF();
 
   Delay_ms(200);
  
    j++;
 //   if (j>1023)j=0;
 //   Bargraph (j ,8) ;
  
   Drapeaux.Lcd=0;
  // indice J 
  // WordToStr(j, txt);
  // Drapeaux.Lcd=1; 
  // gotoxy(0,8);
  // Write_String();
   
  // ---------------------------------------------
 #ifdef Test3
  CRLF();  
  for (j=0;j<31;j++)
  { 
      compteur=(unsigned long)1<<j ;
      distance=  4253L * compteur / 1000L  ; // distance = 42 530 000
      UART1_Write_CText( "j= ");
      WordToStr(j,CRam1);
      UART1_Write_Text(CRam1);  UART1_Write(TAB);
      UART1_Write_CText( "Compteur 1<<j = ");
      LongWordToStr(compteur,CRam1);
      UART1_Write_Text(CRam1);  UART1_Write(TAB);
      // attention aux debordement !
      // 1048576 *4253= 4 459 593 728    => deborde
      //alors que 2^32= 4 294 967 295
      UART1_Write_CText( "Distance (version faux flottant) = ");
      LongWordToStr(distance,CRam1);
      k=strlen(CRam1);  
      // il faut completer par des zeros
      if (distance<1000) CRam1[k-4]='0'; 
      for (i=0;i<k-3;i++) UART1_Write(CRam1[i]);
      UART1_Write('.');
      if (distance<100) CRam1[k-3]='0';
      if (distance<10) CRam1[k-2]='0';   
      for (i=k-3;i<k;i++) UART1_Write(CRam1[i]);
      CRLF();
    }
  CRLF();    
 while(1);
  #endif
  //------------------------------------------------
 
   if( Drapeaux.Full==1)
   { 
      UART1_Write_Text( buffer);
      CRLF();
      if ((buffer[0]=='U') &&  (buffer[19]=='#')) 
      {
          MAJ_RTC();
         Read_Time(&Seconde,&Minute,&Heure,&week_day,&day,&Mois,&Annee);      // read time from RTC(DS1307)
         Transform_Time(&Seconde,&Minute,&Heure,&week_day,&day,&Mois,&Annee); // format date and time
         Display_Time(Seconde, Minute, Heure);
         Display_Date(week_day, day, Mois, Annee);
         
      }    
   
      if ((buffer[0]=='M') && (buffer[1]=='O')&& (buffer[2]=='T')&& (Index1==4))
      {
       Drapeaux.Lcd=1; 
       gotoxy(0,8);
       strConstRamCpy(txt,"Cde Moteur")  ;
       if (buffer[3]=='1') strConstRamCpy(txt+10," ON ");  //   txt= strcpy(txt,"Cde Moteur ON ");
       if (buffer[3]=='0') strConstRamCpy(txt+10," OFF");
       Write_String(txt);
       redirige_RS232(1,txt);
      }
     if ((buffer[0]=='L') && (buffer[1]=='E') && (buffer[2]=='D')&& (Index1==4))
      { // LED1<CR>   LED0<CR>
       Drapeaux.Lcd=1; 
       gotoxy(0,8);
       strConstRamCpy(txt,"Led Rouge");
       if (buffer[3]=='1')
       {
          strConstRamCpy(txt+9,"  ON   ");
          LedRouge=0;     // logic inverse !
       }   
       if (buffer[3]=='0') 
       {
          strConstRamCpy(txt+9,"  OFF  ");
          LedRouge=1;
       }   
       Write_String(txt);
       redirige_RS232(1,txt);
      }
  
      if ((buffer[0]=='R') && (buffer[1]=='A') && (buffer[2]=='Z')&& (Index1==3))
      {  
         UART1_Write_CText("\r\nRAZ Idx saveEeprom\r\n");
         Index=0;
      }
    
    #ifdef EEPROM_24LC256
    if ((buffer[0]=='S') && (buffer[1]=='T') && (buffer[2]=='A') && (Index1==3))
    {// STA<CR>
     UART1_Write_CText("\r\nSTART saveEeprom\r\n");
     Drapeaux.Lcd=1;
     CRam=CRam1;
     gotoxy(0,3);
     strConstRamCpy(CRam,"START saveEeprom");
     Write_String(CRam);
     Drapeaux.Store=1;
     //if (Index==0) Save_Date_Time_2_24LC256(); 
    } 
     
    if ((buffer[0]=='H') && (buffer[1]=='A') && (buffer[Z]=='L')&& (Index1==3))
    { //HAL<CR>
      Drapeaux.Lcd=1;
       CRam=CRam1;
      gotoxy(0,3);
      strConstRamCpy(CRam,"STOP sav/Eeeprom");
      Write_String(CRam);
      redirige_RS232(1,CRam);
       Drapeaux.Store=0;
      }
    if ((buffer[0]=='R') && (buffer[1]=='E') && (buffer[2]=='L')&& (Index1==3))
    { // REL<CR>    Read EEprom   et stoppe
      Drapeaux.Store=0;
      Drapeaux.Lcd=1;
      CRam=CRam1;
      gotoxy(0,3);
      strConstRamCpy( CRam,(const code char *)"Lecture EEprom. ");
      Write_String(CRam);
      Drapeaux.Lcd=0;  
      UART1_Write_CText("\r\nRelit EEPROM\r\n Nb de mots = ");
      k=(Index-6)>>1;
      WordToStr(k,CRam);
      UART1_Write_Text(CRam);
      CRLF();
        CRam=CRam1;
      for (i=0;i<5;i++)
      {
        octet=Read_Byte_from_24LC256(i);
         //UART1_Write(octet);
          ByteToHex(octet,CRam);
           UART1_Write_Text(CRam);
      }
     CRLF();
       CRam=CRam1;
     for(i=6;i<=Index;i++)
     { 
        k=(i-6)>>1;
        WordToStr(k+1,CRam);
        UART1_Write_Text(CRam);    UART1_Write(TAB);
        octet=Read_Byte_from_24LC256(i);
        i++;
        M1= (Read_Byte_from_24LC256(i)<<8 ) + octet;
        WordToStr(M1,CRam);
        UART1_Write_Text(CRam);    UART1_Write(TAB);
        //F1=(float)M1*0.0048828125;
       // Float2Ascii (F1, CRam1,3);  // my function .. keep same width
      // UART1_Write_Text(CRam1);
        CRLF();      
      }
    }
     #endif    // EEPROM_24LC256  
     
    if ((buffer[0]=='D') && (buffer[1]=='U')&& (buffer[2]=='M')&& (Index1==3)) 
     { // DUM<CR>  Dump all
      UART1_Write_CText("\r\nDUMP FLASH PIC \r\n");
      txt=TEXTE;
      for (ir=0;ir<=0x1FFF;ir++)    // 0 à 8191    size= 8Ko
      {
         if ((ir%16)==0)
        {
          CRLF();
          WordToHex(ir,txt);
          UART1_Write_Text(txt);UART1_Write(' ');
        }   
        k1 = FLASH_Read(ir) ;
       WordToHex(k1, txt);
        UART1_Write_Text(txt);UART1_Write(' ');
        Delay_ms(5);
       }
       Delay_ms(200);
       CRLF();
      
     }
     buffer[0]=0;  
    Drapeaux.Full=0;
    Index1=0;
    i1=0;
   }

   #ifdef EEPROM_24LC256
   if (Drapeaux.Store==1)
   {  
      UART1_Write(TAB);
      Drapeaux.Lcd=1;
      CRam=CRam1;
      gotoxy(0,2);
      strConstRamCpy( CRam,(const code char *)"Next Idx=  ");
      WordToStr(Index,CRam+11);
      Write_String(CRam);
      redirige_RS232(1,CRam);
      octet=M1 & 0x00FF;
     Write_Byte_To_24LC256(Index,octet);  
     Delay_ms(10);
     Index++;
     octet= (M1 & 0xFF00) >> 8;
     Write_Byte_To_24LC256(Index,octet);  
     Delay_ms(10);
     Index++;        
    }
   #endif
     
 } //while

}


    

/*

 Platine PIC16F Nokia  14062 
 Read RTC TIME
 Time:  11: 54: 12;Diman 23/  6/ 14;
Keyb Cdes : 
- STArt sauve -> 24LC256
- HALt  sauve x 24LC256
- RELit <-  from 24LC256
- DUMp all Flash area

Init Interrupts UART 
 Time:  11: 54: 16; ADC0=   794; ADC1=   627
Time:  11: 54: 18; ADC0=   794; ADC1=   627
Time:  11: 54: 21; ADC0=   794; ADC1=   627
Time:  11: 54: 23; ADC0=   794; ADC1=   627
Time:  11: 54: 26; ADC0=   795; ADC1=   627
Time:  11: 54: 28; ADC0=   794; ADC1=   627
Time:  11: 54: 31; ADC0=   794; ADC1=   627
Time:  11: 54: 34; ADC0=   794; ADC1=   627
Time:  11: 54: 36; ADC0=   794; ADC1=   627
Time:  11: 54: 39; ADC0=   794; ADC1=   627
U;29;06;14;00;11;58#

   20

Mise a l'heure RTC 
Diman 29/  6/ 14;Time:  11: 58:  0; ADC0=   794; ADC1=   627
Time:  11: 58:  2; ADC0=   794; ADC1=   627
Time:  11: 58:  5; ADC0=   794; ADC1=   627
Time:  11: 58:  7; ADC0=   794; ADC1=   627
STA

START saveEeprom
        Next Idx=      0
Time:  11: 58: 10; ADC0=   794; ADC1=   627        Next Idx=      2
Time:  11: 58: 13; ADC0=   794; ADC1=   627        Next Idx=      4
Time:  11: 58: 15; ADC0=   679; ADC1=   395        Next Idx=      6
Time:  11: 58: 18; ADC0=   788; ADC1=   289        Next Idx=      8
Time:  11: 58: 21; ADC0=   788; ADC1=   219        Next Idx=     10
Time:  11: 58: 23; ADC0=   788; ADC1=    59        Next Idx=     12
Time:  11: 58: 26; ADC0=   788; ADC1=     3        Next Idx=     14
Time:  11: 58: 29; ADC0=   788; ADC1=   200        Next Idx=     16
Time:  11: 58: 31; ADC0=   788; ADC1=   426        Next Idx=     18
Time:  11: 58: 34; ADC0=   788; ADC1=   653        Next Idx=     20
Time:  11: 58: 37; ADC0=   788; ADC1=   775        Next Idx=     22
Time:  11: 58: 39; ADC0=   788; ADC1=   963        Next Idx=     24
Time:  11: 58: 42; ADC0=   788; ADC1=  1023        Next Idx=     26
Time:  11: 58: 44; ADC0=   788; ADC1=   830REL

Time:  12: 22: 44; ADC0=   787; ADC1=   830HAL
STOP sav/Eeeprom

Time:  12: 22: 47; ADC0=   787; ADC1=   830
Time:  12: 22: 49; ADC0=   787; ADC1=   830
REL

Relit EEPROM
 Nb de mots =    24
3E033E033E
    1          830        
    2          830        
    3          830        
    4          830        
    5          830        
    6          830        
    7          830        
    8          830        
    9          830        
   10          830        
   11          830        
   12          830        
   13          830        
   14          830        
   15          830        
   16          830        
   17          830        
   18          830        
   19          830        
   20          830        
   21          830        
   22          830        
   23          830        
   24          830        
   25          830        
Time:  12: 22: 52; ADC0=   787; ADC1=   830
Time:  12: 22: 55; ADC0=   787; ADC1=   830
Time:  12: 22: 57; ADC0=   787; ADC1=   830
Time:  12: 23:  0; ADC0=   787; ADC1=   830
Time:  12: 23:  3; ADC0=   787; ADC1=   830
RAZ

RAZ Idx saveEeprom
Time:  12: 23:  5; ADC0=   787; ADC1=   830
Time:  12: 23:  8; ADC0=   787; ADC1=   830
STA

START saveEeprom
        Next Idx=      0

Time:  12: 23: 10; ADC0=   787; ADC1=   830        Next Idx=      2
Time:  12: 23: 13; ADC0=   787; ADC1=   830        Next Idx=      4
Time:  12: 23: 16; ADC0=   787; ADC1=   830        Next Idx=      6
Time:  12: 23: 18; ADC0=   787; ADC1=   830HAL
STOP sav/Eeeprom
Time:  12: 23: 21; ADC0=   787; ADC1=   830
Time:  12: 23: 23; ADC0=   787; ADC1=   830
STA

START saveEeprom
        Next Idx=      8

Time:  12: 23: 26; ADC0=   787; ADC1=   830        Next Idx=     10
Time:  12: 23: 29; ADC0=   787; ADC1=   830        Next Idx=     12
Time:  12: 23: 31; ADC0=   787; ADC1=   830REL

0 1 mikroCPIC1618.exe -MSF -DBG -pP16F877 -C -DL -O11111015 -fo10 -N"C:\_MickroC\_MesProjets_MickroC\_16F877_Platine\16F877_Platine_Nokia_24LC256_RS.mcppi" -EH"C:\_MickroC\_MesProjets_MickroC\_16F877_Platine\16F877_Platine_CCP1.ihex" -SP"C:\Program Files\Mikroelektronika\mikroC PRO for PIC\defs\" -SP"C:\Program Files\Mikroelektronika\mikroC PRO for PIC\Uses\P16\" -SP"C:\_MickroC\_MesProjets_MickroC\_16F877_Platine\" "16F877_Platine_Nokia_RS_24LC256_140629.c" "__Lib_Math.mcl" "__Lib_MathDouble.mcl" "__Lib_System.mcl" "__Lib_Delays.mcl" "__Lib_CType.mcl" "__Lib_CString.mcl" "__Lib_CStdlib.mcl" "__Lib_Conversions.mcl" "__Lib_SoftI2C.mcl" "__Lib_ADC_A_C.mcl" "__Lib_FLASH_RW.mcl" "__Lib_UART_c67.mcl"  
0 1139 Available RAM: 352 [bytes], Available ROM: 8192 [bytes]  
0 126 All files Preprocessed in 78 ms  
0 122 Compilation Started 16F877_Platine_Nokia_RS_24LC256_140629.c
950 1163 Variable 'j' has been declared, but not used 16F877_Platine_Nokia_RS_24LC256_140629.c
1055 1163 Variable 'i' has been declared, but not used 16F877_Platine_Nokia_RS_24LC256_140629.c
1126 1509 Generated baud rate is 18939 bps (error = 1.36 percent) 16F877_Platine_Nokia_RS_24LC256_140629.c
1533 123 Compiled Successfully 16F877_Platine_Nokia_RS_24LC256_140629.c
922 1164 Variable 'i' has been eliminated by optimizer 16F877_Platine_Nokia_RS_24LC256_140629.c
977 1164 Variable 'c' has been eliminated by optimizer 16F877_Platine_Nokia_RS_24LC256_140629.c
1056 1164 Variable 'avg_output' has been eliminated by optimizer 16F877_Platine_Nokia_RS_24LC256_140629.c
1056 1164 Variable 'temp' has been eliminated by optimizer 16F877_Platine_Nokia_RS_24LC256_140629.c
0 127 All files Compiled in 438 ms  
0 1144 Used RAM (bytes): 189 (54%)  Free RAM (bytes): 163 (46%) Used RAM (bytes): 189 (54%)  Free RAM (bytes): 163 (46%)
0 1144 Used ROM (program words): 6705 (82%)  Free ROM (program words): 1487 (18%) Used ROM (program words): 6705 (82%)  Free ROM (program words): 1487 (18%)
0 125 Project Linked Successfully 16F877_Platine_Nokia_24LC256_RS.mcppi
0 128 Linked in 234 ms  
0 129 Project '16F877_Platine_Nokia_24LC256_RS.mcppi' completed: 812 ms  
0 103 Finished successfully: 29 juin 2014, 14:40:34 16F877_Platine_Nokia_24LC256_RS.mcppi

===================================================
 Test3
 Read TIME
 Time:  10: 31: 26        Mardi 18/  6/ 14;Init Interrupts UART 
 Time:  10: 31: 33        ADC0=   797;ADC1=   535

j=     0        Compteur 1<<j =          1        Distance (version faux flottant) =       0.004
j=     1        Compteur 1<<j =          2        Distance (version faux flottant) =       0.008
j=     2        Compteur 1<<j =          4        Distance (version faux flottant) =       0.017
j=     3        Compteur 1<<j =          8        Distance (version faux flottant) =       0.034
j=     4        Compteur 1<<j =         16        Distance (version faux flottant) =       0.068
j=     5        Compteur 1<<j =         32        Distance (version faux flottant) =       0.136
j=     6        Compteur 1<<j =         64        Distance (version faux flottant) =       0.272
j=     7        Compteur 1<<j =        128        Distance (version faux flottant) =       0.544
j=     8        Compteur 1<<j =        256        Distance (version faux flottant) =       1.088
j=     9        Compteur 1<<j =        512        Distance (version faux flottant) =       2.177
j=    10        Compteur 1<<j =       1024        Distance (version faux flottant) =       4.355
j=    11        Compteur 1<<j =       2048        Distance (version faux flottant) =       8.710
j=    12        Compteur 1<<j =       4096        Distance (version faux flottant) =      17.420
j=    13        Compteur 1<<j =       8192        Distance (version faux flottant) =      34.840
j=    14        Compteur 1<<j =      16384        Distance (version faux flottant) =      69.681
j=    15        Compteur 1<<j =      32768        Distance (version faux flottant) =     139.362
j=    16        Compteur 1<<j =      65536        Distance (version faux flottant) =     278.724
j=    17        Compteur 1<<j =     131072        Distance (version faux flottant) =     557.449
j=    18        Compteur 1<<j =     262144        Distance (version faux flottant) =    1114.898
j=    19        Compteur 1<<j =     524288        Distance (version faux flottant) =    2229.796
OVERSIZE !!--------------
j=    20        Compteur 1<<j =    1048576        Distance (version faux flottant) =     164.626
j=    21        Compteur 1<<j =    2097152        Distance (version faux flottant) =     329.252
j=    22        Compteur 1<<j =    4194304        Distance (version faux flottant) =     658.505
j=    23        Compteur 1<<j =    8388608        Distance (version faux flottant) =    1317.011
j=    24        Compteur 1<<j =   16777216        Distance (version faux flottant) =    2634.022
j=    25        Compteur 1<<j =   33554432        Distance (version faux flottant) =     973.078
j=    26        Compteur 1<<j =   67108864        Distance (version faux flottant) =    1946.157
j=    27        Compteur 1<<j =  134217728        Distance (version faux flottant) =    3892.314
j=    28        Compteur 1<<j =  268435456        Distance (version faux flottant) =    3489.660
j=    29        Compteur 1<<j =  536870912        Distance (version faux flottant) =    2684.354
j=    30        Compteur 1<<j = 1073741824        Distance (version faux flottant) =    1073.741


*/