【发布时间】:2015-02-08 17:21:13
【问题描述】:
这不是作业,我只是想学习嵌入式 C 以摆脱 Arduino。 (基于this interrupt LED example)
我正在尝试创建一个中断输入事件计数器。使用连接到 P3.2 的去抖动按钮开关来增加 LCD 上的 3 位数字。启动时显示 000,并且中断中的切换 LED 确实会关闭和打开,但显示编号不会递增。我的目标是:push 1:显示 001,push 2:显示 002,等等。按下 Reset 回到 000。
我认为问题出在 while() 上。当 while() 被评论时,LCD 显示屏快速闪烁并且看不清楚,但我可以看到在按下开关时出现“1”(LED 亮起)——当释放开关时,LED 熄灭(不切换)然而,'1' 清除。未注释 while() 时,LCD 显示稳定清晰,LED 开关关闭和打开,但没有计数器编号增量。
代码:
// ****Event counter using external interrupt 0****
#include<reg51.h>
#include "lcd4bit.h"
void cct_init(void);
void InitINT0(void);
sbit LED = P2^3; // Int status LED
unsigned char str1[]="Line2";
char stringy[3] = {'0','0','0'};
char count = 0;
int number = 0;
void main()
{
char count = 0;
if (number != 0)
cct_init(); // Make all ports zero
InitINT0(); // Intialize INT0 interrupts
stringy[count]=(number%10)+48;
number/=10;
init_lcd();
gotoxy_lcd(1,1); //(Col, Line#)
print_lcd("Line One");
gotoxy_lcd(1,2);
print_lcd(stringy);
while(1)
{ }
}
void cct_init(void) // Init CCT function
{
P3 = 0x04; // Make P3.2 (INT0) pin high only
}
void InitINT0(void) // External INT0 pin interrupt init function
{
IT0 = 1; //Edge triggered interrupt mode (Neg)
EX0 = 1; //Enable external interrupt INT0
EA = 1; //Enable global interrupts
}
void external0_isr(void) interrupt 0 // Inturrupt 0 ISR
{
number = number + 1;
LED = ~LED; // Toggle LED
}
任何建议表示赞赏。
【问题讨论】:
-
代码在中断期间或
while()循环中似乎没有改变显示,那么除了 LED 之外,为什么任何可见的东西都应该改变? -
您可以在 AVR 上使用嵌入式 C,它将使用您的 Arduino 硬件,让您免于 8051 的恐惧。