Yasser Shekofteh Fall PDF

Please download to get full document.

View again

of 130
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information Report
Category:

Slides

Published:

Views: 180 | Pages: 130

Extension: PDF | Download: 3

Share
Related documents
Description
برنامه نويسي ميكروكنترلرهاي AVR با استفاده از نرم افزار CodeVisionAVR Yasser Shekofteh Fall انواع كامپايلرهاي ميكروكنترلر :AVR -زبان C و ++C: IAR و GCC Imagecraft CodevisionAVR - - زبان بيسيك: و
Transcript
برنامه نويسي ميكروكنترلرهاي AVR با استفاده از نرم افزار CodeVisionAVR Yasser Shekofteh Fall 1387 انواع كامپايلرهاي ميكروكنترلر :AVR -زبان C و ++C: IAR و GCC Imagecraft CodevisionAVR - - زبان بيسيك: و FAST AVR Bascom- - زبان پاسكال: E-lab - CodeVisionAVR is a C cross compiler, Integrated Development p g p Environment and Automatic Program Generator designed for the Atmel AVR family of microcontrollers. مرجع اصلي: :(Codevision) مرجع كمكي -پرتوي فر - ره افروز Application Notes: - AVR033: Getting Started with the CodeVisionAVR C Compiler -AVR035: Efficient C Coding for AVR -CodeVisionAVR User Manual دكاربرد: كاربرد ك لميكروكنترلر: طراحي و ساخت دستگاه اندازهگيري مشخصات سينماتيك حركت دست «مصطفي آقايي پور» كاربرد ك لميكروكنترلر: 1389 بررسي يك مثال در مورد استفاده از پورت هاي I/O و بدون استفاده از (CodeWizard (با بررسي چند مثال عملي - 1 توليد موج نردباني (چگونگي استفاده بهينه از نوع پورت) 2- وي توليد شكل موج سيگنال آنالوگ در خروجي يك پايه ميكرو با استفاده از تايمر و PWM 3- ارتباط LCD با ميكرو 4- استفاده از كليد آنالوگ (استفاده از (A2D 5- ارتباط سريال با كامپيوتر اندازي سنسور دماي (SMT160 از وقفه خارجي ) (راه - استفاده 6 Tools: - The AVR Studio Debugger (The AVR Studio version must be specified in the Debugger list box.) - The AVR Chip Programmer (The CodeVisionAVR IDE (Integrated Development Environment) has a built-in in In-System AVR Chip Programmer that lets you easily transfer your compiled program to the microcontroller for testing.) - The Serial Communication Terminal (The Terminal is intended for debugging embedded systems, which employ serial communication (RS232, RS422, RS485).) In system programming The AVR Chip Programmer: The Programmer is designed to work with the Atmel STK500, AVRISP, AVRISP MkII, AVR Dragon, JTAGICE MkII, AVRProg (AVR910 application note), Kanda Systems STK200+, STK300, Dontronics DT006, Vogel Elektronik VTEC-ISP, Futurlec JRAVR or the MicroTronics ATCPU, Mega2000 development boards. The AVR Chip Programmer (Cont.): The SCK clock frequency used for In-System Programming (ISP) with the STK500, AVRISP or AVRISP MkII can be specified using the SCK Freq. listbox. This frequency must not exceed ¼ of the chip s clock frequency. If you wish to protect your program from copying, you must select the corresponding option using the FLASH Lock Bits radio box. The Programmer has two memory buffers: The FLASH memory buffer The EEPROM memory buffer. CodeVisionAVR: contains the CodeWizardAVR Automatic Program Generator, that allows you to write, in a matter of minutes, all the code needed for implementing the following functions: External memory access setup Chip reset source identification Input/Output Port initialization External Interrupts initialization Timers/Counters initializationi i i Watchdog Timer initialization UART (USART) initialization and interrupt driven buffered serial communication Analog Comparator initialization ADC initialization SPI Interface initialization Two Wire Interface initialization I2C Bus, LM75 Temperature Sensor, DS1621 Thermometer/Thermostat and PCF8563, PCF8583, DS1302, DS1307 Real Time Clocks initialization 1 Wire Bus and DS1820, DS18S20 Temperature Sensors initialization LCD module initialization. CodeWizardAVR: بررسي يك مثال ساده: 1 مي خواهيم يك LED خاموش نماييم كه به پايه PORTD.7 ميكروكنترلر متصل است را با فركانس هرتز روشن و كد C اسمبلي دستورات كد Hex معرفي مختصر كامپايلر C: اين كامپايلر علاوه بر حمايت از اكثر دستورات و توابع C دارد. AVR نيز جهت كار با ميكروكنترلرهاي اي استاندارد دستورات و توابع ويژه درا ادامه به بررسي برخي مطالب مرتبط با دستورات وكا كامپايلر خواهيم پرداخت... اضافه نمودن توضيحات :(Comments) The character string /* marks the beginning of a comment. The string */ marks the end of the comment. Example: /* This is a comment */ /* This is a multiple line comment */ One line comments may be also defined by using the string // . Example: // This is also a comment Nested comments are not allowed. Identifiers: شناسه An identifier is the name you give to a variable, function, label or other object. An identifier can contain letters (A...Z, a...z) and digits (0...9), as well as the underscore character (_). However an identifier can only start with a letter or an underscore. Case is significant; i.e. variable1 is not the same as Variable1. Identifiers can have up to 32 characters. Following is a list of keywords reserved by the compiler. These can not be used as identifier names. :(Reserved( dkeywords) كلمات رزرو شده (d Break, bit, case, char, const, continue, default, do, double, eeprom, else, enum, Extern, flash, float, for, funcused, goto, if, inline, int, interrupt, long, register, Return, short, signed, sizeof, sfrb, sfrw, static, struct, switch, typedef, union, Unsigned, void, volatile, while انواع داده ها: Integer or long integer constants t may be written in decimal form (e.g. 1234), ثابتها :(Constants) in binary form with 0b prefix (e.g. 0b101001), in hexadecimal form with 0x prefix (e.g. 0xff) in octal form with 0-prefix (e.g. 0777). پيشوندها (فرمت اعداد): Unsigned integer constants may have the suffix U (e.g U). Long integer constants may have the suffix L (e.g. 99L). Unsigned long integer constants may have the suffix UL (e.g. 99UL). Floating point constants may have the suffix F (e.g F). پسوندها اعداد): (نوعا ا Character constants must be enclosed in single quotation marks. E.g. 'a'. String constants must be enclosed in double quotation marks. E.g. Hello world . If you place a string between quotes as a function parameter, this string will automatically be considered as constant and will be placed in FLASH memory. Example: flash int integer_constant=1234+5; flash char char_constant= a ; flash long long_int_constant1=99l; flash long long_int_constant2=0x ; flash int integer_array1[]={1,2,3}; /* The first two elements will be 1 and 2, the rest will be 0 */ flash int integer_array2[10]={1,2}; flash int multidim_array[2][3]={{1,2,3},{4,5,6}}; 2 3} {4 flash char string_constant1[]= This is a string constant ; - ثابتها نمي توانند در داخل توابع اعلان شوند.(declared) متغييرها :(Variables) Program variables can be global (accessible to all the functions in the program) or local (accessible only inside the function they are declared). If not specifically initialized, the global variables are automatically set to 0 at program startup. The local variables are not automatically initialized on function call. The syntax is: [ storage modifier ] type definition identifier ; Example: /* Global variables declaration */ char a; int b; /* and initialization */ long c= ; void main(void) { /* Local variables declaration */ char d; int e; /* and initialization iti */ long f= ; } Variables can be grouped in arrays, which can have up to 8 dimensions. The first element of the array has always the index 0. If not specifically initialized, the elements of global variable arrays are automatically set to 0 at program startup. Example: /* All the elements of the array will be 0 */ int global_array1[32]; /* Array is automatically initialized */ int global_array2[] array2[]={1,2,3}; int global_array3[4]={1,2,3,4}; char global_array4[]= This is a string ; /* Only the first 3 elements of the array are initialized, the rest 29 will be 0 */ int global_array5[32]={1,2,3}; /* Multidimensional array */ int multidim_array[2][3]={{1,2,3},{4,5,6}}; void main(void) { /* local array declaration */ int local_array1[10]; /* local array declaration and initialization */ int local_array2[3]={11,22,33}; char local_array3[7]= hello ; } Local variables that must conserve their values during different calls to a function must be declared as static. Example: int alfa(void) { /* declare and initialize the static variable */ static int n=1; return n++; } void main(void) { int i; /* the function will return the value 1 */ i=alfa(); /* the function will return the value 2 */ i=alfa(); } If not specifically initialized, static variables are automatically set to 0 at program startup. Specifying the SRAM Storage Address for Global Variables: Global variables can be stored at specific SRAM locations at design time using operator. Example: /* the integer variable a is stored in SRAM at address 80h */ int /* the structure alfa is stored in SRAM at address 90h */ struct t x { int a; char c; } Example: /* declaration and initialization for an ATtiny2313 chip which has GPIOR0, GPIOR1 and GPIOR2 registers */ bit alfa=1; /* bit0 of GPIOR0 */ bit beta; /* bit1 of GPIOR0 */ void main(void) { if (alfa) beta=!beta; /*... */ } متغييرهاي بيتي( Variables :(Bit Memory allocation for the global bit variables is done, in the order of declaration, starting with bit 0 of GPIOR0, then bit 1 of GPIOR0 and so on, in ascending order. After all the GPIOR registers are allocated, further bit variables are allocated in R2 up to R14. If the chip does not have GPIOR registers, the allocation begins directly from register R2. عملگرها :(Operators) نسبت دهي NOT Com. AND OR XOR عملگرها :(Operators) عملگرها :(Operators) عملگرها :(Operators) بررسي يك مثال در مورد استفاده از پورت هاي I/O و بدون استفاده از (CodeWizard (با بررسي چند مثال عملي - 1 توليد موج نردباني (چگونگي استفاده بهينه از نوع پورت) 2- وي توليد شكل موج سيگنال آنالوگ در خروجي يك پايه ميكرو با استفاده از تايمر و PWM 3- ارتباط LCD با ميكرو 4- استفاده از كليد آنالوگ (استفاده از (A2D 5- ارتباط سريال با كامپيوتر اندازي سنسور دماي (SMT160 از وقفه خارجي ) (راه - استفاده 6 بررسي يك مثال در مورد چگونگي استفاده از پورت هاي I/O (با و بدون استفاده از (CodeWizard ) :(PA0) يا خروجي و تنظيم پايهها به صورت ورودي (PA1) اداه ادامه بررسي پايهها به صورت ورودي و يا خخروجي: استفاده از پايه ورودي به صورت Pull-up نمونه اي از تنظيم اشتباه پايه ها به صورت ورودي: استفاده از پايه ورودي به صورت Tri-State نمونه اي از تنظيم و استفاده صحيح از پايه ها به صورت ورودي: استفاده از پايه ورودي به صورت Tri-State (دو برنامه معادل هم مي باشند) :(PA0) :CodeWizard چگونگي استفاده از گ تنظيم پايهها به صورت ورودي (PA1) و يا خروجي تنظيم پايه ها تنظيم نوع ميكرو و فركانس :CodeWizardC d آمده ازd نمونهاي از كدهاي به دست PORTA.0 =!(PINA.1);! : علامت NOT اگر بخواهيم خروجي LED وووروي ورودي كليدر رفتار عكس هم داشته باشند از دستور روبرو استفاده ميكنيم: #include mega16.h main() { DDRA.0 = 1; DDRA.1 = 0; PORTA.1 = 1; while(1) { if(pina.1==0) { PORTA.0 = 0; }else { PORTA.0 = 1; } } } پياده سازي برنامه قبل با استفاده از دستورات شرطي: /*Set bit 0 of port A as OUTPUT*/ /*Set bit 1 of port A as INPUT*/ /*port A as pull-up*/ /*Loop forever*/ - 1 توليد موج نردباني: Port : Tri State or Pull up Pull-up resistors are used in electronic to ensure that inputs to logic systems settle at expected logic levels if external devices are disconnected. Pull-up resistors may also be used at the interface between two different types of logic devices, possibly operating at different power supply voltages. Also, three-state,, tri-state,, or 3-state logic allows output ports to have a value of logical 0, 1, or Hi-Z. Tri-State استراتژي: يك پايه خروجي صفر و بقيه پايه ها به صورت ورودي و طراحي كل مدار: جهت داده DDRA.i = 1; // Set bit i of Port A as Output DDRA.j = 0; // Set bit j of Port A as Input PORTA.j = 1 : pull up PORTA.i = PINA.j // read bit j of PORT A and write it into bit i of PORT A Wizard تنظيمات پورت ورودي Tri State اگر پايه A.j ورودي باشد :(DDRA.j=0 PORTA.j = 1 : Pull up PORTA.j = 0 : Tri State اگر پايه A.j خروجي باشد :(DDRA.j=1 PORTA.j = 1 : High = 5volt PORTA.j = 0 : low = 0volt ميكرو و فركانس اضافه نمودن رهنمود تاخير تعريف تابع دقت شود با خروجي شدن هر پايه مقدار پيش فرض صفر در آن پايه قرار مي گيرد. پورت تعريف اوليه به صورت ورودي A ورودي :DDRA.j=0 PORTA.j = 0 : Tri State خروجي :DDRA.j=1 PORTA.j = 0 0volt Max voltage = 5 volt Min voltage = 0 برنامه اصلي Max voltage 5 volt شكل موج خروجي: -1-1 توليد شكل موج شبه مثلثي: ايده هاي مرتبط با اصلاح شكل موج فوق!!! مثال 2: توليد شكل موج سيگنال آنالوگ در خروجي يك پايه ميكرو با استفاده از تايمر و :PWM 1- نمونه برداري سيگنال آنالوگ و تغيير مقياس و كوانتيزاسيون و ايجاد Look up tabel 2- طراحي فيلتر پايين گذر 3- توليد PWM نمونه ها فركانس نمونه برداري در Fs = 360 Hz كاربرد : كاليبراسيون دستگاه هاي ثبت ECG طراحي كل مدار: TIMER1 (look up table) اطلاعات مربوط به سيگنال آنالوگ گنالآنالگ ECG Fs = 360 Hz ECG DATA: MIT - BIH TIMER1 Fast PWM Top = 0x3FF محدوده فركانسي سيگنال ECG بين 0.05Hz تا 150Hz مي باشد. طراحي فيلتر پايين گذر: باترورث فركانس قطع فيلتر : Hz بالاترين فركانس موجود در سيگنال آنالوگ بايستي به مراتب كمتر از فركانس PWM باشد. تنظيمات Wizard پايه خروجي Ts Top = 0x3FF Fast PWM تايمر يك تايمر صفر- ميكرو و فركانس OC1A: output,0 t روتين وقفه تايمر صفر (سنكرون با فركانس نمونه برداري) ) OC1A: output,0 خروجي پي پايه 31250/360= = 0x57 TCNT0 = 0xFF 0x57 = 0xAB تنظيمات مربوط به تايمر صفر و يك اصلي!!! برنامه اضافه نمودن اطلاعات موج رو مربوط به شكل سيگنال آنالوگ (ECG) شكل موج خروجي: 4- استفاده از كليد آنالوگ (استفاده از (A2D 4- استفاده از كليد آنالوگ (استفاده از (A2D 4- استفاده از كليد آنالوگ (استفاده از (A2D 1- اتصال كليدها به PA0 (ورودي و (Tri State 2- اتصال LEDها به پورت C (خروجي و مقدار اوليه صفر) 5- ارتباط سريال با كامپيوتر 5- ارتباط سريال با كامپيوتر ارسال پيغام به صورت متوالي به كامپيوتر printf( Hello My Computer ); puts( Hello My Computer ); putsf( Hello My Computer ); char str[20]; sprintf(str, adc0:%04u ADC1:%04u ,read_adc(0),read_adc(1)); d d d (1)) puts(str); 5- ارتباط سريال با كامپيوتر دريافت پيغام به صورت متوالي از كامپيوتر char c; c = getchar(); char n; printf( which ADC channel do you want to read? ); scanf( %u ,&n); printf( channel %u=%u ,n,read_adc(n)); #define RXC 7 unsigned char USART_Receive( void ) { /* Wait for data to be received */ while (!(UCSRA & (1 RXC)) ); /* Get and return received data from buffer */ return UDR; } #define UDRE 5 void USART_Transmit( unsigned char data ) { } /* Wait for empty transmit buffer */ while (!( UCSRA & (1 UDRE)) ); /* Put data into buffer, sends the data */ UDR = data; 5- استفاده از وقفه خارجي (راه اندازي سنسور دماي (SMT160 #include mega16.h #include stdio.h #include delay.h #include stdlib.h /*...*/ // Declare your global variables here float T1,T2; bit edge=0; // External Interrupt 0 service routine interrupt [EXT_INT0] void ext_int0_isr(void) { // Place your code here if (edge) { T2=TCNT1; TCNT1=0; // INT0 Mode: Falling Edge MCUCR=0x02; edge=0; }else { T1=TCNT1; TCNT1=0; // INT0 Mode: Rising Edge MCUCR=0x03; edge=1; } } 5- استفاده از وقفه خارجي (راه اندازي سنسور دماي (SMT اتصال خروجي سنسور به اينتراپت صفر بهپورت C LCD 2- اتصال void main(void) { // Declare your local variables here float dc; float t; char strt[16],str[8]; tt[16] t [8] /*...*/ 5- استفاده از وقفه خارجي (راه اندازي سنسور دماي (SMT160 while (1) { // Place your code here delay_ms(1000); dc=t1/(t1+t2); t=(dc-0.32)/0.0047; lcd_clear(); ftoa(t,2,str); sprintf(strt, %s \XDF C ,str); lcd_puts(strt); }; } stdlib.h stdio.h ارتباط با LCD كدهاي مربوط به استاندارد: كدهاي 3- ارتباط LCD با ميكرو: ارتباط اطلاعات بين ميكرو و :LCD باس چهار سيمه 1 ارتباط 2 ارتباط باس هشت سيمه lcd_clear(); lcd_gotoxy(0,0); lcd_putsf( putsf( hello World! ); نمايش عبارات شامل حروف و اعداد انگليسي char str[20]; /*...*/ lcd_clear(); نمايش متغير فركانس با سه رقم دسيمال lcd_gotoxy(0,0); sprintf(str, frequency:%-3u Hz ,result); lcd_puts(str); char str1[7],str2[24]; /*...*/ lcd_clear(); lcd_gotoxy(0,0); ftoa(result,2,str1); sprintf(str2, frequency:%5s t Hz ,str1); lcd_puts(str2); lcd_gotoxy(a,b); lcd_gotoxy(a,b); /* enables the displaying of the blink */ /* enables the displaying of the cursor */ _lcd_ready(); _lcd_ready(); نمايش چشمكزن نمايش مكان نما _lcd_write_data(0x0d); _lcd_write_data(0x0e); نمايش متغير فركانس با دو رقم اعشار برخي از توابع كتابخانه stdio.h void ftoa(float n, unsigned char decimals, char *str) converts the floating point number n to characters in string str. The number is represented with a specified number of decimals. The optional flags characters are: '-' left-justifies the result, padding on the right with spaces. If it's not present, the result will be right-justified, padded on the left with zeros or spaces; '+' signed conversion results will always begin with a '+' or '-' sign; ' ' if the value isn't negative, the conversion result will begin with a space. If the value is negative then it will begin with a '-' sign. stdio.h از تعاريف ا كتابخانه برخي The following conversion type characters are supported: 'i' - the function argument is a signed decimal integer; 'd' - the function argument is a signed decimal integer; 'u' - the function argument is an unsigned decimal integer; 'e' - the function argument is a float, that will be outputted using the [-]d.dddddd dddddd e[-]dd format 'E' - the function argument is a float, that will be outputted using the [-]d.dddddd E[-]dd format 'f' - the function argument is a float, that will be outputted using the [-]ddd.dddddd format 'x' -the function argument is an unsigned hexadecimal integer, that will be outputted with lowercase characters; 'X' - the function argument is an unsigned hexadecimal integer, that will be outputted with with uppercase characters; 'c' - the function argument is a single character; 's' - the function argument is a pointer to a null terminated char string located in SRAM; 'p' - the function argument is a pointer to a null terminated char string located in FLASH; '%' - the '%' character will be outputted. unsigned char lcd_read_byte(unsigned char addr); توابع كتابخانه lcd.h reads a byte from the LCD character generator or display RAM. The high level LCD Functions are: unsigned char lcd_init(unsigned char lcd_columns) initializes the LCD module, clears the display and sets the printing character position at row 0 and column 0. The numbers of columns of the LCD must be specified (e.g. 16). No cursor is displayed. The function returns 1 if the LCD module is detected and 0 if it is not. This is the first function that must be called before using the other high level LCD Functions. void lcd_clear(void) clears the LCD and sets s the printing character ac position o at row 0 and column 0. توابع كتابخانه lcd.h (ادامه) void lcd_gotoxy(unsigned char x, unsigned char y) sets the current display position at column x and row y. The row and column numbering starts from 0. void lcd_putchar(char c) displays the character c at the current display position. void lcd_puts(char *str) displays at the current display position the string str, located in SRAM. void lcd_putsf(char flash *str) displays at the current display position the string str, located in FLASH. The low level LCD Functions are: void _lcd_ready(void) توابع كتابخانه lcd.h (ادامه) waits until the LCD module is ready to receive data. This function must be called prior to writing data to the LCD with the _lcd_write_data function. void _lcd_write_data(unsigned char data) writes the byte data to the LCD instruction register. This function may be used for modifying the LCD configuration. Example: /* enables the displaying of the cursor */ _lcd_ready(); _lcd_write_data(0xe); data(0xe); void lcd_write_byte(unsigned char addr, unsigned char data); writes a byte to the LCD character generator or display RAM. نمايش عبارت خروجي: تنظيمات Wizard كد برنامه: رهنمود LCD اضافه نمودن ونر ذخيره سازي كاراكترهاي دلخواه كد برنامه (ادامه): تعريف تابع براي ارسال كاراكترهاي تعريف شده در موجود به RAM LCD و تخصيص كد براي هر كاراكتر كاراكترها نمايش اش كا روي LCD بر متن برنامه اصلي با تشكر از توجه شما Yasser Shekofteh اضافه نمودن توضيحات :(Comments) The character string /* marks the beginning of a comment. The string */ marks the end of the comment. Example: /* This is a comment */ /* This is a multiple line comment */ One line comments may be also defined by using the string // . Example: // This is also a comment Nested comments are not allowed. Identifiers: شناسه An identifier is the name you give to a variable, function, label or other object. An identifier can contain letters (A...Z, a...z) and digits (0...9), as well as the underscore character (_). However an identifier can only start with a letter or an underscore. Case is significant; i.e. variable1 is not the same as Variable1. Identifiers can ha
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks