Bonjour,
Je n'arrive pas a synchroniser mon pic avec une rtc DS1307
L'écran affiche le message ci dessous même si on change les paramètres d'initialisation de sec,min, ....
Date:08.04.2010.
Time:23:23:13
Est ce que quelqu'un peut m' aiguiller pour résoudre mon pb ?
Merci.
*/
/*
* Project : Horloge DS1307 sur PIC 16F887
Programmateur EasyPIC6 / MicroC for PIC
*/
/***************************** Defines *****************************/
#define ACK 0
#define NACK 1
/************************* bit definitions *************************/
// Software I2C connections
sbit scl at RC3_bit;
sbit sda at RC4_bit;
//sbit sqw at ?????;
sbit scl_Direction at TRISC3_bit;
sbit sda_Direction at TRISC4_bit;
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
/****************************** ****************************** ********/
void I2C_start();
void I2C_stop();
void I2C_write(unsigned char d);
unsigned char I2C__read(unsigned char);
void initialize();
/******************** global variables *************/
unsigned char sec, min, hr, dy, dt, mn, yr;
/*********** void *************************/
void I2C_start() /* ------ initialisation du start ------------------- */
{
sda = 1; delay_us(8); /* Pas de temps mini !? */
scl = 1; delay_us(8); /* tBUF=4.7µs mini */
sda = 0; delay_us(8); /* tHD:STA=4µs mini */
}
void I2C_stop() /* ----------------------------------------------- */
{
sda = 0; delay_us(15);
scl = 1; delay_us(8);
sda = 1; delay_us(8);
}
unsigned char I2C_write(unsigned char d )
{
unsigned char i, AckMissing;
scl = 0; delay_us(8); /* tLOW=4.7µs mini */
for (i = 1; i <= 8; i++)
{
sda = (d >> 7); delay_us(1); /* SUAT=0.25µs mini */
scl = 1; delay_us(8); /* tHIGH=4µs mini */
d = d << 1; /* increase scl high time */
scl = 0; //delay_us(8); /* tLOW=4.7µs mini */
}
Sda_Direction=1; // 1 : Entrée
AckMissing=sda; // Récup d'un 0 sur sda si OK //
sda_Direction=0; // 0 : Sortie
scl = 1; delay_us(8); /* tHIGH=4µs mini */
scl = 0; delay_us(8);
return AckMissing;
}
unsigned char I2C__read(unsigned char b) /* ----------------------------------- */
{
unsigned char d, i;
d=0;
scl = 0; delay_us(10);
sda_Direction=1; // 1 : Entrée
for (i = 1; i <= 8; i++) // read the msb first
{
d = d << 1;
d = d | sda; // OU bit à bit : Retourne 1 si l'un ou l'autre des deux bits de même poids est à 1 (ou les deux)
scl = 1; delay_us(10);
scl = 0; delay_us(10);
}
Sda_Direction=0; // 0 : sortie
sda = b; delay_us(8);
scl = 1; delay_us(8);
scl = 0; delay_us(8);
return d;
}
//-------------------- Formats date and time
void Transform_Time() {
sec = ((sec & 0xF0) >> 4)*10 + (sec & 0x0F); // Transform seconds
min = ((min & 0xF0) >> 4)*10 + (min & 0x0F); // Transform months
hr = ((hr & 0xF0) >> 4)*10 + (hr & 0x0F); // Transform hours
yr = (dy & 0xC0) >> 6; // Transform year
dy = ((dy & 0x30) >> 4)*10 + (dy & 0x0F); // Transform day
dt = dt ; // date ( 01 - 31 ) a transformer
mn = ((mn & 0x10) >> 4)*10 + (mn & 0x0F); // Transform month
}
//-------------------- Output values to LCD
void Display_Time() {
Lcd_Chr(1, 6, (dy / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1, 7, (dy % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1, 9, (mn / 10) + 48);
Lcd_Chr(1,10, (mn % 10) + 48);
Lcd_Chr(1,15, yr + 48); // Print year variable (start from year 2010)
Lcd_Chr(2, 6, (hr / 10) + 48);
Lcd_Chr(2, 7, (hr % 10) + 48);
Lcd_Chr(2, 9, (min / 10) + 48);
Lcd_Chr(2,10, (min % 10) + 48);
Lcd_Chr(2,12, (sec / 10) + 48);
Lcd_Chr(2,13, (sec % 10) + 48);
}
void Read_Time() {
I2C_start();
I2C_Write(0xD1); // Address for reading R/W=1
sec = I2C__Read(ACK); // Read seconds byte( 00 - 59 )
min = I2C__Read(ACK); // Read minutes byte ( 00 - 59 )
hr = I2C__Read(ACK); // Read hours byte ( 1-12 / 0-23 )
dy = I2C__Read(ACK); // Read day byte( 01 - 07 )
dt = I2C__Read(ACK); // Read date byte ( 01 - 31 )
mn = I2C__Read(ACK); // Read month byte ( 0 - 12 )
yr = I2C__Read(NACK); // Read year byte ( 00 - 99 )
I2C_Stop();
}
void main() {
TRISB = 0;
PORTB = 0xFF;
TRISB = 0xff;
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
Scl_Direction=0; // 0 : sortie
Sda_Direction=0; // 0 : sortie
//INTCON=0; // Disables all interrupts
Lcd_Init(); // 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,8,'.');
Lcd_Chr(1,11,'.');
Lcd_Chr(1,16,'.');
Lcd_Out(2,1,"Time:");
Lcd_Chr(2,8,':');
Lcd_Chr(2,11,':');
Lcd_Out(1,12,"201"); // start from year 2010
I2C_start(); /* The following Enables the Oscillator */
I2C_write(0xd0); /* address the part to write */
I2C_write(0x00); /* position the address pointer to 0 */
I2C_write(0x00); /* write 0 to the seconds register, clear the CH bit */
I2C_stop();
/* 21/08/2011 03H10min 08sec */
sec=0x08; // seconds byte( 00 - 59 )
min=0x0A; // minutes byte ( 00 - 59 )
hr=0x43; // hours byte ( 1-12 / 0-23 ) (0x43 = 3 HEURES 12MODE PM/AM )
dy=0x01; // day byte( 01 - 07 ) ( 1 = sunday )
dt=0x21; // date byte ( 01 - 31 )
mn=0x08; // month byte ( 0 - 12 )
yr=0x11; // year byte ( 00 - 99 )
I2C_start();
I2C_write(0xd0); /* address the part to write */
I2C_write(0x00); /* write register address, 1st clock register */
I2C_write(sec);
I2C_write(min);
I2C_write(hr);
I2C_write(dy);
I2C_write(dt);
I2C_write(mn);
I2C_write(yr);
I2C_stop();
delay_ms(1000);
Read_Time();
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
}
-----