SlideShare a Scribd company logo
9
Most read
11
Most read
12
Most read
FPGA LAB PROJECT Page 1
International Islamic University
Islamabad
FPGA LAB PROJECT
Real Time Clock Interfacing With FPGA
Submitted by:
Mafaz Ahmed 1882-F12D
FPGA LAB PROJECT Page 2
TABLE OF CONTENTS Pages
1. Introduction .…………………………..(1)
2. Real Time Clock RTC module (DS 1307) …………….…….……....(1)
 Description
 Features
 Circuit Diagram
 Data Registers
3. I2Cprotocol …..…………………………………..(5)
 Description
 Features
 Timing Diagram
4. FPGA KIT(SPARTAN2) ...…………………………………….(6)
 Description
 Features
5. Schematic ……………………………………….(6)
6. Verilog Code ………..…………………………….(7)
7. HardwareImplementation .…………………………………….(12)
8. Conclusion .…………………………………….(13)
9. References …………………………………….(13)
FPGA LAB PROJECT Page 3
1.Introduction:
A real-time clock (RTC) is a computer clock (most
often in the formof an integrated circuit) that keeps track of the currenttime.
Although the term often refers to the devices in personal computers, servers
and embedded systems, RTCs arepresent in almost any electronic device
which needs to keep accuratetime.
Itfeatures the DS1307 serialreal-time clock (RTC). Itis
a low-power, fullbinary-coded decimal (BCD) clock/calendar with
programmablesquare-waveoutputsignal. Board uses I2C interface for
communication, and can only use5V power supply. Board features a
3V/230mA lithiumbattery as a backup power supply which ensures that
timekeeping operation continues even when the main power supply goes OFF.
2.Real Time Clock RTC module (DS 1307):
Description:
RTC DS 1307 is a great battery-backed real time clock
(RTC) that allows your microcontroller projectto keep track of time even if it is
reprogrammed, or if the power is lost. Perfect for data logging, clock-building,
time stamping, timers and alarms, etc. The DS1307 is the mostpopular RTC,
and works bestwith 5V-based chips.
FPGA LAB PROJECT Page 4
Features:
 Two wireI2
Cinterface
 Hour : Minutes : Seconds AM/PM
 Day Month, Date - Year
 Leap year compensation
 Accurate calendar up to year 2100
 Battery backup included
 1Hz output pin
 56 Bytes of Non-volatile memory available to user
 0x68 I2
CAddress
Circuit Diagram:
FPGA LAB PROJECT Page 5
DATA Registers:
3. I2C Protocol:
Description:
I²Cuses only two bidirectional open-drain lines, Serial Data Line
(SDA) and Serial Clock Line (SCL), pulled up with resistors. Typicalvoltages used
are +5 V or +3.3 V although systems with other voltages are permitted. The
I²C reference design has a 7-bit or a 10-bit (depending on the device
used) address space. Common I²Cbus speeds are the 100 Kbit/s standard
mode and the 10 Kbit/s low-speed mode.
Features:

 The data signal(SDA) is a bidirectional line, which can be read and
written, by the master and the slave.
 The communication between a master and a slave begins with a START
condition followed by the slaveaddress to be reached, one read/write
bit, a bit of recognition that can be ACK (if the communication was
successful) or NACK (if the communication was unsuccessfulor theend
of the messageis set by the master), the 8 bits of data to send or to
receive, the ACK or NACK bit, and finishes with a STOP condition or a
condition Repeated START.
FPGA LAB PROJECT Page 6
Timing Diagram:
4.FPGA SPARTAN 2 KIT:
Description:
The Spartan 2 family of FPGAs havea regular, flexible, programmable
architecture of ConfigurableLogic Blocks (CLBs), surrounded by a perimeter of
programmableInput/outputBlocks (IOBs).
Features:
 50MHz SMD crystaloscillator
 JTAG-programmable
 9-pin RS-232 SerialPort
 4-digit seven-segmentdisplay
 8 individual LEDs
 8 slide switches
5. Schematic:
Idle
state
s
Start
Communication
Write Pointer
Address
Read Start
from slave
Stop
Communication
Receive
Data
FPGA LAB PROJECT Page 7
6. Verilog Code:
module I2C( inoutsda,output scl,output[27:0]out_data,input clk,output[2:0] ack_out, output [1:0]
ack_count1,input reset);
parameter clock_div=500;// I2C clock 50MHz/(500)=100KHz
//---------------------------I2C State MachineVariables----------------------
parameter Rxidle=0;
parameter Rxstart =1;
parameter RxPointeraddr=2;
parameter Rxstart1 =3;
parameter Rxdata =4;
parameter Rxstop =5;
//-----------------------------------------------------------------------------
reg chk=0;
reg [2:0] ack;
reg [1:0] ack_count=0;
reg [27:0] LED;
assign out_data=LED;
wire [15:0] time_out;
always @ (time_out[3:0])
begin
case(time_out[3:0])
4'h0: LED[6:0] <= 7'b1110111;
4'h1: LED[6:0] <= 7'b0010010;
4'h2: LED[6:0] <= 7'b1011101;
4'h3: LED[6:0] <= 7'b1011011;
4'h4: LED[6:0] <= 7'b0111010;
4'h5: LED[6:0] <= 7'b1101011;
4'h6: LED[6:0] <= 7'b1101111;
4'h7: LED[6:0] <= 7'b1010010;
4'h8: LED[6:0] <= 7'b1111111;
4'h9: LED[6:0] <= 7'b1111010;
4'hA: LED[6:0] <= 7'b1111110;
4'hB: LED[6:0] <= 7'b1111111;
4'hC: LED[6:0] <= 7'b1100101;
4'hD: LED[6:0] <= 7'b1110111;
4'hE: LED[6:0] <= 7'b1101101;
4'hF: LED[6:0] <= 7'b1101100;
default:LED[6:0] <= 7'bxxxxxxx;
endcase
end
always @ (time_out[7:4])
begin
case(time_out[7:4])
4'h0: LED[13:6] <= 7'b1110111;
4'h1: LED[13:6] <= 7'b0010010;
4'h2: LED[13:6] <= 7'b1011101;
4'h3: LED[13:6] <= 7'b1011011;
4'h4: LED[13:6] <= 7'b0111010;
4'h5: LED[13:6] <= 7'b1101011;
4'h6: LED[13:6] <= 7'b1101111;
4'h7: LED[13:6] <= 7'b1010010;
FPGA LAB PROJECT Page 8
4'h8: LED[13:6] <= 7'b1111111;
4'h9: LED[13:6] <= 7'b1111010;
4'hA: LED[13:6] <= 7'b1111110;
4'hB: LED[13:6] <= 7'b1111111;
4'hC: LED[13:6] <= 7'b1100101;
4'hD: LED[13:6] <= 7'b1110111;
4'hE: LED[13:6] <= 7'b1101101;
4'hF: LED[13:6] <= 7'b1101100;
default:LED[13:6] <= 7'bxxxxxxx;
endcase
end
always @ (time_out[11:8])
begin
case(time_out[11:8])
4'h0: LED[20:7] <= 7'b1110111;
4'h1: LED[20:7] <= 7'b0010010;
4'h2: LED[20:7] <= 7'b1011101;
4'h3: LED[20:7] <= 7'b1011011;
4'h4: LED[20:7] <= 7'b0111010;
4'h5: LED[20:7] <= 7'b1101011;
4'h6: LED[20:7] <= 7'b1101111;
4'h7: LED[20:7] <= 7'b1010010;
4'h8: LED[20:7] <= 7'b1111111;
4'h9: LED[20:7] <= 7'b1111010;
4'hA: LED[20:7] <= 7'b1111110;
4'hB: LED[20:7] <= 7'b1111111;
4'hC: LED[20:7] <= 7'b1100101;
4'hD: LED[20:7] <= 7'b1110111;
4'hE: LED[20:7] <= 7'b1101101;
4'hF: LED[20:7] <= 7'b1101100;
default:LED[20:7] <= 7'bxxxxxxx;
endcase
end
always @ (time_out[15:8])
begin
case(time_out[15:8])
4'h0: LED[27:21] <= 7'b1110111;
4'h1: LED[27:21] <= 7'b0010010;
4'h2: LED[27:21] <= 7'b1011101;
4'h3: LED[27:21] <= 7'b1011011;
4'h4: LED[27:21] <= 7'b0111010;
4'h5: LED[27:21] <= 7'b1101011;
4'h6: LED[27:21] <= 7'b1101111;
4'h7: LED[27:21] <= 7'b1010010;
4'h8: LED[27:21] <= 7'b1111111;
4'h9: LED[27:21] <= 7'b1111010;
4'hA: LED[27:21] <= 7'b1111110;
4'hB: LED[27:21] <= 7'b1111111;
4'hC: LED[27:21] <= 7'b1100101;
4'hD: LED[27:21] <= 7'b1110111;
4'hE: LED[27:21] <= 7'b1101101;
4'hF: LED[27:21] <= 7'b1101100;
default:LED[27:21] <= 7'bxxxxxxx;
endcase
end
//------------------------------------------------------------------------------
reg I2Cclk =1; // I2C clock
FPGA LAB PROJECT Page 9
reg [8:0] clkclk=clock_div;// in order to divide50MHz clock
reg sda_int =1; // making SDA high in initial state
reg [15:0] outreg =0;
reg [3:0] I2C_counter=0; // for givingthe pointer address
reg [3:0] bitaddrs7=4'b0001;// initializingvia givingslaveaddress(1001000-0(R/W))
reg [3:0] Rxcount=0;
reg [3:0] slaveaddrs=0;//receivingdata from ds1307
/// ----------------------------------------------------------------------------
reg [2:0] state =Rxidle; // For State machine starting
assign scl =chk?1'b1:I2Cclk;// Generated Clock for I2C
assign time_out = outreg; // output from ds1307
assign sda =sda_int; // UsingSDA pin
assign ack_out=ack;
assign ack_count1=ack_count;
////------------------------------
always @ (posedge clk) /// --------------------- scl generation-----------------
begin
clkclk=clkclk - 1;
if(clkclk==0)
begin
I2Cclk=~I2Cclk;
clkclk =clock_div;
end
end
////.............................................................................
always @(posedge I2Cclk )
if(reset)
begin
chk <=1;
outreg <=0;
I2C_counter <=0;
bitaddrs7 <=4'b0001;
Rxcount <=0;
slaveaddrs <=0;
ack <=0;
ack_count <=0;
I2Cclk <=1;
sda_int <=1;
clkclk <=clock_div;
end
always @(posedge I2Cclk )
begin
////....................................state machine............................
case(state)
//-------------------------------------------------------------------------------
Rxidle:
begin
sda_int=1'b0; //---------------------start sda = 0
chk=1'b0; //---------------------
startscl
state =Rxstart;end
//-------------------------------------------------------------------------------
Rxstart:
FPGA LAB PROJECT Page 10
begin
case(bitaddrs7)
// initial frame1101000 Initializingslaveaddress ds1307
1: begin
sda_int =1'b1;//---------------------sda 1
bitaddrs7=bitaddrs7+1;end
2: begin sda_int =1'b1;//---------------------sda 1
bitaddrs7=bitaddrs7+1;end
3: begin sda_int =1'b0;//---------------------sda 0
bitaddrs7=bitaddrs7+1;end
4: begin sda_int =1'b1;//---------------------sda 1
bitaddrs7=bitaddrs7+1;end
5: begin sda_int =1'b0;//---------------------sda 0
bitaddrs7=bitaddrs7+1;end
6: begin sda_int =1'b0;//---------------------sda 0
bitaddrs7=bitaddrs7+1;end
7: begin sda_int =1'b0;//---------------------sda=0
bitaddrs7=bitaddrs7+1;end
8: begin sda_int =1'b0;
ack_count=2'b00;
bitaddrs7=bitaddrs7+1; // R/W bit
end
9: begin if(sda==1'b0)
ack[0]=1;
state=RxPointeraddr; // Checking ack
end
endcase
end
//---------------------------------------------------------------------------------
RxPointeraddr:
// Pointer Address 00000001 for minute register
begin
case(I2C_counter)
//---------------------sda = 0
0: begin sda_int =1'b0;
I2C_counter=I2C_counter+1;end
//---------------------sda = 0
1: begin sda_int =1'b0;
I2C_counter=I2C_counter+1;end
//---------------------sda = 0
2: begin sda_int =1'b0;
I2C_counter=I2C_counter+1; end
//---------------------sda = 0
3: begin sda_int =1'b0;
I2C_counter=I2C_counter+1; end
//---------------------sda = 0
4: begin sda_int =1'b0;
I2C_counter=I2C_counter+1;end
//---------------------sda = 0
5: begin sda_int =1'b0;
FPGA LAB PROJECT Page 11
I2C_counter=I2C_counter+1;end
//---------------------sda = 0
6: begin sda_int =1'b0;
I2C_counter=I2C_counter+1;end
//---------------------sda = 1
7: begin sda_int =1'b0;
ack_count=2'b01;
I2C_counter=I2C_counter+1;end
8: begin if(sda==1'b0)
ack[1]=1'b0; state=Rxstart1;end // after pointer register sda line
acknoledged by a low pulse
endcase
end
//-----------------------------------start+ slaveaddress byte---------------------
Rxstart1:
begin
case(slaveaddrs)
// initial frame1101000 Initializingslaveaddress includingstart (case0:)
0: begin sda_int=1'b0;
slaveaddrs=slaveaddrs+1;end
1: begin sda_int =1'b1;//---------------------sda 1
slaveaddrs=slaveaddrs+1;end
2: begin sda_int =1'b1;//---------------------sda 1
slaveaddrs=slaveaddrs+1;end
3: begin sda_int =1'b0;//---------------------sda 0
slaveaddrs=slaveaddrs+1;end
4: begin sda_int =1'b1;//---------------------sda 1
slaveaddrs=slaveaddrs+1;end
5: begin sda_int =1'b0;//---------------------sda 0
slaveaddrs=slaveaddrs+1;end
6: begin sda_int =1'b0;//---------------------sda 0
slaveaddrs=slaveaddrs+1;end
7: begin sda_int =1'b0;//---------------------sda=0
slaveaddrs=slaveaddrs+1;end
8: begin sda_int =1'b1;
slaveaddrs=slaveaddrs+1; // R/W bit high
ack_count=2'b10; end
9: begin if(sda==1'b0)
ack[2]=1;
state=Rxdata; // Checking ack
end
endcase
end
//----------------------------------receiving time --------------------------
Rxdata:
begin
case(Rxcount)
FPGA LAB PROJECT Page 12
0: begin outreg[15]=sda;
Rxcount=Rxcount+1;end
1: begin outreg[14]=sda;
Rxcount=Rxcount+1;end
2: begin outreg[13]=sda;
Rxcount=Rxcount+1;end
3: begin outreg[12]=sda;
Rxcount=Rxcount+1;end
4: begin outreg[11]=sda;
Rxcount=Rxcount+1;end
5: begin outreg[10]=sda;
Rxcount=Rxcount+1;end
6: begin outreg[9]=sda;
Rxcount=Rxcount+1;end
7: begin outreg[8]=sda;
Rxcount=Rxcount+1;
ack_count=2'b11; end
// low pulseacknoledgment from ds1307
8: begin outreg[7]=sda;
if(!sda) ack[2]=1;
Rxcount=Rxcount+1;end
9: begin outreg[6]=sda ;
Rxcount=Rxcount+1;end
10: begin outreg[5]=sda ;
Rxcount=Rxcount+1;end
11: begin outreg[4]=sda;
Rxcount=Rxcount+1;end
12: begin outreg[3]=sda;
Rxcount=Rxcount+1;end
13: begin outreg[2]=sda;
Rxcount=Rxcount+1;end
14: begin outreg[1]=sda;
Rxcount=Rxcount+1;end
15: begin sda_int=1'b0;
// checking lowpulseacknoledge frm ds1307
state=Rxstop; end
endcase
end
//-----------------------------------------------------------------------------------------
Rxstop:
begin chk=1; sda_int=1'b1;state= Rxidle; end
endcase
end
endmodule
FPGA LAB PROJECT Page 13
Hardware Implementation:
RTC is connected to Spartan 2 kit
and the result will be shown on seven segments and LED’s. Acknowledgements
will be shown on LED’s and Receive data will be shown on seven segments.
Conclusion:
We have interface RTC with FPGA kit using I2Cprotocol. They
are interface by using SCL and SDA lines. As FPGA havelimited input/output
pins so we cannotinterface parallel RTC, so the best way to interface RTC is to
use I2C protocolwith only 2 wires.
References:
https://www.sparkfun.com/products/12708
http://www.instructables.com/id/Real-Time-Clock-DS1302/
http://en.wikipedia.org/wiki/I%C2%B2C
http://www.digikey.com/catalog/en/partgroup/spartan-ii/14470

More Related Content

PPTX
8251 USART
PPTX
dual-port RAM (DPRAM)
PDF
DRAM Cell - Working and Read and Write Operations
PPT
PDF
Jtag presentation
PDF
ARM CORTEX M3 PPT
PPT
Switch level modeling
PPTX
Xilinx 4000 series
8251 USART
dual-port RAM (DPRAM)
DRAM Cell - Working and Read and Write Operations
Jtag presentation
ARM CORTEX M3 PPT
Switch level modeling
Xilinx 4000 series

What's hot (20)

PPTX
DIgital clock using verilog
PDF
Basics of Digital Design and Verilog
PPTX
Introduction to FPGAs
PPTX
Ec8791 arm 9 processor
PPTX
Study of vco_Voltage controlled Oscillator
PPT
PPTX
ARM Processors
PPTX
Hardware Software Codesign
PPTX
Fpga architectures and applications
PDF
8 bit full adder
DOCX
UNIT-III-DIGITAL SYSTEM DESIGN
PPTX
Architecture of 8051
PDF
Delays in verilog
PPTX
DESIGN AND SIMULATION OF DIFFERENT 8-BIT MULTIPLIERS USING VERILOG CODE BY SA...
PPSX
VLSI Testing Techniques
PDF
Programmable Logic Array(PLA) & Programmable Array Logic(PAL)
PPTX
Asic design flow
PDF
Two stage op amp design on cadence
DIgital clock using verilog
Basics of Digital Design and Verilog
Introduction to FPGAs
Ec8791 arm 9 processor
Study of vco_Voltage controlled Oscillator
ARM Processors
Hardware Software Codesign
Fpga architectures and applications
8 bit full adder
UNIT-III-DIGITAL SYSTEM DESIGN
Architecture of 8051
Delays in verilog
DESIGN AND SIMULATION OF DIFFERENT 8-BIT MULTIPLIERS USING VERILOG CODE BY SA...
VLSI Testing Techniques
Programmable Logic Array(PLA) & Programmable Array Logic(PAL)
Asic design flow
Two stage op amp design on cadence
Ad

Viewers also liked (20)

PPTX
Equipos de computo placa madre
DOCX
Low drift high impedance jfet dc voltmeter
PPT
FPGA Configuration
PDF
Real time image processing in fpga
PDF
снк передачи данных Atl186 ofdm-share
ODP
02 История Open-Source Hardware - Олег Садов
PPT
использование .Net framework
PPTX
динамическое управление частотой выборки ацп с помощью фапч
PPTX
DSP / Filters
PDF
Mastering FPGA Design through Debug, Adrian Hernandez, Xilinx
PPTX
2011 Никифоров А.А. доклад " Применение алгоритма DELAY AND MULTIPLY APPROACH...
PPT
04.02 Marsohod
PDF
MIPI DevCon 2016: Multiple MIPI CSI-2 Cameras Leveraging FPGAs
PPTX
Gps игры: как использовать смартфоны в обучении
PDF
A course in fuzzy systems and control by li xin wang solution manual
PPTX
FPGA Applications in Finance
PPTX
смартфон как физическая лаборатория
PDF
Fpga computing
PPT
DSP by FPGA
PDF
MIPI DevCon 2016: Implementing MIPI C-PHY
Equipos de computo placa madre
Low drift high impedance jfet dc voltmeter
FPGA Configuration
Real time image processing in fpga
снк передачи данных Atl186 ofdm-share
02 История Open-Source Hardware - Олег Садов
использование .Net framework
динамическое управление частотой выборки ацп с помощью фапч
DSP / Filters
Mastering FPGA Design through Debug, Adrian Hernandez, Xilinx
2011 Никифоров А.А. доклад " Применение алгоритма DELAY AND MULTIPLY APPROACH...
04.02 Marsohod
MIPI DevCon 2016: Multiple MIPI CSI-2 Cameras Leveraging FPGAs
Gps игры: как использовать смартфоны в обучении
A course in fuzzy systems and control by li xin wang solution manual
FPGA Applications in Finance
смартфон как физическая лаборатория
Fpga computing
DSP by FPGA
MIPI DevCon 2016: Implementing MIPI C-PHY
Ad

Similar to Real Time Clock Interfacing with FPGA (20)

PPT
Lesson 7+8.pptGroup5_TA2_.pptxGroup5_TA2_.pptx
PPTX
Part-2: Mastering microcontroller with embedded driver development
PDF
A meta model supporting both hardware and smalltalk-based execution of FPGA c...
PDF
PDF
PPT
PIC16F877A C Programming.ppt
DOCX
Study of Data sheet of 56824 DSP processors
PDF
2014 ii c08t-sbc pic para ecg
PPT
INTRODUCTION TO PIC MICROCONTROLLERS 16C6X
DOCX
project 3 full report
DOC
OV7670 Camera interfacing-with-arduino-microcontroller
PPTX
Lcd module interface with xilinx software using verilog
PDF
What will be quantization step size in numbers and in voltage for th.pdf
PPTX
Embedded systems design @ defcon 2015
PDF
DPRSG IC Design
PPT
Day 20.3 frame relay
PDF
4 Channel Relay Board 5V-Bluetooth Compatible for Arduino
PDF
Assembler4
PDF
MC13202.pdf
Lesson 7+8.pptGroup5_TA2_.pptxGroup5_TA2_.pptx
Part-2: Mastering microcontroller with embedded driver development
A meta model supporting both hardware and smalltalk-based execution of FPGA c...
PIC16F877A C Programming.ppt
Study of Data sheet of 56824 DSP processors
2014 ii c08t-sbc pic para ecg
INTRODUCTION TO PIC MICROCONTROLLERS 16C6X
project 3 full report
OV7670 Camera interfacing-with-arduino-microcontroller
Lcd module interface with xilinx software using verilog
What will be quantization step size in numbers and in voltage for th.pdf
Embedded systems design @ defcon 2015
DPRSG IC Design
Day 20.3 frame relay
4 Channel Relay Board 5V-Bluetooth Compatible for Arduino
Assembler4
MC13202.pdf

More from Mafaz Ahmed (20)

PPTX
INTERACTIVE FLOOR PROJECTION SYSTEM
PDF
Series and parallel operation of power devices
PDF
Series and parallel connection of igbt
PDF
Series and parallel connection of mosfet
PDF
Electronic Instrumentation and Measurement Solution Manual
PPTX
Project loon
PPTX
Internet of Things
PPTX
Interoperability among various Generations of Telecom Technologies
PPTX
Dc motor drive
PPTX
Energy Crisis, Different Energy Sources and Role of Power Electronics
PPTX
RADIO ON RADIO OVER FIBER EFFICIENT FRONTHAULING FOR SMALL CELLS AND MOVING C...
DOCX
Temperature Control Fan Using 8051 Microcontroller
PPTX
Project Management Project
DOCX
Speed Control of DC Motor
PDF
Tweety modelling in pro engineering
PDF
Truck modelling in pro engineering
PDF
Bicycle modelling in pro engineering
PDF
Laptop modelling in pro engineering
PDF
Chair modelling in pro engineering
PDF
Fsk modulation and demodulation
INTERACTIVE FLOOR PROJECTION SYSTEM
Series and parallel operation of power devices
Series and parallel connection of igbt
Series and parallel connection of mosfet
Electronic Instrumentation and Measurement Solution Manual
Project loon
Internet of Things
Interoperability among various Generations of Telecom Technologies
Dc motor drive
Energy Crisis, Different Energy Sources and Role of Power Electronics
RADIO ON RADIO OVER FIBER EFFICIENT FRONTHAULING FOR SMALL CELLS AND MOVING C...
Temperature Control Fan Using 8051 Microcontroller
Project Management Project
Speed Control of DC Motor
Tweety modelling in pro engineering
Truck modelling in pro engineering
Bicycle modelling in pro engineering
Laptop modelling in pro engineering
Chair modelling in pro engineering
Fsk modulation and demodulation

Recently uploaded (20)

PPTX
Lecture Notes Electrical Wiring System Components
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
web development for engineering and engineering
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Construction Project Organization Group 2.pptx
PPT
Mechanical Engineering MATERIALS Selection
DOCX
573137875-Attendance-Management-System-original
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Geodesy 1.pptx...............................................
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
PPT on Performance Review to get promotions
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPT
Project quality management in manufacturing
Lecture Notes Electrical Wiring System Components
CH1 Production IntroductoryConcepts.pptx
web development for engineering and engineering
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Construction Project Organization Group 2.pptx
Mechanical Engineering MATERIALS Selection
573137875-Attendance-Management-System-original
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Lesson 3_Tessellation.pptx finite Mathematics
Strings in CPP - Strings in C++ are sequences of characters used to store and...
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Geodesy 1.pptx...............................................
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPT on Performance Review to get promotions
CYBER-CRIMES AND SECURITY A guide to understanding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Operating System & Kernel Study Guide-1 - converted.pdf
Project quality management in manufacturing

Real Time Clock Interfacing with FPGA

  • 1. FPGA LAB PROJECT Page 1 International Islamic University Islamabad FPGA LAB PROJECT Real Time Clock Interfacing With FPGA Submitted by: Mafaz Ahmed 1882-F12D
  • 2. FPGA LAB PROJECT Page 2 TABLE OF CONTENTS Pages 1. Introduction .…………………………..(1) 2. Real Time Clock RTC module (DS 1307) …………….…….……....(1)  Description  Features  Circuit Diagram  Data Registers 3. I2Cprotocol …..…………………………………..(5)  Description  Features  Timing Diagram 4. FPGA KIT(SPARTAN2) ...…………………………………….(6)  Description  Features 5. Schematic ……………………………………….(6) 6. Verilog Code ………..…………………………….(7) 7. HardwareImplementation .…………………………………….(12) 8. Conclusion .…………………………………….(13) 9. References …………………………………….(13)
  • 3. FPGA LAB PROJECT Page 3 1.Introduction: A real-time clock (RTC) is a computer clock (most often in the formof an integrated circuit) that keeps track of the currenttime. Although the term often refers to the devices in personal computers, servers and embedded systems, RTCs arepresent in almost any electronic device which needs to keep accuratetime. Itfeatures the DS1307 serialreal-time clock (RTC). Itis a low-power, fullbinary-coded decimal (BCD) clock/calendar with programmablesquare-waveoutputsignal. Board uses I2C interface for communication, and can only use5V power supply. Board features a 3V/230mA lithiumbattery as a backup power supply which ensures that timekeeping operation continues even when the main power supply goes OFF. 2.Real Time Clock RTC module (DS 1307): Description: RTC DS 1307 is a great battery-backed real time clock (RTC) that allows your microcontroller projectto keep track of time even if it is reprogrammed, or if the power is lost. Perfect for data logging, clock-building, time stamping, timers and alarms, etc. The DS1307 is the mostpopular RTC, and works bestwith 5V-based chips.
  • 4. FPGA LAB PROJECT Page 4 Features:  Two wireI2 Cinterface  Hour : Minutes : Seconds AM/PM  Day Month, Date - Year  Leap year compensation  Accurate calendar up to year 2100  Battery backup included  1Hz output pin  56 Bytes of Non-volatile memory available to user  0x68 I2 CAddress Circuit Diagram:
  • 5. FPGA LAB PROJECT Page 5 DATA Registers: 3. I2C Protocol: Description: I²Cuses only two bidirectional open-drain lines, Serial Data Line (SDA) and Serial Clock Line (SCL), pulled up with resistors. Typicalvoltages used are +5 V or +3.3 V although systems with other voltages are permitted. The I²C reference design has a 7-bit or a 10-bit (depending on the device used) address space. Common I²Cbus speeds are the 100 Kbit/s standard mode and the 10 Kbit/s low-speed mode. Features:   The data signal(SDA) is a bidirectional line, which can be read and written, by the master and the slave.  The communication between a master and a slave begins with a START condition followed by the slaveaddress to be reached, one read/write bit, a bit of recognition that can be ACK (if the communication was successful) or NACK (if the communication was unsuccessfulor theend of the messageis set by the master), the 8 bits of data to send or to receive, the ACK or NACK bit, and finishes with a STOP condition or a condition Repeated START.
  • 6. FPGA LAB PROJECT Page 6 Timing Diagram: 4.FPGA SPARTAN 2 KIT: Description: The Spartan 2 family of FPGAs havea regular, flexible, programmable architecture of ConfigurableLogic Blocks (CLBs), surrounded by a perimeter of programmableInput/outputBlocks (IOBs). Features:  50MHz SMD crystaloscillator  JTAG-programmable  9-pin RS-232 SerialPort  4-digit seven-segmentdisplay  8 individual LEDs  8 slide switches 5. Schematic: Idle state s Start Communication Write Pointer Address Read Start from slave Stop Communication Receive Data
  • 7. FPGA LAB PROJECT Page 7 6. Verilog Code: module I2C( inoutsda,output scl,output[27:0]out_data,input clk,output[2:0] ack_out, output [1:0] ack_count1,input reset); parameter clock_div=500;// I2C clock 50MHz/(500)=100KHz //---------------------------I2C State MachineVariables---------------------- parameter Rxidle=0; parameter Rxstart =1; parameter RxPointeraddr=2; parameter Rxstart1 =3; parameter Rxdata =4; parameter Rxstop =5; //----------------------------------------------------------------------------- reg chk=0; reg [2:0] ack; reg [1:0] ack_count=0; reg [27:0] LED; assign out_data=LED; wire [15:0] time_out; always @ (time_out[3:0]) begin case(time_out[3:0]) 4'h0: LED[6:0] <= 7'b1110111; 4'h1: LED[6:0] <= 7'b0010010; 4'h2: LED[6:0] <= 7'b1011101; 4'h3: LED[6:0] <= 7'b1011011; 4'h4: LED[6:0] <= 7'b0111010; 4'h5: LED[6:0] <= 7'b1101011; 4'h6: LED[6:0] <= 7'b1101111; 4'h7: LED[6:0] <= 7'b1010010; 4'h8: LED[6:0] <= 7'b1111111; 4'h9: LED[6:0] <= 7'b1111010; 4'hA: LED[6:0] <= 7'b1111110; 4'hB: LED[6:0] <= 7'b1111111; 4'hC: LED[6:0] <= 7'b1100101; 4'hD: LED[6:0] <= 7'b1110111; 4'hE: LED[6:0] <= 7'b1101101; 4'hF: LED[6:0] <= 7'b1101100; default:LED[6:0] <= 7'bxxxxxxx; endcase end always @ (time_out[7:4]) begin case(time_out[7:4]) 4'h0: LED[13:6] <= 7'b1110111; 4'h1: LED[13:6] <= 7'b0010010; 4'h2: LED[13:6] <= 7'b1011101; 4'h3: LED[13:6] <= 7'b1011011; 4'h4: LED[13:6] <= 7'b0111010; 4'h5: LED[13:6] <= 7'b1101011; 4'h6: LED[13:6] <= 7'b1101111; 4'h7: LED[13:6] <= 7'b1010010;
  • 8. FPGA LAB PROJECT Page 8 4'h8: LED[13:6] <= 7'b1111111; 4'h9: LED[13:6] <= 7'b1111010; 4'hA: LED[13:6] <= 7'b1111110; 4'hB: LED[13:6] <= 7'b1111111; 4'hC: LED[13:6] <= 7'b1100101; 4'hD: LED[13:6] <= 7'b1110111; 4'hE: LED[13:6] <= 7'b1101101; 4'hF: LED[13:6] <= 7'b1101100; default:LED[13:6] <= 7'bxxxxxxx; endcase end always @ (time_out[11:8]) begin case(time_out[11:8]) 4'h0: LED[20:7] <= 7'b1110111; 4'h1: LED[20:7] <= 7'b0010010; 4'h2: LED[20:7] <= 7'b1011101; 4'h3: LED[20:7] <= 7'b1011011; 4'h4: LED[20:7] <= 7'b0111010; 4'h5: LED[20:7] <= 7'b1101011; 4'h6: LED[20:7] <= 7'b1101111; 4'h7: LED[20:7] <= 7'b1010010; 4'h8: LED[20:7] <= 7'b1111111; 4'h9: LED[20:7] <= 7'b1111010; 4'hA: LED[20:7] <= 7'b1111110; 4'hB: LED[20:7] <= 7'b1111111; 4'hC: LED[20:7] <= 7'b1100101; 4'hD: LED[20:7] <= 7'b1110111; 4'hE: LED[20:7] <= 7'b1101101; 4'hF: LED[20:7] <= 7'b1101100; default:LED[20:7] <= 7'bxxxxxxx; endcase end always @ (time_out[15:8]) begin case(time_out[15:8]) 4'h0: LED[27:21] <= 7'b1110111; 4'h1: LED[27:21] <= 7'b0010010; 4'h2: LED[27:21] <= 7'b1011101; 4'h3: LED[27:21] <= 7'b1011011; 4'h4: LED[27:21] <= 7'b0111010; 4'h5: LED[27:21] <= 7'b1101011; 4'h6: LED[27:21] <= 7'b1101111; 4'h7: LED[27:21] <= 7'b1010010; 4'h8: LED[27:21] <= 7'b1111111; 4'h9: LED[27:21] <= 7'b1111010; 4'hA: LED[27:21] <= 7'b1111110; 4'hB: LED[27:21] <= 7'b1111111; 4'hC: LED[27:21] <= 7'b1100101; 4'hD: LED[27:21] <= 7'b1110111; 4'hE: LED[27:21] <= 7'b1101101; 4'hF: LED[27:21] <= 7'b1101100; default:LED[27:21] <= 7'bxxxxxxx; endcase end //------------------------------------------------------------------------------ reg I2Cclk =1; // I2C clock
  • 9. FPGA LAB PROJECT Page 9 reg [8:0] clkclk=clock_div;// in order to divide50MHz clock reg sda_int =1; // making SDA high in initial state reg [15:0] outreg =0; reg [3:0] I2C_counter=0; // for givingthe pointer address reg [3:0] bitaddrs7=4'b0001;// initializingvia givingslaveaddress(1001000-0(R/W)) reg [3:0] Rxcount=0; reg [3:0] slaveaddrs=0;//receivingdata from ds1307 /// ---------------------------------------------------------------------------- reg [2:0] state =Rxidle; // For State machine starting assign scl =chk?1'b1:I2Cclk;// Generated Clock for I2C assign time_out = outreg; // output from ds1307 assign sda =sda_int; // UsingSDA pin assign ack_out=ack; assign ack_count1=ack_count; ////------------------------------ always @ (posedge clk) /// --------------------- scl generation----------------- begin clkclk=clkclk - 1; if(clkclk==0) begin I2Cclk=~I2Cclk; clkclk =clock_div; end end ////............................................................................. always @(posedge I2Cclk ) if(reset) begin chk <=1; outreg <=0; I2C_counter <=0; bitaddrs7 <=4'b0001; Rxcount <=0; slaveaddrs <=0; ack <=0; ack_count <=0; I2Cclk <=1; sda_int <=1; clkclk <=clock_div; end always @(posedge I2Cclk ) begin ////....................................state machine............................ case(state) //------------------------------------------------------------------------------- Rxidle: begin sda_int=1'b0; //---------------------start sda = 0 chk=1'b0; //--------------------- startscl state =Rxstart;end //------------------------------------------------------------------------------- Rxstart:
  • 10. FPGA LAB PROJECT Page 10 begin case(bitaddrs7) // initial frame1101000 Initializingslaveaddress ds1307 1: begin sda_int =1'b1;//---------------------sda 1 bitaddrs7=bitaddrs7+1;end 2: begin sda_int =1'b1;//---------------------sda 1 bitaddrs7=bitaddrs7+1;end 3: begin sda_int =1'b0;//---------------------sda 0 bitaddrs7=bitaddrs7+1;end 4: begin sda_int =1'b1;//---------------------sda 1 bitaddrs7=bitaddrs7+1;end 5: begin sda_int =1'b0;//---------------------sda 0 bitaddrs7=bitaddrs7+1;end 6: begin sda_int =1'b0;//---------------------sda 0 bitaddrs7=bitaddrs7+1;end 7: begin sda_int =1'b0;//---------------------sda=0 bitaddrs7=bitaddrs7+1;end 8: begin sda_int =1'b0; ack_count=2'b00; bitaddrs7=bitaddrs7+1; // R/W bit end 9: begin if(sda==1'b0) ack[0]=1; state=RxPointeraddr; // Checking ack end endcase end //--------------------------------------------------------------------------------- RxPointeraddr: // Pointer Address 00000001 for minute register begin case(I2C_counter) //---------------------sda = 0 0: begin sda_int =1'b0; I2C_counter=I2C_counter+1;end //---------------------sda = 0 1: begin sda_int =1'b0; I2C_counter=I2C_counter+1;end //---------------------sda = 0 2: begin sda_int =1'b0; I2C_counter=I2C_counter+1; end //---------------------sda = 0 3: begin sda_int =1'b0; I2C_counter=I2C_counter+1; end //---------------------sda = 0 4: begin sda_int =1'b0; I2C_counter=I2C_counter+1;end //---------------------sda = 0 5: begin sda_int =1'b0;
  • 11. FPGA LAB PROJECT Page 11 I2C_counter=I2C_counter+1;end //---------------------sda = 0 6: begin sda_int =1'b0; I2C_counter=I2C_counter+1;end //---------------------sda = 1 7: begin sda_int =1'b0; ack_count=2'b01; I2C_counter=I2C_counter+1;end 8: begin if(sda==1'b0) ack[1]=1'b0; state=Rxstart1;end // after pointer register sda line acknoledged by a low pulse endcase end //-----------------------------------start+ slaveaddress byte--------------------- Rxstart1: begin case(slaveaddrs) // initial frame1101000 Initializingslaveaddress includingstart (case0:) 0: begin sda_int=1'b0; slaveaddrs=slaveaddrs+1;end 1: begin sda_int =1'b1;//---------------------sda 1 slaveaddrs=slaveaddrs+1;end 2: begin sda_int =1'b1;//---------------------sda 1 slaveaddrs=slaveaddrs+1;end 3: begin sda_int =1'b0;//---------------------sda 0 slaveaddrs=slaveaddrs+1;end 4: begin sda_int =1'b1;//---------------------sda 1 slaveaddrs=slaveaddrs+1;end 5: begin sda_int =1'b0;//---------------------sda 0 slaveaddrs=slaveaddrs+1;end 6: begin sda_int =1'b0;//---------------------sda 0 slaveaddrs=slaveaddrs+1;end 7: begin sda_int =1'b0;//---------------------sda=0 slaveaddrs=slaveaddrs+1;end 8: begin sda_int =1'b1; slaveaddrs=slaveaddrs+1; // R/W bit high ack_count=2'b10; end 9: begin if(sda==1'b0) ack[2]=1; state=Rxdata; // Checking ack end endcase end //----------------------------------receiving time -------------------------- Rxdata: begin case(Rxcount)
  • 12. FPGA LAB PROJECT Page 12 0: begin outreg[15]=sda; Rxcount=Rxcount+1;end 1: begin outreg[14]=sda; Rxcount=Rxcount+1;end 2: begin outreg[13]=sda; Rxcount=Rxcount+1;end 3: begin outreg[12]=sda; Rxcount=Rxcount+1;end 4: begin outreg[11]=sda; Rxcount=Rxcount+1;end 5: begin outreg[10]=sda; Rxcount=Rxcount+1;end 6: begin outreg[9]=sda; Rxcount=Rxcount+1;end 7: begin outreg[8]=sda; Rxcount=Rxcount+1; ack_count=2'b11; end // low pulseacknoledgment from ds1307 8: begin outreg[7]=sda; if(!sda) ack[2]=1; Rxcount=Rxcount+1;end 9: begin outreg[6]=sda ; Rxcount=Rxcount+1;end 10: begin outreg[5]=sda ; Rxcount=Rxcount+1;end 11: begin outreg[4]=sda; Rxcount=Rxcount+1;end 12: begin outreg[3]=sda; Rxcount=Rxcount+1;end 13: begin outreg[2]=sda; Rxcount=Rxcount+1;end 14: begin outreg[1]=sda; Rxcount=Rxcount+1;end 15: begin sda_int=1'b0; // checking lowpulseacknoledge frm ds1307 state=Rxstop; end endcase end //----------------------------------------------------------------------------------------- Rxstop: begin chk=1; sda_int=1'b1;state= Rxidle; end endcase end endmodule
  • 13. FPGA LAB PROJECT Page 13 Hardware Implementation: RTC is connected to Spartan 2 kit and the result will be shown on seven segments and LED’s. Acknowledgements will be shown on LED’s and Receive data will be shown on seven segments. Conclusion: We have interface RTC with FPGA kit using I2Cprotocol. They are interface by using SCL and SDA lines. As FPGA havelimited input/output pins so we cannotinterface parallel RTC, so the best way to interface RTC is to use I2C protocolwith only 2 wires. References: https://www.sparkfun.com/products/12708 http://www.instructables.com/id/Real-Time-Clock-DS1302/ http://en.wikipedia.org/wiki/I%C2%B2C http://www.digikey.com/catalog/en/partgroup/spartan-ii/14470