当前位置:   article > 正文

安徽省机器人大赛练习(C赛道)_安徽省21年机器人竞赛的题目

安徽省21年机器人竞赛的题目

 

简单的练习一下,题目要求如下:

  • 用LCD屏显示温湿度
  • 驱动电机转动
  • 当湿度低于某个阈值时电机速度变快
  • 用LCD屏显示电机转速
  • 设置按钮A,使电机停止
  • 设置按钮B,使电机速度进入手动控制模式
  • 设置按钮C,每按一次电机速度发生变化
  • 设置按钮D,再次进入自动模式

代码如下:

LCD12864:

  1. module lcd12864_drive
  2. (
  3. input clock,
  4. input reset,
  5. input [63:0] data_buf,
  6. output lcd12864_rs,
  7. output lcd12864_rw,
  8. output lcd12864_en,
  9. output [7:0] lcd12864_data
  10. );
  11. wire [63:0] data_buf;
  12. /**************************产生lcd12864时钟信号*************************/
  13. reg clk_lcd12864;
  14. reg [19:0]cnt;
  15. always @(posedge clock or posedge reset)
  16. begin
  17. if (reset)
  18. begin
  19. cnt <= 20'b0;
  20. clk_lcd12864 <= 0;
  21. end
  22. else if(cnt == 20'd20000) //时钟频率非常重要!!将近3k,经实测5k会在第0位出错。
  23. begin
  24. cnt <= 20'd0;
  25. clk_lcd12864 <= ~clk_lcd12864;
  26. end
  27. else
  28. cnt <= cnt +1'b1;
  29. end
  30. reg [1:0] clk_lcd12864_sync;
  31. always @(posedge clock or posedge reset)
  32. begin
  33. if (reset)
  34. clk_lcd12864_sync <= 2'b00;
  35. else
  36. clk_lcd12864_sync <= {clk_lcd12864_sync[0],clk_lcd12864};
  37. end
  38. assign clk_lcd12864_pos = (clk_lcd12864_sync == 2'b01);
  39. //****************************lcd12864控制信号*****************************************/
  40. reg [8:0] state; //State Machine code
  41. parameter IDLE = 4'd0;
  42. parameter CMD_WIDTH = 4'd1; //设置数据接口数量
  43. parameter CMD_SET = 4'd2; //选择指令集
  44. parameter CMD_CURSOR = 4'd3; //设置光标
  45. parameter CMD_CLEAR = 4'd4; //清屏
  46. parameter CMD_ACCESS = 4'd5; //输入方式设置:数据读写操作后,地址自动加一/画面不动
  47. parameter CMD_DDRAM = 4'd6; //DDRAM行地址
  48. parameter DATA_WRITE = 4'd7; //数据写入
  49. parameter STOP = 4'd8; //
  50. reg lcd12864_rs_r;
  51. reg [7:0] lcd12864_data_r;
  52. reg [7:0] data_buff;
  53. reg [5:0] cnt_time;
  54. //输出管教配置
  55. assign lcd12864_rs = lcd12864_rs_r;
  56. assign lcd12864_rw = 1'b0;
  57. assign lcd12864_en = clk_lcd12864_sync[1]; //与lcd12864时钟相同
  58. assign lcd12864_data = lcd12864_data_r;
  59. always @(posedge clock or posedge reset)
  60. begin
  61. if(reset)
  62. begin
  63. lcd12864_rs_r <= 1'b0;
  64. state <= IDLE;
  65. // lcd12864_data_r <= 8'bzzzzzzzz; //高阻态
  66. lcd12864_data_r <= 8'b11111111; //高阻态
  67. cnt_time <= 6'd0;
  68. end
  69. else if(clk_lcd12864_pos)
  70. begin
  71. case(state)
  72. IDLE:
  73. begin
  74. lcd12864_rs_r <= 1'b0;
  75. cnt_time <= 6'd0;
  76. state <= CMD_WIDTH;
  77. // lcd12864_data_r <= 8'bzzzzzzzz;
  78. lcd12864_data_r <= 8'b11111111;
  79. end
  80. CMD_WIDTH:
  81. begin
  82. lcd12864_rs_r <= 1'b0;
  83. state <= CMD_SET;
  84. lcd12864_data_r <= 8'h30; //8位数据口
  85. end
  86. CMD_SET:
  87. begin
  88. lcd12864_rs_r <= 1'b0;
  89. state <= CMD_CURSOR;
  90. lcd12864_data_r <= 8'h30; //基本指令集
  91. end
  92. CMD_CURSOR:
  93. begin
  94. lcd12864_rs_r <= 1'b0;
  95. state <= CMD_CLEAR;
  96. lcd12864_data_r <= 8'h0c; // 关光标
  97. end
  98. CMD_CLEAR:
  99. begin
  100. lcd12864_rs_r <= 1'b0;
  101. state <= CMD_ACCESS;
  102. lcd12864_data_r <= 8'h01; //清屏
  103. end
  104. CMD_ACCESS:
  105. begin
  106. lcd12864_rs_r <= 1'b0;
  107. state <= CMD_DDRAM;
  108. lcd12864_data_r <= 8'h06; //进入点设定
  109. end
  110. CMD_DDRAM: //行数命令
  111. begin
  112. lcd12864_rs_r <= 1'b0;
  113. state <= DATA_WRITE;
  114. case (cnt_time)
  115. 6'd0: lcd12864_data_r <= 8'h80;
  116. 6'd16: lcd12864_data_r <= 8'h90;
  117. 6'd32: lcd12864_data_r <= 8'h88;
  118. 6'd48: lcd12864_data_r <= 8'h98;
  119. endcase
  120. end
  121. DATA_WRITE: //写数据
  122. begin
  123. lcd12864_rs_r <= 1'b1;
  124. cnt_time <= cnt_time + 1'b1;
  125. lcd12864_data_r <= data_buff;
  126. case (cnt_time)
  127. 6'd15: state <= CMD_DDRAM;
  128. 6'd31: state <= CMD_DDRAM;
  129. 6'd47: state <= CMD_DDRAM;
  130. 6'd63: state <= STOP;
  131. default: state <= DATA_WRITE;
  132. endcase
  133. end
  134. STOP:
  135. begin
  136. lcd12864_rs_r <= 1'b0;
  137. state <= CMD_DDRAM;
  138. lcd12864_data_r <= 8'h80; //从第几行循环
  139. cnt_time <= 6'd0;
  140. end
  141. default:
  142. state <= IDLE;
  143. endcase
  144. end
  145. end
  146. always @(cnt_time)
  147. begin
  148. case (cnt_time)
  149. 6'd0: data_buff <= "W";
  150. 6'd1: data_buff <= ":";
  151. 6'd2: data_buff <= 8'h20;
  152. 6'd3: data_buff <= data_buf[15:8]+8'h30;//温湿度
  153. 6'd4: data_buff <= 8'h20;
  154. 6'd5: data_buff <= 8'h20;
  155. 6'd6: data_buff <= data_buf[7:0]+8'h30;
  156. 6'd7: data_buff <= 8'h20;
  157. 6'd8: data_buff <= 8'h20;
  158. 6'd9: data_buff <= "S";
  159. 6'd10: data_buff <= ":";
  160. 6'd11: data_buff <= 8'h20;
  161. 6'd12: data_buff <= data_buf[31:24]+8'h30;
  162. 6'd13: data_buff <= 8'h20;
  163. 6'd14: data_buff <= 8'h20;
  164. 6'd15: data_buff <= data_buf[23:16]+8'h30;
  165. 6'd16: data_buff <= 8'h20;//下一行开头
  166. 6'd17: data_buff <= 8'h20;
  167. 6'd18: data_buff <= data_buf[63:56]+8'h30;//速度
  168. 6'd19: data_buff <= 8'h20;
  169. 6'd20: data_buff <= data_buf[55:48]+8'h30;
  170. 6'd21: data_buff <= 8'h20;
  171. 6'd22: data_buff <= data_buf[47:40]+8'h30;
  172. 6'd23: data_buff <= 8'h20;
  173. 6'd24: data_buff <= data_buf[39:32]+8'h30;
  174. 6'd25: data_buff <= 8'h20;
  175. 6'd26: data_buff <= 8'h20;
  176. 6'd27: data_buff <= 8'h20;//数字0
  177. 6'd28: data_buff <= 8'h20;//数字1
  178. 6'd29: data_buff <= 8'h20;//数字2
  179. 6'd30: data_buff <= 8'h20;
  180. 6'd31: data_buff <= 8'h20;
  181. 6'd32: data_buff <= 8'h20;
  182. 6'd33: data_buff <= 8'h20;
  183. 6'd34: data_buff <= 8'h20;
  184. 6'd35: data_buff <= 8'h20;
  185. 6'd36: data_buff <= 8'h20;
  186. 6'd37: data_buff <= 8'h20;
  187. 6'd38: data_buff <= 8'h20;
  188. 6'd39: data_buff <= 8'h20;
  189. 6'd40: data_buff <= 8'h20;
  190. 6'd41: data_buff <= 8'h20;
  191. 6'd42: data_buff <= 8'h20;
  192. 6'd43: data_buff <= 8'h20;
  193. 6'd44: data_buff <= 8'h20;
  194. 6'd45: data_buff <= 8'h20;
  195. 6'd46: data_buff <= 8'h20;
  196. 6'd47: data_buff <= 8'h20;
  197. 6'd48: data_buff <= 8'h20;
  198. 6'd49: data_buff <= 8'h20;
  199. 6'd50: data_buff <= 8'h20;
  200. 6'd51: data_buff <= 8'h20;
  201. 6'd52: data_buff <= 8'h20;
  202. 6'd53: data_buff <= 8'h20;
  203. 6'd54: data_buff <= 8'h20;
  204. 6'd55: data_buff <= 8'h20;
  205. 6'd56: data_buff <= 8'h20;
  206. 6'd57: data_buff <= 8'h20;
  207. 6'd58: data_buff <= 8'h20;
  208. 6'd59: data_buff <= 8'h20;
  209. 6'd60: data_buff <= 8'h20;
  210. 6'd61: data_buff <= 8'h20;
  211. 6'd62: data_buff <= 8'h20;
  212. 6'd63: data_buff <= 8'h20;
  213. default : data_buff <= 8'h02;
  214. endcase
  215. end
  216. endmodule

button:

  1. module button
  2. (
  3. input clock,
  4. input reset,
  5. input [3:0] row, //行
  6. output [3:0] col, //列
  7. output [3:0] key_value,
  8. output key_out_flag
  9. );
  10. reg [3:0] col;
  11. reg [3:0] key_value;
  12. reg [31:0] count;
  13. wire clk_20ms_flag;
  14. reg [2:0] state; //状态标志
  15. reg key_flag; //按键标志位
  16. reg key_out_flag;
  17. reg [3:0] col_reg; //寄存扫描列值
  18. reg [3:0] row_reg; //寄存扫描行值
  19. always @(posedge clock or posedge reset)
  20. begin
  21. if(reset)
  22. count <= 0;
  23. else
  24. count <= count + 1;
  25. end
  26. assign clk_20ms_flag = (count[20:0] == 21'd2000000);
  27. always @(posedge clock or posedge reset)
  28. begin
  29. if(reset)
  30. begin
  31. col <= 4'b0000;
  32. state <= 0;
  33. end
  34. else if(clk_20ms_flag)
  35. case (state)
  36. 0:
  37. begin
  38. col[3:0] <= 4'b0000;
  39. key_flag <= 1'b0;
  40. if(row[3:0] != 4'b1111)
  41. begin
  42. state <= 1;
  43. col[3:0] <= 4'b1110;
  44. end //有键按下,扫描第一行
  45. else
  46. state <= 0;
  47. end
  48. 1:
  49. begin
  50. if(row[3:0] != 4'b1111)
  51. state <= 5;//判断是否是第一行
  52. else
  53. begin
  54. state <= 2;
  55. col[3:0] <= 4'b1101;
  56. end //扫描第二行
  57. end
  58. 2:
  59. begin
  60. if(row[3:0] != 4'b1111)
  61. state <= 5;//判断是否是第二行
  62. else
  63. begin
  64. state <= 3;
  65. col[3:0] <= 4'b1011;
  66. end //扫描第三行
  67. end
  68. 3:
  69. begin
  70. if(row[3:0] != 4'b1111)
  71. state <= 5; //判断是否是第三一行
  72. else
  73. begin
  74. state <= 4;
  75. col[3:0] <= 4'b0111;
  76. end //扫描第四行
  77. end
  78. 4:
  79. begin
  80. if(row[3:0] != 4'b1111)
  81. state <= 5;//判断是否是第一行
  82. else
  83. state <= 0;
  84. end
  85. 5:
  86. begin
  87. if(row[3:0] != 4'b1111)
  88. begin
  89. col_reg <= col; //保存扫描列值
  90. row_reg <= row; //保存扫描行值
  91. state <= 5;
  92. key_flag <= 1'b1; //有键按下
  93. end
  94. else
  95. state <= 0;
  96. end
  97. endcase
  98. end
  99. always @(clock or col_reg or row_reg)
  100. begin
  101. if(key_flag == 1'b1)
  102. begin
  103. key_out_flag <= 1;
  104. case ({col_reg,row_reg})
  105. 8'b1110_1110:key_value <= 4'b0001 ;
  106. 8'b1101_1110:key_value <= 4'b0010;
  107. 8'b1011_1110:key_value <= 4'b0110;
  108. 8'b0111_1110:key_value <= 4'b0000;
  109. // 8'b1110_1101:key_value <= 4;
  110. // 8'b1101_1101:key_value <= 5;
  111. // 8'b1011_1101:key_value <= 6;
  112. // 8'b0111_1101:key_value <= 7;
  113. // 8'b1110_1011:key_value <= 8;
  114. // 8'b1101_1011:key_value <= 9;
  115. // 8'b1011_1011:key_value <= 10;
  116. // 8'b0111_1011:key_value <= 11;
  117. // 8'b1110_0111:key_value <= 12;
  118. // 8'b1101_0111:key_value <= 13;
  119. // 8'b1011_0111:key_value <= 14;
  120. // 8'b0111_0111:key_value <= 15;
  121. default: key_value <= 4'b0000;
  122. endcase
  123. end
  124. else
  125. key_out_flag <= 0;
  126. end
  127. endmodule

speed:

  1. module speed
  2. (
  3. input clock,
  4. input reset,
  5. input pulse_from_motor,
  6. output [19:0] speed_value
  7. );
  8. reg [32:0] cnt;
  9. always @(posedge clock or posedge reset)
  10. begin
  11. if (reset)
  12. cnt <= 0;
  13. // else if(cnt == 33'd6000000000)
  14. else if(cnt == 33'd300000000)
  15. cnt <= 0;
  16. else
  17. cnt <= cnt + 1'b1;
  18. end
  19. wire start_flag;
  20. wire end_flag;
  21. assign start_flag = (cnt <= 33'd5);
  22. //assign end_flag = (cnt == 33'd6000000000);
  23. assign end_flag = (cnt == 33'd300000000);
  24. reg [1:0] pulse_from_motor_sync;
  25. always @(posedge clock or posedge reset)
  26. begin
  27. if (reset)
  28. pulse_from_motor_sync <= 2'b00;
  29. else
  30. pulse_from_motor_sync <= {pulse_from_motor_sync[0],pulse_from_motor};
  31. end
  32. wire pulse_from_motor_pos;
  33. assign pulse_from_motor_pos = (pulse_from_motor_sync == 2'b01);
  34. wire [23 : 0] Q;
  35. c_counter_binary_0 c_counter_binary_0_0
  36. (
  37. .CLK(clock), // input wire CLK
  38. .CE(pulse_from_motor_pos), // input wire CE
  39. .SCLR(start_flag), // input wire SCLR
  40. .Q(Q) // output wire [23 : 0] Q
  41. );
  42. reg [23:0] pulse_cnt;
  43. always @(posedge clock or posedge reset)
  44. begin
  45. if (reset)
  46. pulse_cnt <= 0;
  47. else if(end_flag)
  48. pulse_cnt <= Q;
  49. else
  50. ;
  51. end
  52. wire [15:0] turns_num;
  53. //assign turns_num = pulse_cnt / 20;
  54. assign turns_num = pulse_cnt[15:0];
  55. assign speed_value [3:0] = turns_num % 10;
  56. assign speed_value [7:4] = turns_num % 100 / 10;
  57. assign speed_value [11:8] = turns_num % 1000 / 100;
  58. assign speed_value [15:12] = turns_num % 10000 / 1000;
  59. assign speed_value [19:16] = turns_num % 100000 / 10000;
  60. endmodule

dht:

  1. module dht11_drive
  2. (
  3. input clock,
  4. input reset,
  5. inout dht11,
  6. output reg [31:0] dht11_value,
  7. output [7:0] dht11_value_sd,
  8. output [7:0] dht11_value_wd
  9. );
  10. parameter POWER_ON_NUM = 1000_000; //上电延时1s
  11. parameter st_power_on_wait = 3'd0; //上电延时等待
  12. parameter st_low_20ms = 3'd1; //主机发送20ms低电平
  13. parameter st_high_13us = 3'd2; //主机释放总线13us
  14. parameter st_rec_low_83us = 3'd3; //接收83us低电平响应
  15. parameter st_rec_high_87us = 3'd4; //等待87us高电平(准备接收数据)
  16. parameter st_rec_data = 3'd5; //接收40位数据
  17. parameter st_delay = 3'd6; //延时等待,延时完成后重新操作DHT11
  18. reg [2:0] cur_state; //当前状态
  19. reg [2:0] next_state; //下一个状态
  20. reg [5:0] clock_cnt; //分频计数器
  21. reg clock_1M; //1MHz时钟
  22. reg [20:0] us_cnt; //1微秒计数器
  23. reg us_cnt_clr; //1微秒计数器清零信号
  24. reg [39:0] data_temp; //缓存接收到的数据
  25. reg step; //数据采集状态
  26. reg [5:0] data_cnt; //接收数据用计数器
  27. reg dht11_buffer; //DHT11输出信号
  28. reg [1:0] clock_1M_sync;
  29. wire clock_1M_pos;
  30. wire clock_1M_neg;
  31. reg [1:0] dht11_sync;
  32. wire dht11_pos;
  33. wire dht11_neg;
  34. //1MHz分频时钟
  35. always @ (posedge clock or posedge reset)
  36. begin
  37. if (reset)
  38. clock_cnt <= 6'd0;
  39. else if (clock_cnt == 6'd49)
  40. clock_cnt <= 6'd0;
  41. else
  42. clock_cnt <= clock_cnt + 1'b1;
  43. end
  44. always @ (posedge clock or posedge reset)
  45. begin
  46. if (reset)
  47. clock_1M <= 1'b0;
  48. else if (clock_cnt == 6'd49)
  49. clock_1M <= ~ clock_1M;
  50. else
  51. clock_1M <= clock_1M;
  52. end
  53. always @ (posedge clock or posedge reset)
  54. begin
  55. if (reset)
  56. clock_1M_sync <= 2'b00;
  57. else
  58. clock_1M_sync <= {clock_1M_sync[0],clock_1M};
  59. end
  60. assign clock_1M_pos = (clock_1M_sync == 2'b01);
  61. assign clock_1M_neg = (clock_1M_sync == 2'b10);
  62. always @ (posedge clock or posedge reset)
  63. begin
  64. if (reset)
  65. dht11_sync <= 2'b11;
  66. else if(clock_1M_pos)
  67. dht11_sync <= {dht11_sync[0],dht11};
  68. end
  69. assign dht11_pos = (dht11_sync == 2'b01);
  70. assign dht11_neg = (dht11_sync == 2'b10);
  71. //us计数器
  72. always @ (posedge clock or posedge reset)
  73. begin
  74. if (reset)
  75. us_cnt <= 21'd0;
  76. else if (us_cnt_clr)
  77. us_cnt <= 21'd0;
  78. else if(clock_1M_pos)
  79. us_cnt <= us_cnt + 1'b1;
  80. end
  81. //状态跳转
  82. always @ (posedge clock or posedge reset)
  83. begin
  84. if (reset)
  85. cur_state <= st_power_on_wait;
  86. else
  87. cur_state <= next_state;
  88. end
  89. //状态机读取DHT11数据
  90. always @ (posedge clock or posedge reset)
  91. begin
  92. if(reset)
  93. begin
  94. next_state <= st_power_on_wait;
  95. data_temp <= 40'd0;
  96. step <= 1'b0;
  97. us_cnt_clr <= 1'b0;
  98. data_cnt <= 6'd0;
  99. dht11_buffer <= 1'bz;
  100. end
  101. else if(clock_1M_pos)
  102. begin
  103. case (cur_state)
  104. st_power_on_wait :
  105. begin
  106. if(us_cnt < POWER_ON_NUM)
  107. begin
  108. dht11_buffer <= 1'bz;
  109. us_cnt_clr <= 1'b0;
  110. end
  111. else
  112. begin
  113. next_state <= st_low_20ms;
  114. us_cnt_clr <= 1'b1;
  115. end
  116. end
  117. st_low_20ms :
  118. begin
  119. if(us_cnt < 20000)
  120. begin
  121. dht11_buffer <= 1'b0;
  122. us_cnt_clr <= 1'b0;
  123. end
  124. else
  125. begin
  126. dht11_buffer <= 1'bz;
  127. next_state <= st_high_13us;
  128. us_cnt_clr <= 1'b1;
  129. end
  130. end
  131. st_high_13us :
  132. begin
  133. if (us_cnt < 20)
  134. begin
  135. us_cnt_clr <= 1'b0;
  136. if(dht11_neg)
  137. begin
  138. next_state <= st_rec_low_83us;
  139. us_cnt_clr <= 1'b1;
  140. end
  141. end
  142. else
  143. next_state <= st_delay;
  144. end
  145. st_rec_low_83us :
  146. begin
  147. if(dht11_pos)
  148. next_state <= st_rec_high_87us;
  149. end
  150. st_rec_high_87us :
  151. begin
  152. if(dht11_neg)
  153. begin
  154. next_state <= st_rec_data;
  155. us_cnt_clr <= 1'b1;
  156. end
  157. else
  158. begin
  159. data_cnt <= 6'd0;
  160. data_temp <= 40'd0;
  161. step <= 1'b0;
  162. end
  163. end
  164. st_rec_data :
  165. begin
  166. case(step)
  167. 0 :
  168. begin
  169. if(dht11_pos)
  170. begin
  171. step <= 1'b1;
  172. us_cnt_clr <= 1'b1;
  173. end
  174. else
  175. us_cnt_clr <= 1'b0;
  176. end
  177. 1 :
  178. begin
  179. if(dht11_neg)
  180. begin
  181. data_cnt <= data_cnt + 1'b1;
  182. if(us_cnt < 60)
  183. data_temp <= {data_temp[38:0],1'b0};
  184. else
  185. data_temp <= {data_temp[38:0],1'b1};
  186. step <= 1'b0;
  187. us_cnt_clr <= 1'b1;
  188. end
  189. else
  190. us_cnt_clr <= 1'b0;
  191. end
  192. endcase
  193. if(data_cnt == 40)
  194. begin
  195. next_state <= st_delay;
  196. if(data_temp[7:0] == data_temp[39:32] + data_temp[31:24] + data_temp[23:16] + data_temp[15:8])
  197. dht11_value <= data_temp[39:8];
  198. end
  199. end
  200. st_delay :
  201. begin
  202. if(us_cnt < 2000_000)
  203. us_cnt_clr <= 1'b0;
  204. else
  205. begin
  206. next_state <= st_low_20ms;
  207. us_cnt_clr <= 1'b1;
  208. end
  209. end
  210. default : ;
  211. endcase
  212. end
  213. end
  214. assign dht11 = dht11_buffer;
  215. assign dht11_value_sd[3:0] = dht11_value[31:24] % 10;
  216. assign dht11_value_sd[7:4] = dht11_value[31:24] / 10;
  217. assign dht11_value_wd[3:0] = dht11_value[15:8] % 10;
  218. assign dht11_value_wd[7:4] = dht11_value[15:8] / 10;
  219. endmodule

Judge:(电机驱动,这里用的是步进电机)

  1. module Judge(
  2. input clk,
  3. input rst,
  4. input key_out_flag,
  5. input [7:0]t_data,//温度
  6. input [7:0]s_data,//湿度
  7. input A,//清零
  8. input B,//手动模式
  9. input C,//速度变档
  10. input D,//恢复到自动模式
  11. output [3:0] motor_en
  12. );
  13. reg [3:0] motor_en_reg;
  14. reg [31:0] cnt;
  15. reg [1:0] s;
  16. reg [1:0] state_speed;
  17. always@(posedge key_out_flag)
  18. begin
  19. if(rst||A)
  20. begin
  21. s<=0;
  22. end
  23. else
  24. begin
  25. if(C)
  26. s<=s+1;
  27. end
  28. end
  29. always@(posedge clk)
  30. begin
  31. if(rst)
  32. begin
  33. state_speed<=cnt[21:20];
  34. end
  35. else
  36. begin
  37. case(s)
  38. 2'b00:
  39. state_speed<=cnt[21:20];
  40. 2'b01:
  41. state_speed<=cnt[20:19];
  42. 2'b10:
  43. state_speed<=cnt[19:18];
  44. 2'b11:
  45. state_speed<=cnt[18:17];//由于频率过快,该状态会强制暂停
  46. default:;
  47. endcase
  48. end
  49. end
  50. always@(posedge clk)
  51. begin
  52. if(rst||A)
  53. begin
  54. motor_en_reg<=0;
  55. end
  56. else if(B)//手动模式
  57. begin
  58. case (state_speed)
  59. 2'b00 :
  60. motor_en_reg <= 4'b0001;
  61. 2'b01 :
  62. motor_en_reg <= 4'b0010;
  63. 2'b10 :
  64. motor_en_reg <= 4'b0100;
  65. 2'b11 :
  66. motor_en_reg <= 4'b1000;
  67. endcase
  68. end
  69. else if(s_data[7:4]<4'd3)//湿度小于三十度
  70. begin
  71. case (cnt[19:18])
  72. 2'b00 :
  73. motor_en_reg <= 4'b0001;
  74. 2'b01 :
  75. motor_en_reg <= 4'b0010;
  76. 2'b10 :
  77. motor_en_reg <= 4'b0100;
  78. 2'b11 :
  79. motor_en_reg <= 4'b1000;
  80. endcase
  81. end
  82. else
  83. begin
  84. case (cnt[21:20])
  85. 2'b00 :
  86. motor_en_reg <= 4'b0001;
  87. 2'b01 :
  88. motor_en_reg <= 4'b0010;
  89. 2'b10 :
  90. motor_en_reg <= 4'b0100;
  91. 2'b11 :
  92. motor_en_reg <= 4'b1000;
  93. endcase
  94. end
  95. end
  96. always @(posedge clk )
  97. begin
  98. if (rst)
  99. cnt <= 0;
  100. else
  101. cnt <= cnt + 1'b1;
  102. end
  103. assign motor_en = motor_en_reg;
  104. endmodule

实验效果:

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号