- 5Example
The Real time clock DS1307 IC basically is stand alone time clock. Well, basically we can use a micrcontroller to keep time, but the value would go off as soon as it is powered off.
If you have the DS1307 module you will need to solder the wires to the board, or solder on some inline header pins 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. Connecting this module is easy as header pins are installed on the board at. I want drive a ds1307 with ATmega32 in assembly. 1.i done every thing for testing this program 2. Avr studio 6.1 3. I use bascom 2.0.7.3 for programing avr. The DS1307 is a 5V chip, so make sure to use it with a 5V Arduino. The DS3231 also support 3.3V. After hooking up your chip of choice, press Upload in the Arduino IDE, then open the serial console (the rightmost button on the toolbar).
The RTC DS1307 is a handy solution to keep time all the way, when it is powered by a coin cell.
It is uses I²C (Inter-Integrated Circuit) protocol, referred to as I-squared-C, I-two-C, or IIC for communication with the micrcontroller.
Check the Basics of I2C here, if you are not familiar with it.For details of I2C in AVR, go through AVR Communication Protocols tutorial.
The first thing that the MCU sends to the slave (RTC) is the device ID. The device ID for DS1307, shown below. It also tells weather we want to write to or read from the RTC.
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
1 | 1 | 0 | 1 | 0 | 0 | 0 | R/W |
- bit-0 is 0 than we Write to RTC
- bit-0 is 1 we Read from RTC.
This is defined in the code as:
The RTC keeps the date and time arranged in it's memory as shown below:
ADDRESS | FUNCTION | RANGE |
---|---|---|
00h | Seconds | 00–59 |
01h | Minutes | 00–59 |
02h | Hours | 01-12/00-24 |
03h | Day | 01–07 |
04h | Date | 01–31 |
05h | Month | 01–12 |
06h | Year | 00–99 |
07h | Control | |
08h to 3Fh | RAM | 00h–FFh |
Write to the addresses above we can set the time, and once we set it, we can read it any time we need.
The address 0x07 is a control registered as described below:
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
OUT | 0 | 0 | SQWE | 0 | 0 | RS1 | RS0 |
We write 0x00 to Control register to disable SQW-Out. We do not use any other bits from it, so you need not worry.
Now we can initialize the RTC with the code below
Note: The date and time read from Ds1307 will be of BCD format, like:
- 0x12,0x39,0x26 for 12hr,39min and 26sec.
- 0x15,0x08,0x47 for 15th day,8th month and 47th year
Both the above functions use a simple structure shown below for easy access
Now, let's put together the all that we have discussed in a simple example to read and show the time on character LCD.
Hookup
Code
For those of you, who would like to watch instead of read we have made a video with all the gyan.
Ds1307 Bascom Programming
Download the complete project folder from the below link: https://github.com/ExploreEmbedded/ATmega32_ExploreUltraAvrDevKit/archive/master.zip
Check the Basics of I2C here, if you are not familiar with it.For details of I2C in AVR, go through AVR Communication Protocols tutorial.
The first thing that the MCU sends to the slave (RTC) is the device ID. The device ID for DS1307, shown below. It also tells weather we want to write to or read from the RTC.
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
1 | 1 | 0 | 1 | 0 | 0 | 0 | R/W |
- bit-0 is 0 than we Write to RTC
- bit-0 is 1 we Read from RTC.
This is defined in the code as:
The RTC keeps the date and time arranged in it's memory as shown below:
ADDRESS | FUNCTION | RANGE |
---|---|---|
00h | Seconds | 00–59 |
01h | Minutes | 00–59 |
02h | Hours | 01-12/00-24 |
03h | Day | 01–07 |
04h | Date | 01–31 |
05h | Month | 01–12 |
06h | Year | 00–99 |
07h | Control | |
08h to 3Fh | RAM | 00h–FFh |
Write to the addresses above we can set the time, and once we set it, we can read it any time we need.
The address 0x07 is a control registered as described below:
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
OUT | 0 | 0 | SQWE | 0 | 0 | RS1 | RS0 |
We write 0x00 to Control register to disable SQW-Out. We do not use any other bits from it, so you need not worry.
Now we can initialize the RTC with the code below
Note: The date and time read from Ds1307 will be of BCD format, like:
- 0x12,0x39,0x26 for 12hr,39min and 26sec.
- 0x15,0x08,0x47 for 15th day,8th month and 47th year
Both the above functions use a simple structure shown below for easy access
Now, let's put together the all that we have discussed in a simple example to read and show the time on character LCD.
Hookup
Code
For those of you, who would like to watch instead of read we have made a video with all the gyan.
Ds1307 Bascom Programming
Download the complete project folder from the below link: https://github.com/ExploreEmbedded/ATmega32_ExploreUltraAvrDevKit/archive/master.zip
Ds1307 Bascom Programmer
Have a opinion, suggestion , question or feedback about the article let it out here!
Please enable JavaScript to view the comments powered by Disqus.Here you can find a step-by-step tutorial about the Bascom-AVR code to drive
the AVR Ethernet I/O-board
This tutorial is based on my own hardware
Step 1. Get the hardware.. | |
---|---|
A bare board size 180 x 90 mm. | |
Step 2: Description | |
AVR Ethernet I/O board. Atmega128, 2 x MAX3232EEWE for 2 x RS232, 1 x VNC1L-1A with 2 x USB connector, black/white composite video with Atmega32, DS1307 Real Time Clock with battery backup, PS/2 keyboard connector, Wiz810MJ ethernet module and LCD 16 x 2 with backlight. | |
Step 3: Some images building the board | |
Main processor, Atmega128, 3,3 volt, on 8 Mhz. | |
Step 4: Fusebits Atmega128 and Atmega32 | |
The fusebits of the Atmega32, the composite video controller. The fusebits of the Atmega128, the main processor. | |
Step 5: Atmega32 as a composite video chip | |
On http://www.sbprojects.net/projects/apple1/a-one-terminal.htm you can find the a1term.zip made by a electronics-friend. You can find a copy of the video controller software, including the source file, on his Download page. The program was written using his SB-Assembler. The entire program runs in one single interrupt routine. The main program is empty, well almost empty. Interrupts are spaced exactly 64µs apart for 50Hz systems, or 63.5µs for 60Hz systems. Each interrupt routine starts by generating an H-Sync pulse. A state machine then determines what is to be done during the rest of the line. Care should be taken that the interrupt routine ends before it's time to start a new one. What needs to be done?
All these tasks are performed inside the interrupt routine. A simple state machine determines what task needs to be done during each particular video line. Some lines leave no time to do anything else but control the video/sync outputs. Other lines allow plenty of time ( >55µs ) to do other tasks. These other tasks are: accepting characters from the Apple 1, sending the characters to the serial output, scrolling the screen, flashing the cursor, etc, etc. When the a1video.hex-file has been loaded in the Atmega32, here a demo how to use it with the Atmega128.
Close up of the Atmega32 video controller, Wiz810MJ ethernet module and PS/2 keyboard connector. During testing, small messages on the LCD are essential. The result on a DVD-player with composite connection (yellow plug right) | |
Step 6: Driving LCD 16 x 2 and the serial ports | |
Settings for COM2, in the middle of JP4.
2 x MAX3232EEWE and connectors | |
Step 7: VNC1L-1A - USB Controller | |
Two buttons to program the VNC1L-1A USB-controller on the AVR Ethernet I/O board. Called PROG and RESET. When you buy the VNC1L-1A, it is almost blanc, it has a bootloader. With the COM2 serial port, a terminal program like Hyperterminal (on 115200 baud) and a ROM-file from www.vinculum.com you can load the VNC1L-1A with the functions you want it to have. Once firmware has been loaded, an upgrade or other functions can be flashed by just plugging a USB memory stick in one of the USB connectors. By removing two SMD resistors and placing two resistors on another place both buttons can be connected to the Atmega128, so you will have a total of 5 buttons to work with. 2 x USB connector Don't under-estimate placing this chip on the AVR Ethernet I/O board, the VNC1L-1A. When you put the JP4 jumpers to the right, COM2 with it's MAX3232 is connected to the VNC1L-1A and you can type the commands from Hyperterminal at 9600 baud. When a memorystick is inserted it is detected. First thing is looking for a file to upgrade the firmware of the VNC1L-1A. FS, followed with IDD will show you the information of the memory stick. To create and write to a file this is what you can do in Hyperterminal
And to do this from within Bascom-AVR 'VNC - Doing a DIR of the USB-stick, create a file called AVR.TXT, add or append a line of text and read the file back. 'Monkey proof code $regfile 'm128def.dat' Declare Sub Wait_for_prompt 'dimension used variables Open 'COM2:' For Binary As #2 Config Portc.7 = Output Enable Timer1 On Timer1 Isr_timer1 'Timer interrupt routine Do Call Reset_vnc Call Usb_cmd( 'DIR') Call Usb_cmd( 'IPA') Call Usb_cmd( 'OPW AVR.TXT') Call Usb_cmd( 'WRF 17') Call Usb_cmd( 'Bens HobbyCorner{013}') Call Usb_cmd( 'CLF AVR.TXT') Call Usb_cmd( 'RD AVR.TXT') Incr Teller End 'reset VNC1L-1A Sub Usb_cmd(command As String) Sub Wait_for_prompt Isr_timer1: 'ISR of Timer1 If Usb_handle = 1 Then You have to format your USB memory stick with 512 bytes sector size. This can be done like this: FORMAT E: /FS:FAT32 /A:512 | |
Step 8: Tools and toughts about VNC1L-1A | |
The way to program the blanc VNC1L-1A USB chip. Start VPROG_COM.EXE, select the right ROM-file, select the COM-port, press PROG on the board and power-up the board. Shortly press RESET and the firmware update is started. | |
Step 9: PS/2 keyboard | |
'PS2-keyboard | |
Step 10: Wiz810MJ-ethernet controller | |
' WIZ810MJ Ethernet demo
More examples for the WIZ810MJ can be found here: http://benshobbycorner.tk/bzijlstra/software/examples/wiz810mj.htm | |
Step 10: DS1307 Real Time Clock | |
To check if the DS1307Z+ can be found on the I2c-bus I use the I2c-scanner which I wrote a few years back. and here the code $regfile 'm128def.dat' | |
To be continued.. | |
Thanks to: | |
Thanks to Mark Alberts Thanks to San Bergmans | |
Ben Zijlstra - Ben's HobbyCorner - 2009 |