Digital Clock: A Digital Clock Is A Type of Clock That Displays The Time
Digital Clock: A Digital Clock Is A Type of Clock That Displays The Time
INTRODUCTION
A digital clock is a type of clock that displays the time digitally, as opposed to an analog clock, where the time is displayed by hands. Usually, digital clocks are associated with electronic drives, but the "digital" description refers only to the display, not to the drive mechanism. (Analog clocks are driven mechanically or electronically.)
DISPLAY
To represent the time, most digital clocks use a seven-segment LED, VFD, or LCD display for each of four digits. They generally also include other elements to indicate whether the time is AM or PM, whether or not an alarm is set, and so on. Since they run on electricity, digital clocks must be reset every time the power is cut off. This is a particular problem with alarm clocks that have no "battery" backup, because even a very brief power outage during the night usually results in the clock failing to trigger the alarm in the morning. To reduce the problem, many devices designed to operate on household electricity incorporate a battery backup to maintain the time during power outages and during times of disconnection from the power supply. More recently, some devices incorporate a method for automatically setting the time, such as using a broadcast radio time signal from an atomic clock, getting the time from an existing satellite television or computer connection, or by being set at the factory and then maintaining the time from then on
ADVANTAGES
Even if there is power failure, the clock displays the right time that it should display, when the power supply is back.
USES
Because digital clocks can be very small and inexpensive devices that enhance the popularity of product designs, they are often incorporated into all kinds of devices such as cars, radios, televisions, microwave ovens, standard ovens, computers and cell phones. Sometimes their usefulness is disputed: a common complaint is that when time has to be set to Daylight Saving Time, many household clocks have to be readjusted. The incorporation of automatic synchronisation by a radio time signal is reducing this problem.
COMPONENTS USED
Components
Quantity 1 4 4 1 7 5 4 1 1 2 1 1 1 1 4 1 1
Bread Board Seven Segment Display Transistor (npn) (BC 547) Transistor (pnp) (BC 557) Resistance (330 ) Resistance (2.2 k) Resistance (10 k) Resistance (1 k) Resistance (670 ) Capacitor (22 pf) Buzzer Oscillator (4 MHz) Oscillator (32.768 kHz) RTC battery(3V) Keys PIC 16F873A (microcontroller) DS 1307 (RTC IC)
BLOCK DIAGRAM
DS1307 (RTC)
CIRCUIT DIAGRAM
CODING
#include<pic.h> #define EN_1 RC7 #define EN_2 RC6 #define EN_3 RC5 #define EN_4 RC2 #define SET_KEY RA0 #define INC_KEY RA1 #define DEC_KEY RA2 #define OK_KEY RA3 #define BUZZER RC0 #define SDA RC4 #define SCL RC3 #define FOSC 4000 #define BAUD 100 #define i2c_device_addr 0xD0 #define SSD PORTB //---------------------------------------void ssd_num_dis(unsigned char,unsigned char); void ssd_hour_dis(unsigned char); void ssd_min_dis(unsigned char); void i2c_byte_write(unsigned char, unsigned char); //address,data unsigned char i2c_byte_read(unsigned char); //address void send_i2c_byte(unsigned char); void check_ack(void); void wait_mssp(void); void delay(unsigned int); void set(void); void inc(void); void dec(void); void detect(void); unsigned char ssd_table[]={0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7C,0X07,0 X7F,0X67};
unsigned char ssd_table2[]={0xBF,0X86,0XDB,0XCF,0XE6,0XED,0XFC,0X87, 0XFF,0XE7}; unsigned char hour=15,min=53,count=0; //---------------------------------------------------------void main() { ADCON1=0x06; TRISB=0x00; TRISA=0xFF; TRISC=0x00; SSPCON=0x28; PORTC=0X01; SSPSTAT=0x80; SSPCON2=0x00; SSPADD=FOSC/(4*BAUD)-1; while(1) { count=0; if(SET_KEY==0) { min=i2c_byte_read(0x01); ssd_num_dis(min,hour); hour=i2c_byte_read(0x02); ssd_num_dis(min,hour); if((hour==6)&&(min==0)) { BUZZER=0; } else BUZZER=1; } else { set(); i2c_byte_write(0x01,min); delay(500);
i2c_byte_write(0x02,hour); delay(500); } } } //---------------------------------------------------------void i2c_byte_write(unsigned char i2c_addr,unsigned char i2c_data) { RSEN=1; // START wait_mssp(); //Check_SSPIF send_i2c_byte(i2c_device_addr|0x00); check_ack(); send_i2c_byte(i2c_addr); check_ack(); send_i2c_byte(i2c_data); check_ack(); PEN=1; wait_mssp(); //STOP
} //--------------------------------------------------------------unsigned char i2c_byte_read(unsigned char i2c_addr) { unsigned char i2c_read_data; RSEN=1; // START wait_mssp(); //Check_SSPIF send_i2c_byte(i2c_device_addr|0x00); check_ack(); send_i2c_byte(i2c_addr); check_ack();
RSEN=1; wait_mssp();
// RESTART //Check_SSPIF
send_i2c_byte(i2c_device_addr|0x01); check_ack(); RCEN=1; //read wait_mssp(); i2c_read_data=SSPBUF; ACKDT=1; ACKEN=1; PEN=1; //STOP wait_mssp(); return(i2c_read_data); } void send_i2c_byte(unsigned char i2c_byte) { SSPBUF=i2c_byte; wait_mssp(); //Check_SSPIF } void wait_mssp() { while(!SSPIF); SSPIF=0; delay(500); } void check_ack() { while(ACKSTAT) { BUZZER=0; delay(30000); BUZZER=1;
delay(30000); } } void delay(unsigned int mdelay) { while(mdelay--); } //----------------------------------------------void ssd_num_dis(unsigned char smin,unsigned char shour) {unsigned char stimes; for(stimes=0;stimes<50;stimes++) { EN_1=EN_2=EN_3=EN_4=0; SSD=ssd_table[smin%16]; EN_4=1; delay(200); EN_1=EN_2=EN_3=EN_4=0; SSD=ssd_table[smin/16]; EN_3=1; delay(200); EN_1=EN_2=EN_3=EN_4=0; SSD=ssd_table2[shour%16]; EN_2=1; delay(200); EN_1=EN_2=EN_3=EN_4=0; SSD=ssd_table[shour/16]; EN_1=1; delay(200); } } //----------------------------------------------------------------void ssd_hour_dis(unsigned char shour) { unsigned char stimes; for(stimes=0;stimes<50;stimes++) {
EN_1=EN_2=EN_3=EN_4=0; SSD=ssd_table[shour%16]; EN_2=1; delay(200); EN_1=EN_2=EN_3=EN_4=0; SSD=ssd_table[shour/16]; EN_1=1; delay(200); } } //----------------------------------------------------------------void ssd_min_dis(unsigned char smin) { unsigned char stimes; for(stimes=0;stimes<50;stimes++) { EN_1=EN_2=EN_3=EN_4=0; SSD=ssd_table[smin%16]; EN_4=1; delay(200); EN_1=EN_2=EN_3=EN_4=0; SSD=ssd_table[smin/16]; EN_3=1; delay(200); } } //---------------------------------void inc() { if(count==0) { if(hour==0x23) hour=0x00; if(hour<0x23)
hour++; if(hour==0x0A||hour==0x1A) hour=hour+6; } if(count==1) { if(min==0x59) min=0x00; if(min<0x59) min++; if(min==0x0A||min==0x1A||min==0x2A||min==0x3A|| min==0x4A) min=min+6; } } //----------------------------------------------void dec() { if(count==0) { if(hour==0x00) hour=0x23; if(hour>0x00) hour--; if(hour==0x0F||hour==0x1F) hour=hour-6; } if(count==1) { if(min==0x00) min=0x59; if(min>0x00) min--; if(min==0x0F||min==0x1F||min==0x2F||min==0x3F|| min==0x4F) min=min-6;
} } //------------------------------------------------------void detect() { if(INC_KEY==1) inc(); if(DEC_KEY==1) dec(); if(OK_KEY==1) {count++; while(OK_KEY==1);} } //-------------------------------------------------------void set() { while(count==0) { ssd_hour_dis(hour); detect(); } while(count==1) { ssd_min_dis(min); detect(); } }
WORKING
I2C PROTOCOL
The I2C-bus supports any IC fabrication process (NMOS, CMOS, bipolar). Two wires, serial data (SDA) and serial clock (SCL), carry information between the devices connected to the bus. Each device is recognized by a unique address (whether its a microcontroller, LCD driver, memory or keyboard interface) and can operate as either a transmitter or receiver, depending on the function of the device. Obviously an LCD driver is only a receiver, whereas a memory can both receive and transmit data. In addition to transmitters and receivers, devices can also be considered as masters or slaves when performing data transfers. A master is the device which initiates a data transfer on the bus and generates the clock signals to permit that transfer. At that time, any device addressed is considered a slave. The I2C-bus is a multi-master bus. This means that more than one device capable of controlling the bus can be connected to it. It should be noted that master-slave relationships are not permanent, but only depend on the direction of data transfer at that time. TERM DESCRIPTION Transmitter-The device which sends data to thebus. Receiver-The device which receives data from the bus. Master-The device which initiates a transfer,generates clock signals and terminates a transfer. Slave-The device addressed by a master. Multi-master-More than one master can attempt to control the bus at the same time without corrupting the message. Arbitration-Procedure to ensure that, if more than one master simultaneously tries to control the bus, only one is allowed to do so and the winning message is not corrupted.
DS1307
1 2 3 4
VCC - Primary Power Supply X1, X2 - 32.768 kHz Crystal Connection VBAT - +3V Battery Input GND - Ground SDA - Serial Data SCL - Serial Clock SQW/OUT - Square wave/Output Driver DESCRIPTION The DS1307 Serial Real Time Clock is a low power, full BCD clock/calendar plus 56 bytes of nonvolatile SRAM. Address and data are transferred serially via a 2-wire bi-directional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with less than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. The DS1307 has a built-in power
sense circuit which detects power failures and automatically switches to the battery supply. OPERATION 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 device terminates an access in progress and resets the device address counter. Inputs to the device will not be recognized at this time to prevent erroneous data from being written to the device from an out of tolerance system. When VCC falls below VBAT the device switches into a low current battery backup mode. Upon power up, the device switches from battery to VCC when VCC is greater than VBAT +0.2V and recognizes inputs when VCC is greater than 1.25 x VBAT. The block diagram in shows the main elements of the Serial Real Time Clock. DS1307 ADDRESS MAP SECONDS MINUTES HOURS DAY DATE MONTH YEAR CONTROL RAM 56X8 00H
07H 08H 3FH WORKING Setting of Time In order to set the time, four keys are provided. The first key is set key, second is increment key, third is decrement and fourth is OK key. When you feel the need of setting the time, press set key, now hours start blinking. Change hours by increment and decrement keys. Increment will result in increase in hours and decrease will make hour to decrease. After you reach the desired hours, press OK key. Now you will find minutes start blinking. Set the minutes in the same way as hours. After you press OK key again, the time changes from this current value. What basically happens is that when OK key is pressed twice the data, which is set by user, is written at the corresponding registers in the DS1307. It initializes the two registers and next time when the change is made, the change is displayed in the last value written in the register and hence the desired time is obtained. The data is first converted to BCD code from its hexadecimal counterpart and then it is sent to DS1307. Displaying Time The clock starts displaying time as soon as the power supply is fed to the circuit. When power supply is given to the circuit, the data present in the two registers (at locations 01H and 02H corresponding to minutes and hours respectively) of DS1307 is read by microcontroller using I2C which further displays time on seven segment display. For displaying time on seven segment displays, the data coming from DS1307, in the form of BCD, is converted into hexadecimal code and then it is send to the port.
When there is no power, data automatically changes in DS1307 due to the presence of secondary battery in the circuit and is fed to microcontroller when the power supply is applied to the circuit.
APPLICATIONS
Because digital clocks is very small and inexpensive devices that enhance the popularity of product designs, they are often incorporated into all kinds of devices such as cars radios televisions microwave owens standard ovens computers cell phones.