征服嵌入式系列之AVR单片机笔记_第1页
征服嵌入式系列之AVR单片机笔记_第2页
征服嵌入式系列之AVR单片机笔记_第3页
征服嵌入式系列之AVR单片机笔记_第4页
征服嵌入式系列之AVR单片机笔记_第5页
已阅读5页,还剩100页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、目录TOC o 1-3 h z u HYPERLINK l _Toc341289269 第一课:不同类型单片机开发的比较 PAGEREF _Toc341289269 h 1 HYPERLINK l _Toc341289270 第二课:AVR单片机的优点 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单片机片内内EEPROOM的原理和和使用 PAGEREF _Toc341

3、289276 h 22 HYPERLINK l _Toc341289277 第九课:AVRR单片机片内内ADC的原理理和使用 PAGEREF _Toc341289277 h 27 HYPERLINK l _Toc341289278 第十课:AVRR片内定时器器的原理和使使用方法 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单片机驱驱动LCD11602液晶晶显示器 PAGEREF _Toc341289281 h 65 HYPERLINK l _Toc341289282 第十四课:AVVR单片机驱驱动DS188B20 PAGEREF _Toc341289282 h 73 HYPERLINK l _Toc341289283 第十十五课:AVVR单片机驱驱动DS13302 PAGEREF _Toc341289283 h 80 HYPERLINK l _Toc341289284 第十六课:不同同型号的

5、AVVR单片机之之间程序的移移植方法 PAGEREF _Toc341289284 h 88ATmega116第一课:不同类类型单片机开开发的比较第二课:AVRR单片机的优优点注意:上面的DDDB7应该该是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口口设置为带上上拉的输入 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;/设置置外部中

9、断00的触发方式式 GICR|=BIT(66);/使使能外部中断断 DDRD&=BIT(2); PORTD|=BIT(2);/设置为带上上拉电阻的输输入 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单片机片内内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);/关闭总中断断 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单片机片内内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片内定时器器的原理和使使用方法用普通模式实现现秒表#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单片机驱驱动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,具体函数数自己写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单片机驱驱动DS188B20注意:DS188B20对时时间要求非常常高,延时函函数一定要精精确#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,具体函数数自己写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();/低低字节 j=ds118b20_read_byte();/高高字节 temp=j*2566+i;/26.8 temp=temp*0.6255;/2668 data_pro(ttemp); for(kk=0;k20;k+) dissplay(tablee); 第十五课:AVV

33、R单片机驱驱动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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论