贴两个AVR单片机128 74HC595的数码管程序 给你参考 一个是4位数码管循环显示0-F 另外一个是秒表
#include<iom128v.h>
#include<macros.h>
#define uchar unsigned char
#define uint unsigned int
#pragma data:code
const table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void delay(void)
{
uint i;
for(i=0;i<65000;i++);
}
void main()
{
int k=4,j=0;
DDRA=0xFF;
DDRC=0xFF;
PORTC=0xFF;
while(1)
{
for(k=4;k<8;k++)
{
if(j>=16)
j=0;
PORTA=table[j];
PORTC=~BIT(k);
delay();
delay();
delay();
delay();
j++;
}
}
}
#include <iom128v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
#pragma interrupt_handler timer1:15
uint num = 0;
#pragma data:code
const uchar duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
const uchar wei[]={0x7f,0xbf,0xdf,0xef};
void DelayMs(uint i)
{
while(i--)
{
uint j;
for(j=1;j<=613;j++);
}
}
void show(uchar j,uchar k)
{
PORTA = duan[j];
PORTC = wei[k];
if(k==2)
{
PORTA = duan[j]&(~BIT(7));
}
if(k==0&&(num%2==0))
{
PORTA = duan[j]&(~BIT(7));
}
}
void main()
{
uint i,j;
DDRA = 0xff; //数码管段选
DDRC = 0xff; //位选
TCCR1A = 0x00;
TCCR1B = 0x04;//256分频
TCNT1H=0X48;
TCNT1L=0XE4;//计数寄存器
TIMSK|=BIT(2);//中断屏蔽寄存器
SEI();//开启总中断
while(1)
{
show(num%60%10,0);
DelayMs(2);
show(num%60/10,1);
DelayMs(2);
show(num/60%10,2);
DelayMs(2);
show(num/600,3);
DelayMs(2);
}
}
void timer1(void)
{
TCNT1H=0X48;
TCNT1L=0XE4;
num++;
if(num==3600)
num=0;
} |