我使用的是Vishay,TDCG1050M用于LED显示屏,MSP430 Launchpad用于微控制器,Energia用于草图。我的每个引脚都有110欧姆的电阻器,用于点亮LED和两个NPN晶体管(每显示一位数字)。
我认为我拥有的代码可以正常工作,但是显示仍然不正确。当两个按钮连接到电路上时,LED显示屏必须读取00-99。第一个按钮将值增加一个,而第二个按钮将值减少一个。 LED正确显示值0-9,但是当它在2和5上时分别显示“ 22”和“ 55”。当试图增加(使用按钮)到“ 10”时,LED仅显示“ 0”,而不显示十位的1。 LED确实正确显示20-29(除了显示25,看起来像55)。我还重新布线了整个电路,它仍在读取相同的值。
我尝试更换一些电线,晶体管和LED。我还尝试了一个新的面包板。我尝试了两台不同的计算机,第一台是PC,第二台是Mac。
#define D1 P2_2 //Display digit 1 control
#define D2 P2_0 //Display digit 2 control
#define segA P2_6 //Display segment A control
#define segB P8_2 //Display segment B control
#define segC P3_1 //Display segment C control
#define segD P8_1 //Display segment D control
#define segE P2_3 //Display segment E control
#define segF P7_4 //Display segment F control
#define segG P3_7 //Display segment G control
#define digit_pause 2
#define button1 P2_4
#define button2 P2_5
#define debounceDelay 10
volatile boolean button1_flag = 0;
volatile long lastDebounceTime = 0;
volatile boolean pressed_event_flag = 0;
volatile boolean released_event_flag = 0;
volatile boolean button2_flag = 0;
volatile boolean pressed_event_flag2 = 0;
volatile boolean released_event_flag2 = 0;
int counter = 0;
int number = 0;
int number2 = 0;
int digit = 0;
int digits[] = {D1, D2};
int segments[] {segA, segB, segC, segD, segE, segF, segG};
boolean digit0[] = {0, 0, 0, 0, 0, 0, 1};
boolean digit1[] = {1, 0, 0, 1, 1, 1, 1};
boolean digit2[] = {0, 0, 1, 0, 0, 1, 0};
boolean digit3[] = {0, 0, 0, 0, 1, 1, 0};
boolean digit4[] = {1, 0, 0, 1, 1, 0, 0};
boolean digit5[] = {0, 1, 0, 0, 1, 0, 0};
boolean digit6[] = {0, 1, 0, 0, 0, 0 ,0};
boolean digit7[] = {0, 0 ,0, 1, 1, 1, 1};
boolean digit8[] = {0, 0, 0, 0, 0, 0, 0};
boolean digit9[] = {0, 0, 0, 0, 1, 0, 0};
void setup()
{
pinMode(button1, INPUT);
attachInterrupt(button1, button1_event, CHANGE);
pinMode(button2, INPUT);
attachInterrupt(button2, button2_event, CHANGE);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
for(int sweep = 0; sweep < 2; sweep++) {
digitalWrite(digits[sweep],LOW);
}
Serial.begin(9600);
}
void loop()
{
if(pressed_event_flag) {
if (counter <= 99){
counter++;
}
if(pressed_event_flag) {
if (counter == 100)
counter=99;
}
pressed_event_flag = 0;
}
if(released_event_flag) {
released_event_flag = 0;
}
if(pressed_event_flag2) {
if (counter >= 0){
counter--;
}
pressed_event_flag2 = 0;
}
if(released_event_flag2) {
released_event_flag2 = 0;
}
//the problem could be within this part of the code
delay(2);
number = counter;
digit = number % 10;
digitalWrite(digits[1], HIGH);
digitalWrite(digits[0], LOW);
displayDigit(digit);
delay(5);
number2 = (number - digit) / 10;
digitalWrite(digits[0], HIGH);
digitalWrite(digits[1], LOW);
displayDigit(number2);
number2 = (number - digit) / 10;
delay(5);
}
void displayDigit(int numeral)
{
switch (numeral) {
case 0:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit0[i]);
}
break;
case 1:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit1[i]);
}
break;
case 2:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit2[i]);
}
break;
case 3:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit3[i]);
}
break;
case 4:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit4[i]);
}
break;
case 5:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit5[i]);
}
break;
case 6:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit6[i]);
}
break;
case 7:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit7[i]);
}
break;
case 8:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit8[i]);
}
break;
case 9:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit9[i]);
}
break;
default:
break;
}
}
void button1_event()
{
boolean state = digitalRead(button1); //Determine if pressed or released.
if(state) {
if(!button1_flag) {
if((millis() - lastDebounceTime) >debounceDelay) {
button1_flag = 1;
lastDebounceTime = millis();
pressed_event_flag = 1;
}
}
}
else
{
if(button1_flag) {
if((millis() - lastDebounceTime) >debounceDelay) {
button1_flag = 0;
lastDebounceTime = millis();
released_event_flag = 1;
}
}
}
}
void button2_event()
{
boolean state = digitalRead(button2); //Determine if pressed or released.
if(state) {
if(!button2_flag) {
if((millis() - lastDebounceTime) >debounceDelay) {
button2_flag = 1;
lastDebounceTime = millis();
pressed_event_flag2 = 1;
}
}
}
else
{
if(button2_flag) {
if((millis() - lastDebounceTime) >debounceDelay) {
button2_flag = 0;
lastDebounceTime = millis();
released_event_flag2 = 1;
}
}
}
}