Bonjour,
je sollicite votre aide pour un problème sur lequel je planche depuis plusieurs semaines.
Je réalise un programmateur pour gérer un aquarium et j'ai un souci sur l'affichage de la date et de l'heure récupérer sur un DS1307.
Le programme ci dessous est écrit en Mikropascal. la compilation n'indique pas d'erreur. pourtant, lors de la simulation sur ISIS, le LCD, je n'affiche que des 0.
Après beaucoup de recherche, je pense qu'il y a un problème sur l'initialisation du bus I2C.
Le schéma du montage est en pièce jointe.
quelqu'un a-t-il une idée lumineuse sur le sujet?
Merci pour votre aide.
program ds_1307;
var
seconds, minutes, hours: byte;
day, date, month, year: byte;
i:byte;
compteur : byte;
text1 : ARRAY[8] of char;
procedure Time_Read;
begin
I2C_Stop;
delay_ms(50);
I2C_Start; // start signal
I2C_Wr(%11010000); // Address DS1307, see DS1307 datasheet
I2C_Wr(0); // Start from address 0
I2C_Repeated_Start; // repeated start signal
I2C_Wr(%11010001); // Address DS1307 for reading R/W=1
seconds := I2C_Rd(1); // Read seconds byte
while (I2C_Is_Idle() = 0) do nop;
minutes := I2C_Rd(1); // Read minutes byte
while (I2C_Is_Idle() = 0) do nop;
hours := I2C_Rd(1); // Read hours byte
while (I2C_Is_Idle() = 0) do nop;
day := I2C_Rd(1); // Read day byte
while (I2C_Is_Idle() = 0) do nop;
date := I2C_Rd(1); // Read date byte
while (I2C_Is_Idle() = 0) do nop;
month := I2C_Rd(1); // Read weekday/month byte
while (I2C_Is_Idle() = 0) do nop;
year := I2C_Rd(1); // Read year byte
while (I2C_Is_Idle() = 0) do nop;
for i:= 1 to 56 do //lire la RAM (vide)
I2C_Rd(1);
I2C_Rd(0);
while (I2C_Is_Idle() = 0) do nop;
I2C_Stop; // Issue stop signal
compteur := compteur + 1;
ByteToStr(compteur,text1);
LCD_Out(1,15,text1);
end;
procedure Time_Decode;
begin
seconds := Bcd2Dec((seconds and 0xF0) shr 4) * 10 + Bcd2Dec(seconds and 0x0F);
minutes := Bcd2Dec((minutes and 0xF0) shr 4) * 10 + Bcd2Dec(minutes and 0x0F);
hours := Bcd2Dec((hours and 0xF0) shr 4) * 10 + Bcd2Dec(hours and 0x0F);
year := Bcd2Dec((year and 0xF0) shr 4) * 10 + Bcd2Dec(year and 0x0F);
date := Bcd2Dec((date and 0x30) shr 4) * 10 + Bcd2Dec(date and 0x0F);
month := Bcd2Dec((month and 0x10) shr 4) * 10 + Bcd2Dec(month and 0x0F);
end;
procedure Time_Display_LCD;
var
day_t, day_o, month_t, month_o, year_t, year_o : char;
sec_t, sec_o, min_t, min_o, hour_t, hour_o : char;
begin
day_t := (day / 10) + 48;
day_o := (day mod 10) + 48;
month_t := (month / 10) + 48;
month_o := (month mod 10) + 48;
year_t := (year / 10) + 48;
year_o := (year mod 10) + 48;
sec_t := (seconds / 10) + 48;
sec_o := (seconds mod 10) + 48;
min_t := (minutes / 10) + 48;
min_o := (minutes mod 10) + 48;
hour_t := (hours / 10) + 48;
hour_o := (hours mod 10) + 48;
Lcd_Chr(1, 7, day_t);
Lcd_Chr(1, 8, day_o);
Lcd_Chr(1, 10, month_t);
Lcd_Chr(1, 11, month_o);
Lcd_Chr(1, 13, year_t);
Lcd_Chr(1, 14, year_o);
Lcd_Chr(2, 7, hour_t);
Lcd_Chr(2, 8, hour_o);
Lcd_Chr(2, 10, min_t);
Lcd_Chr(2, 11, min_o);
Lcd_Chr(2, 13, sec_t);
Lcd_Chr(2, 14, sec_o);
end;
//----------------- Main procedure
begin
PORTC := %00011000;
TRISC := %00011000;
// I2C comm init
I2C_Init(100000);
Lcd_Init(PORTD); // Initialize LCD
Lcd_Cmd(LCD_CLEAR); // Clear LCD display
Lcd_Cmd(LCD_CURSOR_OFF); // Turn cursor off
LCD_Out(1,1,'Date:'); // Prepare and output static text on LCD
LCD_Chr(1,9,'/');
LCD_Chr(1,12,'/');
LCD_Out(2,1,'Time:');
LCD_Chr(2,9,':');
LCD_Chr(2,12,':');
Delay_ms(500);
compteur :=0;
while true do
begin
Time_Read; // Read time from RTC (DS1307)
Time_Decode; // Decode (format) date and time
Time_Display_LCD; // Display on LCD
delay_ms(500);
end;
end.
-----