Electronics Lab Philippines

RTC VHDL code

RTC VHDL code
« on: November 12, 2018, 11:12:17 AM »

youtube.com/watch?v=JGRVCtmIEmk



Code: [Select]
----------------------------------------------------------------------------------
-- Company: ACCESS Inc.
-- Engineer: Noel A. Rios
--
-- Create Date:    15:07:30 07/13/2014
-- Design Name:
-- Module Name:    rtc - Behavioral
-- Project Name:
-- Target Devices: Cool runner CPLD
-- Tool versions: WeBpack ISE
-- Description:
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
-- Real time clock with seven segment display, am,pm,colon
--         g                g->6   a->0
--         ---
--       e| f | a           0-> 1011111
--         ---              1-> 0000011
--       d|   | b           2-> 1101101
--         ---              3-> 1100111
--          c               4-> 1010011
--                          5-> 1110110
--                          6-> 1111110
--                          7-> 0100011
--     8-> 1111111
--                          9-> 1110011




----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
use IEEE.NUMERIC_STD.ALL;

-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity rtc is
Port ( reset : in  STD_LOGIC;
           clk : in  STD_LOGIC;
           colon : out bit;
           A1 : out  STD_LOGIC;
           A2 : out  STD_LOGIC;
           A3 : out  STD_LOGIC;
           A4 : out  STD_LOGIC;
           segment: out  STD_LOGIC_VECTOR (6 downto 0));

end rtc;

architecture Behavioral of rtc is


begin

process(reset)
variable tick:integer range 0 to 32768;
variable seconds:integer range 0 to 60;
variable minutesones:integer range 0 to 9;
variable minutestens:integer range 0 to 5;   
variable hoursones:integer range 0 to 13;
variable hourstens:integer range 0 to 1;

begin
if(reset='1') then
tick:=0;
seconds:=0;
minutesones:=0;
minutestens:=0;
hoursones:=0;
hourstens:=0;
end if;

end process;



process(clk)
variable scan:integer range 0 to 5;
variable blink:boolean;
variable tick:integer range 0 to 32768;
variable seconds:integer range 0 to 60;
variable minutesones:integer range 0 to 9;
variable minutestens:integer range 0 to 5;   
variable hoursones:integer range 0 to 13;
variable hourstens:integer range 0 to 1;
variable hourstemp:integer range 0 to 9;

begin

if(clk' event and clk='1') then

scan:=scan+1;
if(scan=5) then
scan:=0;
end if;

tick:=tick+1;
if (tick=32768) then
tick:=0;
seconds:=seconds+1;
blink:=not blink;
if(blink) then
colon<='1';
else colon<='0';
end if;
end if;
if(seconds=60) then
seconds:=0;
minutesones:=minutesones+1;
   end if;

if(minutesones>9) then
minutesones:=0;
minutestens:=minutestens+1;
end if;
if(minutestens>5) then
minutestens:=0;
hoursones:=hoursones+1;
end if;
if(hoursones>12) then
hoursones:=0;
hourstens:=hourstens+1;
end if;
   if(hourstens>1)  then
hourstens:=0;
end if;

if (scan=1) then
A1<='1';
A2<='0';
A3<='0';
A4<='0';
case(minutesones) is
when 0=>segment<="1011111";
when 1=>segment<="0000011";
when 2=>segment<="1101101";
when 3=>segment<="1100111";
when 4=>segment<="1010011";
when 5=>segment<="1110110";
when 6=>segment<="1111110";
when 7=>segment<="0100011";
when 8=>segment<="1111111";
when 9=>segment<="1110011";
end case;
end if;


if (scan=2) then
A1<='0';
A2<='1';
A3<='0';
A4<='0';
case(minutestens) is
when 0=>segment<="1011111";
when 1=>segment<="0000011";
when 2=>segment<="1101101";
when 3=>segment<="1100111";
when 4=>segment<="1010011";
when 5=>segment<="1110110";
end case;
end if;

if (scan=3) then
A1<='0';
A2<='0';
A3<='1';
A4<='0';

if(hoursones>9) then
hourstemp:=hoursones-10;
case(hourstemp) is
when 0=>segment<="1011111";
when 1=>segment<="0000011";
when 2=>segment<="1101101";
when 3=>segment<="1100111";
when 4=>segment<="1010011";
when 5=>segment<="1110110";
when 6=>segment<="1111110";
when 7=>segment<="0100011";
when 8=>segment<="1111111";
when 9=>segment<="1110011";
end case;
end if;


if (scan=4) then
A1<='0';
A2<='0';
A3<='0';
A4<='1';
case(hourstens) is
when 0=>segment<="0000000";
when 1=>segment<="0000011";
end case;
end if;


end if;
end if;

end process;
end Behavioral;