Document Details

SharperDeStijl4573

Uploaded by SharperDeStijl4573

مؤسسه آموزش عالی غیرانتفاعی راغب اصفهانی

false

غزل مقاره زاده

Tags

AVR programming Embedded Systems C programming

Summary

This document contains examples of AVR programming using different methods (e.g., using CodeVision and AVRStudio). Different scenarios are described and implemented through C code. The examples include details of connecting GPIO and LED.

Full Transcript

‫ل ﻘﺎرهزاده‬ ‫‪c‬‬ ‫‪AVR‬‬ ‫‪:3‬‬ c +, ) * ( ' %$ & "$...

‫ل ﻘﺎرهزاده‬ ‫‪c‬‬ ‫‪AVR‬‬ ‫‪:3‬‬ c +, ) * ( ' %$ & "$ "# ! 6 %-" 3& 5 4.. 3 " & 01 2.. %-" ) :. 9 -: ) 8 & 3& 5 7 +, 31 2 >8 ) 6= ;< 0&  %-".- & 6=* ? &4 C % B 31 3& ! A @ 6 3& 5.%? & D6. *  F84 9 D6 E& & ' 31 1 3& 5.%? ‫ل ﻘﺎرهزاده‬ G. 31H I %-" J 3-81 c ) =OP.1 1-6 ) % B N 6 7 +, 2 6 1/5 ) %-" N 6 31 1 6 ) =OP.- % B N 6 31 1 6.1 O TU H.- = 8 ! 4. H Z XY & 1 & O 7 +, ' W ( >8 3V 7 "#  3& 5 %-" #,? (1 &&4 " 1 =OP H.:.1 1 9 < & 1 1 T& # 7 +, & %-" 9 & $ 3 [: .& 3& 5 7 +, ' T& & %-" & ! ‫ل ﻘﺎرهزاده‬ 4-81 c : AVRStudio N Z & % $ #include #include.... ": D [: @ 6 ^ #6 [: int main (void){ %Z D [: ( @ 6 C @ 6 $ ) & return 0; } ‫ل ﻘﺎرهزاده‬ 5-81 c : CodeVision N Z & % $ #include "11 ! ].h" #include "21 ! ].h".... ": D [: @ 6 ^ #6 [: void main (void){ %Z D [: ( @ 6 C @ 6 $ ) & return 0; ‫ل ﻘﺎرهزاده‬ } 6-81 9.. 3& & e $&$ I/O d P c 9 -b 9 -b AVR 9.1 fCH x9 & 4 $ & :DDRx 9 -b . ! g & 4 $; # Y, ' !. & 1Y. ! g & & ;# Y, 5< !. & 1Y ).& T 9 ;U. 6 :[U P :PORTx 9 -b .& & ? 9 -b ) & 3& & 1 x 9 3& &.& T x9 i U h $. 6 :[U P :PINx 9 -b .& 31 $ PINx 9 Z 1 (x 9 3& & 1 $ 6 -: ‫ل ﻘﺎرهزاده‬ 7-81 9 DDRx.& ^ #6 & : A9  DDRA=0x00; DDRA=0b00000000; DDRA=0;.& ^ #6 4 $ : B9  DDRB=0xFF; DDRB=0b11111111; DDRB=255;.& ^ #6 4 $ : PD.7 PD.4  DDRD=0b10010000; DDRD=0x90; &W &! CodeVision & & ‫ل ﻘﺎرهزاده‬ DDRD.4=1;.. - # 0x1F 6 5< &! 3 & I/O 9 -b DDRD.7=1; 8-81 9 PORTx.. 31 0 d & 5 g & PORT 9 -b.& ^ #6 4 $ 9 0 (1.& T 4 $ (& 9 -b ) & 2 !. P ) &.1 & H 42 &1: 1Y & ^ #6 4 $ C 9 ! & 1 #,? DDRC=0xFF; PORTC=42;.& ^ #6 & 9 0 (2 5 # include < mega32. h> int main (void) { void main (void) { PORTB=0xff; PORTB=0xff; DDRB = 0; DDRB = 0; PORTC=0; PORTC=0; DDRC = 255; DDRC = 255; while(1) { while(1) { PORTC = ~PINB; PORTC = ~PINB; } } return 0; } } ‫ل ﻘﺎرهزاده‬ AVRStudio Codevision 30-81 8" Y, -66 1 ' 1 6 %": d 31 & (1 50 6A ,".& | " & & ].: s # h ' ! T ": ] "6 1 ": ( ) 1 J ' & 31 & & #.. ; #.: '.: s z 63 &' ) & $ %$ &. # ]O 8 AVR.& ] O.: # T E 1 6 & $ %$ &. # ATmega32.1 &.: s # T E.1 | " 86 E 3&8 6 86 ' s ‫ل ﻘﺎرهزاده‬ 31-81 T(s) &$ * 61 h 1H TU. 91 # h z 63 & & void main (void) { 7Tr PORTD=255; DDRD = 0; PORTC=0; DDRC = 255; sB 1 C D9 1 while(1) { &1: 1 20 0 (11001100 &1: 1 10 0 if ( PIND == 10 ) PORTC =0b11001100; 0 (01111110 &1: 1 30 0 (10101010 else if ( PIND == 20 ) PORTC =0b10101010; &1: 1 50 0 (11100111 &1: 1 40 else if ( PIND == 30 ) PORTC =0b01111110; else if ( PIND == 40 ) PORTC =0b11100111; 11001010 &1: 1 60 0 10000001 else if ( PIND == 50 ) PORTC =0b10000001; & k & &.1 T C9 else if ( PIND == 60 ) PORTC =0b11001010;.1 { $ 4 $ else PORTC = 0; ‫ل ﻘﺎرهزاده‬ } Codevision } 40-81 # include < mega32. h> void main (void) { 7Tr PORTD=255; DDRD = 0; (] & { ) PORTC=0; DDRC = 255; while(1) { switch (PIND){ case 10 : PORTC =0b11001100;break; case 20 : PORTC =0b10101010;break; case 30 : {PORTC =0b01111110;break;} case 40 : {PORTC =0b11100111;break;} case 50 : {PORTC =0b10000001;break;} case 60 : {PORTC =0b11001010;break;} ‫ل ﻘﺎرهزاده‬ default: PORTC = 0; } } } Codevision 41-81 8Tr '( & (#$ " ! ). + # & '( & #$ ! )* # include < mega32. h> char N=0; void main (void) { PORTA=0xff; DDRA= 0; PORTC=0; DDRC=0xff; while(1) { N=PINA; N=N & 0x0f; PORTC=PINA & 0x0F; PORTC=N; ‫ل ﻘﺎرهزاده‬ Codevision } } 42-81 4 12/3 !0 + !/. - & 9Tr. 5 '( & # include < mega32. h> char N=0; void main (void) { PORTA=0xff; DDRA= 0; PORTC=0; DDRC=0xff; while(1) { N=PINA; N=N >>1; PORTC=PINA>>1; PORTC=N; ‫ل ﻘﺎرهزاده‬ } } Codevision 43-81 ) "6 & & 7 - ) - Z @"4 < P W # 3 " 1.1 & A " 4 $.1 0 ?=.. &1: &. ] 7 &1: & @"4 : " AVRStudio Codevision Proteus ‫ل ﻘﺎرهزاده‬ 44-81 ,Y LED ' % LED.5 " ?.5 EH " ]? ! & ).1 31 u0 ] h 6 a & H6 T 1 1 &.& 3& & H " ?.5 EH ".& & & 4 ! J & B J & 2 ‫ل ﻘﺎرهزاده‬ 45-81 ‫ل ﻘﺎرهزاده‬ ‫‪46-81‬‬ ‫‪3‬‬ ‫‪;7‬‬ ‫‪3‬‬ ‫‪; 16‬‬ ‫ل ﻘﺎرهزاده‬ ‫‪47-81‬‬ € H 1 ! EH " ]? A " 3Z @; a b c d e f g h 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 1 2 0 0 1 0 0 1 0 1 3 0 0 0 0 1 1 0 1 4 1 0 0 1 1 0 0 1 5 0 1 0 0 1 0 0 1 6 0 1 0 0 0 0 0 1 7 0 0 0 1 1 1 1 1 8 0 0 0 0 0 0 0 1 9 0 0 0 0 1 0 0 1 ‫ل ﻘﺎرهزاده‬ 48-81 € H 1 ! EH " 16 - ; PA " 3Z @; a b c d e f g h A 0 0 0 ١ 0 0 ٠ 1 b 1 1 0 0 0 0 0 1 C 0 1 1 0 0 0 1 1 d 1 0 0 0 0 1 0 1 E 0 1 1 0 0 0 0 1 F 0 1 1 1 0 0 0 1 ‫ل ﻘﺎرهزاده‬ 49-81 10 T r 1! " ?.5 EH " & ! 1 C PB 9 6 5< ) &1: 3 " 1.& { $1 EH " ( 1 9 0 8 31 31 $ &1: 0.1 & A " PC € H.. PC0 6 PC7 v66 EH " h 6 a. ! } ‫ل ﻘﺎرهزاده‬ 50-81 # include < avr/io. h> 10 T r int main (void) { unsigned char index , seven_seg_codes[]={0x03, 0x9F, 0x25, 0x0D, 0x99,0x49, 0x41 ,0x1F ,0x01, 0x09}; PORTB=255; DDRB = 0; PORTC=0; DDRC = 255; for(;;) { if ( PINB >= 10 ) PORTC = 0xFF; //Turn off the Common Anode 7-Seg else { index = PINB ; PORTC = seven_seg_codes [ index ]; } } ‫ل ﻘﺎرهزاده‬ return 0; } AVRStudio 51-81 # include < mega32. h> void main (void) { 10 T r PORTB=0xFF; DDRB = 0; PORTC=0;DDRC = 255; (] & { ) while(1) { switch (PINB) { case 0: PORTC = 0x03;break; case 1: PORTC = 0x9F;break; case 2: PORTC = 0x25;break; case 3: PORTC = 0x0D;break; case 4: PORTC = 0x99;break; case 5: PORTC = 0x49;break; case 6: PORTC = 0x41;break; case 7: PORTC = 0x1F;break; case 8: PORTC = 0x01;break; case 9: PORTC = 0x09;break; default: PORTC = 0xFF; } ‫ل ﻘﺎرهزاده‬ } Codevision } 52-81 10 T r.1 H (Look-Up Table: LUT) 4. 4 T 14 " Tr ) sB = 3 $t 4. 4 T 14 ' & 4 $ (& 1 & 4 4 $ & ) , #? 6 { ) ) 2".= T 14 & { g 4 $( &. & = P U " = C [r.& 3& 5 8. 31 2 , 4 $ & ) , W 1 1 = ).& & E T& # 9 < e$ & % '.- - Z 4.. 3& 1 z P % W & 3 $t & 4. 4 T 14 ' & - Z ! &Y -? 6 ( 6- Z ‫ل ﻘﺎرهزاده‬.1 A 8 { &.: ! '" 53-81 # include < avr/io. h> int main (void) { 11 T r PORA=PORB=255; DDRA = DDRB = 0; &1: PA:PB 9 h 1 PORC=PORD=0; h & 255) carry = 1; PORTD = PINB + 0x7A; PORTC = PINA + 0x12 + carry; } ‫ل ﻘﺎرهزاده‬ return 0; AVRStudio } 54-81 12 T r.1 PC & ! &3& ] ? o "O 1 C PB 1 # include < mega32. h> void main (void) { PORTB=255;DDRB = 0; PORTC=0;DDRC = 255; while (1) { unsigned char n = PINB, sum = 0; sum = sum + n % 10 ; //or sum + = n % 10; n = n / 10; sum = sum + n % 10; sum = sum + n / 10; PORTC = sum; ‫ل ﻘﺎرهزاده‬ } Codevision } 55-81 ) "6 & LED 9 < 3 C& f? o 1.1 & H 4 $ AVRStudio Codevision Proteus ‫ل ﻘﺎرهزاده‬ 56-81. & & :' '  OR E &1: ! (. Y D6 1 &1: ' ] -i. & ' ( | E%":).. 5<. Y ' ] -i. E ) &.= : 5< '  AND E &1: ! (. Y D6 1 &1: ' ] -i. & 5< (& E%":).. '. Y 5< ] -i. E ) &.= :W # '  XOR E &1: ! (. Y D6 1 &1: ' ] -i. & W # (^ E%":).. 5<. Y ' ] -i. E ) &.= ‫ل ﻘﺎرهزاده‬ 57-81 9 ). Y D6 1 PB4 LED 1 13 T r.1 ) { $ # include < avr/io. h> # include < util/delay.h> # include < mega32. h> int main (void) { # include < delay.h> PORTB=0x00; void main (void) { DDRB = DDRB | 0x10; PORTB=0x00; while (1) { DDRB. 4 = 1; PORTB = PORTB | 0x10; while (1) { _delay_ms(500); PORTB = PORTB ^ 0x10 ; PORTB. 4 = 1; PORTB = PORTB & 0xEF; _delay_ms(500); delay_ms(500); _delay_ms(500); PORTB. 4 = 0; } delay_ms(500); return 0; } ‫ل ﻘﺎرهزاده‬ } AVRStudio } Codevision 58-81 14 T r 1 C. PB 9 P & 1 % &1:.H 9 Z 1.1 & A " " ?.5 EH " &1: "? &1: ' 9 < ! 1Y. + 9 - 8* / : 6 ‫ل ﻘﺎرهزاده‬ 59-81 14 T r : P.= & ?A9 01< =? E.١.= ' (PC7) yJ." 7-seg € H 1 !.٢.= 5< (PC6,PC5) EH " Y € H 1!.٣.& 1 EH " E 6= &O $~6 1.۴." ' :A ) 8 ": 8;.۵." ' - (PC6) >/ 7-seg < * =.۶." ' 0@ (PC7,PC5) 8* ?< * =.٧ ‫ل ﻘﺎرهزاده‬." ' 4 &AB '.٨ 60-81 14 T r :( & ) P.= & ?A9 =? E.٩.= ' (PC5).." 7-seg € H 1 !.٠١.= 5< (PC7,PC6) EH " Y € H 1!.١١.= &O $~6 1.٢١." ' 6B CD " D' 1 C D.٣١ ‫ل ﻘﺎرهزاده‬ 61-81 # include “mega32.h” # include “delay.h” 14 T r void main( void ){ unsigned char x , n1 , n2 , n3 ; const unsigned char codes [] = {0x03,0x9F,0x25,0x0D,0x99,0x49, 0x41,0x1F,0x01,0x09}; PORTA=0; DDRA = 255; PORTC=0; DDRC = 0xE0; //11100000 PORTB=0xff; DDRB = 0; while (1){ x = PINB; n3 = x%10; x = x/10; n2 = x%10; Codevision ‫ل ﻘﺎرهزاده‬ n1 = x/10; //PINB is converted to decimal digits n1-n2-n3 62-81 14 T r PORTA = codes [n1]; PORTC = 0x80; //10000000:Turn the 1st seven segment on. delay_ms(1); PORTA = codes [n2]; PORTC = 0x40; //01000000:Turn the 2st seven segment on. delay_ms(1); PORTA = codes [n3]; PORTC = 0x20; //00100000:Turn the 3st seven segment on. delay_ms(1); } } Codevision ‫ل ﻘﺎرهزاده‬ 63-81 & 3& 5 "? 1 J " ?.5 EH " 6 H = P U.1 31 = $&.1 TD & =Y 9 < " ?.5 EH " ' 13 7-SEG 1 BCD 1 T1- 6 BCD 1 3& 5 EH " ) 13 * {.1 3& 5 d )... 4011 (7448 (7447 1 6.. G BCD 1 ‫ل ﻘﺎرهزاده‬ 64-81 ‫‪G‬‬ ‫‪8 6‬‬ ‫‪€ H‬‬ ‫‪T 6.%:‬‬ ‫ل ﻘﺎرهزاده‬ ‫‪65-81‬‬ C & # 6 6 p 1 &s : BCD & & & 1: 1-6.*4.& 3& 5 (. 31 3& & ? bcd.h BCD 1 & & &1: 1-6  unsigned char bin2bcd (unsigned char n) a = bin2bcd(0b11001101) & & &1: BCD 1 1-6  unsigned char bcd2bin (unsigned char n) b = bin2bcd(0b01000101) ‫ل ﻘﺎرهزاده‬ 66-81 15 T r & 55h &1: (1 31H 3& H ) 1 & ! PD.0 H 1% ' 0 1 1 }.1 & A " B 9 LED &1: 8 AAh &1: (1 % 1 3& H 9 <.1 & O «' » 1Y 1 3& H ] E 31 0 1% # include < mega32. h> void main (void) { DDRD.0= 0;PORTD.0=1; DDRB=0xFF; PORTB=0; while(1){ while(PIND.0==1) { PORTB=0x55;} PORTB=0xAA; Codevision }} ‫ل ﻘﺎرهزاده‬ 67-81 1P ' (& 3& H. 31 PB2 1% 1 16 T r & 3& H 1 % E.1 q (& 3& & A " C 9 &1:.& 1 6 5< 1Y # include < mega32. h> char up=0; void main (void) { DDRB.2= 0;PORTB.2=1; DDRC=0xFF;PORTC=0; while(1){ if(PINB.2==0) {up=up+1;} if(up>255) { up=0;} PORTC=up; }} H "&.. T %": 9 < ) 4 ‫ل ﻘﺎرهزاده‬.& 4 8 (& & 1 % A & ! -? 6 1 & 68-81 -? ƒ[< 16 T r # include < mega32. h> char up=0; void main (void) { DDRB.2= 0;PORTB.2=1; DDRC=0xFF;PORTC=0; while(1){ if(PINB.2==0) {up=up+1;} while(PINB.0!=1); if(up>255) { up=0;} PORTC=up; }}.. X H 1 % A*4 & &= E&T ) ‫ل ﻘﺎرهزاده‬ 69-81 -? ƒ[< 16 T r 1 h $&$ & 1 $ ( ! 3& 5 61 s H 1% & ) A*4 )1 J 1 " ? T 6 %< [ & 1 3& H ] E 1 3- J& A*4 ).1 1.P 61 & < @,?. P &. ]7 H ) @.. 31 ] O -6 1 J 1 6 (1 % H ' 4.&& 0 ] O 1 % 0A*4 ƒ[,< 6& 3& & ? 6 $6 Z ‫ل ﻘﺎرهزاده‬ 70-81 # include < avr/io. h> # include < util/delay.h> P 6 < { -? ƒ[< 16 T r char up=0; int main (void) { # include < mega32. h> DDRB = DDRB & 0xFE; # include < delay. h> DDRC = 0xFF; char up=0; while(1){ void main (void) { while(PINB & 0x01==1);//Wait until push button is pressed. DDRB.2= 0;PORTB.2=1; _delay_ms(100); DDRC=0xFF;PORTC=0; while(1){ up=up+1; if(PINB.2==0) {up=up+1;} if(up>255) { up=0;} while(PINB.0!=1); PORTC = up; if(up>255) { up=0;} while(PINB & 0x01 ==0);//Wait until push button is released. PORTC=up; _delay_ms(100);} delay_ms(100); return 0; }} ‫ل ﻘﺎرهزاده‬ } 71-81 1 E%": ) !) 9 -b & #.. and # & E%": : && & 9 56  U & &. "" # && 6 < &.1 and = &...& & & X 1 J & && 1. 1 E%": ) !) 9 -b & #.. OR # | E%": : || | 9 56 && 1 U & &. " " # || 6 < &.1 OR =..& & & X 1J & = b a &1: &. # ) 1 a=b.. & &.- = E%": : == = 9 56.. b a 0 # if(a==b) r & & & U 9 & & 7 "# ==...& " b a &) & ‫ل ﻘﺎرهزاده‬ 72-81 17 T r # include < avr/io. h> int main (void) { DDRB = DDRB & 0xFC; z& &. P 1 DDRC = DDRC | 0x03; while(1){ T 1 PB 9 ' 5< if ((PINB & 0x03) ==0x03) { † J& ! 0 …1 PORTC = PORTC | 0x02; PORTC = PORTC & 0xFE; } PC1 V! & & 0 PC0 else if ( 0 < (PINB & 0x03) && (PINB & 0x03) < 0x03){.& ) PORTC = PORTC | 0x01; PORTC = PORTC & 0xFD; }.P &z& & f CH6 1 else if ((PINB & 0x03) ==0){ PORTC = PORTC & 0xFC; } 1 6 «' » Y, T E (z & & }.1 ‫ل ﻘﺎرهزاده‬ return 0; } 73-81 18 T r PB.6 (PC > PD 0 1 C PD PC 1.1 { $ 9 < ) k & ) # include < avr/io. h> int main (void) { DDRC = DDRD = 0; DDRB = DDRB | 0x40; for(;;){ if( PINC > PIND ) PORTB = PORTB | 0x40; else PORTB = PORTB & 0xBF; } return 0; ‫ل ﻘﺎرهزاده‬ } AVRStudio Codevision 74-81 # include < avr/io. h> 19 T r # include < util/delay.h> void mydelay (unsigned char t); int main (void) { & h. 1 DDRC = 0xFF; DDRB = 0; $~6.1 { $ ) v66 PC while(1){ unsigned char t = PINB; &1:. 1 { $ ) ) PORTC = 0x55;.&& 0 =d6& 31 $ PB mydelay (t); PORTC = 0xAA; mydelay (t); } return 0;} void mydelay (unsigned char t){ _delay_ms(50*t); ‫ل ﻘﺎرهزاده‬ } 75-81 :& 3& 5 %-" & C ) &. 0 J HI/ G/ / ( CF / + ) 'J K  ( ) $) 'J K . 'L / C O ) N @ M - !L : AVR GCC L 2. (;) ) #endasm #asm F 'L / / Codevision ‫ل ﻘﺎرهزاده‬ 76-81 3& 5 %-" & 1 s.& 1 & 4 C & 1P ' A$ J &Tr U.& ": 9 < Codevision & &) 3& 5.*4.& # include < mega32. h> void main (void) { DDRA = 0; I. & T# ) 2" DDRB = 255; #asm(“SEI”) //Enable interrupt &. #q 9 -b & ( 5? T#) while(1){ #asm Tr & & 3& 5 SEI %-" IN R12,PINA ROR R12.. 31 t OUT PORTB, R12 #endasm } ‫ل ﻘﺎرهزاده‬ } 77-81 p 1 & SRAM d P &.& q mem.h ] !1 ! 1 d P) & @ 6 3& 5 SRAM & ' 3& & 3 $t  void pokeb (unsigned int addr , unsigned char addr ) SRAM & & 3& & 3 $t  void pokew (unsigned int addr , unsigned int addr ) SRAM ' 3& & 1 $  unsigned char peekb (unsigned int addr) SRAM & 3& & 1 $  unsigned int peekw (unsigned int addr) ‫ل ﻘﺎرهزاده‬ 78-81 20 T r D 100 & )= Q$ D P B ?. &U SRAM TO. 200 & )= O # include “ mega32.h” # include “ mem.h” void main (void) { DDRB = 0; while(1){ if( PINB.0 == 0 ) pokeb(100 , PINB); else pokeb(200 , PINB); ‫ل ﻘﺎرهزاده‬ } } 79-81 SRAM TO. 100 & Q$ D P B ? 21 T r. 5 C SRAM TO. 200 & O D # include “ mega32.h” # include “ mem.h” void main (void) { PORTC=0; DDRC=0xFF; PORTB=0xFF; DDRB = 0; while(1){ if( PINB.0 == 0 ) POTRC=peekb(100); else PORTC=peekb(200); ‫ل ﻘﺎرهزاده‬ } } 80-81

Use Quizgecko on...
Browser
Browser