ECTE333 Lecture 07
ECTE333 Lecture 07
ECTE333 Lecture 07
Week 1 2 3 4 5 L9: Timers Tutorial 9 L10: Pulse width modulator Tutorial 10 L11: Analogue-to-digital converter Tutorial 11 L12: Case studies Lab 12 L13: Revision lecture Final exam (25%), Practical exam (20%), Labs (5%)
Lam Phung University of Wollongong, 2011.
Tutorial (1h)
Lab (2h)
6 7 8 9 10 11 12 13
Lecture 7s sequence
AVR story
Overview of ATmega16
Review of C Programming
Digital IO in ATmega16
AVR podcast by AVR TV.
University of Wollongong, 2011.
Lam Phung
ATmega16 131 16K 1K bytes 512 bytes none 32 4 SPI, USART, Two-wire two 8-bit, one 16-bit 4 Analogue comparator, 8-channel 10-bit ADC 21
6
Lam Phung
Lam Phung
Port D Port B
CPU ADC
Lam Phung University of Wollongong, 2011.
Lam Phung
C has standard libraries for complex tasks: data type conversions, standard input/output, long-integer arithmetic.
In Lecture 7, well study digital IO ports of the ATmega16. Other peripherals will be studied in depth, from Lectures 8 to 11.
Lam Phung University of Wollongong, 2011.
The Atmel AVR instruction set has been designed to support C compilers: C code can be converted efficiently to assembly code.
9
Lam Phung University of Wollongong, 2011.
10
C tools
We need two tools for C development: Atmel AVR Studio and WinAVR. Atmel AVR Studio An integrated development environment for Atmel AVR microcontroller. It includes editor, assembler, emulator, HEX file downloader. Available from Atmel website,
http://www.atmel.com/dyn/products/tools_card.asp?tool_id=2725
Installing C tools
For ECTE333, we use the following versions: * Atmel AVR Studio version 4.18 build 692 * WinAVR release 2010.01.10 1) Uninstall earlier versions of WinAVR and Atmel AVR Studio. 2) Download setup files for Atmel AVR Studio and WinAVR
http://www.elec.uow.edu.au/avr/getdoc.php?doc=tools/AvrStudio4Setup.exe http://www.elec.uow.edu.au/avr/getdoc.php?doc=tools/AVRStudio4.18SP1.exe http://www.elec.uow.edu.au/avr/getdoc.php?doc=tools/WinAVR-20100110-install.exe
WinAVR A C compiler for AVR microcontroller. Can be used alone or as a plug-in for Atmel AVR Studio. Available at: http://winavr.sourceforge.net/
Lam Phung University of Wollongong, 2011.
3) Run setup file for Atmel AVR Studio. Accept default options. 4) Run setup file for WinAVR. Accept default options.
11
Lam Phung University of Wollongong, 2011.
12
A program that lets user turn on LED by pressing a switch. Video demo link: [avr]/ecte333/lab07_task123.mp4 13
Lam Phung University of Wollongong, 2011.
14
Select debug platform and device. debug platform: AVR Simulator device: ATmega16 Click Finish.
Lam Phung
15
Lam Phung
16
Enter a C program
project files
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; DDRB = 0xFF; PORTB = 0x00; // set PORTA for input // set PORTB for output // turn ON all LEDs initially
program led.c
status messages
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; 17
Lam Phung University of Wollongong, 2011.
18
serial cable to PC
Hardware setup for example program. Connections to PORTA & PORTB are only for this example.
19
Lam Phung University of Wollongong, 2011.
20
Lam Phung
21
Lam Phung
22
Here, we review briefly major aspects of the C programming language. 7.3.1 7.3.2 7.3.3 7.4.4 Structure of a C program Data types and operators Flow control in C C functions
In the example below, <avr/io.h> is a header file that contains all register definitions for the AVR microcontroller.
#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; } Lam Phung University of Wollongong, 2011.
Lam Phung
23
24
C comments
Comments are text that the compiler ignores. For a single-line comment, use double back slashes
DDRA = 0x00; // set PORTA for input
C blocks A C block is a group of statements enclosed by braces {}. Usually, a C block is run depending on some logical conditions.
while (1){ // Read input from PORTA - connected to the 8 switches
25
Lam Phung
26
C operators
C has a rich set of operators Arithmetic operators Relational operators
Miscellaneous operators
Lam Phung
27
Lam Phung
28
Arithmetic operators
Operator * / % + ++ Name Multiplication Division Modulo Addition Subtraction Increment Decrement Example x * y x / y x % y x + y x y x++ ++x -x---x Description Multiply x times y
Relational operators
Operator Divide x by y Remainder of x divided by y Add x and y < Subtract y from x <= Increment x by 1 after using it Increment x by 1 before using it Decrement x by 1 after using it Decrement x by 1 before using it Negate x 29
Lam Phung
Name Greater than Greater than or equal to Less than Less than or equal to Equal to Not equal to
Description 1 if x is greater than 5, 0 otherwise 1 is x is greater than or equal to 5, 0 otherwise 1 if x is smaller than y, 0 otherwise 1 is x is smaller than or equal to y, 0 otherwise 1 is x is equal to y, 0 otherwise 1 is x is not equal to 4, 0 otherwise
> >=
x < y x <= y
== !=
x == y x != 4
Lam Phung
Negation
-x
30
Logical operators
Bit-wise operators
These operate on individual bits of a variable/constant.
Lam Phung
31
Lam Phung
32
Data-access operators
These operate on arrays, structures or pointers. Well learn more about these operators later.
Miscellaneous operators
Operator Name Operator Name [] . -> * & Array element Member selection Member selection Indirection Address of Example x[2] x.age p->age *p &x Description Third element of array x Field age of structure variable x ? Field age of structure pointer p Content of memory location pointed by p Conditional evaluation () Function Type cast
Example _delay_ms(250)
Description Call a function to create delay of 250ms x is 8-bit integer x is converted to 16-bit integer This is equivalent to if (x > 5) y = 10; else
(type)
y = 20;
Lam Phung
33
Lam Phung
34
if-else statement
General syntax if (expression) statement_1; else statement_2; Example code 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);
35
Lam Phung University of Wollongong, 2011.
Lam Phung
36
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; }
Lam Phung
0 0
0 0
0 0
0 1
0 1
1 0
1 1
0 1
38
while statement
General syntax while (expression){ statements; } Example code: Compute the sum of 1 + 2+ + 100 int sum, i; i = 1; sum = 0; while (i <= 100){ sum = sum + i; i = i + 1; }
39
Lam Phung University of Wollongong, 2011.
40
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 code: Compute the sum of 1 + 2+ + 10 int sum; sum = 0; for (int i = 1; i <= 10; i++){ sum = sum + i; }
Lam Phung University of Wollongong, 2011.
do statement
General syntax do { statements; } while (expression);
Example code: compute the sum of 1 + 2 + + 10 int sum, i; i = 1; sum = 0; do{ sum = sum + i; i = i + 1; } while (i <= 10);
41
Lam Phung
42
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; }
What is the value of sum after the following code is executed? int sum, i; i = 1; sum = 0; while (i <= 10){ i = i + 1; if (i < 5) continue; sum = sum + i; }
Lam Phung
43
Lam Phung
44
C arrays
An array is a list of values that have the same data type. In C, array index starts from 0. 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;
7.3.4 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.
45
Lam Phung
46
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; }
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 void factorial(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 }
Lam Phung
47
Lam Phung
48
49
Lam Phung
50
6
1
5
1
4
1
3
0
2
0
1
0
0
0
for output
Lam Phung University of Wollongong, 2011.
for input 52
51
Lam Phung
Register Input Pins Address (PINx) is used to read input data from port. Example: To read the input pins of Port A, we write C code
unsigned char temp; // temporary variable temp = PINA; // read input
53
Lam Phung
54
Lecture 7s summary
What we learnt in this lecture: An overview of ATmega16. The tools and the steps for programming the Atmel AVR. Basics about C programming. Programming the digital IO ports of ATmega16. What are next activities? Tutorial 7: Introduction to C programming for the AVR . Lab 7: Introduction to C programming for the AVR Complete online Pre-lab Quiz for Lab 7. Write programs for tasks 4-5 of Lab 7.
Demo in slide 14.
55
Lam Phung
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; }
Lam Phung University of Wollongong, 2011.
56
Lecture 7 references
J. Pardue, C Programming for Microcontrollers, 2005, SmileyMicros, [Chapters 1-6].
S. F. Barrett and D. J. Pack, Atmel AVR Microcontroller Primer: Programming and Interfacing, 2008, Morgan & Claypool Publishers, [Chapter 1].
Atmel Corp., 8-bit AVR microcontroller with 16K Bytes In-System Programmable Flash ATmega16/ATmega16L, 2007.
Lam Phung
57