目标
对7个led进行流水灯编程
程序
// 定义一个模块类,这里命名为LedWaterFlow
class LedWaterFlow extends Module {
// IO端口,这里我们假设有一个时钟输入和复位信号
val io = IO(new Bundle {
val clock = Input(Clock())
val reset = Input(Bool())
val leds = Output(Vec(7, Bool())) // 7个LED,每个LED一个布尔输出端口
})
// 使用寄存器来存储当前LED的位置
val currentLed = RegInit(0.U(3.W)) // 3位足够表示0到7
// 在每个时钟上升沿,更新当前LED的位置
io.clock.foreach { clock =>
when (reset) {
// 如果复位信号为真,重置LED位置
currentLed := 0.U
} .otherwise {
// 否则,更新LED位置
when (currentLed === 6.U) { // 如果当前是最后一个LED
currentLed := 0.U // 回到第一个LED
} .otherwise {
currentLed := currentLed + 1.U // 否则,移动到下一个LED
}
}
}
// 根据当前LED的位置,设置LED状态
io.leds := (0 until 7).map(i => currentLed === i.U) // 点亮当前位置的LED
// 可选:添加一个快速闪烁的LED来表示系统状态
// 例如,每4个时钟周期闪烁一次
val blink = RegInit(false.B)
when (clock) {
blink := !blink // 每时钟周期翻转状态
}
io.leds(6) := blink // 第7个LED用于快速闪烁
}
// 实例化LedWaterFlow模块
object LedWaterFlow extends App {
// 使用Chisel的Driver来运行模拟
chisel3.Driver.execute(args, () => new LedWaterFlow)
}