今天一个学弟来问我关于状态机的事,我就给他讲了下经典的三段式状态机,然后就让他试着用状态机去点亮几个LED灯。
状态一:让所有的灯全部熄灭;状态二:全部的灯按照奇偶位的来进行闪烁;状态三:全部的灯从左边开始亮,一直到右边;状态四:全部的灯从右边开始亮直到左边。他写了一个小时左右还是做不出来,一气之下,我自己来写,结果我自己也没写出来,我觉得是状态跳转的太快了,所以导致LED看起来不清楚。
于是,我就用序列机写了一个花样流水灯。效果是,开始二十六个灯全部按照奇偶位闪烁八次,然后从左边依次点亮所有的LED灯,再从右边依次点亮所有的LED灯,然后就是普通的流水灯了,一直循环下去。
代码比较简单,就不解释了。刚才导师到酒店里打包回来了一大桌子菜,吃的有点小撑,有一个同学劝我多吃一些,我就说没有干体力劳动,不用多吃,导师就说,干体力活的吃白馒头就行,写代码的消耗更大,要吃肉,终于找到一个能解释为什么我吃这么多了,不是因为贪吃,二十确实是处在巨大消耗当中呀。
1 module flash_LED( 2 clk, 3 rst_n, 4 LED, 5 ); 6 7 input clk; 8 input rst_n; 9 output reg[25:0] LED; 10 11 reg[27:0] cnt; 12 13 always@(posedge clk or negedge rst_n) 14 if(!rst_n) 15 cnt <= 28'd0; 16 else if(cnt == 4999999) //100ms 17 cnt <= 28'd0; 18 else 19 cnt <= cnt + 1'b1; 20 21 reg div_clk; 22 always@(posedge clk or negedge rst_n) //10Hz 分频 23 if(!rst_n) 24 div_clk <= 0; 25 else if(cnt == 4999999) 26 div_clk <= ~div_clk; 27 else 28 div_clk <= div_clk; 29 30 reg[6:0] counter; 31 always@(posedge div_clk or negedge rst_n) //序列机 32 if(!rst_n) 33 counter <= 7'd0; 34 else if(counter == 113) 35 counter <= 7'd0; 36 else 37 counter <= counter + 1'b1; 38 39 always@(posedge clk or negedge rst_n) 40 if(!rst_n) 41 LED <= 26'b000000000000000000000000000; 42 else begin 43 case(counter) 44 0:LED <= 26'b01010101010101010101010101; 45 1:LED <= 26'b10101010101010101010101010; 46 2:LED <= 26'b01010101010101010101010101; 47 3:LED <= 26'b10101010101010101010101010; 48 4:LED <= 26'b01010101010101010101010101; 49 5:LED <= 26'b10101010101010101010101010; 50 6:LED <= 26'b01010101010101010101010101; 51 7:LED <= 26'b10101010101010101010101010; 52 8:LED <= 26'b00000000000000000000000001; 53 9:LED <= 26'b00000000000000000000000011; 54 10:LED <= 26'b00000000000000000000000111; 55 11:LED <= 26'b00000000000000000000001111; 56 12:LED <= 26'b00000000000000000000011111; 57 13:LED <= 26'b00000000000000000000111111; 58 14:LED <= 26'b00000000000000000001111111; 59 15:LED <= 26'b00000000000000000011111111; 60 16:LED <= 26'b00000000000000000111111111; 61 17:LED <= 26'b00000000000000001111111111; 62 18:LED <= 26'b00000000000000011111111111; 63 19:LED <= 26'b00000000000000111111111111; 64 20:LED <= 26'b00000000000001111111111111; 65 21:LED <= 26'b00000000000011111111111111; 66 22:LED <= 26'b00000000000111111111111111; 67 23:LED <= 26'b00000000001111111111111111; 68 24:LED <= 26'b00000000011111111111111111; 69 25:LED <= 26'b00000000111111111111111111; 70 26:LED <= 26'b00000011111111111111111111; 71 27:LED <= 26'b00000111111111111111111111; 72 28:LED <= 26'b00001111111111111111111111; 73 29:LED <= 26'b00011111111111111111111111; 74 30:LED <= 26'b00111111111111111111111111; 75 31:LED <= 26'b01111111111111111111111111; 76 32:LED <= 26'b11111111111111111111111111; 77 33:LED <= 26'b01111111111111111111111111; 78 34:LED <= 26'b00111111111111111111111111; 79 35:LED <= 26'b00011111111111111111111111; 80 36:LED <= 26'b00001111111111111111111111; 81 37:LED <= 26'b00000111111111111111111111; 82 38:LED <= 26'b00000011111111111111111111; 83 39:LED <= 26'b00000001111111111111111111; 84 40:LED <= 26'b00000000111111111111111111; 85 41:LED <= 26'b00000000011111111111111111; 86 42:LED <= 26'b00000000001111111111111111; 87 43:LED <= 26'b00000000000111111111111111; 88 44:LED <= 26'b00000000000011111111111111; 89 45:LED <= 26'b00000000000001111111111111; 90 46:LED <= 26'b00000000000000111111111111; 91 47:LED <= 26'b00000000000000011111111111; 92 48:LED <= 26'b00000000000000001111111111; 93 49:LED <= 26'b00000000000000000111111111; 94 50:LED <= 26'b00000000000000000011111111; 95 51:LED <= 26'b00000000000000000001111111; 96 52:LED <= 26'b00000000000000000000111111; 97 53:LED <= 26'b00000000000000000000011111; 98 54:LED <= 26'b00000000000000000000001111; 99 55:LED <= 26'b00000000000000000000000111; 100 56:LED <= 26'b00000000000000000000000011; 101 57:LED <= 26'b00000000000000000000000001; 102 58:LED <= 26'b01010101010101010101010101; 103 59:LED <= 26'b10101010101010101010101010; 104 60:LED <= 26'b01010101010101010101010101; 105 61:LED <= 26'b10101010101010101010101010; 106 62:LED <= 26'b00000000000000000000000001; 107 63:LED <= 26'b00000000000000000000000010; 108 64:LED <= 26'b00000000000000000000000100; 109 65:LED <= 26'b00000000000000000000001000; 110 66:LED <= 26'b00000000000000000000010000; 111 67:LED <= 26'b00000000000000000000100000; 112 68:LED <= 26'b00000000000000000001000000; 113 69:LED <= 26'b00000000000000000010000000; 114 70:LED <= 26'b00000000000000000100000000; 115 71:LED <= 26'b00000000000000001000000000; 116 72:LED <= 26'b00000000000000010000000000; 117 73:LED <= 26'b00000000000000100000000000; 118 74:LED <= 26'b00000000000001000000000000; 119 75:LED <= 26'b00000000000010000000000000; 120 76:LED <= 26'b00000000000100000000000000; 121 77:LED <= 26'b00000000001000000000000000; 122 78:LED <= 26'b00000000010000000000000000; 123 79:LED <= 26'b00000000100000000000000000; 124 80:LED <= 26'b00000001000000000000000000; 125 81:LED <= 26'b00000010000000000000000000; 126 82:LED <= 26'b00000100000000000000000000; 127 83:LED <= 26'b00001000000000000000000000; 128 84:LED <= 26'b00010000000000000000000000; 129 85:LED <= 26'b00100000000000000000000000; 130 86:LED <= 26'b01000000000000000000000000; 131 87:LED <= 26'b10000000000000000000000000; 132 88:LED <= 26'b01000000000000000000000000; 133 89:LED <= 26'b00100000000000000000000000; 134 90:LED <= 26'b00010000000000000000000000; 135 91:LED <= 26'b00001000000000000000000000; 136 92:LED <= 26'b00000100000000000000000000; 137 93:LED <= 26'b00000010000000000000000000; 138 94:LED <= 26'b00000001000000000000000000; 139 95:LED <= 26'b00000000100000000000000000; 140 96:LED <= 26'b00000000010000000000000000; 141 97:LED <= 26'b00000000001000000000000000; 142 98:LED <= 26'b00000000000100000000000000; 143 99:LED <= 26'b00000000000010000000000000; 144 100:LED <= 26'b00000000000010000000000000; 145 101:LED <= 26'b00000000000001000000000000; 146 102:LED <= 26'b00000000000000100000000000; 147 103:LED <= 26'b00000000000000010000000000; 148 104:LED <= 26'b00000000000000001000000000; 149 105:LED <= 26'b00000000000000000100000000; 150 106:LED <= 26'b00000000000000000010000000; 151 107:LED <= 26'b00000000000000000001000000; 152 108:LED <= 26'b00000000000000000000100000; 153 109:LED <= 26'b00000000000000000000010000; 154 110:LED <= 26'b00000000000000000000001000; 155 111:LED <= 26'b00000000000000000000000100; 156 112:LED <= 26'b00000000000000000000000010; 157 113:LED <= 26'b00000000000000000000000001; 158 default:LED<=26'b00000000000000000000000000; 159 endcase 160 end 161 endmodule