1 / 59

EE138 Introduction to Embedded Control System Design

EE138 Introduction to Embedded Control System Design. Agenda. Overview of Micro-controller market Overview of Atmel ATmega16 & AT91SAMD20 Overview of Atmel Development Kit STK500 C Development using Atmel AVR Studio 6.0 Quick review of C development with focus in micro-controller programming

Download Presentation

EE138 Introduction to Embedded Control System Design

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. EE138Introduction toEmbedded Control System Design

  2. Agenda • Overview of Micro-controller market • Overview of Atmel ATmega16 & AT91SAMD20 • Overview of Atmel Development Kit STK500 • C Development using Atmel AVR Studio 6.0 • Quick review of C development with focus in micro-controller programming • ATmega16 Digital I/O

  3. Overview of Micro-controller Market • 2011 and 2010 worldwide microcontroller revenue share by supplier. 2011 microcontroller market report18 April 2012 http://www.dataweek.co.za/article.aspx?pklarticleid=7412

  4. Overview of Atmel Devices • Different price bands for different applications and markets • Microcontrollers in the 8-bit AVR family share a similar instruction set and architecture. • The ATmega16 has components that are useful for typical microcontroller applications, such as • Digital IO • Serial communications • Interrupts, Timers • Pulse-width-modulator. • Analog-to-digital converter • STK500 Development Kit • Atmega16 SAMD20 Explnd Pro Eval Kit • AT91 SAM D20 (*NEW*)

  5. SAMD20 vs ATmega16

  6. SAM D20 and ATmega16

  7. ATmega16 ─ Block diagram Timers Port C Port A Interrupt Serial Port D Port B CPU ADC

  8. SAMD20 ─ Block diagram

  9. Digital IO in ATmega16 ─ Pins Port A Port B Port C Port D ATmega16 chip

  10. SAMD20 Pin layout

  11. SAMD20 Xplained PRO Evaluation Kit EE138 – SJSU

  12. Atmel Development Kit STK500

  13. C Development using Atmel AVR Studio 6.0 Why using C? • C is a high-level programming language: C code is easier to understand compared to other languages. C supports low-level programming: We can use C to access all hardware components of the microcontroller. C has standard libraries for complex tasks: data type conversions, standard input/output, long-integer arithmetic. The Atmel AVR instruction set is designed to support C compilers: C code can be converted efficiently to assembly code.

  14. C tools • Atmel AVR Studio • An integrated development environment for Atmel AVR microcontroller. • It includes editor, assembler, emulator, HEX file downloader. • Available at: www.atmel.com/tools/studioarchive.aspx • We need two tools for C development: Atmel AVR Studio and WinAVR. • WinAVR • A C compiler for AVR microcontrollers. • Can be used alone, or as a plug-in for Atmel AVR Studio. • Available at: winavr.sourceforge.net Download setup files for Atmel AVR Studio and WinAVR • Run setup file for Atmel AVR Studio. Accept default options • Run setup file for WinAVR. Accept default options

  15. Development cycle for C Step 1: Create AVR Studio project • project name • project type C • select simulator and device Step 2: Enter a C program. Step 3: Compile the C program to produce a HEX file. Step 4: Download and test the HEX file on Atmel AVR microcontroller. We’ll illustrate these steps using an example.

  16. Development cycle for C ─ Example A program that lets user turn on LED by pressing a switch. Video demo: Lab1_AVR6_LED

  17. Step 1: Create AVR Studio project • Start the AVR Studio program. • Select menu Project | New Project. • project type: AVR GCC • project name: led • project location: C:\AVR • option ‘Create initial file’ • option ‘Create folder’ • Click ‘Next’.

  18. Create AVR Studio project • Select debug platform and device. • debug platform: AVR Simulator • device: ATmega16 • Click ‘Finish’.

  19. Step 2: Enter a C program The AVR Studio window. project files program led.c status messages

  20. Enter a C program c /* File: led.c Description: Simple C program for the ATMEL AVR uC (ATmega16 chip) It lets user turn on LEDs by pressing the switches on STK500 board */ #include <avr/io.h> // AVR header file for all registers/pins int main(void){ unsigned char i; // temporary variable DDRA = 0x00; // set PORTA for input DDRB = 0xFF; // set PORTB for output PORTB = 0x00; // turn ON all LEDs initially while(1){ // Read input from PORTA. // This port will be connected to the 8 switches i = PINA; // Send output to PORTB. // This port will be connected to the 8 LEDs PORTB = i; } return 1; }

  21. Step 3: Compile the C program Select menu Build | Build to generate HEX file led.c.

  22. Step 4: Download/test HEX file on microcontroller PORTA to switches power switch Hardware setup for example program. Connections to PORTA & PORTB are only for this example. 12-V power supply ATmega16 chip serial cable to PC programming mode PORTB to LEDs

  23. Download/run HEX file on microcontroller • Select menu Tools | Program AVR | Connect. • Select platform and port. • ‘STK500 or AVRISP’ • ‘Auto’ • Click ‘Connect’.

  24. Download/run HEX file on microcontroller • Select Input HEX file, generated in Step 3. • Click ‘Program’. • After this, the program is downloaded to the STK500 board and runs. • The program remains even after power-off. To erase, click ‘Erase Device’.

  25. Structure of a C program • A C program typically has two main sections. • Section #include: to insert header files. • Section main(): code that runs when the program starts. #include <avr/io.h> // avr header file for all registers/pins int main(void){ unsigned char i; // temporary variable DDRA = 0x00; // set PORTA for input DDRB = 0xFF; // set PORTB for output PORTB = 0x00; // turn ON all LEDs initially while(1){ // Read input from PORTA, which is connected to the 8 switches i = PINA; // Send output to PORTB, which is connected to the 8 LEDs PORTB = i; } return 1; } In this example, header file <avr/io.h> contains all register definitions for the selected AVR microcontroller.

  26. C comments • Comments are text that the compiler ignores. • For a single-line comment, use double forward slashes: DDRA = 0x00; // set PORTA for input • For a multi-line comment, use the pair /* and */: /* File: led.c Description: Simple C program for the ATMEL AVR(ATmega16 chip) It lets user turn on LEDs by pressing the switches on the STK500 board */ Always use comments to make program easy to understand.

  27. C statements and blocks • C statements • C statements control the program flow. • They consist of keywords, expressions and other statements. • A statement ends with semicolon. DDRB = 0xFF; // set PORTB for output • C blocks • A C block is a group of statements enclosed by braces {}. • It is typically associated with if, switch, for, do, while, or functions. while (1){ // Read input from PORTA - connected to the 8 switches i = PINA; // Send output to PORTB - connected to the 8 LEDs PORTB = i; }

  28. Data types and operators • The main data types in C are • char: 8-bit integer in AVR • int: 16-bit integer in AVR • long int: 32-bit integer in AVR • The above data types can be modified by keyword ‘unsigned’. char a; // range of values for a: -128, …, 0, …, 127 unsigned char b; // range of values for b: 0, 1, 2, …, 255 unsigned long intc; // range of values for c: 0, 1, …, 232 - 1 • Examples of variable assignment: a = 0xA0; // enter value in hexadecimal format a = 0b11110000; // enter value in binary format b = ‘1’; // b stores ASCII code of character ‘1’ c = 2000ul; // c stores an unsigned long integer 2000

  29. C operators • C has a rich set of operators: • Arithmetic operators • Relational operators • Logical operators • Bit-wise operators • Data access operators • Miscellaneous operators

  30. Arithmetic operators

  31. Relational operators

  32. Logical operators These operators are applied on logical variables or constants.

  33. Bit-wise operators These operators are applied on individual bits of a variable or constant.

  34. Data-access operators These operators are for arrays, structures or pointers. We’ll learn more about them when required.

  35. Miscellaneous operators commonly used by C coders.

  36. Flow control in C • By default, C statements are executed sequentially. To change the program flow, there are six types of statements • if-else statement • switch statement • while statement • for statement • do statement • goto statement Conditional Iterative Should be avoided!

  37. ‘if-else’ statement • General syntax if (expression) statement_1; else statement_2; • Example char a, b, sum; a = 4; b = -5; sum = a + b; if (sum < 0) printf(“sum is negative”); else if (sum > 0) printf(“sum is positive”); else printf(“sum is zero”);

  38. ‘switch’ statement • General syntax switch (expression) case constant_1: statement_1; break; case constant_2: statement_2; break; … case constant_n: statement_n; break; default: statement_other; } Use ‘break’ to separate different cases.

  39. ‘switch’ statement ─ Example Lab 7: Find the bit pattern to display a digit on the 7-segment LED. (a) 7 segments of the LED (b) Bit assignment on the 8-pin connector

  40. ‘switch’ statement ─ Example unsigned char digit; // input unsigned char led_pattern; // output switch (digit) case ‘0’: led_pattern = 0b00111111; break; case ‘1’: led_pattern = 0b00000110; break; case ‘2’: led_pattern = 0b01011011; break; //you can complete more cases here... default: } PORTB = led_pattern; // send to PORTB and 7-segment LED

  41. ‘while’ statement • General syntax while (expression){ statements; } • Example: Compute the sum of 1 + 2 + … + 10. int sum, i; i = 1; sum = 0; while (i <= 10){ sum = sum + i; i = i + 1; }

  42. ‘for’ statement • General syntax for (expression1; expression2; expression3){ statements; } • expression1 is run before the loop starts. • expression2 is evaluated before each iteration. • expression3 is run after each iteration. • Example: Compute the sum of 1 + 2 + … + 10. int sum = 0; for (int i = 1; i <= 10; i++){ sum = sum + i; }

  43. ‘do’ statement • General syntax do { statements; } while (expression); • Example: Compute the sum of 1 + 2 + … + 10. int sum, i; i = 1; sum = 0; // Initialization do{ sum = sum + i; i = i + 1; }while (i <= 10);

  44. ‘break’ statement in loop • The ‘break’ statement inside a loop forces early termination of the loop. • What is the value of ‘sum’ after the following code is executed? int sum, i; i = 1; sum = 0; while (i <= 10){ sum = sum + i; i = i + 1; if (i > 5) break; } sum = ?

  45. ‘continue’ statement in loop • The ‘continue’ statement skips the subsequent statements in the code block, and forces the execution of the next iteration. • What is the value of ‘sum’ after the following code is executed? int sum, i; i = 1; sum = 0; // Initialization while (i <= 10){ i = i + 1; if (i < 5) continue; sum = sum + i; } sum = ?

  46. C arrays In C, array index starts from 0. • An array is a list of values that have the same data type. An array can be one-dimensional, two-dimensional or more. • This code example creates a 2-D array (multiplication table): int a[8][10]; for (int i = 0; i < 8; i++) for (int j = 0; i < 10; j++) a[i][j]= i * j; • An array can be initialized when it is declared. int b[3] = {4, 1, 10}; unsigned char keypad_key[3][4] = {{'1', '4', '7', '*'}, {'2', '5', '8', '0'}, {'3', '6', '9', '#'}};

  47. C functions • C functions are sub-routines that can be called from the main program or other functions. Functions enable modular designs, code reuse, and hiding of complex implementation details. A C function can have a list of parameters and produce a return value. Let us study C functions through examples.

  48. Functions ─ Example 1 Write a function to compute the factorial n! for a given n. // factorial is the name of the custom function // it accepts an input n of type int, and return an output of type int int factorial(int n){ int prod = 1; for (int i = 1; i <=n; i++) prod = prod * i; return prod; // return the result } int main(void){ int n = 5; // some example value of n int v; // variable to storage result v = factorial(n); // call the function, store return value in v return 1; }

  49. Functions ─ Example 2 Write a function to compute the factorial n! for a given n. // factorial is the name of the custom function // it accepts an input n of type int, // it stores output at memory location by int pointer p voidfactorial(int n, int* p){ int prod = 1; for (int i = 1; i <=n; i++) prod = prod * i; *p = prod;// store output at memory location pointed by p } int main(void){ int n = 5; // some example value of n int v; // variable to storage result factorial(n, &v); // call the function, store return value in v }

More Related