Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Flashlight problem please help me

Status
Not open for further replies.

Beenmagic

Newbie level 1
Newbie level 1
Joined
Jan 15, 2014
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
7
Hello,
I need help with one program, which i am trying to make for Altera DE2, I am just basic programmer in verilog and I would be really happy if you'll help me. My problem is so I don't know how to make so when I push button for >= MAX_TIME so I need set diods on 18'b000000000000000000 and so on fourth mode I need to make cyclic turning on diods from left (only on fifth diods from right). Please could you tell me how to make it? Here is my code, but maybe it could be made better (especially mode 5 "Kit")
Code:
module nios_DE2_demo
	( 
		CLOCK_50,	
		KEY,			
		HEX0,			
		LEDR
	);


input				CLOCK_50;
input		[3:0]	KEY;
output		[6:0]	HEX0;
output reg	[17:0]	LEDR;

reg		[2:0]		mode;
reg		[2:0]		pmode; 
parameter LAST_MODE = 3'b101;


reg		[7:0]		delay;
reg					switching;	
parameter MAX_DELAY = 18'b111111111111111111;

assign switchbtn = KEY[0];

reg switchBtnPressed;

Display(mode, HEX0);

reg		[3:0]		p_delay;
reg					countdown;

reg					backward;

always@(posedge CLOCK_50) begin
	
	if(!switchbtn) begin
		if(!switchBtnPressed) begin
		
			if(delay >= MAX_DELAY) begin
				mode = 8;
			end
			else begin
				if(mode == LAST_MODE)
					mode = 0;
				else begin
					mode = mode + 1;
				end
			end
			
			switchBtnPressed = 1;
		end
	end
	else
		switchBtnPressed = 0;
end

clk_div divider1
(
	.CLK(CLOCK_50) ,	
	.RST(switchbtn) ,	
	.CLK_DIV(clk_div) 	// output  CLK_DIV_sig
);
defparam divider1.divider = 4_000_000;

always@(posedge clk_div) begin
	delay = delay + 1;
	case(mode)
	
		// constant lighting
	
		3'b000: begin
			LEDR = 18'b000000000000000000; 
			delay = 0;
			pmode = mode;
		end
		
		3'b001: begin
			LEDR = 18'b11111;
			pmode = mode;
		end
		
		// flashing
		
		3'b010: begin
			if(pmode == 3'b001) begin
				p_delay = 0;
				pmode = mode;
				LEDR = 18'b11111;
			end
			
			if(p_delay == 4'b1111) begin
				LEDR = 18'b00000;
				countdown = 1;
			end
			
			if(p_delay == 0) begin
				LEDR = 18'b11111;
				countdown = 0;
			end
			
			if(countdown)
				p_delay = p_delay - 1;
			else
				p_delay = p_delay + 1;
		end
		
		// different speed of flashing
		
		3'b011:
			if(LEDR == 18'b11111) begin
				LEDR = 18'b00000;
				pmode = mode;
			end
			else begin
				LEDR = 18'b11111;
				pmode = mode;
			end
		
		// gradual switching on
		3'b100:
				if(LEDR != 18'b00000) begin
					LEDR = 5'b00001;
					LEDR = LEDR << LEDR + 1'b1;
					LEDR = 5'b10000;
				end
				else begin
					LEDR = LEDR << LEDR - 1'b1;
					LEDR = 5'b00001;
				end
					
			
		// KIT - night rider effect
			
		3'b101:
			if(pmode == 3'b100) begin
				LEDR = 5'b10000;
				pmode = mode;
				backward = 1;
			end
			else begin
				if((LEDR == 5'b10000) || (LEDR == 5'b00000)) begin
					if(backward == 0) begin
						backward = 1;
						if (LEDR == 1'b1) begin
							LEDR = LEDR << 1;
						end
						else begin
						LEDR = LEDR >> 1;
						end
					end
					else begin
						LEDR = LEDR << 1; 
						backward = 0;
						LEDR = 1'b1;
					end
				end
				else begin
					if(backward == 0) LEDR = LEDR << 1; 
					else LEDR = LEDR >> 1;
				end
			end
			
			3'b111: 
				if(delay >= MAX_DELAY) begin
					LEDR = 18'b000000000000000000;
				end
	endcase
end

endmodule
...and this is code for display:
Code:
module Display(binnum, display);

input	[3:0]	binnum;
output	[6:0]	display;

reg		[6:0]	number;

parameter nula  = 7'b1000000; // 0111111;
parameter jedna   = 7'b1111001; // 0000110;
parameter dve   = 7'b0100100; // 1011011;
parameter trojka = 7'b0110000; // 1001111;
parameter ctyri  = 7'b0011001; // 1100110;
parameter pet  = 7'b0010010; // 1101101;
parameter sest   = 7'b0000010; // 1111101;
parameter sedum = 7'b1111000; // 0000111;
parameter osum = 7'b0000000; // 1111111;
parameter devet  = 7'b0010000; // 1101111;
parameter A     = 7'b0001000; // 1110111;
parameter B     = 7'b0000011; // 1111100;
parameter C     = 7'b1000110; // 0111001;
parameter D     = 7'b0100001; // 1011110;
parameter E     = 7'b0000110; // 1111001;
parameter F     = 7'b0001110; // 1110001;
parameter G     = 7'b1000000; // 1110001;

assign display = number;

always @(binnum)
	case(binnum)
		4'b0000: number <= nula;
		4'b0001: number <= jedna;
		4'b0010: number <= dve;
		4'b0011: number <= trojka;
		4'b0100: number <= ctyri;
		4'b0101: number <= pet;
		4'b0110: number <= sest;
		4'b0111: number <= sedum;
		4'b1000: number <= osum;
		4'b1001: number <= devet;
		4'b1010: number <= A;
		4'b1011: number <= B;
		4'b1100: number <= C;
		4'b1101: number <= D;
		4'b1110: number <= E;
		4'b1111: number <= F;
		4'b1001: number <= G;
	endcase

endmodule
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top