Ds1307 Bascom Program Examples

Posted by admin- in Home -17/01/18
Ds1307 Bascom Program Examples Rating: 4,5/5 5605votes

Real Time Clocks, as the name suggests are clock modules. They are available as integrated circuits (ICs) and manages timing like a clock. Some RTC ICs also manages date like a calendar. The main advantage is that they have a system of battery backup which keeps the clock/ca lender running even in case of power failure.

The DS1307 operates as a slave device on the serial bus. Access is obtained by implementing a START condition and providing a device identification code followed by a register address. Subsequent registers can be accessed sequentially until a STOP condition is executed. When VCC falls below 1.25 x VBAT the.

A very small current is required for keeping the RTC alive. This in most case is provided by a miniature 3v lithium coin cell. So even if the embedded system with RTC is powered off the RTC module is up and running by the backup cell. This same technique is used in PC timing also. If you have opened your computer case you will notice a small coin cell in the mother board. In this tutorial we will learn to use a very famous RTC IC named. The DS1307 is described in the datasheet as follows The DS1307 is a low-power clock/calendar with 56 bytes of battery-backed SRAM.

The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The DS1307 operates as a slave device on the I2C bus. So the aim of the project will be to access the DS1307 registers, read time • Access the DS1307 registers i.e. Read/write data to/from the DS1307 IC • Format the read data and display in LCD • Ability to get time from user and store it to DS1307. This provide means to setup the RTC module with correct time. DS1307 Internal Registers From software point of view the DS1307 is just a collection of some 8 bit registers.

You can read these register to obtain the current time and date. You can also modify them to hold the correct time. After that the DS1307 keeps then updated with current date and time. The following registers are there.

Fig.: BCD Format in DS1307 Registers. The DS1307.c and DS1307.h provide easy access to the registers. I have provided just two functions that allows you to read and write the DS1307 registers.

/*************************************************** Function To Read Internal Registers of DS1307 --------------------------------------------- address: Address of the register data: value of register is copied to this. Returns: 0= Failure 1= Success ***************************************************/ uint8_t DS1307Read(uint8_t address,uint8_t *data) /*************************************************** Function To Write Internal Registers of DS1307 --------------------------------------------- address: Address of the register data: value to write. Returns: 0= Failure 1= Success ***************************************************/ uint8_t DS1307Write(uint8_t address,uint8_t data) These functions depends on the I2C library.

The I2C (Inter IC Communication) is a popular communication protocol between ICs. The DS1307 and our AVR ATmega8 communicates using the I2C bus. I will give more details on I2C in a separate tutorial. A Simple RTC Module The require an, a 3v lithium battery and 2 pull up registers to function. So I will make a small PCB that will hold all these. The image of module is shown below.

The module can be connected to using. Fig.:AVR ATmega8 RTC Interface Circuit Diagram. Avr-gcc software The full software for the example is written in C language and compiled with avr-gcc. The whole software is very modular.

Following software modules are used. • LCD Interface modules for handling the display device. More detail is given on these pages. • for MCUs • for MCUs • Low Level I2C Interface Library. This handles the data communication using the hardware I2C module present inside AVR/PIC MCUs.

• For Atmel AVR MCUs (Yet to be written) • For PIC16 MCUs.(Yet to be written) • For PIC18 MCUs.(Yet to be written) • DS1307 Module: This module built on top of above I2C module help in reading/writing data to and from the DS1307 chip. Functions are very simple and documented inside the.c and.h files itself. • The main application module RTC.c is the application software that uses the above modules to communicate with both the LCD and DS1307 chip. It also manages the handling of UI (User Interface) by the help of 3 push buttons.

Fabrication Instructions Assemble the circuits according to the circuit diagrams shown above. Burn the hex file (download link given at the end of article) to an ATmega8 using. Set the fuse byte as follows LOW=21 HEX HIGH=D9 HEX.

Apply power to the circuit and adjust the LCD Contrast Pot until the display is clear. The initial time should be shown as 00:00:00. Now press the 'menu' key to enter the main menu. Use the LEFT and RIGHT Key to move between options and ENTER to select an item. The main menu has the following options. • Set Time • Set On Time (NOT IMPLEMENTED YET) • Set Off Time (NOT IMPLEMENTED YET) • Quit. Fig.: Setting the time.

Use the 'Move Sel' key to move between Hour,Minute,Second,AM/PM, and. Use the 'Increase'/'Decrease' buttons to adjust value. Finally go to and press any key. A message will be shown, 'Main Time Set'. The main menu will return. Select 'Quit' from the main menu and the main screen showing the current time will be displayed.

Now the RTC is setup, it will keep the time even if you switch off the circuit. Next the you power up the circuit it will still show the REAL time! I just finished my first project using the DS1307, and I appreciate your idea of building the complete module on perf board.

I think I might go back and do that 🙂 There is a way around needing the pull-up resistors on the clock module (at least with an ATMEGA168): Set the SCL and SDA lines high, which enables pull-up resistors inside the controller. I only know how to do this in the Arduino environment, but I do know that it’s possible. I also used the SRAM, which was super convenient for when I wanted to be able to reset the atmega, but be able to pick up from where I left off.

Hello Debbie, You could easily get that line if you know C well! The time is a string.

So Time[0] is actually the 10th part of the hour. So if time is 11:00:00 AM then Time[0] is ‘1’ and if time is 03:15:24 PM then Time[0] is ‘0’ note that as Time[] is a string each element of this array is in ASCII.

If you consult the ASCII Table you will note that numbers starts after 48 in ASCII table so ASCII code of ‘0’ is 48 and ‘1’ is 49 and so on. So 48 is added to it. If you look at DS1307 register table given above you will find out that 10th position of hour is stored in 5th BIT HOUR register.

And since we are running in 12hour mode the 10th position can only be 0 or 1. It is 0 when time is from 1 to 9 and 1 when time is from 10 to 12. So just one bit is required. And since hour reg holds other values too, so to extract only the tenth part we are ANDING it with binary 00010000 (written as 0b00010000 so the compiler can get its binary number).

So now all BITs are set to 0 execpt the fifth bit. Which is either 0 or 1 as in original REG. Now we shift this value to 4 places to left. This bring this bit to the 0th position. Now we add 48 to it to get an ASCII equivalent. All this were just requires knowledge of basic computing and C programming nothing special. Before you delve into embedded be a good programmer first.

I have been coding since my childhood with Logo and Basic languages! @AV Got it working on an atmega32/16. Had to re-fuse it to default values (1MHZ) ->Lfuse E1 Hfuse D9 Lock 3F. I couldnt get it to work at 16MHZ.

Anybody got a solution? Wheres my 20×4 LCD support?:o) BTW I found this EXCELLENT avrdude GUI >Google “SinaProg 1.6.5.10” for an excellent avrdude gui.

Best Ive seen so far. By a long shot. I have to use this till I get an avrstudio 4 compatible prog.

Also, The way youve got the ds1307 hooked up in your schematic (above) doesnt work – at least for me. It did work after I hooked up: PIN4 ->GND / PIN8 ->VCC / also used 10k resistors as pullups on SDA, SCL line eventhough 4.7k no doubt works fine as well. Thanks again Av.

I see what you mean now by “portability”. Thnx for the info.but i’m doing a data logger project,where on the sda,scl line of atmega8,one ds1307 & one 24c64 eeprom are connected.RTC runs well generally,bt when the eepromwrite() function is being executed, the time on the screen is halted.next time i powering on the circuit,the date and time are shown is completely changed.so,my question is when the rtc is showing time & date on the 16×2 lcd screen,can i access the eeprom(read & write),or i have to do some other coding for that? That’s why i was asking you if i can connect the ds1307 & 24c64eeprom in the same sda,scl linei’m completely using your rtc & eeprom routines.individually they are fabulous.so i.ve a big hope upon you.pls do rply. And what about the addres lines.i think that would be useful when we are using multiple eeproms in same sda,scl line.bt in this case there is one ds1307 & one 24c64 eeprom.so how can i utilise the address lines? Hello sir, as you completed this project i am asking the problem that i am facing. When i set time i.e.

12:50:38 PM and i connect 3v dc supply to rtc between +ve on 3rd and -ve on 4th and 4th is grounded. But it still shows 12:50:38 PM. The time is not increasing or probably rtc is not working.

For 3 volt battery i am using 2 pencil cells made o Zinc Chloride that gives 3 volt dc. I have measured it by mutimeter also. 8th pin of rtc is given vcc. 5th and 6th pins are connected to 4.7k resistor and to vcc. Crystal oscillator is connected between first and second. I have also connected 4.7k resistor to 7th pin and to vcc.

Dear Avinash Gupta, First I would like to say gratulations and thank you about your website and projects! They are very useful and great solutions! I would like to ask you about this DS1307 project. Is it possible to save a given time point in DS1307? (Beside the current time, I would store an alarm time.) If it is possible could you explain me how? You mentioned that it is also can handle and following dates, calendars as well. How does it possible to store date and read it back?

Really really thank you your help! Have a nice day and good projects. Regards, Gabor •.

Sir, LCD module of this project is working. But when i set time i.e. 12:50:38 PM and i connect 3v dc supply to rtc between +ve on 3rd and -ve on 4th and 4th is grounded. But it still shows 12:50:38 PM.

The time is not increasing or probably rtc is not working. For 3 volt battery i am using 2 pencil cells made o Zinc Chloride that gives 3 volt dc. I have measured it by mutimeter also. 8th pin of rtc is given vcc. 5th and 6th pins are connected to 4.7k resistor and to vcc. Crystal oscillator is connected between first and second. I have also connected 4.7k resistor to 7th pin and to vcc.

Words cannot express how delighted i am to follow your write_ups.its just straight forward and plain english. Many unreserved thanks. I tried the above project after downloading the zip file but when i tried to build it in atmelstudio 6,, i get the following errors in that order. I used ATMega328p for it.

Pls what should i do ‘TCCR0’ undeclared (first use in this function) Warning2each undeclared identifier is reported only once for each function it appears in Error3‘TIMSK’ undeclared (first use in this function).

We keep getting requests on how to use DS1307 and DS3231 real-time clock modules with Arduino from various sources – so this is the first of a two part tutorial on how to use them. For this Arduino tutorial we have two real-time clock modules to use, one based on the Maxim: and another: There are two main differences between the ICs on the real-time clock modules, which is the accuracy of the time-keeping. The used in the first module works very well, however the external temperature can affect the frequency of the oscillator circuit which drives the DS1307’s internal counter. This may sound like a problem, however will usually result with the clock being off by around five or so minutes per month. The is much more accurate, as it has an internal oscillator which isn’t affected by external factors – and thus is accurate down to a few minutes per year at the most. If you have a DS1307 module- don’t feel bad, it’s still a great value board and will serve you well.

With both of the modules, a backup battery is installed when you receive them from Tronixlabs, however these are an inexpensive variety and shouldn’t be relied on for more than twelve months. If you’re going to install the module in a more permanent project, it’s a good idea to buy a new CR2032 battery and fit it to the module. Along with keeping track of the time and date, these modules also have a small EEPROM, an alarm function (DS3231 only) and the ability to generate a square-wave of various frequencies – all of which will be the subject of a second tutorial. Connecting your module to an Arduino Both modules use the I2C bus, which makes connection very easy.

If you’re not sure about the I2C bus and Arduino, check out the I2C tutorials (chapters and ), or review chapter seventeen of my book ““. Moving on – first you will need to identify which pins on your Arduino or compatible boards are used for the I2C bus – these will be knows as SDA (or data) and SCL (or clock). On Arduino Uno or compatible boards, these pins are A4 and A5 for data and clock: If you’re using an Arduino Mega the pins are D20 and D21 for data and clock: If you’re using an Pro Mini-compatible the pins are A4 and A5 for data and clock, which are parallel to the main pins, as shown below: DS1307 module If you have the DS1307 module you will need to solder the wires to the board, or solder on some so you can use jumper wires. Then connect the SCL and SDA pins to your Arduino, and the Vcc pin to the 5V pin and GND to GND. DS3231 module Connecting this module is easy as header pins are installed on the board at the factory. You can simply run jumper wires again from SCL and SDA to the Arduino and again from the module’s Vcc and GND pins to your board’s 5V or 3.3.V and GND. However these are duplicated on the other side for soldering your own wires. Both of these modules have the required pull-up resistors, so you don’t need to add your own.

Like all devices connected to the I2C bus, try and keep the length of the SDA and SCL wires to a minimum. Reading and writing the time from your RTC Module Once you have wired up your RTC module. Enter and upload the following sketch. Although the notes and functions in the sketch refer only to the DS3231, the code also works with the DS1307.

} There may be a lot of code, however it breaks down well into manageable parts. It first includes the Wire library, which is used for I2C bus communication, followed by defining the bus address for the RTC as 0x68. These are followed by two functions that convert decimal numbers to BCD (binary-coded decimal) and vice versa. These are necessary as the RTC ICs work in BCD not decimal.

The function setDS3231time() is used to set the clock. Using it is very easy, simple insert the values from year down to second, and the RTC will start from that time. For example if you want to set the following date and time – Wednesday November 26, 2014 and 9:42 pm and 30 seconds – you would use. SetDS3231time ( 30, 42, 21, 4, 26, 11, 14 ); Note that the time is set using 24-hour time, and the fourth paramter is the “day of week”.

This falls between 1 and 7 which is Sunday to Saturday respectively. These parameters are byte values if you are subsituting your own variables. Once you have run the function once it’s wise to prefix it with // and upload your code again, so it will not reset the time once the power has been cycled or micrcontroller reset. Reading the time form your RTC Is just as simple, in fact the process can be followed neatly inside the function displayTime(). You will need to define seven byte variables to store the data from the RTC, and these are then inserted in the function readDS3231time(). For example if your variables are. ReadDS3232time ( & second, & minute, & hour, & dayOfWeek, & dayOfMonth, & month, & year ); Then you can use the variables as you see fit, from sending the time and date to the serial monitor as the example sketch does – to converting the data into a suitable form for all sorts of output devices.

Just to check everything is working, enter the appropriate time and date into the demonstration sketch, upload it, comment out the setDS3231time() function and upload it again. Then open the serial monitor, and you should be provided with a running display of the current time and date, for example: From this point you now have the software tools to set data to and retrieve it from your real-time clock module, and we hope you have an understanding of how to use.

You can learn more about the particular real-time clock ICs from the manufacturer’s website – and. And if you enjoyed this article, or want to introduce someone else to the interesting world of Arduino – check out my book (now in a fourth printing!) “”.

Popular Posts