用AVR單片機(jī)實(shí)現(xiàn)頻率計(jì)設(shè)計(jì)
//AVR單片機(jī)課程設(shè)計(jì)程序//名稱(chēng):頻率計(jì)//硬件:ATMEGA16_FHz-QLD100623產(chǎn)品 8MHz 晶振 三位數(shù)碼管顯示//程序說(shuō)明:程序初調(diào)階段,可測(cè)頻率范圍1Hz--65KHz 自動(dòng)換檔顯示//編寫(xiě)人:李春起#includeiom16v.h> #include macros.h>//-------------位拉高--------------------------------------------#define LED1_H PORTD|=BIT(PD4) #define LED2_H PORTD|=BIT(PD5) #define LED3_H PORTD|=BIT(PD6) #define AT1_H PORTA|=BIT(PA0) #define AT2_H PORTA|=BIT(PA1) #define AT3_H PORTA|=BIT(PA2) //-------------位拉低--------------------------------------------#define LED1_L PORTD=~BIT(PD4) #define LED2_L PORTD=~BIT(PD5) #define LED3_L PORTD=~BIT(PD6) #define AT1_L PORTA=~BIT(PA0) #define AT2_L PORTA=~BIT(PA1) #define AT3_L PORTA=~BIT(PA2) #define NOP() asm("nop")#define uchar unsigned char #define uint unsigned int//==================聲明全局變量====================================uint data,F_data=0,F_data10K=0;F_data1M=0;//頻率分段儲(chǔ)存uint decimal1,decimal2,decimal3;//===================數(shù)據(jù)表==============================================__flash uchar SEG[]={0X03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09};//==================mS延時(shí)子程序=========================================void delayms(uint m){uint i,j;for(i=0;im;i++){for(j=0;j1141;j++);}}//==================IO口初始化=======================================void IO_init(void){DDRA = 0XFF;DDRB = 0X00;//PB0為頻率輸入,其它為按鍵DDRC = 0XFF;DDRD = 0XFF;}//====================定時(shí)器T0初始化===================================void timer0_init(void){TCCR0 = 0x0F; //T0上升沿觸發(fā)CTC模式TCNT0 = 0x00; //計(jì)數(shù)器清0OCR0 = 255; //計(jì)數(shù)值達(dá)100中斷TIMSK |=0x02; //使能OCIE0TIFR |=0x02; //清OCIF0中斷標(biāo)識(shí)位}//====================定時(shí)器T1初始化===================================void timer1_Init(void)//用于產(chǎn)生標(biāo)準(zhǔn)1S時(shí)間{ TCCR1A=0x00; //WGM1[3:0]=0100,OCR1ATCCR1B=0x0c; //256 division,8M/256,CTC ModeTCNT1=0X00; //Clear CounterOCR1A=31250; //31250*256/8M=1STIMSK|=0X10; //比較匹配A中斷使能TIFR |=0X10; //中斷標(biāo)志寫(xiě)1清0}//=====================使能全局中斷================================void Interrupt_init(void){SREG|=0x80;}//====================MCU初始化==========================================void MCU_init(void){IO_init();timer0_init();timer1_Init();Interrupt_init();}//=============================================================//-------------------------------------------------------------//---------------主函數(shù)----------------------------------------//-------------------------------------------------------------void main(void){MCU_init();PORTD=0XFF;while(1){PORTC=(SEG[(data%10)]-decimal1);AT3_H;delayms(5);AT3_L;PORTC=(SEG[(data/10)%10]-decimal3);AT2_H;delayms(5);AT2_L;PORTC=(SEG[(data/100)%10]-decimal2);AT1_H;delayms(5);AT1_L;}}//--------------------------------------------------------------//--------------------------------------------------------------//==============================================================//====================定時(shí)器T0中斷服務(wù)子程序==========================#pragma interrupt_handler Timer0_Interrupt:20void Timer0_Interrupt(void){TCNT0 = 0X00;//計(jì)數(shù)器清0if(++F_data10K>=65535){F_data10K=0;if(++F_data1M>=65535){F_data1M=0;}}}//=====================定時(shí)器T1中斷服務(wù)==============================#pragma interrupt_handler Timer1_Interrupt:7void Timer1_Interrupt(void){TCNT1 = 0X00;//計(jì)數(shù)數(shù)清0F_data = TCNT0;F_data |= F_data10K8; TCNT0 = 0X00;F_data10K = 0;if(F_data1000){decimal3=0;decimal2=0;decimal1=1;data=F_data;LED1_L;LED2_H;LED3_H;}if(F_data>=1000){decimal3=0;decimal2=1;decimal1=0;data=(F_data/10);LED1_H;LED2_L;LED3_H;}if(F_data>=10000){decimal3=1;decimal2=0;decimal1=0;data=(F_data/100);}//if(F_data>=65534){F_data = (F_data>>8);F_data |= F_data1M8;// F_data=(F_data/1000);}}
單片機(jī)相關(guān)文章:單片機(jī)教程
單片機(jī)相關(guān)文章:單片機(jī)視頻教程
單片機(jī)相關(guān)文章:單片機(jī)工作原理
塵埃粒子計(jì)數(shù)器相關(guān)文章:塵埃粒子計(jì)數(shù)器原理
評(píng)論