AVR单片机 mega128 例程大全.docx

上传人:小飞机 文档编号:3153154 上传时间:2023-03-11 格式:DOCX 页数:39 大小:46.67KB
返回 下载 相关 举报
AVR单片机 mega128 例程大全.docx_第1页
第1页 / 共39页
AVR单片机 mega128 例程大全.docx_第2页
第2页 / 共39页
AVR单片机 mega128 例程大全.docx_第3页
第3页 / 共39页
AVR单片机 mega128 例程大全.docx_第4页
第4页 / 共39页
AVR单片机 mega128 例程大全.docx_第5页
第5页 / 共39页
亲,该文档总共39页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《AVR单片机 mega128 例程大全.docx》由会员分享,可在线阅读,更多相关《AVR单片机 mega128 例程大全.docx(39页珍藏版)》请在三一办公上搜索。

1、AVR单片机 mega128 例程大全/16超声波LED显示 #include /PA0接控制引脚PA7接接收引脚 #include #define chao 100 longint time=0; void delay(long int z) inti,j; for(i=0;iz;i+) for(j=0;j100;j+); void main int o=0; DDRD=0xff; DDRA=0x0f; TCNT1H=0x00; TCNT1L=0x00; while(1) PORTA=0x0f; delay(1); PORTA=0x00; while(PINA&0x80)=0x00); TC

2、CR1B=0x03; while(PINA&0x80)=0x80); TCCR1B=0x00; time=TCNT1H*256+TCNT1L; TCNT1H=0x00; TCNT1L=0x00; if(timechao) if(timechao) if(timechao) o=500; while(o-) PORTD=0x08; /AD #include #include #define unint unsigned int #define unchar unsigned char #pragma data:code const 0x07, 0x7f,0x6f,0x77,0x7c,0x39,0

3、x5e,0x79,0x71; unint mega128_ad unintaddata; DDRF&=BIT(0); PORTF&=BIT(0); ADMUX=0; ADCSRA=0X80; ADCSRA|=BIT(ADSC); while(!(ADCSRA&(BIT(ADIF); addata=ADCL; addata=addata+ADCH*256; returnaddata; void show(count) unchar temp1,temp2,temp3,temp4; temp1=count/1000; table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d

4、, temp2=(count%1000)/100; temp3=(count%100)/10; temp4=count%10; HC_595_OUT(tabletemp4); DDRB|=(14); PORTB|=(14); delay(10); DDRB|=(14); PORTB&=(14); HC_595_OUT(tabletemp3); DDRB|=(15); PORTB|=(15); delay(10); DDRB|=(15); PORTB&=(15); HC_595_OUT(tabletemp2); DDRB|=(16); PORTB|=(16); delay(10); DDRB|=

5、(16); PORTB&=(16); HC_595_OUT(tabletemp1); DDRB|=(17); PORTB|=(17); delay(10); DDRB|=(17); PORTB&=(17); void HC_595_OUT(count) PORTB&=(10); SPDR =count ; while(!(SPSR & (1SPIF); delay(10); PORTB|=(10); void delay(z) unintx,y; for(x=0;xz;x+) for(y=0;y200;y+); void main unintcount,chl; DDRC |= (17); P

6、ORTC &= (17); PORTB = 0x0F; DDRB |= (12)|(11)|(10); SPCR (1SPE)|(1MSTR)|(1SPR0)|(1SPR1); while(1) count=mega128_ad;/用参数代替老是出问题,这一点需要再次认知考虑 /*for(i=0;i4;i+) = ad3-i=shuzhi%10; shuzhi=shuzhi/10; */ show(count); /ADC #include #include #define unint unsigned int #define unchar unsigned char #pragma data

7、:code const 0x07, 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71; unint mega128_ad unintaddata; table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, DDRF&=BIT(0); PORTF&=BIT(0); ADMUX=0; ADCSRA=0X80; ADCSRA|=BIT(ADSC); while(!(ADCSRA&(BIT(ADIF); addata=ADCL; addata=addata+ADCH*256; returnaddata; void show(count) unch

8、ar temp1,temp2,temp3,temp4; temp1=count/1000; temp2=(count%1000)/100; temp3=(count%100)/10; temp4=count%10; HC_595_OUT(tabletemp4); DDRB|=(14); PORTB|=(14); delay(10); DDRB|=(14); PORTB&=(14); HC_595_OUT(tabletemp3); DDRB|=(15); PORTB|=(15); delay(10); DDRB|=(15); PORTB&=(15); HC_595_OUT(tabletemp2)

9、; DDRB|=(16); PORTB|=(16); delay(10); DDRB|=(16); PORTB&=(16); HC_595_OUT(tabletemp1); DDRB|=(17); PORTB|=(17); delay(10); DDRB|=(17); PORTB&=(17); void HC_595_OUT(count) PORTB&=(10); SPDR =count ; while(!(SPSR & (1SPIF); delay(10); PORTB|=(10); void delay(z) unintx,y; for(x=0;xz;x+) for(y=0;y200;y+

10、); void main unintcount,chl; DDRC |= (17); PORTC &= (17); PORTB = 0x0F; DDRB |= (12)|(11)|(10); SPCR (1SPE)|(1MSTR)|(1SPR0)|(1SPR1); while(1) count=mega128_ad;/用参数代替老是出问题,这一点需要再次认知考虑 /*for(i=0;i4;i+) ad3-i=shuzhi%10; shuzhi=shuzhi/10; */ show(count); = /CTC /该程序并未在示波器上测试,在仿真软件上仿真时只有PB5上有方波信号出现 #incl

11、ude #include #define uchar unsigned char #define uint unsigned int void main DDRB|=0X60; TCCR1A=0X50; TCCR1B=0X09; OCR1A=0X7CF; /D口矩阵键盘检测 #include config.h #include #include #define unint unsigned int #define unchar unsigned char const unsigned Seg7_Data=0x3F,0x06,0x5B,0x4F,0x66, 0x6D,0x7D,0x07,0x7F

12、,0x6F, 0x77,0x7C,0x39,0x5E,0x79,0x71,0x00; unchar temp1,temp2,temp3,temp4; unintdata,z; void HC_595_init DDRC=0x80; char PORTC=0x00; PORTB=0x0F; DDRB=0x07; SPCR =0x53; void Seg7_Led_display(unint data) unchar temp1,temp2,temp3,temp4; temp1=data/1000; temp2=(data%1000)/100; temp3=(data%100)/10; temp4

13、=data%10; HC_595_OUT(Seg7_Datatemp4); PORTB|=(14); delay(20); PORTB&=(14); HC_595_OUT(Seg7_Datatemp3); PORTB|=(15); delay(20); PORTB&=(15); HC_595_OUT(Seg7_Datatemp2); PORTB|=(16); delay(20); PORTB&=(16); HC_595_OUT(Seg7_Datatemp1); PORTB|=(17); delay(20); PORTB&=(17); void HC_595_OUT(unchar data) P

14、ORTB=0x06; SPDR =data ; while(SPSR=0); delay(10); PORTB=0x01; void delay(unint z) uninti,j; for(i=0;iz;i+) for(j=0;j100;j+); void delay_1us(void) /1us延时函 数 asm(nop); void delay_nus(unsigned int n) /N us延时函数 unsignedint i=0; for (i=0;in;i+) delay_1us; void delay_1ms(void) 数 unsignedint i; for (i=0;i1

15、140;i+); void delay_nms(unsigned int n) 数 unsignedint i=0; for (i=0;in;i+) delay_1ms; /1ms延时函 /N ms延时函 voidBuzz_init(void) DDRG |= (14); PORTG &= (14); void Beep(unsigned intH_time,unsignedintL_time) PORTG |= (14); delay_nms(H_time); PORTG &= (14); delay_nms(L_time); intkey_press(void) int k; delay_

16、nms(5); PORTD=0x01; delay_nms(5); if(PIND&0xF0)!=0x00) delay_nms(10); if(PIND&0xF0)!=0x00) if(PIND=0x11) Beep(100,50); PORTA=0x1F; k=1; else if(PIND=0x21) Beep(100,50); PORTA=0xFD; k=2; else if(PIND=0x41) Beep(100,50); PORTA=0xFB; k=3; else if(PIND=0x81) Beep(100,50); PORTA=0xF7; k=4; else ; else ;

17、else ; /第二行 delay_nms(5); PORTD=0x02; delay_nms(5); if(PIND&0xF0)!=0x00) delay_nms(10); if(PIND&0xF0)!=0x00) if(PIND=0x12) Beep(100,50); PORTA=0x1F; k=5; else if(PIND=0x22) Beep(100,50); PORTA=0xFD; k=6; else if(PIND=0x42) Beep(100,50); PORTA=0xFB; k=7; else if(PIND=0x82) Beep(100,50); PORTA=0xF7; k

18、=8; else ; else ; else ; /第三行 delay_nms(5); PORTD=0x04; delay_nms(5); if(PIND&0xF0)!=0x00) delay_nms(10); if(PIND&0xF0)!=0x00) if(PIND=0x14) Beep(100,50); PORTA=0x1F; k=9; else if(PIND=0x24) Beep(100,50); PORTA=0xFD; k=10; else if(PIND=0x44) Beep(100,50); PORTA=0xFB; k=11; else if(PIND=0x84) Beep(10

19、0,50); PORTA=0xF7; k=12; else ; else ; else ; /第四行 delay_nms(5); PORTD=0x08; delay_nms(5); if(PIND&0xF0)!=0x00) delay_nms(10); if(PIND&0xF0)!=0x00) if(PIND=0x18) else if(PIND=0x28) Beep(100,50); PORTA=0xFD; k=14; else if(PIND=0x48) Beep(100,50); PORTA=0xFB; k=15; else if(PIND=0x88) Beep(100,50); POR

20、TA=0xF7; k=16; else; else ; else ; return k; void main(void) inti,j,x; DDRA=0xFF; PORTA=0xFF; DDRD=0x0F; DDRE=0xFF; PORTE=0xFF; Buzz_init; while(1) x=key_press; HC_595_init; Seg7_Led_display(x); /1602液晶 #include #define uchar unsigned char #define uint unsigned int #define uchar unsigned char #defin

21、e uint unsigned int #define RS 0 #define RW 1 #define EN 2 ucharwz=*WEN.JIU.LONG*; uchargd=Good Luck!; voids_ms(uintms) for(;ms1;ms-); /写指令 voidwritecom(uchar com) PORTC&=(1RS); /RS=0 命令选择 PORTC&=(1RW); PORTC|=(1EN); s_ms(500); PORTA = com; s_ms(500); PORTC&=(1EN); /1602初始化 void LcdInit(void) writec

22、om(0x01); writecom(0x02); writecom(0x06); writecom(0x0c); /RW=0 写选择 /EN=1 使能信号 /输出指令 /EN=0 writecom(0x38); /没理解好这里设置的是四位总线 ,而且设不成5X10的点阵 /= / 描述:在任意位置写任意多个字符 / 入口:row要写的字符所在的行,只能为1或2; / col要写的字符所在的列,只能为0-15 / num要写字符的个数 / pbuffer要写字符的首地址 /= void WriteChar(ucharrow,ucharcol,uintnum,uchar *pBuffer) uc

23、hari,t,add; if (row = 1) add = 0x80 + col+0x10; else add = 0x80+0x40 + col+0x10; writecom(add); PORTC&=(1RW); s_ms(500); PORTC|=(1RS); for(i=num;i!=0;i-) t = *pBuffer; PORTA = t; PORTC|=(1EN); s_ms(500);/必须有个延时 PORTC&=(1EN); pBuffer+; void main int i; DDRA=0xff; PORTA=0xff; DDRC=0xff; PORTC=0xff; Lc

24、dInit; WriteChar(1,1,14,wz); WriteChar(2,3,10,gd); for(i=0;i16;i+) writecom(0X18); s_ms(50000); s_ms(50000); s_ms(50000); s_ms(50000); s_ms(50000); s_ms(50000); s_ms(50000); s_ms(50000); /按键加显示的最简写法.txt #include config.h #include #include #define unint unsigned int #define unchar unsigned char const

25、 unsigned char Seg7_Data=0x3F,0x06,0x5B,0x4F,0x66, 0x6D,0x7D,0x07,0x7F,0x6F, 0x77,0x7C,0x39,0x5E,0x79,0x71,0x00; unchar Seg7_Led_Buf4,temp1,temp2,temp3,temp4; unintdata,z; void HC_595_init DDRC=0x80; PORTC=0x00; PORTB=0x0F; DDRB=0x07; SPCR =0x53; void Seg7_Led_display(unint data) unchar temp1,temp2,

26、temp3,temp4; temp1=data/1000; temp2=(data%1000)/100; temp3=(data%100)/10; temp4=data%10; HC_595_OUT(Seg7_Datatemp4); PORTB|=(14); delay(20); PORTB&=(14); HC_595_OUT(Seg7_Datatemp3); PORTB|=(15); delay(20); PORTB&=(15); HC_595_OUT(Seg7_Datatemp2); PORTB|=(16); delay(20); PORTB&=(16); HC_595_OUT(Seg7_

27、Datatemp1); PORTB|=(17); delay(20); PORTB&=(17); void HC_595_OUT(unchar data) PORTB=0x06; SPDR =data ; while(SPSR=0); delay(10); PORTB=0x01; void delay(unint z) uninti,j; for(i=0;iz;i+) for(j=0;j100;j+); voidKey_init(void) KEY_DDR &= 0xFF; KEY_PORT |= 0xFF; unsigned char get_key(void) unsigned char

28、Key_Value,tmp; Key_Value=KEY_PIN & 0xFF; if(Key_Value != 0xFF) delay_nms(2); if(Key_Value != 0xFF) Key_Value=PIND; returnKey_Value; void delay_1us(void) 数 asm(nop); void delay_nus(unsigned int n) unsignedint i=0; /1us延时函/N us延时函数 for (i=0;in;i+) delay_1us; void delay_1ms(void) /1ms延时函数 unsignedint i

29、; for (i=0;i1140;i+); void delay_nms(unsigned int n) /N ms延时函数 unsignedint i=0; for (i=0;in;i+) delay_1ms; voidBuzz_init(void) BUZZ_DDR |= (1SPK); BUZZ_PORT &= (1SPK); void Beep(unsigned intH_time,unsignedintL_time) BUZZ_PORT |= (1SPK); delay_nms(H_time); BUZZ_PORT &= (1SPK); delay_nms(L_time); void

30、 main(void) unsigned char Key; DDRA=0xFF; PORTA=0x00; DDRE = 0xFF; PORTE = 0xFF; Key_init; Buzz_init; while(1) int w=10; Key=get_key; switch(Key) case (0xFE): case (0xFD): case (0xFB): Beep(100,50); PORTA=0xFE; break; Beep(100,50); PORTA=0xFD; HC_595_init; Seg7_Led_display( 617); break; Beep(100,50)

31、; PORTA=0xFB; HC_595_init; case (0xF7): case (0xEF): while(w-) Seg7_Led_display( 327); break; PORTA=0xF7; Beep(100,50); HC_595_init; while(w-) Seg7_Led_display( 307); break; case (0xDF): PORTA=0xEF; Beep(100,50); HC_595_init; while(w-) Seg7_Led_display( 617); break; PORTA=0xDF; Beep(100,50); HC_595_

32、init; while(w-) Seg7_Led_display( 327); break; case (0xBF): case (0x7F): PORTA=0xBF; Beep(100,50); HC_595_init; while(w-) Seg7_Led_display( 307); break; PORTA=0x7F; Beep(100,50); HC_595_init; while(w-) Seg7_Led_display( 307); break; default: break; /18B20 #include config.h #include #include #include

33、 hc595.h #include spi.h #include 18b20.h voidspi_init(void) DDRB |= (1MOSI)|(1SCK)|(1SS);/设置MOSI,SCK输出 SPCR 主机模式 voidSPI_MasterTransmit(char Data) /* 启动数据传输 */ SPDR = Data; /* 等待传输结束 */ while(!(SPSR & (1SPIF) ; const /0,1,2,3,4 0x6D,0x7D,0x07,0x7F,0x6F, /5,6,7,8,9 0x77,0x7C,0x39,0x5E,0x79,0x71,0x00;

34、 unsigned char Seg7_Data=0x3F,0x06,0x5B,0x4F,0x66, = (1SPE)|(1MSTR)|(1SPR0)|(1SPR1);/使能SPI,/a,b,c,d,e,f volatile unsigned char Seg7_Led_Buf4,point=0,point_pos=0;/point是小数点标志1代表有小数点point_pos表示小数点位置 void HC_595_init(void) OE_DDR |= (1OE); OE_PORT &= (1OE); PORTB = 0x0F; spi_init; Seg7_Led_Buf0=16; Seg

35、7_Led_Buf1=16; Seg7_Led_Buf2=16; Seg7_Led_Buf3=16; void HC_595_OUT(unsigned char data) SS_L; SPI_MasterTransmit(data); SS_H; void Seg7_Led_Update(void) HC_595_OUT(Seg7_DataSeg7_Led_Buf0); Seg7_Bit0_En; delay_nus(60); Seg7_Bit0_Dis; HC_595_OUT(Seg7_DataSeg7_Led_Buf1); if(point=1)&(point_pos=1) HC_595_OUT(Seg7_DataSeg7_Led_Buf1)|(1dp); Seg7_Bit1_En; delay_nus(60); Seg7_Bit1_Dis; HC_595_OUT(Seg7_DataSeg7_Led_Buf2); if(p

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号