ATMEGA128單片機(jī)定時(shí)器+數(shù)碼管顯示
掃描二維碼
隨時(shí)隨地手機(jī)看文章
#define SS 0 //注意,不要漏掉了() #define led1_en() {DDRB|=1<<5;PORTB|=(1<<5);} #define led2_en() {DDRB|=1<<6;PORTB|=(1<<6);} #define led3_en() {DDRB|=1<<7;PORTB|=(1<<7);} #define OE 7 #include volatile unsigned char led_buffer[4]; void delay_nus(unsigned int n) //N us延時(shí)函數(shù) void leddis_update(void) void display_led(unsigned int data) void timer1_init(void) #pragma interrupt_handler timer1_ovf_isr:15 void init_devices(void) void main(void)
#define SCK1
#define MOSI 2
#define MISO 3
#define SS_H() PORTB|=(1<
#define led0_en() {DDRB|=1<<4;PORTB|=(1<<4);} //開(kāi)第一個(gè)數(shù)碼管的位選
#define led0_dis() {DDRB|=1<<4;PORTB&=~(1<<4);} //關(guān)第一個(gè)數(shù)碼管的位選
#define led1_dis() {DDRB|=1<<5;PORTB&=~(1<<5);}
#define led2_dis() {DDRB|=1<<6;PORTB&=~(1<<6);}
#define led3_dis() {DDRB|=1<<7;PORTB&=~(1<<7);}
#define point 4
#define dp 7
#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
void delay_1us(void) //1us延時(shí)函數(shù)
{
asm("nop");
}
{
unsigned int i=0;
for (i=0;i
delay_1us();
}
void delay_1ms(void) //1ms延時(shí)函數(shù)
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延時(shí)函數(shù)
{
unsigned int i=0;
for (i=0;i
delay_1ms();
}
void spi_init(void)
{
DDRB |= (1<
void SPI_MasterTransmit(char Data)
{
SPDR = Data;
while(!(SPSR & (1<
}
void HC_595_init(void)
{
DDRC |= (1<
spi_init();
led_buffer[0]=16; //初始化數(shù)碼管段碼
led_buffer[1]=16;
led_buffer[2]=16;
led_buffer[3]=16;
}
void HC_595_OUT(unsigned char data)
{
SS_L();
SPI_MasterTransmit(data);
SS_H();
}
{
if(point==0)
HC_595_OUT(table[led_buffer[3]]|(1<
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<
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<
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<
HC_595_OUT(table[led_buffer[0]]);
led3_en();
delay_nus(60);
led3_dis();
}
{
if(data>9999)
{
HC_595_OUT(0xFF);//當(dāng)計(jì)數(shù)大于9999時(shí),四個(gè)數(shù)碼管同時(shí)輸出8
PORTB|=((1<<4)|(1<<5)|(1<<6)|(1<<7));
}
else if(data>999)
{
led_buffer[0]=data/1000;
led_buffer[1]=(data00)/100;
led_buffer[2]=(data0)/10;
led_buffer[3]=data;
leddis_update();
}
else if(data>99)
{
led_buffer[0]=16; //關(guān)閉最高位的那個(gè)數(shù)碼管
led_buffer[1]=(data00)/100;
led_buffer[2]=(data0)/10;
led_buffer[3]=data;
leddis_update();
}
else if(data>9)
{
led_buffer[0]=16;
led_buffer[1]=16;
led_buffer[2]=(data0)/10;
led_buffer[3]=data;
leddis_update();
}
else
{
led_buffer[0]=16;
led_buffer[1]=16;
led_buffer[2]=16;
led_buffer[3]=data;
leddis_update();
}
}
volatile unsigned int countnum=0;
{
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
}
void timer1_ovf_isr(void)
{
TCNT1H = 0x8F; //reload counter high value
TCNT1L = 0x81; //reload counter low value
countnum++;
if(countnum>9999) countnum=0;
}
{
CLI(); //disable all interrupts
timer1_init();
TIMSK = 0x04; //timer interrupt sources
SEI(); //re-enable interrupts
}
{
init_devices();
HC_595_init();
while(1)
{
display_led(countnum);
//Seg7_Led_float(25.13);
}
}