【发布时间】:2021-10-01 00:15:44
【问题描述】:
我目前正在以汇编语言在 Mplab IDE 上使用 PIC18f4420 进行一个项目,其中我有四个 LED。 2 个 LED 告诉我操作#1 的状态,另外 2 个 LED 告诉我另一个操作#2 的状态。最后,我还有 2 个输出 LED 可以根据它们的状态告诉我这 4 个 LED 的状态。例如,下面有一张我要实现的真值表的图片。
我需要帮助,用汇编语言编写一个算法函数。我正在考虑使用 switch 语句来检查 4 个 LED 的状态,从而将 OUTPUT LED 驱动为绿色或红色。
4 个 LED 连接到 PIC 中的不同端口引脚,输出 LED 连接到另一个引脚。我只是不知道如何在 Mplab V8 上为 PIC18f4420 编写汇编代码。我不清楚如何创建此功能。欢迎任何帮助。谢谢
最终输出 LED 将基于 4 个 LED 的颜色状态 RED/GREEN/OFF。
到目前为止,这是我得到的:
Leds_out ; function name
; LED OUT Green = op2_green & ~op1_red
movlw op1_red ; Put the value of op1_red it on the working register
movwf 0x39, f, a ; save it on the file register at this location
comf 0x39, f, a ; complement op1_red of the value on the file register and keep vlaue on the same location on the file register
movlw op2_green ; move op2_green value into the working register
andwf 0x39, w, a ; AND the complement of op1_red in the file register with the vlaue bit of op2_green on the working register and keep value on the Wreg
movwf 0x40, f, a ; move the contents of the Wreg into the file register at this location
movff 0x40, l_out_green, a ; finally move content from file register 0x40 to the l_out_green
;LED Out red = op1red | op2red
movlw op1_red ; put op1_red on the wokring register
movwf 0x41, f, a ; move contents of the Wreg to file register 0x41
movlw op2_red ; Then move op2_red on the working register
iorwf 0x41, f, a ; OR the contents of the working register with what is in the file register 0x41 which is the bit value of op1_red, and keep OR value in the same location on the file register
movff 0x41, l_out_red, a ; Finally move the value bit in the location 0x41 into the port bit l_out_red
| OP#1 Red | OP#1 Green | OP#2 Red | OP#2 Green | LED Out RED | LED Out Green | |
|---|---|---|---|---|---|---|
| 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2 | 0 | 0 | 0 | 1 | 0 | 1 |
| 3 | 0 | 0 | 1 | 0 | 1 | 0 |
| 4 | 0 | 1 | 0 | 0 | 0 | 0 |
| 5 | 1 | 0 | 0 | 0 | 0 | 0 |
| 6 | 1 | 0 | 1 | 0 | 1 | 0 |
| 7 | 0 | 1 | 0 | 1 | 0 | 1 |
| 8 | 0 | 1 | 1 | 0 | 1 | 0 |
| 9 | 1 | 0 | 0 | 1 | 1 | 0 |
【问题讨论】:
-
您不需要在代码中进行分支:如果您可以有效地将 4 个输入位转换为一个 4 位整数,则可以只使用数据查找表。或者,如果两个输出位都可以计算为 4 个输入的布尔函数,那么这也很好。例如LED OUT Green =
op2green & ~op1red似乎适用于您在真值表中显示的 10 行; IDK 如果其他 6 种可能的状态中的任何一种都可能是该表达式的问题,如果它们可能的话。 (我假设第 4 列应该是 OP#2 Green,而不是另一个 OP#1 Green?) -
我也认为
outred = op1red | op2red适用于第一个输出行。 -
哦,是的,看起来它需要一个
& ~out2green(再次假设这是第 4 个输入列,因为图像仍然有重复的标签。另外,不要发布文本图片。堆栈溢出有表格格式markdown:New Feature: Table Support,或者使用代码块。) -
将文本(尤其是代码)复制/粘贴为文本,而不是图像。 (另外,看起来你使用了
add而不是and,除非这是有意使用 add 的 xor + 进位操作。) -
不要发布你的代码图片。
标签: c assembly microcontroller microchip mplab