1 / 50

20 Feb 2012

20 Feb 2012. ไมโครคอนโทรลเลอร์ AVR. ผลิต โดยบริษัท ATMEL มี สถาปัตยกรรมแบบ RISC คือหนึ่งคำสั่งทำงานโดยใช้สัญญาณนาฬิกาเพียง 1 ลูก เทียบ กับไมโครคอนโทรลเลอร์ตระกูล MCS 51 ที่ต้องใช้สัญญาณนาฬิกา 6-12 ลูก

diem
Télécharger la présentation

20 Feb 2012

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 20 Feb 2012

  2. ไมโครคอนโทรลเลอร์ AVR • ผลิตโดยบริษัท ATMEL • มีสถาปัตยกรรมแบบ RISC คือหนึ่งคำสั่งทำงานโดยใช้สัญญาณนาฬิกาเพียง 1 ลูก • เทียบกับไมโครคอนโทรลเลอร์ตระกูล MCS 51 ที่ต้องใช้สัญญาณนาฬิกา 6-12 ลูก • การเขียนโปรแกรม ภาษา C ด้วย AVR Studio 4 / WinAVR C Compiler เพื่อไปควบคุมการทำงานของไมโครคอนโทรลเลอร์ตระกูล AVR • ศึกษาคุณสมบัติของ AVR เบอร์ที่เราเลือกใช่งานก่อนเช่น ขาพอร์ตอินพุตเอาต์พุต ขาVCC ขาGND ไทเมอร์ เคาน์เตอร์ รีจีสเตอร์ที่เกี่ยวข้องกับโมดูลนั้นนั้น

  3. ลองเลือกใช้เบอร์ ATmega16

  4. การใช้งาน AVR Studio 4 /WinAVR • AVR Studio 4 เป็นเครื่องมือในการพัฒนาโปรแกรมภาษาแอสแซมบลี • ไมโครคอนโทรลเลอร์ AVR ในรูปแบบ IDE คือการรวมเครื่องมือพัฒนาทั้งแอสแซมเบลอร์ดีบักเกอร์ และอิดิเตอร์ไว้ในตัวเดียวกัน • นำมาทำงานร่วมกับWinAVRก็สามารถที่จะคอมไพล์ภาษา C สำหรับ AVR ได้ • สามารถดาวน์โหลดโปรแกรม WinAVRและ AVR Studio 4 ได้ที่http:www.eSnack.net

  5. วงจรที่ใช้เบอร์ ATmega16

  6. การเรียกใช้งานโปรแกรม AVR Studio 4 เปิดโปรแกรม AVR Studio 4 ขึ้นมาโดยเรียกที่ Start>>Programs>>Atmel AVR Tools>>AVR Studio 4

  7. หน้าต่าง Welcome to AVR Studio 4 • ไม่ปรากฏหน้าต่างนี้ขึ้นมาให้ไปเลือกที่คำสั่งบน เมนูบาร์ Project>>Project Wizard ก็จะปรากฏหน้าต่าง Welcome to AVR Studio 4 ขึ้นมาให้เช่นกัน

  8. การสร้าง Project ใหม่ คลิกที่ปุ่ม New Project ที่หน้าต่าง Welcome to AVR Studio 4 หรือเลือกคำสั่งบนเมนูบาร์ Project>>New Project ก็จะปรากฏหน้าต่างดังภาพด้านล่างนี้

  9. Project type ให้คลิกเลือก AVR GCC • เพื่อเลือกเขียนโปรแกรมด้วยภาษา C ที่ช่อง Project name • ใส่ชื่อ Project ลงไป ในช่อง Initial file เป็นชื่อเดียวกันกับชื่อในช่อง Project name ที่เลือก

  10. การสร้าง Project • ให้คลิกเลือก Create initial file เพื่อสร้างไฟล์ภาษา C • คลิกเลือก Create folder เพื่อสร้างโฟลเดอร์ไว้เก็บข้อมูลโปรเจ็ค • คลิกที่ปุ่ม เพื่อเลือกที่เก็บข้อมูลโปรเจ็ค

  11. การสร้าง Project • เมื่อเลือกใส่ค่าต่างๆเรียบร้อยแล้ว ให้คลิกต่อไปจะปรากฏหน้าต่างดังภาพด้านล่าง ในช่อง Debug platform • ให้คลิกเลือก AVR Simulator และในช่อง Device ให้เลือก ATmega16(ATmega88) จากนั้นคลิกที่สำเร็จก็จะปรากฏหน้าต่างสำหรับเขียนโค๊ดโปรแกรมขึ้นมา

  12. การเขียนโค้ดโปรแกรม • #include <avr/io.h>#define F_CPU 8000000UL#include <util/delay.h>void delay_ms (unsigned inti ){for (;i>0; i--)_delay_ms(1);}int main (void){DDRA = 0xFF;PORTA = 0X00;while (1){PORTA = 0x01;delay_ms (1000);PORTA = 0x02;delay_ms (1000); PORTA = 0x04;delay_ms (1000);PORTA = 0x08;delay_ms (1000);PORTA = 0x10;delay_ms (1000);PORTA = 0x20;delay_ms (1000);PORTA = 0x40;delay_ms (1000);PORTA = 0x80;delay_ms (1000);}return 0;}

  13. การเขียนโค้ดโปรแกรม • ทดลองเขียนโค้ดโปรแกรมตามตัวอย่างนี้ลงไป โปรแกรมตามตัวอย่างนี้ถ้านำไปบันทึกในATmega16(88) ในวงจรไฟวิ่งที่กล่าวไว้ข้างต้นนี้ผลลัพธ์ที่ได้LED ก็จะติดจากดวงที่1 และวิ่งไปถึงดวงที่ 8 และวิ่งวนไปตลอด • ให้นักศึกษาออกแบบและเขียนโปรแกรมให้เหมาะสมกับ eSnack

  14. การเขียนโค้ดโปรแกรม • ในช่อง Device เลือกเบอร์ AVR ที่ใช้ ในที่นี้เลือกATmega16(88) • ในช่อง Frequency ให้ใส่ค่าความถี่ที่ใช้หรือเลือกให้ตรงกับความถี่ของบอร์ดทดลองที่เลือกใช้ ในบทความนี้เลือก 8 MHz (xx)

  15. การเขียนโค้ดโปรแกรม ที่ช่อง Optimization จะเป็นการเลือกการบีบอัดข้อมูลของโปรแกรม ที่จะนำไปบันทึกลงในไอซี ขอแนะนำให้เลือกใช้ -03 หรือ -0S คลิกเลือก Create Hex File เพื่อสร้างไฟล์โปรแกรมที่จะนำไปบันทึกในไอซี ส่วน Map File และ List Fileจะเลือกหรือไม่เลือกก็ได้เพราะในบทความนี้จะใช้เฉพาะ Hex File เท่านั้น จากนั้นคลิก OK

  16. การเขียนโค้ดโปรแกรม • และเมื่อเขียนโค้ดโปรแกรมเสร็จเรียบร้อยแล้วเลือกคำสั่งที่เมนูบาร์ Project>>Configuration Option จะปรากฏหน้าต่าง Options ขึ้นมา

  17. การบันทึก Project • ให้เลือกคำสั่งบนเมนูบาร์ File>>Save ก็จะบันทึกไฟล์.C ไว้ในโฟลเดอร์โปรเจ็ค

  18. การบันทึก Project • ต้องการที่จะบันทึกโปรเจ็คให้เลือกคำสั่งบนเมนูบาร์ Project>>Save Project ก็จะบันทึกไฟล์โปรเจ็ค ไว้ในโฟลเดอร์โปรเจ็ค

  19. การบันทึก Project • แสดงไฟล์.C และไฟล์โปรเจ็คที่บันทึกไว้ในโฟลเดอร์โปรเจ็ค

  20. การคอมไฟล์โปรแกรม • เขียนโค้ดโปรแกรม C เสร็จเรียบร้อยแล้วสามารถที่จะคอมไฟล์WinAVRผ่าน AVR Studio ได้โดยเลือกที่คำสั่งบนเมนูบาร์ Build>>Build

  21. การคอมไฟล์โปรแกรม • เรียกใช้คำสั่งคอมไฟล์แล้ว ในหน้าต่าง Build จะแสดงคำสั่งที่เรียกใช้งานWinAVRและแสดงผลลัพธ์จากคำสั่งนั้นนั้น • หากว่าไม่เกิดข้อผิดพลาดใดใดจะแสดงสัญลักษณ์รูปวงกลมสีเขียว • หากว่ามีข้อผิดพลาดเกิดขึ้น จะแสดงสัญลักษณ์วงกลมสีเหลืองและสีแดงขึ้นหน้าบรรทัดนั้น • สามารถที่จะดับเบิ้ลคลิกที่บรรทัดนั้น ก็จะกระโดดไปยังบรรทัดที่ผิดพลาดของโปรแกรมเพื่อให้แก้ไขได้

  22. การคอมไฟล์โปรแกรม • เมื่อคอมไฟล์ผ่านเรียบร้อยแล้วจะได้โฟลเดอร์ default เก็บอยู่ในโฟลเดอร์โปรเจ็ค • ภายในโฟลเดอร์ default จะมีไฟล์การทำงานต่างๆอยู่รวมทั้งไฟล์.Hex ที่จะนำไปบันทึกลงในไอซีด้วย • สำหรับรายละเอียดเพิ่มเติมของการใช้งานโปรแกรม AVR Studio สามารถอ่านเพิ่มเติมได้จาก Help ของโปรแกรม

  23. การ SIMULATE วงจรด้วยโปรแกรม Proteus 7.6 • โปรแกรม ต่อไปจะนำไฟล์.Hex ที่ได้จากการคอม ไฟล์ไปทดลอง SIMULATE การทำงานของโปรแกรมด้วยโปรแกรม Proteus 7.6 สำหรับโปรแกรม Proteus http://www.labcenter.co.uk

  24. การ SIMULATE วงจรด้วยโปรแกรม Proteus 7.6 • หลังจากที่ได้ลงโปรแกรม Proteus เสร็จเรียบร้อยแล้วให้เรียกใช้งานเพื่อจำลองการทำงานของวงจร ถ้าหากปรากฏหน้าต่าง View Sample Designs ให้คลิก NO

  25. การ SIMULATE วงจรด้วยโปรแกรม Proteus 7.6

  26. การ SIMULATE วงจรด้วยโปรแกรม Proteus 7.6 • เริ่มวาดวงจรไฟวิ่งดังตัวอย่างที่กล่าวไว้ข้างต้น • 1. คลิกที่ Component Mode • 2. คลิกที่ Pick form Libraries • 3. พิมพ์ AVR ในช่อง Keywords เลือก Device เบอร์ ATMAGA 16 • 4. คลิก OK

  27. การ SIMULATE วงจรด้วยโปรแกรม Proteus 7.6

  28. การ SIMULATE วงจรด้วยโปรแกรม Proteus 7.6 • คลิกซ้ายบริเวณพื้นที่วาดวงจรจะปรากฏรูปร่างของอุปกรณ์ขึ้นมาให้เลื่อนไปยังตำแหน่งที่ต้องการวางอุปกรณ์ • คลิกซ้ายเพื่อวางอุปกรณ์ หากต้องการRotate , Mirror , หรือ Delete ให้คลิกขวาที่ตัวอุปกรณ์ก็จะมีคำสั่งให้เลือกใช้งาน • หากต้องการเคลื่อนย้ายตัวอุปกรณ์ให้คลิกซ้ายที่ตัวอุปกรณ์ อุปกรณ์ตัวนั้นก็จะมีสีแดงจากนั้นคลิกซ้ายค้างแล้วเลื่อนไปยังตำแหน่งที่ ต้องการวาง • เมื่อวางอุปกรณ์เรียบร้อยแล้วให้คลิกซ้ายบริเวณพื้นที่ว่างสี แดงที่ตัวอุปกรณ์ก็จะหายไป

  29. การ SIMULATE วงจรด้วยโปรแกรม Proteus 7.6 • 5. คลิกที่ Component Mode • 6. คลิกที่ Pick form Libraries • 7. พิมพ์ Resistor ในช่อง Keywords เลือกResistor • 8. คลิก OK • ให้วางตัว Resistor ลงในพื้นที่วาดวงจร • ให้คลิกขวาที่ตัว Resistor เลือกคำสั่ง Edit properties จะปรากฏหน้า • Edit Component ขึ้นมา ในช่อง Resistance (Ohms) ให้ใส่ค่า 220 ลงไป

  30. การ SIMULATE วงจรด้วยโปรแกรม Proteus 7.6

  31. การ SIMULATE วงจรด้วยโปรแกรม Proteus 7.6

  32. การ SIMULATE วงจรด้วยโปรแกรม Proteus 7.6 • 9. คลิกที่ Component Mode • 10. คลิกที่ Pick form Libraries • 11. พิมพ์ LED-BLUE ในช่อง Keywords เลือก LED-BLUE หรือหากต้องการเลือกสีอื่นก็สามารถเลือกได้ • 12. คลิก OK และวาง LED-BLUE ลงในพื้นที่วาดวงจร • 13. คลิกที่ Terminals Mode คลิกเลือก GROUND จากนั้นนำไปวางในพื้นที่วาดวงจร • 14. คลิกที่ Terminals Mode เพื่อลากเส้นสัญญาณเชื่อมต่อระหว่างขาอุปกรณ์ โดยคลิกซ้ายที่ขาอุปกรณ์ที่เป็นจุดเริ่มต้นและลากไปคลิกซ้ายที่ขาอุปกรณ์ที่ เป็นจุดสิ้นสุด

  33. การ SIMULATE วงจรด้วยโปรแกรม Proteus 7.6

  34. การ SIMULATE วงจรด้วยโปรแกรม Proteus 7.6 • เมื่อวาดวงจรเสร็จเรียบร้อยแล้วให้คลิกขวาที่อุปกรณ์ ATMAGA16 เลือกคำสั่ง Edit properties ที่ช่อง Program File • ให้คลิกที่ และไปเลือกไฟล์.Hex ที่ได้จากการคอมไฟล์ด้วยโปรแกรมWinAVRข้างต้นที่เก็บอยู่ในโฟลเดอร์ default ในโฟลเดอร์โปรเจ็ค • ที่ช่อง CKSEL Fuses: ให้เลือกความถี่คริสตอลที่ใช้งานในบทความนี้เลือกใช้ 8 MHz เท่านี้ก็พร้อมที่จะจำลองการทำงานของโปรแกรมได้แล้ว

  35. การ SIMULATE วงจรด้วยโปรแกรม Proteus 7.6 • 15. ให้คลิกปุ่ม Play เพื่อดูผลการจำลองการทำงานของวงจร ผลที่ได้ก็คือ LED ก็จะติดจากดวงที่1 และวิ่งไปถึงดวงที่ 8และวิ่งวนไปตลอด หากต้องการหยุดการทำงานให้คลิกที่ปุ่ม STOP

  36. การ SIMULATE วงจรด้วยโปรแกรม Proteus 7.6

  37. Starting Atmel AVR C Programming Tutorial 1 • Starting Atmel AVR C Programming Tutorial 1 • In this two series of tutorial, we will provides you with the information on the tools and the basic steps that are involved in using the C programming language for the Atmel AVR microcontrollers. • It is intended for people who are new to this type of microcontrollers. The AVRJazz Mega168 board will be use in this tutorial, however this information could be applied to other AVR family as well.

  38. 1. Which tools should I use • To start C programming language on Atmel AVR Microcontroller you need to download these following tools: • Down load the latest Atmel AVR Studio which provide you with the complete IDE (integrated development environment) for managing project, program editing, compiling, debugging and downloader for all Atmel AVR Microcontroller series. • The Atmel AVR Studio only provide you with native microcontroller language (assembler), so you need to down load the WinAVR Project which provide you with AVR GCC (GNU C Compiler for AVR Microcontroller) base compiler and library for window environment. This AVR GCC is fully integrated and supported by Atmel AVR Studio.

  39. 1. Which tools should I use • After downloading, first install the Atmel AVR Studio and just follow all the default setting and secondly install the WinAVR, again follow the default setting.

  40. 2. My first AVR Project • To create your first AVR project go to Start -> All Programs -> Atmel AVR Tools -> AVR Studio 4, this will launch the AVR Studio 4 application and the Welcome to AVR Studio 4 form appear, click on the New Project button and it will show the Create new project screen as follow:

  41. 2. My first AVR Project On Project Type choose AVR GCC, enter myfirstc for the project name and you could also change the project location directory as you like,  then click on the Next>> button, you will be asked for debugging platform on the following screen:

  42. 2. My first AVR Project Choose AVR Simulator for debug platform and ATmega168 for the device, then click on the Finish button. This will create all the necessary files and directories needed for this project. Now the AVR Studio 4 IDE is ready and display the empty myfirstc.c file.

  43. 2. My first AVR Project Enter or cut and paste this source program bellow to the program code windows:

  44. /***************************************************************************/*************************************************************************** • //  File Name    : myfirstc.c • //  Version      : 1.0 • //  Description  : Learning Lessons 1: Hello World • //  Author(s)    : RWB • //  Target(s)    : AVRJazz Mega168 Board • //  Compiler     : AVR-GCC 4.3.0; avr-libc 1.6.2 (WinAVR 20080610) • //  IDE          : Atmel AVR Studio 1.14 • //  Last Updated : 21 March 2008 • //*************************************************************************** • #include <avr/io.h> • #include <util/delay.h> • int main(void) • { •     DDRD=0xFF;                   // Set all the PORTD bit (8 bit) for Output •     for(;;) {                    // Loop Forever •       PORTD=0xFF;                // Turn On all the PORT D bit •       _delay_ms(100);            // Delay 100 millisecond •       PORTD=0x00;                // Turn Off all the PORT D bit •       _delay_ms(100);            // Delay 100 millisecond •     } •     return 0;                    // Standard Return Code • }

  45. 2. My first AVR Project • The program start with the standard comment (all the statement start with double slash is consider as comment in standard C syntax) that give the information about the program, version and compiler version, etc. The next two include statement tell the C compiler to include the AVR microcontroller specific library <avr/io.h> and the delay library <util/delay.h> when compiling the program. • Inside the main program, first we instruct the AVR microcontroller (i.e. Mega168) to enable it’s 8 bit PORT-D input/output port for output by setting the data direction register (DDRD) on PORT-D to 0xFF.

  46. 2. My first AVR Project • Next inside the blank for loops statement (it’s mean we loop continuously) we simple instruct the AVR microcontroller to turn on and off all it’s 8 bit PORT-D by assigning the value 0xFF (on) and 0×00 (off) to the PORT-D register (PORTD). The 100 millisecond delay is put there so our eye could catch the process, otherwise the process is so fast that we see the port is always on.

  47. 3. Building the HEX Files • Before compiling and building the HEX files we have to tell the AVR Studio about our board device, by clicking the menu Project -> Configuration Options the Configuration options form will appear as the following picture: • Make sure the device selected is atmega168 and the frequency is set to 11059200hz (this is the AVRJazz Mega168 board default frequency). Leave the other setting to it’s default then click the Ok button.

  48. 3. Building the HEX Files The next step is to building the program by clicking the menu Build -> Rebuild All, if there is no error the myfirstc.hex file will be produced and the status message will show Build succeeded with 0 Warning:

  49. 3. Building the HEX Files If there are error messages, check and recheck again your C code. Most often, they are caused by some typos or syntax errors. In our next tutorial we will show you how to debug the code and download it to the AVRJazz Mega168 Board.

  50. 3. Building the HEX Files

More Related