Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////////////
- // This file is knight_rider
- // Creation date is 00:26:37 10/27/2020 by Miguel Angel Rodriguez Jodar
- // (c)2020 Miguel Angel Rodriguez Jodar. ZXProjects
- //
- // This core is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This core is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this core. If not, see <https://www.gnu.org/licenses/>.
- //
- // All copies of this file must keep this notice intact.
- //
- //////////////////////////////////////////////////////////////////////////////////
- `timescale 1ns / 1ns
- `default_nettype none
- module knight_rider (
- input wire clk,
- output wire [15:0] led
- );
- reg [26:0] count_per_shift = 'd0;
- reg [26:0] count_per_fade = 'd0;
- reg [26:0] count_per_pwm = 'd0;
- parameter [26:0] FREQ_CLK = 'd100_000_1000;
- parameter [26:0] FREQ_SHIFT = 'd10;
- parameter [26:0] FREQ_FADEOUT = 'd100;
- parameter [26:0] FREQ_PWM = 25 * FREQ_CLK / 128;
- localparam [26:0] PERIOD_SHIFT = (FREQ_CLK / FREQ_SHIFT)-1;
- localparam [26:0] PERIOD_FADEOUT = (FREQ_CLK / FREQ_FADEOUT)-1;
- localparam [26:0] PERIOD_PWM = (FREQ_CLK / FREQ_PWM)-1;
- always @(posedge clk) begin
- if (count_per_shift == PERIOD_SHIFT)
- count_per_shift <= 'd0;
- else
- count_per_shift <= count_per_shift + 'd1;
- if (count_per_fade == PERIOD_FADEOUT)
- count_per_fade <= 'd0;
- else
- count_per_fade <= count_per_fade + 'd1;
- if (count_per_pwm == PERIOD_PWM)
- count_per_pwm <= 'd0;
- else
- count_per_pwm <= count_per_pwm + 'd1;
- end
- wire enable_shift = (count_per_shift == 'd0);
- wire enable_fadeout = (count_per_fade == 'd0);
- wire enable_pwm = (count_per_pwm == 'd0);
- reg [15:0] estado_led = 16'b1000_0000_0000_0000;
- reg sentido = 1'b0; // 0=izq-der, 1=der-izq
- genvar i;
- generate
- for (i=0; i<=15; i=i+1) begin : controlador_led
- ledpwm l (clk, enable_fadeout, enable_pwm, estado_led[i], led[i]);
- end
- endgenerate
- always @(posedge clk) begin
- if (enable_shift == 1'b1) begin
- if (sentido == 1'b0)
- estado_led <= {1'b0, estado_led[15:1]};
- else
- estado_led <= {estado_led[14:0], 1'b0};
- if (estado_led[14] == 1'b1 && sentido == 1'b1 || estado_led[1] == 1'b1 && sentido == 1'b0)
- sentido <= ~sentido;
- end
- end
- endmodule
- module ledpwm (
- input wire clk,
- input wire clkefade,
- input wire clkepwm,
- input wire pulso_encendido,
- output wire led
- );
- reg [7:0] cnt = 'h00;
- reg [7:0] brillo = 'h00;
- reg [7:0] lut[0:255];
- reg [7:0] indxlut = 'd255;
- integer i;
- localparam NUMERO_E = 2.7182818284590452353602874713527;
- initial begin
- for (i=0; i<256; i=i+1) begin
- lut[i] = 255*(NUMERO_E ** (1-i/40.0))/NUMERO_E;
- end
- // Grafica de la función generada:
- // http://fooplot.com/?lang=es#W3sidHlwZSI6MCwiZXEiOiJlXigxLXgvNDApKjI1NS9lIiwiY29sb3IiOiIjMDAwMDAwIn0seyJ0eXBlIjoxMDAwLCJ3aW5kb3ciOlsiMCIsIjI1NSIsIjAiLCIyNTUiXSwiZ3JpZCI6WyI1IiwiNSJdfV0-
- end
- always @(posedge clk) begin
- if (clkepwm == 1'b1)
- cnt <= cnt + 'h01;
- end
- always @(posedge clk) begin
- brillo <= lut[indxlut];
- if (pulso_encendido == 1'b1)
- indxlut <= 'h00;
- else if (clkefade == 1'b1 && indxlut != 'd255) begin
- indxlut <= indxlut + 'h01;
- end
- end
- assign led = (brillo > cnt || brillo == 'hFF)? 1'b1: 1'b0;
- endmodule
Add Comment
Please, Sign In to add comment