06. Anlu SparkRoad domestic FPGA evaluation [Learning] RGB lights
[Copy link]
This post was last edited by 1nnocent on 2022-7-27 15:44
The function implemented by the RGB three-color lamp routine is to light up the three colors R, G, and B alternately. I would like to see a combination of multiple color lamps, but the program does not implement this function. The RGB lamp is actually very simple, which is three LEDs of different colors. When analyzing the code, you can modify the code by the way (the specific function is that when one lamp is on, the three lamps are lit in sequence; when two lamps are on, two lamps are lit in sequence; when three lamps are on, all three are lit, and then all three are off. There are eight cases in total) to achieve a combination of multiple colors.
The following is the hardware schematic and the corresponding FPGA pins:
Next, we will briefly analyze the following code: the input interface is a 24M clock, and the interface for controlling the three RGB lights. The value of the counter CNT in this routine is 24 000 000, so the interval between color switching is 1S.
First define constants. Each state of the RGB light corresponds to one state, for a total of eight.
parameter S0 = 3'b110;
parameter S1 = 3'b101;
parameter S2 = 3'b011;
parameter S3 = 3'b100;
parameter S4 = 3'b001;
parameter S5 = 3'b010;
parameter S6 = 3'b000;
parameter S7 = 3'b111;
The state of the RGB light is switched every 1S, and state is the state of the light:
always @(posedge clk_24m or negedge rst_n)
begin
if (!rst_n)
begin
cnt_time <= 0;
state <= S0;
end
else if(cnt_time == CNT - 1)
begin
cnt_time <= 0;
state <= state + 1'b1;
end
else
cnt_time <= cnt_time + 1'b1;
end
Then assign values to the RGB lights according to the state:
always@(*)
begin
case(state)
3'd0:led_rgb <= S0;
3'd1:led_rgb <= S1;
3'd2:led_rgb <= S2;
3'd3:led_rgb <= S3;
3'd4:led_rgb <= S4;
3'd5:led_rgb <= S5;
3'd6:led_rgb <= S6;
3'd7:led_rgb <= S7;
default:state <= state;
endcase
end
assign led_r = led_rgb[0];
assign led_g = led_rgb[1];
assign led_b = led_rgb[2];
The following is the running effect:
RGB
|