項目-avr程序?qū)嵗齧egac1.參考書P121數(shù)碼管輪流顯示_第1頁
項目-avr程序?qū)嵗齧egac1.參考書P121數(shù)碼管輪流顯示_第2頁
項目-avr程序?qū)嵗齧egac1.參考書P121數(shù)碼管輪流顯示_第3頁
項目-avr程序?qū)嵗齧egac1.參考書P121數(shù)碼管輪流顯示_第4頁
項目-avr程序?qū)嵗齧egac1.參考書P121數(shù)碼管輪流顯示_第5頁
已閱讀5頁,還剩33頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1.P121#include<mega16.h>#include<delay.h>flashunsignedcharled_7[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,flashunsignedcharposition[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};voidmain(void){unsignedchar {{{}}}#includeflashunsignedcharflashunsignedcharposition[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};unsignedchardisbuff[8];//顯示緩沖區(qū),存放要顯示的8個字符的段碼值unsignedcharposit,time_counter;bitvoid 6位LED{PORTC=PORTA=led_7[disbuff[posit]];PORTC=position[posit];if(++posit>=8)posit=}Timer0interrupt P] { LEDif{time_counter=time_500ms_ok=}}void{unsignedchar I/O//T/C0 //OCR0=0x7C(124),(124+1)/62.5=2ms(2ms中斷一次,電平方向變化一次) //允許T/C0比較匹配中斷posit=0; while{{if(++new>=16)new=0;}}}8,逐段、#include#includeflashunsignedcharled_7[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};flashunsignedcharposition[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};voidmain(void){unsignedchar {{{}}}#include<mega16.h>flashunsignedcharposition[2]={0xfe,0xfd};unsignedchar //unsignedchar //6int //unsignedcharposit; void 6LED{PORTC=PORTA=led_7[dis_buff[posit]];PORTC=position[posit];if(++posit>=2)posit=}Timer0interrupt P] { //調(diào)用LEDif{time_counter=time_1s_ok=}}void //{dis_buff[0]=time%10;dis_buff[1]=time/10;}void{ //I/OT/C0 //(M/64=62.5KHz,CC //= //T/C0posit= //while{if 1{time_1s_ok=iftime0)//{} //}}}數(shù)碼管動態(tài)掃描0170 #include<mega16.h>#include<delay.h>flashunsignedcharflashunsignedchar void{while{unsignedintpoint=0,show=0,i=0,k=0,position2=7;unsignedintmax=8,count=50;chartime_counter=100;{{{{}}}}} #include<mega16.h>#include<delay.h>flashunsignedcharposition[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};unsignedchardisbuff[8];void{unsignedchar //I/O {{{{}}}}}}}#include<mega16.h>#include<delay.h>flashunsignedcharposition[2]={0xfe,0xfd};unsignedchar //6unsignedcharvoid{unsignedchari,j;{}}void{}interrupt[EXT_INT0]void{ interrupt[EXT_INT1]void{if(num)num--;elsenum=99;}void //允許INT0、INT1中斷 //INT0、INT1下降沿觸發(fā) //清除INT0、INT1中斷標志位 //全局中斷允許while{}}#include<mega16.h>#include<delay.h>flashunsignedcharposition[2]={0xfe,0xfd};unsignedchar //6unsignedcharvoid 6LED{PORTC=PORTA=led_7[dis_buff[posit]];PORTC=position[posit];if(++posit>=2)posit=Timer0interrupt{P]//調(diào)用LED}void{}interrupt[EXT_INT0]void{}interrupt[EXT_INT1]void{if(num)num--;elsenum=99;}void //允許INT0、INT1中斷 //INT0、INT1下降沿觸發(fā) //清除INT0、INT1中斷標志位T/C0 //(M/64=62.5KHz,CC //= //T/C0 //while{}}#include<mega16.h>flashunsignedcharled_7[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};flashunsignedcharposition[2]={0xfe,0xfd};unsignedcharcounter,time_10ms_counter=0;unsignedchardisbuffer[2],i=0;bitvoid }interrupt P] { { }}void{ }#definekey_input7PIND.7#definekey_input6PIND.6#definekey_state_00#definekey_state_1#definekey_state_2unsignedchar{staticunsignedcharkey_state7=0;unsignedcharkey_press7,key_return7=0;{casecase {}case}return}unsignedchar{staticunsignedcharkey_state6=0;unsignedcharkey_press6,key_return6=0;{casecase{ }case}return}void while{ {{ }{ elsecounter=99;} }}#include#definerow#definespeed flashunsignedcharchar_7[row]={0x10,0x38,0x7C,0xFE,0x38,0x38,0x38,0x38,0x00,0x00,0x00,0x00};bittime_1ms_ok;unsignedchardis_buff[8];voiddisplay(void){staticunsignedcharPORTC=PORTA=PORTC=if(++i>=8)i=}interrupt P] 1ms{display();time_1ms_ok=1;}void{unsignedcharmove_speed,i,j; T/C0CTC for(i=0;i<=8;i++){dis_buff[i]=char_7[i];}j=8;while{if{time_1ms_ok=if+move_speed>= {move_speed=0; move_speed,則依次向上移動一行for(i=0;i<=6;i++){dis_buff[i]=}dis_buff[7]=char_7[j];if(++j>=row)j=0;}}}}#include<mega16.h>flashunsignedchartab[8][4]= flashunsignedcharunsignedchark=0;unsignedcharj=0,m=0;inttime_1s_counter=0;bittime_1s_ok=0;voiddisplay(unsignedcharelsem=i/8+1; }interrupt P] { { }}//Declareyourglobalvariablesherevoidmain(void) while(1){ { }}書P269使用定時器的產(chǎn)生特定頻率,輸出到蜂鳴器上產(chǎn)生不同的音調(diào)。使用定時器產(chǎn)生DOREMIFASOLA聲音,每個音長1秒。(假設(shè)do256re288mi320fa341so384la426si480hz#include<mega16.h>flashunsignedintt[9]=flashunsignedchard[9]={0,105,116,132,140,157,176,198,209};#defineMax_note flashunsignedcharmusic[Max_note]=unsignedcharnote_n;unsignedintint_n;bitplay_on;//ExternalInterrupt1serviceroutineinterrupt[EXT_INT1]voidext_int1_isr(void){if{TCCR1B=}}//Timer1outputcompareAinterruptserviceroutineinterrupt[ PA]void {if{note_n=int_n=play_on=}{if(--int_n=={TCCR1B=if(note_n<{OCR1A=4*t[music[note_n]];int_n=d[music[note_n]];int_n=int_n*music[note_n];TCCR1B=}play_on=}}}//Declareyourglobalvariablesvoid{TCNT1=0x00;ICR1H=0x00;ICR1L=0x00;OCR1A=0x00;OCR1B=//Timer(s)/Counter(s)Interrupt(s)initialization//ExternalInterrupt(s)//INT0://INT1://INT1Mode:Falling//INT2:Off//Globalenableinterruptswhile{}}設(shè)計一個簡單系統(tǒng),有2個按鍵和8個LED數(shù)碼管作為顯示。兩個按鍵分別代表輸入字符“a”和“b”,且具備類似PC機鍵盤的連發(fā)功能:如按下“a”鍵不放,第1秒內(nèi)輸入1“a”,以后每隔0.2“a”……。輸入的“a”在8LED#includeflashunsignedcharflashunsignedcharposition[8]=0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};unsignedchardis_buff[8];//顯示緩沖區(qū),存放要顯示的8個字符的段碼值unsignedcharunsignedcharkey_stime_counter;bitkey_stime_ok;void 8位LED{PORTC=PORTA= PORTC=if(++posit>=8)posit= //}Timer0比較匹配中斷服務(wù),2msinterruptinterrupt P] { LEDif(++key_stime_counter key_stime_counter= key_stime_ok 10ms}#define //按鍵 #definekey_input6 #definekey_state_00#definekey_state_1#definekey_state_2#definekey_state_3unsignedchar{staticunsignedcharkey_state7=0,key_time=unsignedcharkey_press,key_return7=key_press 讀按鍵I/Oswitch case if(!key_presskey_state7key_state_1 case if key_state7key_state_2;按鍵仍按下,狀態(tài)轉(zhuǎn)換到計時 key_time key_state7 case if key_state7key_state_0; key_return7 輸出 elseif(++key_time key_state7 key_time key_return7 輸出 case if key_state7key_state_0 if(++key_time>={{輸出輸出key_return7=key_time=}} return}unsignedchar{staticunsignedcharkey_state6=0,key_time=unsignedcharkey_press,key_return6=key_press 讀按鍵I/Oswitch case if(!key_presskey_state6key_state_1 case if key_state6key_state_2按鍵仍按下,狀態(tài)轉(zhuǎn)換到計時 key_time key_state6 case if key_state6key_state_0; key_return6 輸出 elseif(++key_time key_state6 key_time key_return6 輸出 case if key_state6key_state_0 if(++key_time key_time key_return6 輸出 return}void{ unsignedcharPORTA DDRA=PORTC=DDRC=DDRD PD7、PD6T/C0TCCR0 TCNT0=OCR0= //OCR0=TIMSK posit= while if key_stime_ok 10ms switch case case }} switch case case if(n>=8) }#include<mega16.h>flashunsignedcharflashunsignedcharposition[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};unsignedcharposit;unsignedchar bitunsigned void 8位LED{PORTC=PORTA=led_3[dis_buff[posit]];PORTC=position[posit];if(++posit>=8)posit=}Timer0,2msinterrupt P] { LED掃描顯示if(++key_stime_counter>=5){key_stime_counter=key_stime_ok 10ms}}#definekey_input_a //按鍵輸#definekey_input_bPIND.1 //按鍵輸b#definekey_state_00#definekey_state_1#definekey_state_2#definekey_state_3unsignedchar{staticunsignedcharkey_state=0,key_time=0;unsignedcharkey_press,key_return=0;key_press a的I/Oswitch{case if(!key_presskey_statekey_state_1;case if{key_state=key_state_2;//按鍵仍按下,狀態(tài)轉(zhuǎn)換到計時1key_time=0; //清另按鍵時間計數(shù)器}key_statekey_state_0;casekey_state_2:if(key_press){key_statekey_state_0;key_return }elseif(++key_time {key_state=key_state_3;//按下時間>1s,狀態(tài)轉(zhuǎn)換到計時2key_time=0; //清按鍵計數(shù)器key_return }casekey_state_3:if(key_press)key_statekey_state_0;{if(++key_time {key_time >0.2s,key_return }}}return}unsignedchar{staticunsignedcharkey_state=0,key_time=0;unsignedcharkey_press,key_return=0;key_press b的I/Oswitch{case if(!key_presskey_statekey_state_1;case if{key_state=key_state_2;//按鍵仍按下,狀態(tài)轉(zhuǎn)換到計時1key_time=0; //清另按鍵時間計數(shù)器}key_statekey_state_0;casekey_state_2:if(key_press){key_statekey_state_0;key_return }elseif(++key_time {key_state=key_state_3;//按下時間>1s,狀態(tài)轉(zhuǎn)換到計時2key_time=0; //清按鍵計數(shù)器key_return }casekey_state_3:if(key_press)key_statekey_state_0;{

if(++key_time {key_time >0.2s,key_return }}}return}void{unsignedcharPORTA I/ODDRA=PORTC=DDRC=DDRD= ////T/C0 //OCR0= T/C0for(i=0;i<8 //LED初始顯示8個"" //開放全局中斷while{if{key_stime_ok=0; 10ms到switch(read_key_n_a()){casefor(i=0;i<7;{dis_buff[i]=dis_buff[i+1];} //LED顯示左移一位dis_buff[7]=1; //最右顯示acase2:for(i=0;i<7;{dis_buff[i]=dis_buff[i+1];} //LED顯示左移一位dis_buff[7]=1; //最右顯示a}switch{casefor(i=0;i<7;{dis_buff[i]=dis_buff[i+1];} //LED顯示左移一位dis_buff[7]=2; //最右顯示bcase2:for(i=0;i<7;{dis_buff[i]=dis_buff[i+1];} //LED顯示左移一位dis_buff[7]=2; //最右顯示b}}}}個作為數(shù)字按下兩次,將LED數(shù)碼管內(nèi)容清為0。初始狀態(tài)顯示0。 #include<mega16.h>#defineucharunsignedchar#definenokey255flashucharflashucharposition[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};uchardisbuff[8];ucharposit=0;unsignedlongintresult[3];ucharkeynow; uchartime_10ms_counter;bittime_10ms_ok;ucharreset=0,i,j,k=0;bitplus=0,cleardis=0,clearres=0;voiddisplay() interruptinterrupt P] uchar{staticucharkeystate=0,keyvalue,keyline;ucharkeyreturn=nokey; case case case case case casecasecasecasecasecasecasecasecase}}}}casecase}}returnreturn}}void{ 該是while(1) {{}}}}{{}}}}}}#include<mega16.h>#defineucharunsignedchar#definenokey255flashucharled_7[13]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x00};flashucharposition[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};uchardisbuff[8];ucharposit=0;unsignedlongintresult[3];ucharkeynow;uchartime_10ms_counter;bittime_10ms_ok;ucharbitplus=0,cleardis=0,clearres=0;voiddisplay() }interrupt P] {{ }}uchar{staticucharkeystate=0,keyvalue,keyline;ucharkeyreturn=nokey;{case{case1:

}

{}{{case0b case0b case0b case0b case0b case0b case0b case0b case0b case0b case0b case0b }}case2:}

return}void 該是while(1){{ {

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論