|
|
View previous topic :: View next topic |
Author |
Message |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jun 02, 2010 11:44 am |
|
|
Try to use this ds1307 driver and the example code:
http://www.ccsinfo.com/forum/viewtopic.php?t=23255
Make certain that:
1. You have a 4.7K pull-up resistor on each i2c line: SDA and SCL.
2. You have a 3 volt lithium battery on the Vbat pin of the ds1307.
3. You must have a 32.768 KHz watch crystal on the ds1307.
Notice how this little board has the pull-up resistors, the battery, and
the watch crystal. All of these are needed. If you forget one of them,
your project will not work.
http://www.futurlec.com/Pictures/ET-MINI_DS1307.jpg |
|
|
tieungu13
Joined: 03 Mar 2010 Posts: 6
|
|
Posted: Wed Jun 02, 2010 11:57 am |
|
|
PCM programmer wrote: | Try to use this ds1307 driver and the example code:
http://www.ccsinfo.com/forum/viewtopic.php?t=23255
Make certain that:
1. You have a 4.7K pull-up resistor on each i2c line: SDA and SCL.
2. You have a 3 volt lithium battery on the Vbat pin of the ds1307.
3. You must have a 32.768 KHz watch crystal on the ds1307.
Notice how this little board has the pull-up resistors, the battery, and
the watch crystal. All of these are needed. If you forget one of them,
your project will not work.
http://www.futurlec.com/Pictures/ET-MINI_DS1307.jpg |
Thanks for helping. I will try now ! |
|
|
tieungu13
Joined: 03 Mar 2010 Posts: 6
|
|
Posted: Wed Jun 02, 2010 1:13 pm |
|
|
I try but it did not work. I want to show values (min , hour, sec, month , date....) on the LCD.
This is my code and I demo on proteus. I used 18f4550 pic.
Code: |
///////////////////////////my function ////////////
#include "C:\Users\Thanh Hai\Desktop\final code\18F4550.h"
#use delay(clock=20000000)
#fuses HS
#define RTC_SDA PIN_B0
#define RTC_SCL PIN_B1
#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)
#include <lcd_lib_8bit.c>
#include <ds1307_thuong.c>
void main()
{
BYTE sec;
BYTE min;
BYTE hrs;
BYTE day;
BYTE month;
BYTE yr;
BYTE dow;
ds1307_init();
ds1307_set_date_time(15,6,5,2,15,20,55);
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_oscillator(False);
Lcd_init();
//set_tris_b(0x00);
///set_tris_d(0x00);
// TODO: USER CODE!!
while(1)
{
delay_ms(1000);
ds1307_get_date(day,month,yr,dow);
ds1307_get_time(hrs,min,sec);
printf(LCD_putchar,"\f\%02d/\%02d/\%02d\r\n",day,month,yr);
printf(LCD_putchar,"\%02d:\%02d:\%02d", hrs,min,sec);
}
}
////////////////////////my lib LCD ////////////////
#define LCD_RS PIN_B2
//#define LCD_RW
#define LCD_EN PIN_B3
#define LCD_data Port_D
// misc display defines-
#define Line_1 0x80
#define Line_2 0xC0
#define Clear_Scr 0x01
// prototype statements
#separate void LCD_Init ( void );// ham khoi tao LCD
#separate void LCD_SetPosition ( unsigned int cX );//Thiet lap vi tri con tro
#separate void LCD_PutChar ( char cX );// Ham viet1kitu/1chuoi len LCD
#separate void LCD_PutCmd (int cX) ;// Ham gui lenh len LCD
#separate void LCD_PulseEnable ( void );
#separate int1 LCD_ready();
// D/n Cong
#use standard_io (D)
#use standard_io (B)
//khoi tao LCD**********************************************
#separate void LCD_Init ( void )
{
output_D(~0x00); delay_ms(200);
LCD_Putcmd(0x38);
LCD_Putcmd(0x0C);
LCD_Putcmd(0x01);
LCD_Putcmd(0x06);
}
#separate void LCD_PutChar ( char cX )
{
output_high(LCD_RS);
// output_low(LCD_RW);
output_D(cX);
LCD_PulseEnable();
delay_ms(5);
}
#separate void LCD_PutCmd (int cX )//Gui lenh den LCD
{
output_low(LCD_RS);//RS = 0
// output_low(LCD_RW);//RW = 0
output_D(cX); // Dua du lieu ra
LCD_PulseEnable();
delay_ms(5);
}
//#separate int1 LCD_ready ()
//{ int1 busy_flag;
// trisc7 = 1;//set input
// output_low(LCD_RS);
// output_high(LCD_RW);
// LCD_pulseEnable();
// busy_flag = RC7;
// return(busy_flag);
//}
#separate void LCD_PulseEnable ( void )
{
output_high ( LCD_EN );// E = 1
delay_us ( 3 ); // was 10
output_low ( LCD_EN );
delay_ms ( 3 ); // was 5
}
////////////////////////////my lib DS1307//////////
////////////////////////////////////////////////////////////////////////////////
/// DS1307.C ///
/// Driver for Real Time Clock ///
/// ///
/// ds1307_init() - Enable oscillator without clearing the seconds register -///
/// used when PIC loses power and DS1307 run from 3V BAT ///
/// - Disable squarewave output ///
/// ///
/// ds1307_set_date_time(day,mth,year,dow,hour,min,sec) Set the date/time ///
/// ///
/// ds1307_get_date(day,mth,year,dow) Get the date ///
/// ///
/// ds1307_get_time(hr,min,sec) Get the time ///
/// ///
////////////////////////////////////////////////////////////////////////////////
#define RTC_SDA PIN_B0
#define RTC_SCL PIN_B1
#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)
BYTE bin2bcd(BYTE binary_value);
BYTE bcd2bin(BYTE bcd_value);
void ds1307_init(void)
{
BYTE seconds = 0;
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x00); // REG 0
i2c_start();
i2c_write(0xD1); // RD from RTC
seconds = bcd2bin(i2c_read(0)); // Read current "seconds" in DS1307
i2c_stop();
seconds &= 0x7F;
delay_us(3);
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x00); // REG 0
i2c_write(bin2bcd(seconds)); // Start oscillator with current "seconds value
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x07); // Control Register
i2c_write(0x80); // Disable squarewave output pin
i2c_stop();
}
void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
sec &= 0x7F;
hr &= 0x3F;
i2c_start();
i2c_write(0xD0); // I2C write address
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_write(bin2bcd(sec)); // REG 0
i2c_write(bin2bcd(min)); // REG 1
i2c_write(bin2bcd(hr)); // REG 2
i2c_write(bin2bcd(dow)); // REG 3
i2c_write(bin2bcd(day)); // REG 4
i2c_write(bin2bcd(mth)); // REG 5
i2c_write(bin2bcd(year)); // REG 6
i2c_write(0x80); // REG 7 - Disable squarewave output pin
i2c_stop();
}
void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x03); // Start at REG 3 - Day of week
i2c_start();
i2c_write(0xD1);
dow = bcd2bin(i2c_read() & 0x7f); // REG 3
day = bcd2bin(i2c_read() & 0x3f); // REG 4
mth = bcd2bin(i2c_read() & 0x1f); // REG 5
year = bcd2bin(i2c_read(0)); // REG 6
i2c_stop();
}
void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec)
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_start();
i2c_write(0xD1);
sec = bcd2bin(i2c_read() & 0x7f);
min = bcd2bin(i2c_read() & 0x7f);
hr = bcd2bin(i2c_read(0) & 0x3f);
i2c_stop();
}
BYTE bin2bcd(BYTE binary_value)
{
BYTE temp;
BYTE retval;
temp = binary_value;
retval = 0;
while(1)
{
// Get the tens digit by doing multiple subtraction
// of 10 from the binary value.
if(temp >= 10)
{
temp -= 10;
retval += 0x10;
}
else // Get the ones digit by adding the remainder.
{
retval += temp;
break;
}
}
return(retval);
}
// Input range - 00 to 99.
BYTE bcd2bin(BYTE bcd_value)
{
BYTE temp;
temp = bcd_value;
// Shifting upper digit right by 1 is same as multiplying by 8.
temp >>= 1;
// Isolate the bits for the upper digit.
temp &= 0x78;
// Now return: (Tens * 8) + (Tens * 2) + Ones
return(temp + (temp >> 2) + (bcd_value & 0x0f));
} |
I hope every one will help me ! Thanks To All. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jun 02, 2010 1:17 pm |
|
|
Quote: | I try but it did not work. |
Tell us what you see. Give the details on how "it did not work". |
|
|
tieungu13
Joined: 03 Mar 2010 Posts: 6
|
|
Posted: Wed Jun 02, 2010 11:42 pm |
|
|
PCM programmer wrote: | Quote: | I try but it did not work. |
Tell us what you see. Give the details on how "it did not work". |
This is my project: time, date, sec, min ....the values did not show on the LCD.
I have a Proteus file in the folder. Please check it for me. Thanks.
http://megashare.vn/dl.php/1232346 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|