PA0-PA7 连到八段数码管的a-p ,PC0-PC5 分别连六个数码管的共阴极
c程序:
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.5a Evaluation
Date : 2011/12/14
Author : Freeware, for evaluation and
non-commercial use only
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 4.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <delay.h>
flash unsigned char led_7[10] = {0x3F,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
flash unsigned char position[6]= {0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
unsigned char time[3];
unsigned char dis_buff[6];
int time_counter;
unsigned char posit;
bit point_on,time_1s_ok;
void display(void)
{
unsigned char i;
for(i=0 ;i<=5; i++)
{
PORTC = 0xff; //防止在PORTC在上个循环选定的位置的数码管输出,造成一个数码管输出两个不同的数,从而在视觉上产生错误的图像
PORTA = led_7[dis_buff[i]];
if (point_on && (i==2 || i==4 )) PORTA |= 0x80;
PORTC = position[i];
}
PORTC = 0xff; //使最后一个数码管显示的时间 和前5个基本一样 这样亮度才能均匀
}
interrupt [EXT_INT1] void ext_int1_isr(void)
{
if ( ++time_counter >= 500)
{
time_counter = 0;
time_1s_ok = 1;
}
}
void time_to_disbuffer(void)
{
unsigned char i,j = 0;
for(i=0 ; i <=2; i++)
{
dis_buff[j++] = time[i] %10;
dis_buff[j++] = time[i] /10;
}
}
void main(void)
{
PORTA = 0x00;
DDRA = 0xFF;
PORTC = 0x3f;
DDRC = 0x3f;
time[2] = 23; time[1] = 58 ; time[0]=55 ;
posit = 0;
time_to_disbuffer();
GICR |= 0x80;
MCUCR = 0x08;
GIFR = 0x80;
#asm("sei")
while(1)
{
display();
if(time_1s_ok)
{
time_1s_ok = 0;
point_on = ~point_on;
if( ++time[0] >= 60)
{
time[0] = 0;
if(++time[1] >=60)
{
time[1] = 0;
if(++time[2] >=24 ) time[2] = 0;
}
}
time_to_disbuffer();
}
} ;
}