Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

DS1302 with PIC18F4520 Help...

Status
Not open for further replies.

camelia222

New Member
Hello everybody (i 'm a french girl so sorry for my english)
I'm trying to use a DS1302 with a 18F4520 that i program with MikroC. The problem is that my LCD only write 0 and not the date. I have test my LCD and it works (I display my name on it). I think the problem come from my program for the DS1302 but i dont see where is the mistake so if somebody can help me it will be very very very nice. Here is my code (under MikroC)

Code:
#include "built_in.h"
// Prototypes de mes fonctions ----------------------


void Mise_a_lheure(unsigned char Jour,
                unsigned char Mois,
                unsigned char Annee,
                unsigned char Num_Jour,
                unsigned char Heure,
                unsigned char Min,
                unsigned char Sec) ;

void Obtenir_Date(unsigned char *Obt_Jour, unsigned char *Obt_Mois, unsigned char *Obt_Annee, unsigned char *Obt_Num_Jour);
void Obtenir_Heure(unsigned char *Obt_Heure, unsigned char *Obt_Minute, unsigned char *Obt_Seconde);
unsigned char Normal_BCD(unsigned char Donnee_Normale);
unsigned char BCD_Normal(unsigned char Donnee_BCD);

//------------------------------------------------------

unsigned char Jour1, Mois1, Num_Jour1, Annee1, Heure1, Minute1, Seconde1;
unsigned char Obt_Jour, Obt_Mois, Obt_Annee, Obt_Num_Jour, Obt_Heure, Obt_Minute, Obt_Seconde;
unsigned char *tc, *tc2, *tc3, *tc4;

// Display conversion -----------

char *txt, tnum[4];

char *R_Trim(char *str1){

  while (*str1 == ' ')
    str1++;
  return str1;
}

void Zero_Fill(char *value) {
  if (value[1] == 0) {
    value[1] = value[0];
    value[0] = 48;
    value[2] = 0;
  }
}


//----------------------------------------------------



void RTC_Init()
{
   TRISB.RB1 = 0;
   TRISB.RB2 = 0;
   TRISB.RB3 = 0;

   PORTB.RB1 = 0;
   PORTB.RB2 = 0;
   PORTB.RB3 = 0;
}


void Ecrire_RTC(unsigned char Donnee, int Longueur)
{
     int i;
     i = 0;
     
     TRISB.RB3 = 0;
     
     for(i = 0; i < Longueur; i++)
     {
          if(Donnee.F0)
          {
              PORTB.RB3 = 1;
          }
          else
          {
              PORTB.RB3 = 0;
          }
          Donnee = Donnee >> 1;
          PORTB.RB1 = 1;
          Delay_us(10);
          PORTB.RB1 = 0;
          Delay_us(10);
     }
}

unsigned char Lire_RTC(int Longueur)
{
     int i ;
     unsigned char Donnee;
     
     i = 0;

     for(i = 0; i < Longueur; i++)
     {
          PORTB.RB1 = 1;
          TRISB.RB3 = 1;

          if(PORTB.RB3)
          {
               Donnee.F7 = 1;
          }
          else
          {
               Donnee.F7 = 0;
          }
          Donnee = Donnee >> 1 ;
          
          PORTB.RB1 = 0;
          Delay_us(10);
     }
     TRISB.RB3 = 0;
     return Donnee;
}

unsigned char BCD_Normal(unsigned char Donnee_BCD)
{
    unsigned char Donnee_Normale;

    Donnee_Normale = Bcd2Dec(Donnee_BCD);

    return Donnee_Normale;
}

unsigned char Normal_BCD(unsigned char Donnee_Normale)
{
    unsigned char Donnee_BCD;

    Donnee_BCD = Dec2Bcd(Donnee_Normale);
    
    return Donnee_BCD;
}

void Mise_a_lheure()
{
     unsigned char Commande ;
     unsigned char Jour, Mois, Annee, Num_Jour, Heure, Min, Sec;
     
     Jour = 14 ;
     Mois = 3 ;
     Annee = 8 ;
     Num_Jour =  5 ;
     Heure = 10 ;
     Min = 5;
     Sec = 0;

     PORTB.RB1 = 0;

     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x8A ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Jour), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x88 ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Mois), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x8C ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Annee), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x86 ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Num_Jour), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x84 ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Heure), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x82 ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Min), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x80 ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Sec), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
}

void Obtenir_Date()
{
     unsigned char Commande;

     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x8D ;
     Ecrire_RTC(Commande, 8) ;
     Obt_Jour = BCD_Normal(Lire_RTC(8)) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x89 ;
     Ecrire_RTC(Commande, 8) ;
     Obt_Mois = BCD_Normal(Lire_RTC(8)) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x8D ;
     Ecrire_RTC(Commande, 8) ;
     Obt_Annee = BCD_Normal(Lire_RTC(8)) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x87 ;
     Ecrire_RTC(Commande, 8) ;
     Obt_Num_Jour = BCD_Normal(Lire_RTC(8)) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
}

void Obtenir_Heure()
{
     unsigned char Commande;

     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x85 ;
     Ecrire_RTC(Commande, 8) ;
     Obt_Heure = BCD_Normal(Lire_RTC(8)) ;
     PORTB.RB2 = 0;
     
     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x83 ;
     Ecrire_RTC(Commande, 8) ;
     Obt_Minute = BCD_Normal(Lire_RTC(8)) ;
     PORTB.RB2 = 0;
     
     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x81 ;
     Ecrire_RTC(Commande, 8) ;
     Obt_Seconde = BCD_Normal(Lire_RTC(8)) ;
     PORTB.RB2 = 0;
}

void Display_Time(char sec, char mnt, char hr, char day, char mn, char year) {
  char *tc;

   ByteToStr(day, tnum);                           // day
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   Lcd8_Out(1,6, tc);
   
   ByteToStr(mn, tnum);                            // month
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   Lcd8_Out(1,9, tc);
   
   ByteToStr(year, tnum);                            // month
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   Lcd8_Out(1,14, tc);
   
//   LCD_Chr(1,15,52+year);                          // year
   ByteToStr(hr,tnum);                             // hour
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   Lcd8_Out(2,6,tc);
   
   ByteToStr(mnt,tnum);                            // minute
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   Lcd8_Out(2,9,tc);
   
   ByteToStr(sec,tnum);                            // seconds
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   Lcd8_Out(2,12,tc);
}//~

     
void main(void)
{
     Lcd8_Config(&PORTB, &PORTD, 5,7,6, 7,6,5,4,3,2,1,0);
     Lcd8_Cmd(LCD_CURSOR_OFF);
     Lcd8_Cmd(LCD_CLEAR);

     ADCON1 = 0x80;

     RTC_Init() ;

     tc = "Bonjour" ;
     tc2 = "La forme?" ;
     Lcd8_Out(1,1,tc);
     Lcd8_Out(2,1,tc2);

     Delay_ms(2000) ;

     Mise_a_lheure();

     while(1)
     {
     Obtenir_Heure();
     Obtenir_Date();
     Display_Time(Obt_Seconde, Obt_Minute, Obt_Heure, Obt_Jour, Obt_Mois, Obt_Annee);
     Delay_ms(2000) ;
     }

}

Thank you for your future reply

kiss++
 
Last edited:
Your code is a little hard to follow but I did notice a flaw in your read routine.

Code:
unsigned char Lire_RTC(int Longueur)
{
     int i ;
     unsigned char Donnee;
     for(i = 0; i < Longueur; i++)
     {
          TRISB.RB3 = 1;		//swapped, could be outside loop.
          PORTB.RB1 = 1;		//swapped
          Delay_us(10);			//allow to settle
          Donnee = Donnee >> 1 ;	//should be here
          if(PORTB.RB3)
          {
               Donnee.F7 = 1;
          }
          else
          {
               Donnee.F7 = 0;
          }
          //Donnee = Donnee >> 1 ;	//shouldn't be here
          
          PORTB.RB1 = 0;
          Delay_us(10);
     }
     TRISB.RB3 = 0;
     return Donnee;
}

Mike.
 
Last edited:
Just had a look at the data sheet and the data is clocked into the chip on the rising edge and clocked out on the falling edge. This means that you loose bits between read and write.

Can you try the following,
Code:
#include "built_in.h"
// Prototypes de mes fonctions ----------------------


void Mise_a_lheure(unsigned char Jour,
                unsigned char Mois,
                unsigned char Annee,
                unsigned char Num_Jour,
                unsigned char Heure,
                unsigned char Min,
                unsigned char Sec) ;


void Obtenir_Date(unsigned char *Obt_Jour, unsigned char *Obt_Mois, unsigned char *Obt_Annee, unsigned char *Obt_Num_Jour);
void Obtenir_Heure(unsigned char *Obt_Heure, unsigned char *Obt_Minute, unsigned char *Obt_Seconde);
unsigned char Normal_BCD(unsigned char Donnee_Normale);
unsigned char BCD_Normal(unsigned char Donnee_BCD);

//------------------------------------------------------

unsigned char Jour1, Mois1, Num_Jour1, Annee1, Heure1, Minute1, Seconde1;
unsigned char Obt_Jour, Obt_Mois, Obt_Annee, Obt_Num_Jour, Obt_Heure, Obt_Minute, Obt_Seconde;
unsigned char *tc, *tc2, *tc3, *tc4;

// Display conversion -----------

char *txt, tnum[4];

char *R_Trim(char *str1){

  while (*str1 == ' ')
    str1++;
  return str1;
}

void Zero_Fill(char *value) {
  if (value[1] == 0) {
    value[1] = value[0];
    value[0] = 48;
    value[2] = 0;
  }
}


//----------------------------------------------------



void RTC_Init()
{
   TRISB.RB1 = 0;
   TRISB.RB2 = 0;
   TRISB.RB3 = 0;

   PORTB.RB1 = 0;
   PORTB.RB2 = 0;
   PORTB.RB3 = 0;
}


void Ecrire_RTC(unsigned char Donnee, int Longueur)
{
     int i;

     PORT.RB1=0;			//clock low
     PORTB.RB2 = 1;			// /rst high
     Delay_us(10);
     
     TRISB.RB3 = 0;			//data output
     
     for(i = 0; i < Longueur; i++)
     {
          PORTB.RB1 = 0;		//clock low
          Delay_us(10);
          if(Donnee.F0)
          {
              PORTB.RB3 = 1;
          }
          else
          {
              PORTB.RB3 = 0;
          }
          Donnee = Donnee >> 1;
          PORTB.RB1 = 1;		//clock high
          Delay_us(10);
     }
}

unsigned char Lire_RTC(int Longueur)
{
     int i ;
     unsigned char Donnee;
     for(i = 0; i < Longueur; i++)
     {
          TRISB.RB3 = 1;		//data input
          PORTB.RB1 = 0;		//clock low
          Donnee = Donnee >> 1 ;	
	  delay_us(10);
          if(PORTB.RB3)
          {
               Donnee.F7 = 1;
          }
          else
          {
               Donnee.F7 = 0;
          }
          PORTB.RB1 = 1;
          Delay_us(10);
     }
     return Donnee;
}

unsigned char BCD_Normal(unsigned char Donnee_BCD)
{
    unsigned char Donnee_Normale;

    Donnee_Normale = Bcd2Dec(Donnee_BCD);

    return Donnee_Normale;
}

unsigned char Normal_BCD(unsigned char Donnee_Normale)
{
    unsigned char Donnee_BCD;

    Donnee_BCD = Dec2Bcd(Donnee_Normale);
    
    return Donnee_BCD;
}

void Mise_a_lheure()
{
     unsigned char Commande ;
     unsigned char Jour, Mois, Annee, Num_Jour, Heure, Min, Sec;
     
     Jour = 14 ;
     Mois = 3 ;
     Annee = 8 ;
     Num_Jour =  5 ;
     Heure = 10 ;
     Min = 5;
     Sec = 0;

     Commande = 0x8A ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Jour), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     Commande = 0x88 ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Mois), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     Commande = 0x8C ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Annee), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     Commande = 0x86 ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Num_Jour), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     Commande = 0x84 ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Heure), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     Commande = 0x82 ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Min), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     Commande = 0x80 ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Sec), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
}

void Obtenir_Date()
{
     unsigned char Commande;

     Commande = 0x8D ;
     Ecrire_RTC(Commande, 8) ;
     Obt_Jour = BCD_Normal(Lire_RTC(8)) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     Commande = 0x89 ;
     Ecrire_RTC(Commande, 8) ;
     Obt_Mois = BCD_Normal(Lire_RTC(8)) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     Commande = 0x8D ;
     Ecrire_RTC(Commande, 8) ;
     Obt_Annee = BCD_Normal(Lire_RTC(8)) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     Commande = 0x87 ;
     Ecrire_RTC(Commande, 8) ;
     Obt_Num_Jour = BCD_Normal(Lire_RTC(8)) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
}

void Obtenir_Heure()
{
     unsigned char Commande;

     Commande = 0x85 ;
     Ecrire_RTC(Commande, 8) ;
     Obt_Heure = BCD_Normal(Lire_RTC(8)) ;
     PORTB.RB2 = 0;
     
     Commande = 0x83 ;
     Ecrire_RTC(Commande, 8) ;
     Obt_Minute = BCD_Normal(Lire_RTC(8)) ;
     PORTB.RB2 = 0;
     
     Commande = 0x81 ;
     Ecrire_RTC(Commande, 8) ;
     Obt_Seconde = BCD_Normal(Lire_RTC(8)) ;
     PORTB.RB2 = 0;
}

void Display_Time(char sec, char mnt, char hr, char day, char mn, char year) {
  char *tc;

   ByteToStr(day, tnum);                           // day
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   Lcd8_Out(1,6, tc);
   
   ByteToStr(mn, tnum);                            // month
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   Lcd8_Out(1,9, tc);
   
   ByteToStr(year, tnum);                            // month
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   Lcd8_Out(1,14, tc);
   
//   LCD_Chr(1,15,52+year);                          // year
   ByteToStr(hr,tnum);                             // hour
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   Lcd8_Out(2,6,tc);
   
   ByteToStr(mnt,tnum);                            // minute
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   Lcd8_Out(2,9,tc);
   
   ByteToStr(sec,tnum);                            // seconds
   tc = R_Trim(tnum);
   Zero_Fill(tc);
   Lcd8_Out(2,12,tc);
}//~

     
void main(void)
{
     Lcd8_Config(&PORTB, &PORTD, 5,7,6, 7,6,5,4,3,2,1,0);
     Lcd8_Cmd(LCD_CURSOR_OFF);
     Lcd8_Cmd(LCD_CLEAR);

     ADCON1 = 0x80;

     RTC_Init() ;

     tc = "Bonjour" ;
     tc2 = "La forme?" ;
     Lcd8_Out(1,1,tc);
     Lcd8_Out(2,1,tc2);

     Delay_ms(2000) ;

     Mise_a_lheure();

     while(1)
     {
     Obtenir_Heure();
     Obtenir_Date();
     Display_Time(Obt_Seconde, Obt_Minute, Obt_Heure, Obt_Jour, Obt_Mois, Obt_Annee);
     Delay_ms(2000) ;
     }

}

Mike.
 
Last edited:
Sorry but always the same result. There is one thing strange, it's that my LCD always display 0 no bad date or bad hour, but always 0 with no variation. Like if the ds1302 didn't send data... i don't understand where the mistake....
 
What happens if you insert the following line,
Code:
     while(1)
     {
     Obtenir_Heure();
     Obtenir_Date();
[COLOR="Blue"]     Obt_Seconde=0x23;		//inserted[/COLOR]
     Display_Time(Obt_Seconde, Obt_Minute, Obt_Heure, Obt_Jour, Obt_Mois, Obt_Annee);
     Delay_ms(2000) ;
     }

As well, I notice you don't clear the Write protect bit. Add a write to clear the control register (0x8e).

Mike.
 
i have found a problem, if you look this part of code:

Code:
unsigned char Lire_RTC(int Longueur)
{
     int i ;
     unsigned char Donnee;
     for(i = 0; i < Longueur; i++)
     {
          TRISB.RB3 = 1;		//data input
          PORTB.RB1 = 0;		//clock low
          Donnee = Donnee >> 1 ;	
	  delay_us(10);
          if(PORTB.RB3)
          {
               Donnee.F7 = 1;
          }
          else
          {
               Donnee.F7 = 0;
          }
          PORTB.RB1 = 1;
          Delay_us(10);
     }
     return Donnee;
}

When RB3 is at High level Donnee.F7 = 1 else Donnee.F7 = 0.

I have modify this part of code and put this new code:

Code:
for(i = 0; i < (Longueur); i++)
     {
          TRISB.RB3 = 1;
          PORTB.RB1 = 0;
          Delay_us(10);
          Donnee = Donnee >> 1 ;

          if(PORTB.RB3)
          {
               Donnee.F7 = 1;
               Lcd8_Out(2,1,tc3);
               Delay_ms(2000);
          }
          else
          {
               Donnee.F7 = 0;
               Lcd8_Out(2,1,tc4);
               Delay_ms(2000);
          }
          
          PORTB.RB1 = 1;
          Delay_us(10);
     }
     PORTB.RB2 = 0;
     Delay_us(10);
     return Donnee;
}

with tc3 and tc4 two differents texts. When i test this code, my LCD always display the text for RB3 = 0. So i have deconnect the wire connecting RB3 and the DS1302 and i have try to send 0 or 5V directly on RB3 but always the same result. It means that my PIC don't see the changing of level and considere that RB3 is always at low level so it always send 0. I think it's one of my problem, pearhaps a Pic's register problem...
 
Mike i tried your code and for the second my LCD display 35 so it display the good result but you are right it's perhaps a problem of WP too. But in my code i have add a constant WP = 0 and i send it ti the DS1302 at the adress 0x8E so normally it's good no ?
 
Last edited:
I didn't even consider that you may have analogue inputs. Change ADCON1 = 0x80; to ADCON1 = 0x0f;

Mike.
 
Thank you, i have modify ADCON and do the same test and now it works, if i put 5V the first text is displayed and if i put 0 it's the second. I have tried the program with the new ADCON it didn't display 0 but other value as 93, 89, 93, 80...) but with no variation so it's not again good but there is a big progress... it's perhaps the WP or CH bit now whick causes problem
 
Try adding a write to the WP bit,

Code:
void Mise_a_lheure()
{
     unsigned char Commande ;
     unsigned char Jour, Mois, Annee, Num_Jour, Heure, Min, Sec;
     
     Jour = 14 ;
     Mois = 3 ;
     Annee = 8 ;
     Num_Jour =  5 ;
     Heure = 10 ;
     Min = 5;
     Sec = 0;

[COLOR="Blue"]     Commande = 0x8E ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(0, 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);[/COLOR]

     Commande = 0x8A ;
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(Normal_BCD(Jour), 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);

You already write to the seconds location and so this clears the CH bit.

Mike.
P.S. Bed time now, 1am here. I'll look in the morning.
 
Last edited:
I had another look at your code and chopped it down to it's simplest form. MAKE SURE YOU SAVE YOUR OLD CODE.

Can you try this and see what it does,
Code:
#include "built_in.h"
// Prototypes de mes fonctions ----------------------


void Mise_a_lheure() ;
void Obtenir_Heure();


//------------------------------------------------------
unsigned char Jour1, Mois1, Num_Jour1, Annee1, Heure1, Minute1, Seconde1;
unsigned char Obt_Jour, Obt_Mois, Obt_Annee, Obt_Num_Jour, Obt_Heure, Obt_Minute, Obt_Seconde;
unsigned char *tc, *tc2, *tc3, *tc4;



void RTC_Init()
{
   TRISB.RB1 = 0;
   TRISB.RB2 = 0;
   TRISB.RB3 = 0;

   PORTB.RB1 = 0;
   PORTB.RB2 = 0;
   PORTB.RB3 = 0;
}


void Ecrire_RTC(unsigned char Donnee, int Longueur)
{
     int i;
     TRISB.RB3 = 0;     		//data = output
     for(i = 0; i < Longueur; i++)
     {
          if(Donnee.F0)
          {
              PORTB.RB3 = 1;
          }
          else
          {
              PORTB.RB3 = 0;
          }
          Donnee = Donnee >> 1;
          PORTB.RB1 = 0;
          Delay_us(10);
          PORTB.RB1 = 1;
          Delay_us(10);
     }
}

unsigned char Lire_RTC(int Longueur)
{
     int i ;
     unsigned char Donnee;

     TRISB.RB3 = 1;			//data=input
     for(i = 0; i < Longueur; i++)
     {
          PORTB.RB1 = 1;
          Delay_us(10);
          PORTB.RB1 = 0;
          Donnee = Donnee >> 1 ;
          if(PORTB.RB3)
          {
               Donnee.F7 = 1;
          }
          else
          {
               Donnee.F7 = 0;
          }
     }
     return Donnee;
}


void Mise_a_lheure()
{
     unsigned char Commande ;
     
     PORTB.RB1 = 0;
     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x8E ;		//ensure WP=0
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(0, 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
     
     PORTB.RB1=0;     
     PORTB.RB2 = 1;
     Delay_us(10);
     Commande = 0x80 ;		//ensure CP=0
     Ecrire_RTC(Commande, 8) ;
     Ecrire_RTC(0, 8) ;
     PORTB.RB2 = 0;
     Delay_ms(10);
}


void main(void)
{
unsigned char temp;
     Lcd8_Config(&PORTB, &PORTD, 5,7,6, 7,6,5,4,3,2,1,0);
     Lcd8_Cmd(LCD_CURSOR_OFF);
     Lcd8_Cmd(LCD_CLEAR);

     ADCON1 = 0x0f;

     RTC_Init() ;

     tc = "Bonjour" ;
     tc2 = "La forme?" ;
     Lcd8_Out(1,1,tc);
     Lcd8_Out(2,1,tc2);

     Delay_ms(2000) ;

     Mise_a_lheure();

     while(1)
     {
        PORTB.RB1=0;     
        PORTB.RB2 = 1;
        Delay_us(10);
        Commande = 0x81 ;		//just read seconds
        Ecrire_RTC(Commande, 8) ;
        Obt_Seconde = Lire_RTC(8) ;
        PORTB.RB2 = 0;

        temp=(Obt_Seconde>>4)+0x30;
        LCD8_Out(1,1,temp);
        temp=(Obt_Seconde&0x0f)+0x30;
        LCD8_Out(1,2,temp);

        Delay_ms(200) ;
     }
}

It should just display the seconds on the LCD.

Mike.
 
Good to hear it's working. You should be able to work from that to get the rest of it working.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top