Saturday, April 3, 2010

Reloj 00-24 veriloco


Esto no tiene precio :)

* Programar en codigo verilog en Quartus
* Simular, simular y simular
* Bajar el programa al FPGA Ciclone II EP2C35F672C6 (Altera DE2)

Helo aquí:

GENERADOR DEL PULSO CADA SEGUNDO
(Medio ciclo de trabajo)

module pulso_segundo(clk,reset,q1);
input clk,reset;
output reg q1;
wire Carry_seg;
reg [24:0] q; // registro de 25

always @ (negedge reset or posedge clk)
if (!reset)
q <= 0;
else if(q == 50000) // si llegas a 25 millones, entonces q=0
q <= 0;
else
q <= q + 1'b1 ; // si no has llegado a 25 mill, aumenta q

assign Carry_seg = q == 50000 ? 1'b1:1'b0; // poner carry_seg a 1, cuando q sea igual a 25 mill

always @ ( posedge clk, negedge reset)
if (!reset)
q1 <= 0;
else if (Carry_seg) // si carry_seg es igual a 1, entra y cambia q1 al estado contrario en el que estaba
// carry_seg va a ser igual a 1 cada 25 mill, o sea que q1 va a cambiar cada 25 mill
q1 <= ! q1;
else
q1 <= q1;
endmodule

CONTADORES SEGUNDOS, MINUTOS Y HORAS
module contadores(reset,en,pulso_segundo,rs,OE,contadorseg,OEmin,contadormin,OEhr,contadorhr);
input reset,en,rs;
output reg [5:0]contadorseg,contadormin;
output reg [4:0]contadorhr;
input pulso_segundo;
output OE,OEmin,OEhr;

always @(posedge pulso_segundo ,negedge reset)
begin
if(!reset)
contadorseg<= 0;
else if (en)
begin
if(!rs)contadorseg<=0;
else if (OE==1)
contadorseg<=0;
else
contadorseg = contadorseg + 1'b1;
end
end

assign OE = (contadorseg==59)?1'b1:1'b0;

/////////////////////////////////////////////////

always @(posedge pulso_segundo ,negedge reset)
begin
if(!reset)
contadormin<= 0;
else if (en)
begin
if(!rs)contadormin<=0;
else if (OE==1)
begin
if (OEmin==1)
contadormin<=0;
else
contadormin = contadormin + 1'b1;
end
end
end

assign OEmin = (contadormin==59 && OE)?1'b1:1'b0;

/////////////////////////////////////////////////

always @(posedge pulso_segundo ,negedge reset)
begin
if(!reset)
contadorhr<= 0;
else if (en)
begin
if(!rs)contadorhr<=0;
else if (OEmin==1)
begin
if (OEhr==1)
contadorhr<=0;
else
contadorhr = contadorhr + 1'b1;
end
end
end

assign OEhr = (contadorhr==23 && OEmin)?1'b1:1'b0;

endmodule

CONVIERTE BINARIO A BCD

module binarytobcd(binario,bcd0_out,bcd1_out); // bcd1_out y bcd2_out seran mis salidas del modulo
input [5:0] binario;
reg [5:0] bcd0,bcd1; // cuando van dentro del always tienen que llevar reg
output [3:0] bcd0_out,bcd1_out; // declaración de las variables que si van a salir

always @(binario) // Todo esto va combinacional, no necesita reset o clock
begin
if (binario>=60)
begin
bcd1<=4'b0000; //mas significativo
bcd0<=4'b0000; //menos significativo
end
else if (binario>=50)
begin
bcd1<=4'b0101; // En todos los casos se forza a bcd1 y bcd2 a ser de 4 bits para que puedan ir al decoder
bcd0<=binario-6'b110010;
end
else if (binario>=40)
begin
bcd1<=4'b0100;
bcd0<=binario-6'b101000;
end
else if (binario>=30)
begin
bcd1<=4'b0011;
bcd0<=binario-6'b011110;
end
else if (binario>=20)
begin
bcd1<=4'b0010;
bcd0<=binario-6'b010100;
end
else if (binario>=10)
begin
bcd1<=4'b0001;
bcd0<=binario-6'b001010;
end
else
begin
bcd1<=4'b0000;
bcd0<=binario;
end
end

assign bcd1_out=bcd1[3:0]; // se tuvo que hacer esta asignacion para que finalmente bcd1 se quede con bcd1_out
assign bcd0_out=bcd0[3:0];

endmodule

DECODIFICADOR DE BCD A DISPLAY SIETE SEGMENTOS

module decoder(binary_in,decoder_out);
input [3:0] binary_in; // 4 bit binary input
output reg [6:0] decoder_out; // 6-bit out

always @(binary_in)
begin
case(binary_in)
0: decoder_out = 7'b1000000;
1: decoder_out = 7'b1111001;
2: decoder_out = 7'b0100100;
3: decoder_out = 7'b0110000;
4: decoder_out = 7'b0011001;
5: decoder_out = 7'b0010010;
6: decoder_out = 7'b0000010;
7: decoder_out = 7'b1111000;
8: decoder_out = 7'b0000000;
9: decoder_out = 7'b0010000;
default decoder_out=7'b0000110;
endcase
end
endmodule

TODO EL RELOJ CON INSTANCIACIONES
(La maravilla de las instanciaciones)

module reloj_verilog(clk,reset,en,rs,OE,decoder_out0,decoder_out1,OEmin,decoder_out2,decoder_out3,OEhr,decoder_out4,decoder_out5);

input clk,reset,en,rs;
output OE,OEmin,OEhr;
output [6:0] decoder_out0,decoder_out1,decoder_out2,decoder_out3,decoder_out4,decoder_out5;

wire q1;
wire [5:0]contadorseg,contadormin;
wire [4:0]contadorhr;
wire [3:0]bcd0,bcd1,bcd2,bcd3,bcd4,bcd5;
wire OE,OEmin,OEhr;

pulso_segundo segundero (clk,reset,q1); // No lleva reset
contadores todoscontadores (reset,en,q1,rs,OE,contadorseg,OEmin,contadormin,OEhr,contadorhr);

binarytobcd btobcdseg (contadorseg,bcd0,bcd1);
decoder D0 (bcd0,decoder_out0);
decoder D1 (bcd1,decoder_out1);

binarytobcd btobcdmin (contadormin,bcd2,bcd3);
decoder D2 (bcd2,decoder_out2);
decoder D3 (bcd3,decoder_out3);

binarytobcd btobcdhr (contadorhr,bcd4,bcd5);
decoder D4 (bcd4,decoder_out4);
decoder D5 (bcd5,decoder_out5);

MODULITO PARA DESCARGAR AL FPGA

module descargas(CLOCK_50,SW,HEX0,HEX1,HEX2,HEX3,HEX4,HEX5,LEDR);

input CLOCK_50;
input [2:0]SW;
output [6:0]HEX1,HEX0,HEX2,HEX3,HEX4,HEX5;
output [2:0]LEDR;
reloj_verilog R1(CLOCK_50,SW[2],SW[1],SW[0],LEDR[0],HEX0,HEX1,LEDR[1],HEX2,HEX3,LEDR[2],HEX4,HEX5);
endmodule


ASIGNAR LOS PINES
Usar el documento DE2_pin_assignments.csv
Assignments/Import assignments
LISTO!!!!!!

No comments: