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)
Thank you for your future reply
kiss++
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: