[英]How to change led direction depending on button pressed using C, PIC16 and PIC development board?
我创建了一个程序来根据在端口 a 和端口 b 上按下哪个按钮来切换 LED 灯,因为当在端口 a 上按下按钮时,LED 会从左到右或从右到左闪烁。 当按下按钮 3 时,LED 开始从左到右弹跳并重置并继续永远做同样的事情。 当按下按钮 0 时,它会反转方向并从右向左移动。 当按下按钮 4 时,它再次反转方向,现在它从左向右移动。 我的问题是,一旦按下按钮 4,我无法通过按下按钮 0 再次反转方向。我将如何修改代码以使其工作? 使用PIC16F877A和pic开发板
#include <system.h>
void delay(int j) {
int i;
int x = 8600;
while (j != 0) {
for (i = x; i != 0; i--);
j--;
}
}
void main() {
trisb = 0; // sets all bits in port B as outputs
adcon1 = 0x06; // sets port A as digital inputs
while (1) // creates infinite loop
{
if ((porta & 0x8)) { // Switch 3 - Left bounces from left to right and right to left
portb = 0x80;
delay(1);
while (1) {
while ((portb != 0)) { // Shifts LED from left to right as long as LED 0 is not active
portb = portb >> 1;
delay(1);
if ((portb == 0)) { // When LED 0 is lit, it resets to LED 7
portb = 0x80;
delay(1);
}
if ((porta & 0x1)) { // If SA0 is pressed, LED shifting direction will reverse (right to left)
while ((portb != 0x80)) {
portb = portb << 1;
delay(1);
if ((portb == 0x80)) {
portb = 0x1;
delay(1);
}
if ((porta & 0x10)) { // If SA4 is pressed, LED shifting direction will reverse (left to right)
while ((portb != 0)) {
portb = portb >> 1;
delay(1);
if ((portb == 0)) {
portb = 0x80;
delay(1);
}
}
}
}
}
}
}
}
}
}
编辑:
void shiftRight() {
char value;
value = 0x80;
for (int i = 0; i < 8; i++) {
portb = value;
value = value >> 1;
delay(1);
}
}
void shiftLeft() {
char value;
value = 0x1;
for (int i = 0; i < 8; i++) {
portb = value;
value = value << 1;
delay(1);
}
}
编辑2:
#include <system.h>
void delay(int j) {
int i;
int x = 8600;
while (j != 0) {
for (i = x; i != 0; i--);
j--;
}
}
void shiftRight() {
char value = 0b10000000;
for (int i = 0; i < 8; i++) {
portb = value;
value = value >> 1;
delay(1);
}
}
void shiftLeft() {
char value = 0b00000001;
for (int i = 0; i < 8; i++) {
portb = value;
value = value << 1;
delay(1);
}
}
void main() {
trisb = 0; // sets all bits in port B as outputs
adcon1 = 0x06; // sets port A as digital inputs
int movingRight = 1; //which way the led is moving - 0 = left, 1 = right
// creates infinite loop
while(1)
{
if(movingRight == 1)
{
//led is moving right
shiftRight();
if((porta & 0b00000001) && portb == 0b00000001) /*right button is pressed AND led is at the far right*/
{
//flip direction
movingRight = 0;
}
}
else
{
//led is moving left
shiftLeft();
if((porta& 0b00010000) && portb == 0b10000000) /*left button is pressed AND led is at the left right*/
{
//flip direction
movingRight = 1;
}
}
}
}
使用 state 变量来跟踪 LED 是否应该弹跳、向左移动或向右移动,并使用没有任何嵌套 while 循环的单个 while 循环。 每次通过循环时,您会延迟一段时间,然后根据变量向左或向右移动 LED,然后检查按钮的状态,相应地设置 state 变量。 就像 Weather Vane 所说,将 LED 模式保持在变量中也是一个好主意。
可能的类似 Python 的伪代码是
state = 0 # 0 = LEDs off
# 1 = LEDs bounce left
# 2 = LEDs bounce right
# 3 = LEDs cycle left
# 4 = LEDs cycle right
led_pattern = 0x00
while True:
set_leds(led_pattern) # portb = led_pattern
delay()
# Update led_pattern according to the current state
if state == 1 or state == 3: # bounce or cycle left
led_pattern <<= 1
if led_pattern & 0xFF == 0x00:
if state == 1: # bounce
led_pattern = 0x40
state = 2 # start bouncing right
else:
led_pattern = 0x01
if state == 2 or state == 4: # bounce or cycle right
led_pattern >>= 1
if led_pattern == 0x00:
if state == 2: # bounce
led_pattern = 0x02
state = 1 # start bouncing left
else:
led_pattern = 0x80
# Check the buttons and change state if necesssary
if button_3_pressed(): # porta & 0x08
if state == 1 or state == 3:
state = 1 # continue moving left if already moving left
else:
if state == 0:
led_pattern = 0x80
state = 2 # otherwise start moving LEDs to the right
if button_0_pressed(): # porta & 0x01
state = 3 # start cycling left
if button_3_pressed(): # porta & 0x10
state = 4 # start cycling right
该程序以state
变量设置为0
开始,而 led_pa led_pattern
变量设置为0x00
。 在 state 0
中,循环不会更改led_pattern
,因此它始终保持0x00
保持所有 LED 关闭。 但是,它正在检查按钮的状态,如果按下其中任何一个按钮,它将更改 state 变量的值。
在 state 0
中按下按钮 3 会导致state
变量更改为2
并将led_pattern
变量设置为0x80
。 而在 state 2
中,循环的每次迭代将led_pattern
向右移动一位。 当这导致led_pattern
变为 0 时,它会“反弹”将led_pattern
设置为 0x02(因为它在循环开始时是 0x01),并将state
变量更改为1
。 在 state 1
中, led_pattern
变量向左移动,一旦它“反弹”, state 就会变回2
。
在任何 state 原因中按下按钮 1 会导致state
变量更改为3
。 在此 state 中, led_pattern
变量在循环的每次迭代期间向左移动,就像在 state 1
中一样,但不是“弹跳”它“循环”。 由于这不会改变 LED 应该移动的方向,因此 state 保持为3
,并且仅更新了led_pattern
。 同样按下按钮 4 将 state 更改为4
,其中 LED 循环向右。
现在,如果这实际上是您希望代码执行的操作,您的问题并不完全清楚。 例如,在检查按钮之前,它不会等到移动的 LED 达到其移动的终点。 您可以随时按下按钮,这将导致 LED 立即开始朝指示的方向移动。
无论如何,您应该能够看到如何仅使用一个 while 循环和一个或两个 state 变量来实现您想要的任何行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.