PDA

Επιστροφή στο Forum : avr ds1307 πρόβλημα την ημερομηνία



Δημήτρηςκα
29-09-13, 11:27
Καλησπέρα παιδιά, έχω έναν Mega16 ένα ds1307 και μια lcd και προσπαθώ να εμφανίσω την ηεμρωμηνία και την ώρα στην lcd.
Ο κώδικάς:


#include <avr/io.h>
#include <util/delay.h>
#include "lcd_drv.h"
#include "i2cmaster.h"
#define DS1307 0b11010000
#include <stdlib.h>



//////////////////////////////////////////VOIDS
//////////////////////////LCD VOIDS START
static void lcd_nibble( uint8_t d )
{
LCD_D4 = 0; if( d & 1<<4 ) LCD_D4 = 1;
LCD_D5 = 0; if( d & 1<<5 ) LCD_D5 = 1;
LCD_D6 = 0; if( d & 1<<6 ) LCD_D6 = 1;
LCD_D7 = 0; if( d & 1<<7 ) LCD_D7 = 1;

LCD_E0 = 1;
_delay_us( 1 ); // 1us
LCD_E0 = 0;
}

static void lcd_byte( uint8_t d )
{
lcd_nibble( d );
lcd_nibble( d<<4 );
_delay_us( 50 ); // 50us
}


void lcd_command( uint8_t d )
{
LCD_RS = 0;
lcd_byte( d );
if( d <= 3 )
_delay_ms( 2 ); // wait 2ms
}

void lcd_putchar( uint8_t d )
{
LCD_RS = 1;
lcd_byte( d );
}

void lcd_puts(void *vp)
{
char *p = (char*)vp;
while (*p) lcd_putchar(*p++);
}

/////////////////////////////////LCD VOIDS END


////////////////////////////////BCD/DEC START

// Convert Decimal to Binary Coded Decimal (BCD)
char dec2bcd(char num)
{
return ((num/10 * 16) + (num % 10));
}

// Convert Binary Coded Decimal (BCD) to Decimal
char bcd2dec(char num)
{
return ((num/16 * 10) + (num % 16));
}

/////////////////////////////////////BCD/DEC END
//////////////////////////////////////RTC VOIDS START
// Variable Declaration
char ds1307_sec[7];///00-59
char ds1307_min[7];//00-59
char ds1307_hour[7];//00-23
char ds1307_day[7];//////mon-sut
char ds1307_dayy[7];////////0-31
char ds1307_month[7];///////1-12
char ds1307_year[7];//00-99
volatile uint8_t min,hour, Seconds;


void WriteDS1307(void)
{

// Start the I2C Write Transmission
i2c_start(DS1307+I2C_WRITE);

// Start from Address 0x00

// Start from Address 0x00
i2c_write(0x00);
i2c_write(0x50); // Set second
i2c_write(0x59); // Set minute
i2c_write(0x23); // Set hour
//date
i2c_write(0x06); // Set day mon-sut
i2c_write(0x29); // Set day1-31
i2c_write(0x09); // Set month
i2c_write(0x13); // Set year

//date
// Stop I2C Write
i2c_stop();

}
void ReadDS1307(void)
{

uint8_t TempData;

i2c_start(DS1307+I2C_WRITE);
i2c_write(0x00);
// Start I2C Read
i2c_start(DS1307+I2C_READ);

// Read the Second Register,
TempData = i2c_readAck();
TempData = bcd2dec(TempData);
Seconds = TempData;

// Convert number to string

itoa(TempData,ds1307_sec,10); ///sec0-59

// Read the Minute Register,
TempData = i2c_readAck();
TempData = bcd2dec(TempData);
min=TempData;
// Convert number to string
itoa(TempData,ds1307_min,10); ///min0-59

// Read the Hour Register,
TempData = i2c_readNak();
TempData = bcd2dec(TempData);
hour=TempData;
// Convert number to string
itoa(TempData,ds1307_hour,10); //hour0-59
// Stop I2C Read

///////////////////////date

// Read the day mon-sut 1-7 Register,
TempData = i2c_readNak();
TempData = bcd2dec(TempData);
itoa(TempData,ds1307_day,10); ///day0-7 mon-su

// Read the dayy 1-31 Register,
TempData = i2c_readNak();
TempData = bcd2dec(TempData);
itoa(TempData,ds1307_dayy,10); //dayy0-31

// Read the day month Register,
TempData = i2c_readNak();
TempData = bcd2dec(TempData);
itoa(TempData,ds1307_month,10); //month0-12

// Read the day mon-sut Register,
TempData = i2c_readNak();
TempData = bcd2dec(TempData);
itoa(TempData,ds1307_year,10); year0-99
///////////////////////date
// Stop I2C Read
i2c_stop();



}


/////////////////////////////////////////////////////RTC VOIDS END
//////////////////////////////////////////////////////////////////VOIDS END
int main( void )
{
MCUCSR |=(1<<JTD); //disable jtd
MCUCSR |=(1<<JTD);




lcd_init();


lcd_xy( 3, 0 );
lcd_puts( "DATE" );
lcd_xy( 0, 1 );
lcd_puts( "DS1307" );
_delay_ms(200);
_delay_ms(200);
_delay_ms(200);
_delay_ms(200);


uint8_t i=255;
char j[5];
/////RTC
i2c_init();
WriteDS1307();






for(;;){




ReadDS1307();

lcd_xy( 0, 0 );
if(hour<10){lcd_puts("0");}
lcd_puts(ds1307_hour);
lcd_puts(":");
if(min<10){lcd_puts("0");}
lcd_puts(ds1307_min);
lcd_puts(":");
if(Seconds<10){lcd_puts("0");}
lcd_puts(ds1307_sec);
lcd_xy( 0, 1 );
lcd_puts(ds1307_day);
lcd_puts(ds1307_dayy);
lcd_puts(ds1307_month);
lcd_puts(ds1307_year);

_delay_ms(200);
_delay_ms(200);
_delay_ms(200);
_delay_ms(200);
_delay_ms(200);
_delay_ms(200);



}
}

Το πρόβλημά μου είναι πως ενώ την ώρα τη βγάζει σωστή στην ημερωμηνία μου βγάζει 165,
πιό συγκεκριμένα
LCD16X2:
|--------------------- |
|HH:MM:SS########|
|165165165165##### |
|--------------------- |

Το λάθος μου πρέπει να είναι μάλλων στο πως γράφω την ημερωμηνία ή στο πως την καλώ
Καμία ιδέα;

Δημήτρηςκα
29-09-13, 11:29
Edit: έσβησα κάποιες συναρτήσεις της lcd στο κώδικα επειδή δεν χώραγε στο τέλος θα αναρτήσω όλο το κώδικα με τις βιβλιοθήκες σε αρχεία σε περίπτωση που κάποιος τον θέλει



Παιδιά μετά από προσπάθιες τη βρίκα τη λύση.
Το πρόβλημα ήταν πως όταν διάβαζα την ημερωμηνία πρέπει να δηλώνω για μέρα μήνα κλπ ξανα και ξανά την διεύθυνση τουυ ds1307 κάτι που δεν κάνω στην ώρα αλλά δουλεύει σωστά όσο παράξενο και αν είναι.
Το αρχείο επειδή είναι μεγάλο το ανέβασα στο rapidshare και το link του είναι:

http://rapidshare.com/share/D9EB9A42C5E5D4BBDFA25B26437E6415
Υπάρχει κάτι άλλο που θα πρέπει να προσέξω; Έχω ακούσει πως το ds1307 χάνει 1 δευτερόλεπτο την μέρα, έχει κάνει κάποιος άλλος πλακέτα με το ds1307 να με φωτήσει;
Ευχαριστώ!