我试图在微控制器ATmega16 到 7 段显示器上显示我的输入。
我在http://www.avr-tutorials.com/projects/atmega16-based-digital-clock上找到了以下代码,但是当我实施它时,它不起作用。我得到一个波动的输出,即我无法控制特定引脚的输出。 我有两个输入 temp1 和 temp2,我想在三个 7 段显示器上显示它们中的每一个。此外,我没有使用引脚 2 和 3,因为它们是中断引脚,我在其他地方使用过它们。使用 Pin0、1、4、5、6、7。
当添加一些延迟时,代码可以正常工作,否则会在随机引脚处生成输出。 即我想从 PIND1 显示的输出显示在所有引脚上,
我的代码:
#include <avr/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define SegDataPort PORTC
#define SegDataPin PINC
#define SegDataDDR DDRC
#define SegCntrlPort PORTD
#define SegCntrlPin PIND
#define SegCntrlDDR DDRD
/*
* Function Description:
* Encode a Decimal Digit 0-9 to its Seven Segment Equivalent.
*
* Function Arguments:
* digit - Decimal Digit to be Encoded
* common - Common Anode (0), Common Cathode(1)
* SegVal - Encoded Seven Segment Value
*
* Connections:
* Encoded SegVal is return in the other G-F-E-D-C-B-A that is A is the least
* significant bit (bit 0) and G bit 6.
*/
unsigned char DigitTo7SegEncoder(int digit, unsigned char common)
{
unsigned char SegVal;
switch(digit)
{
case 0: if(common == 1) SegVal = 0b00111111;
else SegVal = ~0b00111111;
break;
case 1: if(common == 1) SegVal = 0b00000110;
else SegVal = ~0b00000110;
break;
case 2: if(common == 1) SegVal = 0b01011011;
else SegVal = ~0b01011011;
break;
case 3: if(common == 1) SegVal = 0b01001111;
else SegVal = ~0b01001111;
break;
case 4: if(common == 1) SegVal = 0b01100110;
else SegVal = ~0b01100110;
break;
case 5: if(common == 1) SegVal = 0b01101101;
else SegVal = ~0b01101101;
break;
case 6: if(common == 1) SegVal = 0b01111101;
else SegVal = ~0b01111101;
break;
case 7: if(common == 1) SegVal = 0b00000111;
else SegVal = ~0b00000111;
break;
case 8: if(common == 1) SegVal = 0b01111111;
else SegVal = ~0b01111111;
break;
case 9: if(common == 1) SegVal = 0b01101111;
else SegVal = ~0b01101111;
}
return SegVal;
}
int main(void)
{
int temp1,temp2;
//Suppose my input is 105 and 210, i.e. temp1=105 and temp2=210;
// it contains other information also, not required here
SegDataDDR = 0xFF;
SegCntrlDDR = 0xF3;
SegCntrlPort = 0xF3;
SegDataPort = 0x00;
while(1){
SegDataPort = DigitTo7SegEncoder(temp1%10,1);
SegCntrlPort = ~0x01;
SegDataPort = DigitTo7SegEncoder((temp1/10)%10,1);
SegCntrlPort = ~0x02;
SegDataPort = DigitTo7SegEncoder(temp1/100,1);
SegCntrlPort = ~0x10;
SegDataPort = DigitTo7SegEncoder(temp2%10,1);
SegCntrlPort = ~0x20;
SegDataPort = DigitTo7SegEncoder((temp2/10)%10,1);
SegCntrlPort = ~0x40;
SegDataPort = DigitTo7SegEncoder(temp2/100,1);
SegCntrlPort = ~0x80;
}}