Advertisement
Sidsh

LED Documented (sec*)

Mar 3rd, 2022
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module LED(
  2. input clk_50,
  3. input [6:0]id,              //id of the color received
  4. input [1:0]reset,
  5. output R1,
  6. output G1,
  7. output B1,
  8. output R2,
  9. output G2,
  10. output B2,
  11. output R3,
  12. output G3,
  13. output B3
  14. );reg [8:0]rgb = 9'b000000000;          //rgb value is stored together in this variable(3 bits each)
  15. reg clk_1=0;                                    //for 2.5 MHz cll
  16. reg [1:0]count=0;                               //Count for color case
  17. reg [20:0]sec=0;
  18. reg [4:0]C_COUNT=0;
  19. reg flag = 0;                                   //Is enabled when to detect color
  20.  
  21. always @(posedge clk_50)begin               //Converts 50 MHz clk to 1 MHz
  22.     if(reset==2'b01)
  23.     begin
  24.         clk_1<=0;
  25.         C_COUNT<=0;
  26.     end
  27.     if(C_COUNT == 49)begin
  28.         C_COUNT <= 1;
  29.     end else begin
  30.         C_COUNT <= C_COUNT + 1;
  31.     end
  32.     clk_1 <= (C_COUNT > 10);
  33. end
  34.  
  35.  
  36. always @(posedge clk_1)
  37. begin
  38.     if(reset==2'b01)               
  39.     begin
  40.         rgb <= 9'b000000000;        //Variable storing rgb vlaue is reset
  41.         count <= 0;
  42.         flag <= 0;
  43.     end
  44.     if(id == 0)                        
  45.     begin
  46.         flag <= 1;                      //flag resets to 1, to detect color only once
  47.     end
  48.     if((id == 1) && (flag == 1))            //if id 1, red color is stored in rgb variable
  49.     begin
  50.         rgb[3*count+2] <= 1;
  51.         rgb[3*count+1] <= 0;
  52.         rgb[3*count+0] <= 0;
  53.         count <= count + 1;
  54.         flag <= 0;
  55.     end
  56.     else if((id == 2) && (flag == 1))       //if id 2, green color is stored in rgb variable
  57.     begin
  58.         rgb[3*count+2] <= 0;
  59.         rgb[3*count+1] <= 1;
  60.         rgb[3*count+0] <= 0;
  61.         count <= count + 1;
  62.         flag <= 0;
  63.     end
  64.     else if((id == 3) && (flag == 1))       //if id 3, blue color is stored in rgb variable
  65.     begin
  66.         rgb[3*count+2] <= 0;
  67.         rgb[3*count+1] <= 0;
  68.         rgb[3*count+0] <= 1;
  69.         count <= count + 1;
  70.         flag <= 0;
  71.     end
  72.    
  73.     if(reset == 2'b11)
  74.     begin
  75.         sec <= sec+1;
  76.        
  77.         if(sec < 1000000)
  78.         begin
  79.             rgb <= 9'b111111111;
  80.         end else begin
  81.             rgb <= 9'b0;
  82.         end
  83.     end
  84. end
  85.  
  86. assign R3 = rgb[8];
  87. assign G3 = rgb[7];
  88. assign B3 = rgb[6];
  89. assign R2 = rgb[5];
  90. assign G2 = rgb[4];
  91. assign B2 = rgb[3];
  92. assign R1 = rgb[2];
  93. assign G1 = rgb[1];
  94. assign B1 = rgb[0];
  95.  
  96.  
  97. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement