0% found this document useful (0 votes)
16 views24 pages

Micro 8658

S

Uploaded by

ghaderiimuhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views24 pages

Micro 8658

S

Uploaded by

ghaderiimuhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

‫‪03/28/1436‬‬

‫‪University of Kashan‬‬
‫‪Faculty of Electrical and Computer Engineering‬‬
‫‪Department of Computer Engineering‬‬

‫‪Lecture note 8‬‬

‫‪AVR Programming in C‬‬

‫‪Hossein Sabaghian-Bidgoli‬‬
‫‪[email protected]‬‬
‫‪Fall 2014‬‬

‫برنامه نویسی ‪ AVR‬با زبان ‪C‬‬


‫‪ ‬مزایای آن نسبت به اسمبلی؟‬
‫برنامه نویسی به زبان ‪ C‬راحتتراست‬ ‫‪‬‬
‫زمان کمتری میگیرد‬ ‫‪‬‬
‫تغییر دادن آن آسان تر است‬ ‫‪‬‬
‫میتوان از توابع کتابخانهای آماده استفاده کرد‬ ‫‪‬‬
‫براحتی و با تغییرات جزئی میتوان از همان برنامه برای میکروکنترلرهای دیگر‬ ‫‪‬‬
‫استفاده کرد‬

‫‪ ‬معایب آن نسبت به زبان اسمبلی‬


‫طوالنی تر شدن کدماشین نهایی ‪C‬‬ ‫‪‬‬
‫‪ ‬اشغال فضای بیشترحافظه‬
‫‪ ‬کند تر شدن اجرای برنامه‬
‫‪2‬‬ ‫‪Microprocessors‬‬ ‫‪University of Kashan‬‬

‫‪1‬‬
‫‪03/28/1436‬‬

‫برنامه نویسی ‪ AVR‬با زبان ‪C‬‬

‫‪‬نکات قابل توجه‬


‫وجود کامپایلرهای مختلف مثل ‪ CodeVisionAVR‬و ‪WinAVR‬‬ ‫‪‬‬
‫تفاوت نام و عملکرد توابع کتابخانهای در کمپایلرهای مختلف و ‪ Include‬فایلها‬ ‫‪‬‬
‫امکان شبیه سازی در ‪AVR Studio‬‬ ‫‪‬‬
‫فراهم کردن قالب آماده برای برنامه نویسی توسط ‪ Wizard‬قوی در‬ ‫‪‬‬
‫‪CodeVisionAVR‬‬

‫‪3‬‬ ‫‪Microprocessors‬‬ ‫‪University of Kashan‬‬

‫قالب یک برنامهی ‪C‬‬


‫>‪.h‬نام فایل سرآیند< ‪#include‬‬
‫;معرفی متغیرهای سراسری‬
‫;معرفی توابع‬
‫{ )‪int main(void‬‬
‫;معرفی متغیرهای محلی‬
‫;دستوراتی که پس از روشن شدن میکرو یکبار اجرا میشوند‬
‫{ )‪while(1‬‬
‫;دستورات حلقه اصلی برنامه‬
‫}‬ ‫‪ ‬چند نکته‬
‫;‪return 0‬‬ ‫‪ ‬انتهای هر دستور عالمت ; الزم است‬
‫}‬ ‫‪ ‬عالمت { شروع و } پایان یک تابع‪ ،‬حلقه‪ ،‬دستور شرطی یا بلوک را نشان میدهد‪.‬‬
‫عالمتهای ‪ //‬در ابتدای سطر نشانه توضیح است و آن سطر توسط کامپایلر نادیده گرفته میشود‬ ‫‪‬‬
‫عالمتهای *‪ /‬شروع و ‪ */‬پایان توضیح است که معموال برای نادیده گرفتن بخشی از برنامه یا‬ ‫‪‬‬
‫بخشی از یک سطر بکار میرود‬

‫‪4‬‬ ‫‪Microprocessors‬‬ ‫‪University of Kashan‬‬

‫‪2‬‬
‫‪03/28/1436‬‬

‫انواع داده (متغیر)ها‬


‫‪ ‬متغیر چیست؟‬
‫یک فضای حافظه با طول معین که برای نگهداری داده استفاده میشود‬ ‫‪‬‬
‫‪ ‬انواع داده (جدول زیر)‬
‫کاراکتر ‪ -‬عدد صحیح (باعالمت و بدون عالمت) ‪ -‬عدد ممیز شناور‬ ‫‪‬‬
‫‪ ‬انواع حافظه (‪ EEPROM ،SRAM‬و ‪)Flash‬‬
‫در ‪ CodeVision‬نوع حافظه بشکل زیر تعریف میشود‬ ‫‪‬‬
‫;‪float f‬‬
‫;‪eeprom char b‬‬
‫;‪flash int a‬‬

‫‪5‬‬ ‫‪Microprocessors‬‬ ‫‪University of Kashan‬‬

‫مثالهای فصل ‪7‬‬

‫‪6‬‬ ‫‪Microprocessors‬‬ ‫‪University of Kashan‬‬

‫‪3‬‬
03/28/1436

7 ‫مثالهای فصل‬

7 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

8 Microprocessors University of Kashan

4
03/28/1436

7 ‫مثالهای فصل‬

9 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

10 Microprocessors University of Kashan

5
03/28/1436

7 ‫مثالهای فصل‬

11 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

12 Microprocessors University of Kashan

6
03/28/1436

7 ‫مثالهای فصل‬

13 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

14 Microprocessors University of Kashan

7
03/28/1436

7 ‫مثالهای فصل‬

15 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

16 Microprocessors University of Kashan

8
03/28/1436

‫عملگرهای بیتی‬

17 Microprocessors University of Kashan

‫عملگرهای بیتی‬

18 Microprocessors University of Kashan

9
03/28/1436

7 ‫مثالهای فصل‬

19 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

20 Microprocessors University of Kashan

10
03/28/1436

7 ‫مثالهای فصل‬

21 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

22 Microprocessors University of Kashan

11
03/28/1436

7 ‫مثالهای فصل‬

23 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

24 Microprocessors University of Kashan

12
03/28/1436

7 ‫مثالهای فصل‬

25 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

26 Microprocessors University of Kashan

13
03/28/1436

7 ‫مثالهای فصل‬

27 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

28 Microprocessors University of Kashan

14
03/28/1436

7 ‫مثالهای فصل‬

29 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

30 Microprocessors University of Kashan

15
03/28/1436

7 ‫مثالهای فصل‬

31 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

32 Microprocessors University of Kashan

16
03/28/1436

7 ‫مثالهای فصل‬

33 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

34 Microprocessors University of Kashan

17
03/28/1436

7 ‫مثالهای فصل‬

35 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

36 Microprocessors University of Kashan

18
03/28/1436

7 ‫مثالهای فصل‬

37 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

38 Microprocessors University of Kashan

19
03/28/1436

7 ‫مثالهای فصل‬

39 Microprocessors University of Kashan

7 ‫مثالهای فصل‬

40 Microprocessors University of Kashan

20
03/28/1436

7 ‫مثالهای فصل‬

41 Microprocessors University of Kashan

‫فایلهای سرآیند و توابع کتابخانهای‬

Some function defined in stdlib.h

Other include files


 #include <math.h>
 #include <stdio.h>
 #include <string.h>

42 Microprocessors University of Kashan

21
03/28/1436

LCD ‫مثال‬

43 Microprocessors University of Kashan

LCD ‫مثال‬

44 Microprocessors University of Kashan

22
03/28/1436

LCD ‫مثال‬
#include <AVR/IO.h>
#include <util/delay.h>

#define RS 0
#define RW 1
#define E 2

void SendCommand(char cmd){


PORTC&=~(1<<RS);
PORTC&=~(1<<RW);
PORTD=cmd;
PORTC|=1<<E;
PORTC&=~(1<<E);
_delay_ms(2);
}
void SendData(char Data){
PORTC|=(1<<RS);
PORTC&=~(1<<RW);
PORTD=Data;
PORTC|=1<<E;
PORTC&=~(1<<E);
_delay_ms(2);
}

45 Microprocessors University of Kashan

LCD ‫مثال‬
main(){
unsigned char i;
char msg[]="This is a test_123456789";
DDRC=0xff;
DDRD=0xff;
_delay_ms(15);
SendCommand(0x38); //initialization
SendCommand(0x0e);
SendCommand(0x01); //clear LCD
_delay_ms(5);
for (i=0; i<14; i++) SendData(msg[i]);
while(1);
}

46 Microprocessors University of Kashan

23
03/28/1436

The End

Questions?

47 Microprocessors University of Kashan

24

You might also like