Projektdaten für das ESY1B Praktikum im Sommersemester 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LED_control.sv 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // ==================================================================
  2. // >>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<
  3. // ------------------------------------------------------------------
  4. // Copyright (c) 2022 by Ampelsteuerungsgruppe
  5. //
  6. // --------------------------------------------------------------------
  7. //
  8. // Project: iCE5UP 5K RGB LED Steuerung
  9. // File: LED_control.sv
  10. // Title: LED Ampelsteuerung
  11. // Description: Creates RGB PWM per data input
  12. //
  13. //
  14. // --------------------------------------------------------------------
  15. //
  16. //------------------------------------------------------------
  17. // Notes:
  18. //
  19. //
  20. //------------------------------------------------------------
  21. // Development History:
  22. //
  23. // __DATE__ _BY_ _____REV_ _DESCRIPTION___________________________
  24. // 17.02.22 AmpelGr 1.0 Initial design for Lattice Radiant Ampelsteuerung
  25. //
  26. //------------------------------------------------------------
  27. // Dependencies:
  28. //
  29. //
  30. //
  31. //------------------------------------------------------------
  32. module LED_control1 (
  33. // inputs
  34. input wire clk12M, // 12M clock
  35. input wire enable, // Asynchronous enable/disable wire
  36. input wire [7:0] data_input, // for selecting color using sensor data
  37. input wire data_valid, // Data on Parallelport is valid on posedge
  38. //outputs
  39. output reg red_pwm, // Red LED
  40. output reg grn_pwm, // Green LED
  41. output reg alarm // Alarmmeldung
  42. );
  43. //------------------------------
  44. // INTERNAL SIGNAL DECLARATIONS:
  45. //------------------------------
  46. // parameters (constants)
  47. parameter on_hi = 2'b10;
  48. parameter on_lo = 2'b01;
  49. parameter off = 2'b00;
  50. parameter Brightness=4'b0111; //50% Brightness
  51. // wires (assigns)
  52. wire [4:0] red_intensity;
  53. wire [4:0] grn_intensity;
  54. wire clk24M;
  55. wire LOCK;
  56. // regs (always)
  57. reg [1:0] RGB_color_s; // selected Color
  58. reg [3:0] Brightness_s;
  59. reg [1:0] red_set; // hi/lo/off
  60. reg [1:0] grn_set;
  61. reg [17:0] red_peak; // LED 'on' peak intensity (high precision)
  62. reg [17:0] grn_peak;
  63. reg [17:0] curr_red; // current LED intensity ( /256 = PWM duty cycle)
  64. reg [17:0] curr_grn;
  65. reg [17:0] pwm_count; // PWM counter
  66. reg [7:0] count = 8'b0;
  67. //------------------------------
  68. // PLL Instantiation
  69. //------------------------------
  70. //Block to reset the PLL initially
  71. pll_24M __(.ref_clk_i(clk12M ), .rst_n_i(~enable), .lock_o(LOCK), .outcore_o( ), .outglobal_o(clk24M));
  72. // Capture stable parameters in local clock domain
  73. always @ (posedge clk24M or posedge enable)
  74. if (enable) begin
  75. RGB_color_s <= 2'b00; //turn off
  76. Brightness_s <= 4'b0000;
  77. alarm <= 0;
  78. end else if(!alarm) begin
  79. if(data_valid) begin
  80. if(data_input <100) begin
  81. RGB_color_s <= 2'b10; // set green
  82. Brightness_s <= Brightness;
  83. end else if(data_input >168) begin
  84. alarm <=1'b1;
  85. end else begin
  86. RGB_color_s <= 2'b11; // set yellow
  87. Brightness_s <= Brightness;
  88. end
  89. end
  90. end else begin
  91. RGB_color_s <= 2'b01; // set Color red
  92. Brightness_s <= Brightness;
  93. end
  94. // interpret 'brightness' setting
  95. assign red_intensity = Brightness_s + 1'b1;
  96. assign grn_intensity = Brightness_s + 1'b1;
  97. // interpret 'color' setting
  98. always @ (RGB_color_s)
  99. case (RGB_color_s)
  100. 2'b01: begin red_set <= on_hi; grn_set <= off; end //Red
  101. 4'b10: begin red_set <= off; grn_set <= on_hi; end //Green
  102. 4'b11: begin red_set <= on_hi; grn_set <= on_hi; end //Yellow
  103. default: begin red_set <= off; grn_set <= off; end //2'b00 off
  104. endcase
  105. // set peak values per 'brightness' and 'color'
  106. // when color setting is 'on_lo', then peak intensity is divided by 2
  107. always @ (posedge clk24M or posedge enable)
  108. if (enable) begin
  109. red_peak <= 18'b0;
  110. end else begin
  111. case (red_set)
  112. on_hi: red_peak <= {red_intensity, 13'h000}; // 100%
  113. on_lo: red_peak <= {1'b0,red_intensity, 12'h000}; // 50%
  114. default: red_peak <= 18'h00000;
  115. endcase
  116. end
  117. always @ (posedge clk24M or posedge enable)
  118. if (enable) begin
  119. grn_peak <= 32'b0;
  120. end else begin
  121. case (grn_set)
  122. on_hi: grn_peak <= {grn_intensity, 13'h000}; // 100%
  123. on_lo: grn_peak <= {1'b0,grn_intensity, 12'h000}; // 50%
  124. default: grn_peak <= 18'h00000;
  125. endcase
  126. //$monitor("grn_peak=%d",grn_peak);
  127. end
  128. // set PWM duty cycle. 8-bit resolution 0x100 is 100% on
  129. always @ (posedge clk24M or posedge enable)
  130. if (enable) begin
  131. curr_red <= 18'b0;
  132. curr_grn <= 18'b0;
  133. end else begin
  134. curr_red <= red_peak;
  135. curr_grn <= grn_peak;
  136. end
  137. // generate PWM outputs
  138. always @ (posedge clk24M or posedge enable)
  139. if (enable) begin
  140. pwm_count <= 18'b0;
  141. red_pwm <= 0;
  142. grn_pwm <= 0;
  143. end else begin
  144. if(pwm_count < 131071)
  145. pwm_count <= pwm_count + 1;
  146. else
  147. pwm_count <= 0;
  148. if(pwm_count < curr_red)
  149. red_pwm <= 1;
  150. else
  151. red_pwm <= 0;
  152. if(pwm_count < curr_grn)
  153. grn_pwm <= 1;
  154. else
  155. grn_pwm <= 0;
  156. end
  157. endmodule // LED_control