预览加载中,请您耐心等待几秒...
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10

亲,该文档总共16页,到这已经超出免费预览范围,如果喜欢就直接下载吧~

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

1.跑马灯 #include<iom16v.h> /*延时函数*/ voidDelay() { unsignedchara,b; for(a=1;a;a++) for(b=1;b;b++); } voidmain() { inti; DDRB=0xFF;/*定义B口输出*/ while(1) { PORTB=0x01;/*点亮一个LED灯*/ Delay(); for(i=0;i<7;i++) { PORTB<<=1;/*被点亮的LED灯移位*/ Delay(); } } } PROTEUS仿真图: 2.方波产生 //ICC-AVRapplicationbuilder:2011-7-1820:05:48 //Target:M16 //Crystal:7.3728Mhz #include<iom16v.h> #include<macros.h> voidmain(void) { PORTA=0x00; DDRA=0xFF; //初始化A口 while(1) { PORTA=0XFF; //A口置高电平 DELAY(50); //高电平延时 PORTA=0X00; //A口置低电平 DELAY(50); //低电平延时 } } //延时函数 void DELAY(unsigned inti) { unsignedint a,b; for(b=0;b<i;b++) for(a=0;a;a++); } PROTEUS仿真图: 3.外部按键中断 //ICC-AVRapplicationbuilder:2011-7-9PM03:34:01 //Target:M16 //Crystal:7.3728Mhz #include<iom16v.h> #include<macros.h> voidport_init(void) { PORTB=0xFF; DDRB=0xFF; PORTD=0xFF; DDRD=0x00; } #pragmainterrupt_handlerint0_isr:2 voidint0_isr(void) { PORTB=~PORTB; //externalinteruptonINT0 } //callthisroutinetoinitializeallperipherals voidinit_devices(void) { //stoperrantinterruptsuntilsetup CLI();//disableallinterrupts port_init(); MCUCR=0x0A; GICR=0x40; TIMSK=0x00;//timerinterruptsources SEI();//re-enableinterrupts //allperipheralsarenowinitialized } voidmain(void) { init_devices(); while(1) {} } PROTEUS仿真图: 4.定时器中断 //ICC-AVRapplicationbuilder:2011-7-10AM10:41:53 //Target:M16 //Crystal:7.3728Mhz #include<iom16v.h> #include<macros.h> unsignedinti=0; voidport_init(void) { PORTB=0x01; DDRB=0xFF; } //TIMER0initialize-prescale:1024 //WGM:Normal //desiredvalue:10mSec //actualvalue:10.000mSec(0.0%) voidtimer0_init(void) { TCCR0=0x00;//stop TCNT0=0xB8;//setcount OCR0=0x48;//setcompare TCCR0=0x05;//starttimer } #pragmainterrupt_handlertimer0_ovf_isr:10 voidtimer0_ovf_isr(void) { i++; if(i==100) {PORTB<<=1; i=0; if(PORTB==0X00) {PORTB=0X01;}} TCNT0=0xB8;//reloadcountervalue } //callthisroutinetoinitializeallperipherals voidinit_devices(void) { //stoperrantinterruptsuntilsetup CLI();//disableallinterrupts port