//注意,不要漏掉了()
#define led0_en() {DDRB|=1<<4;PORTB|=(1<<4);} //开第一个数码管的位选
#define led0_dis() {DDRB|=1<<4;PORTB&=~(1<<4);} //关第一个数码管的位选
#define led1_en() {DDRB|=1<<5;PORTB|=(1<<5);}
#define led1_dis() {DDRB|=1<<5;PORTB&=~(1<<5);}
#define led2_en() {DDRB|=1<<6;PORTB|=(1<<6);}
#define led2_dis() {DDRB|=1<<6;PORTB&=~(1<<6);}
#define led3_en() {DDRB|=1<<7;PORTB|=(1<<7);}
#define led3_dis() {DDRB|=1<<7;PORTB&=~(1<<7);}
#define OE 7
#define point 4
#define dp 7
#include
#include
const unsigned char table[]={0x3F,0x06,0x5B,0x4F,0x66, //0,1,2,3,4
0x6D,0x7D,0x07,0x7F,0x6F, //5,6,7,8,9
0x77,0x7C,0x39,0x5E,0x79,0x71,0x00}; //a,b,c,d,e,f
volatile unsigned char led_buffer[4];
void delay_1us(void) //1us延时函数
{
asm("nop");
}
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i delay_1us();
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i delay_1ms();
}
/*完成spi的初始化*/
void spi_init(void)
{
DDRB |= (1< SPCR = (1<}
/*spi主机传送数据*/
void SPI_MasterTransmit(char Data)
{
/* 启动数据传输 */
SPDR = Data;
/* 等待传输结束 */
while(!(SPSR & (1< ;
}
/*完成对HC595的初始化*/
void HC_595_init(void)
{
DDRC |= (1< PORTC &= (1< PORTB = 0x0F; //同时打开四个数码管的位选
spi_init();
led_buffer[0]=16; //初始化数码管段码
led_buffer[1]=16;
led_buffer[2]=16;
led_buffer[3]=16;
}
/*HC595完成传送数据*/
void HC_595_OUT(unsigned char data)
{
SS_L();
SPI_MasterTransmit(data);
SS_H();
}
void leddis_update(void)
{
/*最低位数码管,第四个数码管*/
if(point==0)
HC_595_OUT(table[led_buffer[3]]|(1< else
HC_595_OUT(table[led_buffer[3]]);
led0_en();
delay_nus(60);
led0_dis();
if(point==1)
HC_595_OUT(table[led_buffer[2]]|(1< else
HC_595_OUT(table[led_buffer[2]]);
led1_en();
delay_nus(60);
led1_dis();
if(point==2)
HC_595_OUT(table[led_buffer[1]]|(1< else
HC_595_OUT(table[led_buffer[1]]);
led2_en();
delay_nus(60);
led2_dis();
/*最高位数码管,第一个数码管*/
if(point==3)
HC_595_OUT(table[led_buffer[0]]|(1< else
HC_595_OUT(table[led_buffer[0]]);
led3_en();
delay_nus(60);
led3_dis();
}
void display_led(unsigned int data)
{
if(data>9999)
{
HC_595_OUT(0xFF);//当计数大于9999时,四个数码管同时输出8
PORTB|=((1<<4)|(1<<5)|(1<<6)|(1<<7));
}
else if(data>999)
{
led_buffer[0]=data/1000;
led_buffer[1]=(data%1000)/100;
led_buffer[2]=(data%100)/10;
led_buffer[3]=data%10;
leddis_update();
}
else if(data>99)
{
led_buffer[0]=16; //关闭最高位的那个数码管
led_buffer[1]=(data%1000)/100;
led_buffer[2]=(data%100)/10;
led_buffer[3]=data%10;
leddis_update();
}
else if(data>9)
{
led_buffer[0]=16;
led_buffer[1]=16;
led_buffer[2]=(data%100)/10;
led_buffer[3]=data%10;
leddis_update();
}
else
{
led_buffer[0]=16;
led_buffer[1]=16;
led_buffer[2]=16;
led_buffer[3]=data%10;
leddis_update();
}
}
volatile unsigned int countnum=0;
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x8F; //setup
TCNT1L = 0x81;
OCR1AH = 0x70;
OCR1AL = 0x7F;
OCR1BH = 0x70;
OCR1BL = 0x7F;
OCR1CH = 0x70;
OCR1CL = 0x7F;
ICR1H = 0x70;
ICR1L = 0x7F;
TCCR1A = 0x00;
TCCR1B = 0x04; //start Timer
}
#pragma interrupt_handler timer1_ovf_isr:15
void timer1_ovf_isr(void)
{
TCNT1H = 0x8F; //reload counter high value
TCNT1L = 0x81; //reload counter low value
countnum++;
if(countnum>9999) countnum=0;
}
void init_devices(void)
{
CLI(); //disable all interrupts
timer1_init();
TIMSK = 0x04; //timer interrupt sources
SEI(); //re-enable interrupts
}
void main(void)
{
init_devices();
HC_595_init();
while(1)
{
display_led(countnum);
//Seg7_Led_float(25.13);
}
}