目录第一课:不同类型单片机开发的比较....................................................................................................................2第二课:AVR单片机的优点..................................................................................................................................3第三课:AVR单片机头文件简介与使用...............................................................................................................6第四课:AVR单片机I/O口的4种位操作方法...................................................................................................8第五课:AVR熔丝位简介....................................................................................................................................11第六课:如何把51单片机的程序移植到AVR单片机......................................................................................15第七课:AVR单片机中断的原理和使用.............................................................................................................15第八课:AVR单片机片内EEPROM的原理和使用.........................................................................................22第九课:AVR单片机片内ADC的原理和使用..................................................................................................27第十课:AVR片内定时器的原理和使用方法.....................................................................................................36第十一课:AVR单片机TWI总线的原理..........................................................................................................49第十二课:AVR单片机USART通信接口的原理和使用.................................................................................56第十三课:AVR单片机驱动LCD1602液晶显示器..........................................................................................66第十四课:AVR单片机驱动DS18B20................................................................................................................73第十五课:AVR单片机驱动DS1302..................................................................................................................80第十六课:不同型号的AVR单片机之间程序的移植方法................................................................................88ATmega16第一课:不同类型单片机开发的比较第二课:AVR单片机的优点注意:上面的DDB7应该是DDRB7.点亮第一个LED灯#includeiom16v.h#includemacros.hvoidmain(){DDRB=0xff;PORTB=0xff;DDRA=0xff;PORTA=0xfe;while(1);}第三课:AVR单片机头文件简介与使用几种操作方法的比较#includeiom16v.h#includemacros.hvoidmain(){DDRB=0xff;PORTB=0xff;DDRA=0xff;PORTA=0;PORTA|=(12);PORTA|=BIT(3);PORTA|=BIT(PORTA);}第四课:AVR单片机I/O口的4种位操作方法闪烁灯#includeiom16v.h#includemacros.hvoiddelay(unsignedcharz){unsignedcharx,y;for(x=0;x=z;x++)for(y=0;y110;y++);}voidmain(){DDRB=0xff;PORTB=0xff;DDRA=0xff;PORTA=0xff;while(1){PORTA&=~0x80;delay(500);PORTA|=0x80;delay(500);}}用按键控制灯的闪烁#includeiom16v.h#includemacros.hvoidmain(){DDRB=0xff;PORTB=0xff;DDRA=0xff;PORTA=0xff;DDRD=0;PORTD=0xff;//PD口设置为带上拉的输入while(1){if(PIND&0x40)//00000100PORTA|=BIT(7);elsePORTA&=~BIT(7):}}第五课:AVR熔丝位简介第六课:如何把51单片机的程序移植到AVR单片机第七课:AVR单片机中断的原理和使用#includeiom16v.h#includemacros.h#defineucharunsignedchar#defineuintunsignedintuchartable[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};voiddelay(){uintx,y;for(x=0;x1000;x++)for(y=0;y500;y++);}voidinit(){DDRB|=BIT(6);PORTB|=BIT(6);DDRA=0xff;PORTA=0xff;MCUCR|=0x02;//设置外部中断0的触发方式GICR|=BIT(6);//使能外部中断DDRD&=~BIT(2);PORTD|=BIT(2);//设置为带上拉电阻的输入SREG|=BIT(7);}voidmain(){init();while(1){PORTA|=BIT(0);delay();PORTA&=~BIT(0);delay();}}#pragmainterrupt_handlerint0_isr:2voidint0_isr(){PORTA=0;delay();delay();delay();PORTA=0xff;}第八课:AVR单片机片内EEPROM的原理和使用#includeiom16v.h#includemacros.h#defineucharunsignedchar#defineuintunsignedintucharcodesmg_du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};ucahrcodesmg_we[]={0x08,0x18,0x28,0x38,0x48,0x58,0x68,0x78};//位选是用74HC138译码器控制的ucharcodetable[]={0,0,0,0,0,0,0,0};voiddelay(){uintx;for(x=0;x300;x++);}voidinit(){DDRB=0xff;PORTB=0;DDRC=0x78;//01111000PORTC=0;}voiddisplay(ucahr*p){uchari;for(i=0;i8;i++){PORTB=smg_du[*p];p++;PORTC=smg_we[i];delay();}}voidEEPROM_write(uintaddr,uintadata){SREG&=~BIT(7);//关闭总中断while(EECR&BIT(EEWE));EEAR=addr;EEDR=adata;EECR|=BIT(EEMWE);EECR|=BIT(EEWE);SREG|=BIT(7);}ucharEEPROM_read(ucharaddr){ucharedata;SREG&=~BIT(7);while(EECR&BIT(EEWE));EEAR=addr;EECR|=BIT(EERE);edata=EEDR;returnedata;}voidmain(){uchari;init();i=EEPROM_read(2);i++;EEPROM_write(2,i);table[0]=i;while(1){display(table);}}第九课:AVR单片机片内ADC的原理和使用#includeiom16v.h#includemacros.h#defineucharunsignedchar#defineuintunsignedintucharcodesmg_du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};ucahrcodesmg_we[]={0x08,0x18,0x28,0x38,0x48,0x58,0x68,0x78};//位选是用74HC138控制的ucharcodetable[]={0,0,0,0,0,0,0,0};voiddelay(){uintx;for(x=0;x300;x++);}voidinit(){DDRB=0xff;PORTB=0;DDRC=0x78;//01111000PORTC=0;}voiddisplay(uchar*p){uchari;for(i=0;i8;i++){PORTB=smg_du[*p];p++;PORTC=smg_we[i];delay();}}voidadc_init(){DDRA&=~BIT(0);PORTA&=~BIT(0);ADMUX|=0x40;ADCSRA|=0xe8;SFIOR=0x00;SREG|=BIT(7);}voiddata_pro(uinttemp_l,uinttemp_h){uinttemp1,temp2;temp1=temp_h*256;temp2=temp1+temp_l;table[3]=temp2/1000;temp1=temp2%1000;table[2]=temp1/100;temp2=temp1%100;table[1]