C Arduino:如何解决计时器不能在4位7段显示器上正确显示数字的问题?
代码的作用:C Arduino:如何解决计时器不能在4位7段显示器上正确显示数字的问题?,c,arduino,arduino-uno,C,Arduino,Arduino Uno,代码的作用: 代码首先显示一个简短的“闪烁动画”,其中a-F段一次打开和关闭一个 “闪烁动画”持续3-10秒,计时器启动 计时器使用数字1和2从0-99秒开始计数,而数字3和4分别显示第100和第10秒(对不起,英语中的装置使用不当!基本上=100毫秒和10毫秒) 通过使用已连接的按钮,您可以通过按下它来停止计时器。计时器停止并显示时间 我的问题: 我面临的问题是,在闪烁的动画结束和计时器启动后,显示器将不会在数字2和3中显示单个数字,而是在计时器计数时显示大量数字。然而,我确实注意到,基本上,
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <avr/io.h>
#include <util/delay.h>
#define PORTB_ON PORTB = 255;
#define PORTD_ON PORTD = 255;
#define PORTB_OFF PORTB = 0;
#define PORTD_OFF PORTD = 0;
//SEGEMENTS ON
#define SEGMENT_A_ON PORTB = PORTB | 1 << PB3; //SEGMENT-A light on (Arduino Pin 11)
#define SEGMENT_B_ON PORTD = PORTD | 1 << PD7; //SEGMENT-B light on (Arduino Pin 7)
#define SEGMENT_C_ON PORTD = PORTD | 1 << PD4; //SEGMENT-C light on (Arduino Pin 4)
#define SEGMENT_D_ON PORTD = PORTD | 1 << PD2; //SEGMENT-D light on (Arduino Pin 2)
#define SEGMENT_E_ON PORTB = PORTB | 1 << PB5; //SEGMENT-E light on (Arduino Pin 13)
#define SEGMENT_F_ON PORTB = PORTB | 1 << PB2; //SEGMENT-F light on (Arduino Pin 10)
#define SEGMENT_G_ON PORTD = PORTD | 1 << PD5; //SEGMENT-G light on (Arduino Pin 5)
//SEGMENTS OFF
#define SEGMENT_A_OFF PORTB = PORTB & ~1 << PB3; //SEGMENT-A light off (Arduino Pin 11)
#define SEGMENT_B_OFF PORTD = PORTD & ~1 << PD7; //SEGMENT-B light off (Arduino Pin 7)
#define SEGMENT_C_OFF PORTD = PORTD & ~1 << PD4; //SEGMENT-C light off (Arduino Pin 4)
#define SEGMENT_D_OFF PORTD = PORTD & ~1 << PD2; //SEGMENT-D light off (Arduino Pin 2)
#define SEGMENT_E_OFF PORTB = PORTB & ~1 << PB5; //SEGMENT-E light off (Arduino Pin 13)
#define SEGMENT_F_OFF PORTB = PORTB & ~1 << PB2; //SEGMENT-F light off (Arduino Pin 10)
#define SEGMENT_G_OFF PORTD = PORTD & ~1 << PD5; //SEGMENT-G light off (Arduino Pin 5)
//DIGITS ON = Display , 2, 3 & 4
#define DIGIT_1_ON PORTB = PORTB & ~1 << PB4; //DISPLAY 1 off (Arduino Pin 12)
#define DIGIT_2_ON PORTB = PORTB & ~1 << PB1; //DISPLAY 2 off (Arduino Pin 9)
#define DIGIT_3_ON PORTB = PORTB & ~1 << PB0; //DISPLAY 3 off (Arduino Pin 8)
#define DIGIT_4_ON PORTD = PORTD & ~1 << PD6; //DISPLAY 4 off (Arduino Pin 6)
//DIGITS OFF = Display: 1, 2, 3 & 4
#define DIGIT_1_OFF PORTB = PORTB | 1 << PB4; //DISPLAY 1 on (Arduino Pin 12)
#define DIGIT_2_OFF PORTB = PORTB | 1 << PB1; //DISPLAY 2 on (Arduino Pin 9)
#define DIGIT_3_OFF PORTB = PORTB | 1 << PB0; //DISPLAY 3 on (Arduino Pin 8)
#define DIGIT_4_OFF PORTD = PORTD | 1 << PD6; //DISPLAY 4 on (Arduino Pin 6)
void startDisplay();
void stopDisplay();
void choseDigit(int x);
void showNumberOnDisplay(int x);
void blinkingAnimation();
void zero();
void one();
void two();
void three();
void four();
void five();
void six();
void seven();
void eight();
void nine();
int x = 100;
long number = 0;
int recentTime;
int digit1, digit2, digit3, digit4;
int stopButton;
int main()
{
DDRB = 255;
DDRD = 0b11011111;
srand(time(NULL));
while (1)
{
if (number == 0)
{
int randomWaitTime = rand() % 10 + 3; //Calls random number between 3-10
for (int i = 0; i < randomWaitTime; i++)
{
blinkingAnimation();
}
}
startDisplay();
if (PIND & 1 << PD3)
{
stopButton = 0;
}
else
{
stopButton = 1;
}
if (stopButton == 0)
{
stopDisplay();
}
}
return 0;
}
void startDisplay()
{
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(1); //Turn Display 1 on
showNumberOnDisplay((number / 1000)); //Get value of thousand
_delay_ms(2.5); //Delay 2.5ms
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(2); //Turn Display 2 on
showNumberOnDisplay((number % 1000) / 100); //Get value of hundred
_delay_ms(2.5); //Delay 2.5ms
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(3); //Turn Display 3 on
showNumberOnDisplay(number % 100 / 10); //Get value of ten
_delay_ms(2.5); //Delay 2.5ms
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(4); //Turn Display 3 on
showNumberOnDisplay(number % 10); //Get value of single digit
_delay_ms(2.5); //Delay 2.5ms
number++;
if (number == 9999) // Reset number count to 1;
{
number = 1;
}
}
void stopDisplay()
{
recentTime = number;
digit1 = recentTime / 1000;
digit2 = (recentTime % 1000) / 100;
digit3 = recentTime % 100 / 10;
digit4 = recentTime % 10;
while (stopButton == 0)
{
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(1); //Turn Display 1 on
showNumberOnDisplay(digit1); //Get value of thousand
_delay_ms(2.5); //Delay 2.5ms
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(2); //Turn Display 2 on
showNumberOnDisplay(digit2); //Get value of hundred
_delay_ms(2.5); //Delay 2.5ms
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(3); //Turn Display 3 on
showNumberOnDisplay(digit3); //Get value of ten
_delay_ms(2.5); //Delay 2.5ms
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(4); //Turn Display 3 on
showNumberOnDisplay(digit4); //Get value of single digit
_delay_ms(2.5); //Delay 2.5ms
}
}
void choseDigit(int x)
{
DIGIT_1_OFF
DIGIT_2_OFF
DIGIT_3_OFF
DIGIT_4_OFF
switch (x)
{
case 1:
DIGIT_1_ON //Display 1 on
break;
case 2:
DIGIT_2_ON //Display 2 on
break;
case 3:
DIGIT_3_ON //Display 3 on
break;
case 4:
DIGIT_4_ON //Display 4 on
break;
}
}
void showNumberOnDisplay(int x)
{
switch (x)
{
case 1:
one();
break;
case 2:
two();
break;
case 3:
three();
break;
case 4:
four();
break;
case 5:
five();
break;
case 6:
six();
break;
case 7:
seven();
break;
case 8:
eight();
break;
case 9:
nine();
break;
default:
zero();
break;
}
}
void blinkingAnimation()
{
DIGIT_1_ON
DIGIT_2_ON
DIGIT_3_ON
DIGIT_4_ON
SEGMENT_B_OFF
SEGMENT_E_ON;
_delay_ms(165);
SEGMENT_E_OFF;
SEGMENT_D_ON
_delay_ms(165);
SEGMENT_D_OFF
SEGMENT_C_ON
_delay_ms(165);
SEGMENT_C_OFF
SEGMENT_B_ON
_delay_ms(165);
SEGMENT_B_OFF
SEGMENT_A_ON
_delay_ms(165);
SEGMENT_A_OFF
SEGMENT_F_ON
_delay_ms(165);
SEGMENT_F_OFF
}
void turnAllSegmentsOff()
{
PORTB_OFF
PORTD_OFF
}
//Display number 0
void zero()
{
SEGMENT_A_ON;
SEGMENT_B_ON;
SEGMENT_C_ON;
SEGMENT_D_ON;
SEGMENT_E_ON;
SEGMENT_F_ON;
}
//DISPLAY number 1
void one()
{
SEGMENT_B_ON;
SEGMENT_C_ON;
}
//DISPLAY number 2
void two()
{
SEGMENT_A_ON;
SEGMENT_B_ON;
SEGMENT_G_ON;
SEGMENT_E_ON;
SEGMENT_D_ON;
}
//DISPLAY number 3
void three()
{
SEGMENT_A_ON;
SEGMENT_B_ON;
SEGMENT_G_ON;
SEGMENT_C_ON;
SEGMENT_D_ON;
}
//DISPLAY number 4
void four()
{
SEGMENT_F_ON;
SEGMENT_G_ON;
SEGMENT_B_ON;
SEGMENT_C_ON;
}
//DISPLAY number 5
void five()
{
SEGMENT_A_ON;
SEGMENT_F_ON;
SEGMENT_G_ON;
SEGMENT_C_ON;
SEGMENT_D_ON;
}
//DISPLAY number 6
void six()
{
SEGMENT_A_ON;
SEGMENT_F_ON;
SEGMENT_E_ON;
SEGMENT_D_ON;
SEGMENT_C_ON;
SEGMENT_G_ON;
}
//DISPLAY number 7
void seven()
{
SEGMENT_A_ON;
SEGMENT_B_ON;
SEGMENT_C_ON;
}
//DISPLAY number 8
void eight()
{
SEGMENT_A_ON;
SEGMENT_B_ON;
SEGMENT_C_ON;
SEGMENT_D_ON;
SEGMENT_E_ON;
SEGMENT_F_ON;
SEGMENT_G_ON;
}
//DISPLAY number 9
void nine()
{
SEGMENT_A_ON;
SEGMENT_F_ON;
SEGMENT_G_ON;
SEGMENT_B_ON;
SEGMENT_C_ON;
SEGMENT_D_ON;
}
#包括
#包括
#包括
#包括
#包括
#在端口B=255上定义端口B_;
#在PORTD=255上定义PORTD_;
#定义端口B_OFF端口B=0;
#定义PORTD_OFF PORTD=0;
//分段
#定义段A_ON PORTB=PORTB | 1两个观察A)main()
具有不匹配的{
大括号}
和b)您从不调用函数stopDisplay()
.Hmm…噢!我会看一看,可能只是清理代码时的一个愚蠢的失误。我只想保留对我的问题至关重要的东西,同时保持整洁并消除任何干扰。有很多不必要的描述,不太清楚什么有效,什么无效。我编辑了问题。在代码中添加了“stopDisplay”部分并修复了大括号。我在“代码的作用”中发布的所有内容都有效。只是当数字显示在屏幕上时,它不会正确地显示它们。这个问题在“问题”一节中有更详细的解释。对于每个数字,您似乎要关闭所有段,然后只启用其中一个,然后延迟。此外,在choseDigit()
中,您似乎将它们全部打开,然后关闭一个,尽管注释中显示的是相反的digital\u 1\u off//Display 1 on
。可见性最差的数字是其值变化最频繁的数字。你认为1/100秒变化的数字看起来有多清晰?但我并没有真正遵循逻辑。我不明白你为什么不连续写下所有的4位数字。还要注意的是,如果你一直启用和禁用,你会得到闪烁。
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <avr/io.h>
#include <util/delay.h>
#define PORTB_ON PORTB = 255;
#define PORTD_ON PORTD = 255;
#define PORTB_OFF PORTB = 0;
#define PORTD_OFF PORTD = 0;
//SEGEMENTS ON
#define SEGMENT_A_ON PORTB = PORTB | 1 << PB3; //SEGMENT-A light on (Arduino Pin 11)
#define SEGMENT_B_ON PORTD = PORTD | 1 << PD7; //SEGMENT-B light on (Arduino Pin 7)
#define SEGMENT_C_ON PORTD = PORTD | 1 << PD4; //SEGMENT-C light on (Arduino Pin 4)
#define SEGMENT_D_ON PORTD = PORTD | 1 << PD2; //SEGMENT-D light on (Arduino Pin 2)
#define SEGMENT_E_ON PORTB = PORTB | 1 << PB5; //SEGMENT-E light on (Arduino Pin 13)
#define SEGMENT_F_ON PORTB = PORTB | 1 << PB2; //SEGMENT-F light on (Arduino Pin 10)
#define SEGMENT_G_ON PORTD = PORTD | 1 << PD5; //SEGMENT-G light on (Arduino Pin 5)
//SEGMENTS OFF
#define SEGMENT_A_OFF PORTB = PORTB & ~1 << PB3; //SEGMENT-A light off (Arduino Pin 11)
#define SEGMENT_B_OFF PORTD = PORTD & ~1 << PD7; //SEGMENT-B light off (Arduino Pin 7)
#define SEGMENT_C_OFF PORTD = PORTD & ~1 << PD4; //SEGMENT-C light off (Arduino Pin 4)
#define SEGMENT_D_OFF PORTD = PORTD & ~1 << PD2; //SEGMENT-D light off (Arduino Pin 2)
#define SEGMENT_E_OFF PORTB = PORTB & ~1 << PB5; //SEGMENT-E light off (Arduino Pin 13)
#define SEGMENT_F_OFF PORTB = PORTB & ~1 << PB2; //SEGMENT-F light off (Arduino Pin 10)
#define SEGMENT_G_OFF PORTD = PORTD & ~1 << PD5; //SEGMENT-G light off (Arduino Pin 5)
//DIGITS ON = Display , 2, 3 & 4
#define DIGIT_1_ON PORTB = PORTB & ~1 << PB4; //DISPLAY 1 off (Arduino Pin 12)
#define DIGIT_2_ON PORTB = PORTB & ~1 << PB1; //DISPLAY 2 off (Arduino Pin 9)
#define DIGIT_3_ON PORTB = PORTB & ~1 << PB0; //DISPLAY 3 off (Arduino Pin 8)
#define DIGIT_4_ON PORTD = PORTD & ~1 << PD6; //DISPLAY 4 off (Arduino Pin 6)
//DIGITS OFF = Display: 1, 2, 3 & 4
#define DIGIT_1_OFF PORTB = PORTB | 1 << PB4; //DISPLAY 1 on (Arduino Pin 12)
#define DIGIT_2_OFF PORTB = PORTB | 1 << PB1; //DISPLAY 2 on (Arduino Pin 9)
#define DIGIT_3_OFF PORTB = PORTB | 1 << PB0; //DISPLAY 3 on (Arduino Pin 8)
#define DIGIT_4_OFF PORTD = PORTD | 1 << PD6; //DISPLAY 4 on (Arduino Pin 6)
void startDisplay();
void stopDisplay();
void choseDigit(int x);
void showNumberOnDisplay(int x);
void blinkingAnimation();
void zero();
void one();
void two();
void three();
void four();
void five();
void six();
void seven();
void eight();
void nine();
int x = 100;
long number = 0;
int recentTime;
int digit1, digit2, digit3, digit4;
int stopButton;
int main()
{
DDRB = 255;
DDRD = 0b11011111;
srand(time(NULL));
while (1)
{
if (number == 0)
{
int randomWaitTime = rand() % 10 + 3; //Calls random number between 3-10
for (int i = 0; i < randomWaitTime; i++)
{
blinkingAnimation();
}
}
startDisplay();
if (PIND & 1 << PD3)
{
stopButton = 0;
}
else
{
stopButton = 1;
}
if (stopButton == 0)
{
stopDisplay();
}
}
return 0;
}
void startDisplay()
{
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(1); //Turn Display 1 on
showNumberOnDisplay((number / 1000)); //Get value of thousand
_delay_ms(2.5); //Delay 2.5ms
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(2); //Turn Display 2 on
showNumberOnDisplay((number % 1000) / 100); //Get value of hundred
_delay_ms(2.5); //Delay 2.5ms
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(3); //Turn Display 3 on
showNumberOnDisplay(number % 100 / 10); //Get value of ten
_delay_ms(2.5); //Delay 2.5ms
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(4); //Turn Display 3 on
showNumberOnDisplay(number % 10); //Get value of single digit
_delay_ms(2.5); //Delay 2.5ms
number++;
if (number == 9999) // Reset number count to 1;
{
number = 1;
}
}
void stopDisplay()
{
recentTime = number;
digit1 = recentTime / 1000;
digit2 = (recentTime % 1000) / 100;
digit3 = recentTime % 100 / 10;
digit4 = recentTime % 10;
while (stopButton == 0)
{
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(1); //Turn Display 1 on
showNumberOnDisplay(digit1); //Get value of thousand
_delay_ms(2.5); //Delay 2.5ms
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(2); //Turn Display 2 on
showNumberOnDisplay(digit2); //Get value of hundred
_delay_ms(2.5); //Delay 2.5ms
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(3); //Turn Display 3 on
showNumberOnDisplay(digit3); //Get value of ten
_delay_ms(2.5); //Delay 2.5ms
turnAllSegmentsOff(); //Turn ALL segments off
choseDigit(4); //Turn Display 3 on
showNumberOnDisplay(digit4); //Get value of single digit
_delay_ms(2.5); //Delay 2.5ms
}
}
void choseDigit(int x)
{
DIGIT_1_OFF
DIGIT_2_OFF
DIGIT_3_OFF
DIGIT_4_OFF
switch (x)
{
case 1:
DIGIT_1_ON //Display 1 on
break;
case 2:
DIGIT_2_ON //Display 2 on
break;
case 3:
DIGIT_3_ON //Display 3 on
break;
case 4:
DIGIT_4_ON //Display 4 on
break;
}
}
void showNumberOnDisplay(int x)
{
switch (x)
{
case 1:
one();
break;
case 2:
two();
break;
case 3:
three();
break;
case 4:
four();
break;
case 5:
five();
break;
case 6:
six();
break;
case 7:
seven();
break;
case 8:
eight();
break;
case 9:
nine();
break;
default:
zero();
break;
}
}
void blinkingAnimation()
{
DIGIT_1_ON
DIGIT_2_ON
DIGIT_3_ON
DIGIT_4_ON
SEGMENT_B_OFF
SEGMENT_E_ON;
_delay_ms(165);
SEGMENT_E_OFF;
SEGMENT_D_ON
_delay_ms(165);
SEGMENT_D_OFF
SEGMENT_C_ON
_delay_ms(165);
SEGMENT_C_OFF
SEGMENT_B_ON
_delay_ms(165);
SEGMENT_B_OFF
SEGMENT_A_ON
_delay_ms(165);
SEGMENT_A_OFF
SEGMENT_F_ON
_delay_ms(165);
SEGMENT_F_OFF
}
void turnAllSegmentsOff()
{
PORTB_OFF
PORTD_OFF
}
//Display number 0
void zero()
{
SEGMENT_A_ON;
SEGMENT_B_ON;
SEGMENT_C_ON;
SEGMENT_D_ON;
SEGMENT_E_ON;
SEGMENT_F_ON;
}
//DISPLAY number 1
void one()
{
SEGMENT_B_ON;
SEGMENT_C_ON;
}
//DISPLAY number 2
void two()
{
SEGMENT_A_ON;
SEGMENT_B_ON;
SEGMENT_G_ON;
SEGMENT_E_ON;
SEGMENT_D_ON;
}
//DISPLAY number 3
void three()
{
SEGMENT_A_ON;
SEGMENT_B_ON;
SEGMENT_G_ON;
SEGMENT_C_ON;
SEGMENT_D_ON;
}
//DISPLAY number 4
void four()
{
SEGMENT_F_ON;
SEGMENT_G_ON;
SEGMENT_B_ON;
SEGMENT_C_ON;
}
//DISPLAY number 5
void five()
{
SEGMENT_A_ON;
SEGMENT_F_ON;
SEGMENT_G_ON;
SEGMENT_C_ON;
SEGMENT_D_ON;
}
//DISPLAY number 6
void six()
{
SEGMENT_A_ON;
SEGMENT_F_ON;
SEGMENT_E_ON;
SEGMENT_D_ON;
SEGMENT_C_ON;
SEGMENT_G_ON;
}
//DISPLAY number 7
void seven()
{
SEGMENT_A_ON;
SEGMENT_B_ON;
SEGMENT_C_ON;
}
//DISPLAY number 8
void eight()
{
SEGMENT_A_ON;
SEGMENT_B_ON;
SEGMENT_C_ON;
SEGMENT_D_ON;
SEGMENT_E_ON;
SEGMENT_F_ON;
SEGMENT_G_ON;
}
//DISPLAY number 9
void nine()
{
SEGMENT_A_ON;
SEGMENT_F_ON;
SEGMENT_G_ON;
SEGMENT_B_ON;
SEGMENT_C_ON;
SEGMENT_D_ON;
}