哪位大虾有atmega16单片机使用74HC595控制数码管的程序,可否给小弟邮一份?感激不尽!

要有详细注释!
2024-12-29 00:03:44
推荐回答(1个)
回答1:

//编译环境 winavr

#include
void send_byte(char byte);
void display(char *p);
void delay(int ms);

#define data_high() PORTC |= 1<<1 //data
#define data_low() PORTC &= ~(1<<1)
#define bit_clock_high() PORTC |= 1<<0 //clock
#define bit_clock_low() PORTC &= ~(1<<0)
#define byte_clock_high() PORTC |= 1<<2 //latch
#define byte_clock_low() PORTC &= ~(1<<2)

//注意:这是共阳数码管的码表,但用在共阴数码管上,两者之间转换容易,把数据取反即可
const char table[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0x7F,0xFF};
// 0 1 2 3 4 5 6 7 8 9 A b C d E F . (space)

int main(void)
{
DDRC |= 0x07;
char hour=21,minute=35,second=45;
char str[8];
while(1)
{
delay(1000);
second++;
if(second == 60)
{
second = 0;
minute++;
if(minute == 60)
{
minute = 0;
hour++;
if(hour == 24)
hour = 0;
}
}

str[0] = hour/10 + '0';
str[1] = hour%10 + '0';
str[2] = '.';
str[3] = minute/10 + '0';
str[4] = minute%10 + '0';
str[5] = '.';
str[6] = second/10 + '0';
str[7] = second%10 + '0';

display(str);
}
}

void send_byte(char byte)
{
int i;
for(i=0; i<8; i++)
{
bit_clock_low();
if(byte&0x80) data_high();
else data_low();
byte = byte<<1;
bit_clock_high();
}
}

void display(char *p)
{
int i=7;
byte_clock_low();
for( i=7; i>=0; i--)
{
switch( *(p+i) )
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': send_byte( table[ *(p+i) - '0' ] );break;

case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f': send_byte( table[ *(p+i) - 'a' +10 ] );break;

case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F': send_byte( table[ *(p+i) - 'A' +10 ] );break;

case '.': send_byte( table[16] );break;

case ' ': send_byte( table[17] );break;
}
}
byte_clock_high();
}

void delay(int ms)
{
int i,j;
for(i=0; i for(j=0; j < 50 ; j++);
}