征服嵌入式系列之AVR單片機筆記_第1頁
征服嵌入式系列之AVR單片機筆記_第2頁
征服嵌入式系列之AVR單片機筆記_第3頁
征服嵌入式系列之AVR單片機筆記_第4頁
征服嵌入式系列之AVR單片機筆記_第5頁
已閱讀5頁,還剩100頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、目錄TOC o 1-3 h z u HYPERLINK l _Toc341289269 第一課:不同類型單片機開發(fā)的比較 PAGEREF _Toc341289269 h 1 HYPERLINK l _Toc341289270 第二課:AVR單片機的優(yōu)點 PAGEREF _Toc341289270 h 3 HYPERLINK l _Toc341289271 第三課:AVRR單片機頭文文件簡介與使使用 PAGEREF _Toc341289271 h 6 HYPERLINK l _Toc341289272 第四課:AVRR單片機I/OO口的4種位操作方方法 PAGEREF _Toc341289272

2、 h 8 HYPERLINK l _Toc341289273 第五課:AVRR熔絲位簡介介 PAGEREF _Toc341289273 h 11 HYPERLINK l _Toc341289274 第六課:如何把把51單片機的的程序移植到到AVR單片機機 PAGEREF _Toc341289274 h 14 HYPERLINK l _Toc341289275 第七課:AVRR單片機中斷斷的原理和使使用 PAGEREF _Toc341289275 h 15 HYPERLINK l _Toc341289276 第八課:AVRR單片機片內(nèi)內(nèi)EEPROOM的原理和和使用 PAGEREF _Toc341

3、289276 h 22 HYPERLINK l _Toc341289277 第九課:AVRR單片機片內(nèi)內(nèi)ADC的原理理和使用 PAGEREF _Toc341289277 h 27 HYPERLINK l _Toc341289278 第十課:AVRR片內(nèi)定時器器的原理和使使用方法 PAGEREF _Toc341289278 h 36 HYPERLINK l _Toc341289279 第十一課:AVVR單片機TWII總線的原理理 PAGEREF _Toc341289279 h 48 HYPERLINK l _Toc341289280 第十二課:AVVR單片機USART通信信接口的原理理和使用 P

4、AGEREF _Toc341289280 h 55 HYPERLINK l _Toc341289281 第十三課:AVVR單片機驅(qū)驅(qū)動LCD11602液晶晶顯示器 PAGEREF _Toc341289281 h 65 HYPERLINK l _Toc341289282 第十四課:AVVR單片機驅(qū)驅(qū)動DS188B20 PAGEREF _Toc341289282 h 73 HYPERLINK l _Toc341289283 第十十五課:AVVR單片機驅(qū)驅(qū)動DS13302 PAGEREF _Toc341289283 h 80 HYPERLINK l _Toc341289284 第十六課:不同同型號的

5、AVVR單片機之之間程序的移移植方法 PAGEREF _Toc341289284 h 88ATmega116第一課:不同類類型單片機開開發(fā)的比較第二課:AVRR單片機的優(yōu)優(yōu)點注意:上面的DDDB7應(yīng)該該是DDRBB7.點亮第一個LEED燈#includde#includdevoid maain() DDRB=00 xff; PORTB=0 xff; DDRA=00 xff; PORTA=0 xfe; while(1);第三課:AVRR單片機頭文文件簡介與使使用幾種操作方法的的比較#includde#includdevoid maain() DDRB=00 xff; PORTB=0 xff; D

6、DRA=00 xff; PORTA=0; PORTA|=(12); PORTA|=BIT(3); PORTA|=BIT(PORTAA);第四課:AVRR單片機I/O口的4種種位操作方法法閃爍燈#includde#includdevoid deelay(uunsignned chhar z) unsignned chhar x,y; for(x=0;x=zz;x+) for(y=0;y1100;y+);void maain() DDRB=00 xff; PORTB=0 xff; DDRA=00 xff; PORTA=0 xff; while(1) PORTAA&=0 xx80; delayy(5

7、00); PORTAA|=0 x880; delayy(500); 用按鍵控制燈的的閃爍#includde#includdevoid maain() DDRB=00 xff; PORTB=0 xff; DDRA=00 xff; PORTA=0 xff; DDRD=00; PORTD=0 xff;/PD口口設(shè)置為帶上上拉的輸入 while(1) if(PIIND&0 xx40)/000000100 PORTAA|=BITT(7); else PORTAA&=BIIT(7): 第五課:AVRR熔絲位簡介介第六課:如何把把51單片機機的程序移植植到AVR單單片機第七課:AVRR單片機中斷斷的原理和使

8、使用#includde#includde#definee uchaar unssignedd charr#definee uintt unsiigned intuchar ttable=0 xxfe,0 xxfd,0 xxfb,0 xxf7,0 xxef,0 xxdf,0 xxbf,0 xx7f;void deelay() uint xx,y; for(x=0;x1000;x+) for(y=0;yy500;y+);void innit() DDRB|=BIT(66); PORTB|=BIT(6); DDRA=00 xff; PORTA=0 xff; MCUCR|=0 x022;/設(shè)置置外部中

9、斷00的觸發(fā)方式式 GICR|=BIT(66);/使使能外部中斷斷 DDRD&=BIT(2); PORTD|=BIT(2);/設(shè)置為帶上上拉電阻的輸輸入 SREG|=BIT(77);void maain() init(); while(1) PORTTA|=BIIT(0); delaay(); PORTTA&=BBIT(0); delaay(); #pragmaa inteerruptt_handdler iint0_iisr:2void innt0_issr() PORTA=0; delay(); delay(); delay(); PORTA=0 xff;第八課:AVRR單片機片內(nèi)內(nèi)EEP

10、ROOM的原理和和使用#includde#includde#definee uchaar unssignedd charr#definee uintt unsiigned intuchar ccode ssmg_duu=00 x3f,0 x006,0 x55b,0 x44f,0 x666,0 x66d,0 x77d,0 x007, 0 xx7f,0 x6f,0 x777,0 x77c,0 x339,0 x55e,0 x779,0 x771;ucahr ccode ssmg_wee=00 x08,00 x18,00 x28,00 x38,00 x48,00 x58,00 x68,00 x78;/

11、位選是用774HC1338譯碼器控制的的uchar ccode ttable=0,0,0,00,0,0,0,0;void deelay() uint xx; for(x=0;x3300;x+);void innit() DDRB=00 xff; PORTB=0; DDRC=00 x78;/011111000 PORTC=0;void diisplayy(ucahhr *p) uchar i; for(i=0;i88;i+) PORTTB=smgg_du*p; p+; PORTTC=smgg_weii; delaay(); void EEEPROM_writee(uintt addrr,uint

12、t adatta) SREG&=BIT(7);/關(guān)閉總中斷斷 while(EECR&BIT(EEEWE); EEAR=aaddr; EEDR=aadata; EECR|=BIT(EEEMWE); EECR|=BIT(EEEWE); SREG|=BIT(77);uchar EEEPROMM_readd(uchaar adddr) uchar edataa; SREG&=BIT(7); while(EECR&BIT(EEEWE); EEAR=aaddr; EECR|=BIT(EEERE); edata=EEDR; returnn edatta;void maain() uchar i; init(

13、); i=EEPRROM_reead(2); i+; EEPROMM_writte(2,ii); table0=i; while(1) dispplay(ttable); 第九課:AVRR單片機片內(nèi)內(nèi)ADC的原原理和使用#includde#includde#definee uchaar unssignedd charr#definee uintt unsiigned intuchar ccode ssmg_duu=00 x3f,0 x006,0 x55b,0 x44f,0 x666,0 x66d,0 x77d,0 x007, 0 xx7f,0 x6f,0 x777,0 x77c,0 x339,

14、0 x55e,0 x779,0 x771;ucahr ccode ssmg_wee=00 x08,00 x18,00 x28,00 x38,00 x48,00 x58,00 x68,00 x78;/位選是用774HC1338控制的uchar ccode ttable=0,0,0,00,0,0,0,0;void deelay() uint xx; for(x=0;x3300;x+);void innit() DDRB=00 xff; PORTB=0; DDRC=00 x78;/011111000 PORTC=0;void diisplayy(uchaar *p) uchar i; for(i=0

15、;i88;i+) PORTTB=smgg_du*p; p+; PORTTC=smgg_weii; delaay(); void addc_iniit() DDRA&=BIT(0); PORTA&=BITT(0); ADMUX|=0 x400; ADCSRAA|=0 xee8; SFIOR=0 x00; SREG|=BIT(77);void daata_prro(uinnt temmp_l,uuint ttemp_hh) uint ttemp1,temp22; temp1=temp_h*2566; temp2=temp11+tempp_l; table3=teemp2/11000; temp1=

16、temp22%10000; table2=teemp1/1100; temp2=temp11%100; table1=teemp2/110; table0=teemp2%110;void maain() init(); adc_innit(); while(1);#pragmaa inteerruptt_handdler aadc_issr:15void addc_isrr() uint aadc_l,adc_hh; adc_l=ADCL; adc_h=ADCH; displaay(tabble);第十課:AVRR片內(nèi)定時器器的原理和使使用方法用普通模式實現(xiàn)現(xiàn)秒表#includde#inclu

17、dde#definee uchaar unssignedd charr#definee uintt unsiigned intuchar ccode ssmg_duu=00 x3f,0 x006,0 x55b,0 x44f,0 x666,0 x66d,0 x77d,0 x007, 0 xx7f,0 x6f,0 x777,0 x77c,0 x339,0 x55e,0 x779,0 x771;ucahr ccode ssmg_wee=00 x08,00 x18,00 x28,00 x38,00 x48,00 x58,00 x68,00 x78;/位選是用774HC1338控制的uchar ccod

18、e ttable=0,0,0,00,0,0,0,0;uchar nnum;void deelay() uint xx; for(x=0;x3300;x+);void innit() DDRB=00 xff; PORTB=0; DDRC=00 x78;/011111000 PORTC=0;void diisplayy(ucahhr *p) uchar i; for(i=0;i88;i+) PORTTB=smgg_du*p; p+; PORTTC=smgg_weii; delaay(); void tiimer1_init() TCCR1BB=0 x044;/2556分頻 TCNT1HH=0 x8

19、ff; TCNT1LL=0 x800; TIMSK|=BIT(2); SREG|=BIT(77);void maain() init(); timer11_initt(); while(1) displlay(taable); #pragmaa inteerruptt_handdler ttimer11_isr:9void tiimer1_isr() num+; if(numm=60) num=0; table0=nuum%10; table1=nuum/10; TCNT1HH=0 x8ff; TCNT1LL=0 x800;用CTC模式輸輸出1KHZZ的方波.#includde#includd

20、e#definee uchaar unssignedd charr#definee uintt unsiigned intvoid maain() DDRD=00 x30; TCCR1BB=0 x099; TCCR1AA=0 x500;/選擇擇模式4 OCR1A=3685;#includde#includde #definee uchaar unssignedd charr#definee uintt unsiigned intvoid maain() DDRD|=0 x30; TCCR1AA=0 x633; TCCR1BB=0 x1bb;/選用用模式15 OCR1A=575;/頻率為2200

21、HZ OCR1B=115;/占空比為為1/5第十一課:AVVR單片機TTWI總線的的原理第十二課:AVVR單片機UUSART通通信接口的原原理和使用#includde#includde#definee uchaar unssignedd charr#definee uintt unsiigned int#definee foscc 73722800uchar rrdata,flag;void uaart_innit(uiint baaud) uint aa; UCSRC=0 x86; a=foscc/16/bbaud-11; UBRRL=a%2566; UBRRH=a/2566; UCSRB=

22、0 x98; SREG|=BIT(77);void uaart_seend(ucchar ddata) while(!(UCSSRA&BIIT(5); UDR=daata; while(!(UCSSRA&BIIT(6); UCSRA|=BIT(6);#pragmaa inteerruptt_handdler uuartreece_issr:12void uaartercce_isrr() UCSRB&=BITT(7); rdata=UDR; flag=11; UCSRB|=BIT(7);void maain() uchar i=h; uart_iinit(99600); /uartt_send

23、d(i); whilee(1) if(fllag) flaag=0; uarrt_sennd(rdaata); 第十三課:AVVR單片機驅(qū)驅(qū)動LCD11602液晶晶顯示器#includde#includde#definee uchaar unssignedd charr#definee uintt unsiigned int#definee RS_CCLR POORTC&=BIT(4)#definee RS_SSET POORTC|=BIT(44)#definee RS_OOUT DDDRC|=BBIT(4)#definee RW_CCLR POORTC&=BIT(5)#definee RW_

24、SSET POORTC|=BIT(55)#definee RW_OOUT DDDRC|=BBIT(5)#definee E_CLLR PORRTC&=BIT(66)#definee E_SEET PORRTC|=BBIT(6)#definee E_OUUT DDRRC|=BIIT(6)uchar ttable11=00123455678988765433;uchar ttable22=aabcdeffg!#$%&*(;void deelay_nnms(uiint nmms) 延時n個mss,具體函數(shù)數(shù)自己寫void wrrite_ccom(ucchar ccom) RS_SETT; RW_SE

25、TT; E_CLR; RS_CLRR; RW_CLRR; PORTA=com; delay_nms(11);/延延時1ms E_SET; delsy_nms(11); E_CLR;void wrrite_ddata(uuchar data) RS_CLRR; RW_SETT; E_CLR; RS_SETT; RW_CLRR; PORTA=data; delay_nms(11);/延延時1ms E_SET; delsy_nms(11); E_CLR;void innit() DDRA=00 xff; PORTA=0; RS_OUTT; E_OUT; RW_OUTT; delay_nms(220)

26、; write_com(00 x38); delay_nms(55); write_com(00 x38); delay_nms(55); write_com(00 x38); write_com(00 x38); write_com(00 x08); write_com(00 x01); write_com(00 x06); write_com(00 x0c);void maain() uchar i; init(); write_com(00 x80); for(i=0;i116;i+) writee_dataa(tablle1i); delayy_nms(2); write_com(00

27、 x80+00 x40); for(i=0;i116;i+) writte_datta(tabble2ii); writte_comm(0 x800); while(1);第十四課:AVVR單片機驅(qū)驅(qū)動DS188B20注意:DS188B20對時時間要求非常常高,延時函函數(shù)一定要精精確#includde#includde#definee uchaar unssignedd charr#definee uintt unsiigned int#definee DQ_IIN DDRRC&=BBIT(2)#definee DQ_OOUT DDDRC|=BBIT(2)#definee DQ_SSET PO

28、ORTC|=BIT(22)#definee DQ_CCLR POORTC&=BIT(2)#definee DQ_RR PINCC&BIT(2)uchar ccode ssmg_duu=00 x3f,0 x006,0 x55b,0 x44f,0 x666,0 x66d,0 x77d,0 x007, 0 xx7f,0 x6f,0 x777,0 x77c,0 x339,0 x55e,0 x779,0 x771;ucahr ccode ssmg_wee=00 x08,00 x18,00 x28,00 x38,00 x48,00 x58,00 x68,00 x78;/位選是用774HC1338控制的uc

29、har ccode ttable=0,0,0,00,0,0,0,0;void deelay() uint xx; for(x=0;x3300;x+);void innit() DDRB=00 xff; PORTB=0; DDRC=00 x78;/011111000 PORTC=0;void diisplayy(uchaar *p) uchar i; for(i=0;i88;i+) PORTTB=smgg_du*p; p+; PORTTC=smgg_weii; if(ii=1) PORTBB|=BITT(7); elsee PORTTB&=BBIT(7); delaay(); void deel

30、ay_nnus(uiint nmms) 延時n個uss,具體函數(shù)數(shù)自己寫uchar dds18b220_resset() uchar i; DQ_OUTT; DQ_CLRR; delay_nus(5500); DQ_SETT; delay_nus(1100); DQ_IN; i=DQ_RR; delay_nus(5500); returnn i; void dss18b200_writte_bytte(uchhar vaalue) uchar i; for(i=0;i1; uchar dds18b220_reaad_bytte() uchar i,vallue; for(i=0;i11; DQ

31、_OOUT; DQ_CCLR; delaay_nuss(10); DQ_SSET; DQ_IIN; if(DDQ_R) vvalue|=0 x800; dellay_nuus(50); returrn vallue;void daata_prro(uinnt temmp) uint aa; table2=teemp/1000; a=tempp%100; table1=a/10; table0=a%10;void maain() uchar i,j,kk; uint ttemp; init(); while(1) ds18bb20_reeset(); ds18bb20_wrrite_bbyte(

32、00 xcc); ds18bb20_wrrite_bbyte(00 x44); delayy_nus(2000); ds18bb20_reeset(); ds18bb20_wrrite_bbyte(00 xcc); ds18bb20_wrrite_bbyte(00 xbe); i=ds118b20_read_byte();/低低字節(jié) j=ds118b20_read_byte();/高高字節(jié) temp=j*2566+i;/26.8 temp=temp*0.6255;/2668 data_pro(ttemp); for(kk=0;k20;k+) dissplay(tablee); 第十五課:AVV

33、R單片機驅(qū)驅(qū)動DS13302#includde#includde#definee uchaar unssignedd charr#definee uintt unsiigned int#definee SCK_CLR PPORTB&=BITT(0)#definee SCK_SET PPORTB|=BIT(0)#definee SCK_OUT DDDRB|=BIT(00)#definee RST_CLR PPORTD&=BITT(6)#definee RST_SET PPORTD|=BIT(6)#definee RST_OUT DDDRD|=BIT(66)#definee IO_CCLR POO

34、RTB&=BIT(1)#definee IO_SSET POORTB|=BIT(11)#definee IO_OOUT DDDRB|=BBIT(1)#definee IO_IIN DDRRB&=BBIT(1)#definee IO_RR PINBB&BIT(1)uchar ccode ssmg_duu=00 x3f,0 x006,0 x55b,0 x44f,0 x666,0 x66d,0 x77d,0 x007, 0 xx7f,0 x6f,0 x777,0 x77c,0 x339,0 x55e,0 x779,0 x771;ucahr ccode ssmg_wee=00 x08,00 x18,0

35、0 x28,00 x38,00 x48,00 x58,00 x68,00 x78;/位選是用774HC1338控制的uchar ccode ttable=0,0,0,00,0,0,0,0;uchar ttime_ddata77=100,6,4,17,111,59,335;/年周月日時時分秒uchar wwrite_add77=0 xx8c,0 x8a,0 x888,0 x886,0 x884,0 x882,0 x880;uchar rread_aadd7=0 x88d;0 x88b,0 x889,0 x887,0 x885,0 x883,0 x881;void deelay() uint xx; for(x=0;x3300;x+);void innit() DDRB=00 xff; PORTB=0; DDRC=00 x78;/011111000 PORTC=0;void diisplayy(uchaar *p) uchar i; for(i=0;i88;i+) PORTTB=smgg_

溫馨提示

  • 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)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論