Lam Phung © University of Wollongong, 2010. 3 Lam Phung © University of Wollongong, 2010. 4
AVR story 7.1 Overview of ATmega16
Lam Phung © University of Wollongong, 2010. 5 Lam Phung © University of Wollongong, 2010. 6
Lam Phung © University of Wollongong, 2010. 9 Lam Phung © University of Wollongong, 2010. 10
Why using C? We need two tools for C development: Atmel AVR Studio and WinAVR.
Lam Phung © University of Wollongong, 2010. 11 Lam Phung © University of Wollongong, 2010. 12
Installing C tools Development cycle for C
For ECTE333, we use the following versions: Step 1: Create AVR Studio project
Lam Phung © University of Wollongong, 2010. 13 Lam Phung © University of Wollongong, 2010. 14
Click Next.
A program that lets user turn on LED by pressing a switch.
Video demo link: [avr]/ecte333/lab07_task123.mp4
Lam Phung © University of Wollongong, 2010. 15 Lam Phung © University of Wollongong, 2010. 16
Create AVR Studio project Step 2: Enter a C program
Lam Phung © University of Wollongong, 2010. 17 Lam Phung © University of Wollongong, 2010. 18
while(1){
// Read input from PORTA.
// This port will be connected to the 8 switches
i = PINA;
PORTA to
switches power switch
12-V power
Select menu Tools |
ATmega16
supply Program AVR | Connect.
chip
Click ‘Connect’.
programming
mode
PORTB to LEDs
Lam Phung © University of Wollongong, 2010. 21 Lam Phung © University of Wollongong, 2010. 22
Select Input HEX file, Most students in this class learnt C programming in their first year.
generated in Step 3.
The program remains even In all lectures of ECTE333, C code examples will be used extensively.
after power-off. To erase,
click ‘Erase Device’.
Lam Phung © University of Wollongong, 2010. 23 Lam Phung © University of Wollongong, 2010. 24
7.3.1 Structure of a C program C comments
A C program typically has two main sections.
Comments are text that the compiler ignores.
#include section: to insert header files.
main() section: code that runs when the program starts.
For a single-line comment, use double back slashes
In the example below, <avr/io.h> is a header file that contains all
DDRA = 0x00; // set PORTA for input
register definitions for the AVR microcontroller.
#include <avr/io.h> // avr header file for all registers/pins
int main(void){ For a multi-line comment, use the pair /* and */
unsigned char i; // temporary variable /* File: led.c
DDRA = 0x00; // set PORTA for input Description: Simple C program for the ATMEL AVR(ATmega16 chip)
DDRB = 0xFF; // set PORTB for output It lets user turn on LEDs by pressing the switches on the STK500
PORTB = 0x00; // turn ON all LEDs initially board
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;
Always use comments to make program easy to understand.
}
return 1;
}
Lam Phung © University of Wollongong, 2010. 25 Lam Phung © University of Wollongong, 2010. 26
A C block is a group of statements enclosed by braces {}. unsigned char b; // b value range 0, 1, 2, …, 255
unsigned long int c; // c value range 0,…, 232 - 1
Usually, a C block is run depending on some logical conditions.
while (1){
// Read input from PORTA - connected to the 8 switches Some examples of variable assignment
i = PINA; a = 0xA0; // a stores hexadecimal value of A0
// Send output to PORTB - connected to the 8 LEDs b = ‘1’; // b stores ASCII code of character ‘1’
PORTB = i; c = 2000ul; // c stores a unsigned long integer 2000
}
Lam Phung © University of Wollongong, 2010. 27 Lam Phung © University of Wollongong, 2010. 28
C operators Arithmetic operators
- Negation -x Negate x
Lam Phung © University of Wollongong, 2010. 29 Lam Phung © University of Wollongong, 2010. 30
< Less than x < y 1 if x is smaller than y, 0 otherwise && Logical AND x && y 1 is both x and y are 1, otherwise 0
<= Less than or x <= y 1 is x is smaller than or equal to y, 0 otherwise || Logical OR x || y 0 if both x and y are 0, otherwise 1
equal to
== Equal to x == y 1 is x is equal to y, 0 otherwise
Lam Phung © University of Wollongong, 2010. 31 Lam Phung © University of Wollongong, 2010. 32
Bit-wise operators Data-access operators
Lam Phung © University of Wollongong, 2010. 33 Lam Phung © University of Wollongong, 2010. 34
Operator Name Example Description To change the program flow, there are six types of statements
() Function _delay_ms(250) Call a function to create delay of
250ms if-else statement
Conditional
(type) Type cast char x = 3; x is 8-bit integer switch statement
(int) x x is converted to 16-bit integer
? Conditional char x; This is equivalent to
while statement
evaluation y=(x>5)?10:20; if (x > 5)
y = 10; for statement Iterative
else do statement
y = 20;
Lam Phung © University of Wollongong, 2010. 35 Lam Phung © University of Wollongong, 2010. 36
If-else statement Switch statement
General syntax General syntax
if (expression)
switch (expression)
statement_1; case constant_1:
else statement_1;
statement_2; break;
case constant_2:
Example code statement_2;
char a, b, sum; break;
a = 4; b = -5; …
sum = a + b; case constant_n: Use ‘break’ to separate
if (sum < 0) statement_n; different cases.
printf(“sum is negative”); break;
else if (sum > 0) default:
printf(“sum is positive”); statement_other;
else }
printf(“sum is zero”);
Lam Phung © University of Wollongong, 2010. 37 Lam Phung © University of Wollongong, 2010. 38
Lab 7: Find the bit pattern to display a digit on the 7-segment LED. unsigned char digit;
unsigned char led_pattern;
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:
Bit pattern for digit ‘1’: 0 0 0 0 0 1 1 0 }
Bit pattern for digit ‘2’: 0 0 0 1 1 0 1 1 PORTB = led_pattern; // send to PORTB and 7-segment LED
Lam Phung © University of Wollongong, 2010. 39 Lam Phung © University of Wollongong, 2010. 40
While statement For statement
General syntax
General syntax
for (expression1; expression2; expression3){
while (expression){ statements;
statements; }
}
expression1 is run before the loop starts.
expression2 is evaluated before each iteration.
Example code: Compute the sum of 1 + 2+ …+ 100
expression3 is run after each iteration.
General syntax The ‘break’ statement inside a loop forces early termination of the
do { loop.
statements;
} while (expression);
What is the value of ‘sum’ after the following code is executed?
Lam Phung © University of Wollongong, 2010. 43 Lam Phung © University of Wollongong, 2010. 44
Continue statement in loop C arrays
An array is a list of values that have the same data type.
The ‘continue’ statement skips the subsequent statements in the
code block and forces the execution of the next iteration. In C, array index starts from 0.
Lam Phung © University of Wollongong, 2010. 45 Lam Phung © University of Wollongong, 2010. 46
C functions are sub-routines that can be called from the main Write a function to compute the factorial n! for a given n.
program or other functions.
Lam Phung © University of Wollongong, 2010. 47 Lam Phung © University of Wollongong, 2010. 48
Functions ─ Example 2 Guidelines on C coding and documentation
Optimize the C code for efficiency and length.
Write a function to compute the factorial n! for a given n.
Delete unnecessary lines of code.
The C code must be properly formatted.
// factorial is the name of the custom function
// it accepts an input n of type int, For printing, use a fixed-width font such as Courier New for code.
// it stores output at memory location by int pointer p
void factorial(int n, int* p){ Use indentation to show the logical structure of the program.
int prod = 1;
for (int i = 1; i <=n; i++) Use a blank line to separate code sections.
prod = prod * i;
*p = prod;// store output at memory location pointed by p Use meaningful variable names and function names.
}
If a C statement is too long for one printed line, split it logically into
int main(void){
int n = 5; // some example value of n multiple lines.
int v; // variable to storage result
factorial(n, &v); // call the function, store return value in v Use C comments concisely to explain code.
}
Observe the way that C code is presented in the lecture notes or
lab notes.
Lam Phung © University of Wollongong, 2010. 49 Lam Phung © University of Wollongong, 2010. 50
Lam Phung © University of Wollongong, 2010. 51 Lam Phung © University of Wollongong, 2010. 52
Digital IO in ATmega16 ─ Configuring for input/output Digital IO in ATmega16 ─ Reading from/Writing to Port
For each port, there are three relevant 8-bit registers. Register Data Register (PORTx) is used to write output data to port.
Data Direction Register (DDRx) Example: To write a binary 0 to output pin 6, binary 1 to other
Input Pins Address (PINx) pins of Port A, we write C code
PORTA = 0b10111111; // write output
Data Register (PORTx)
Here, x denotes A, B, C or D.
Register Input Pins Address (PINx) is used to read input data from port.
Data Direction Register (DDRx) is used to configure a specific port Example: To read the input pins of Port A, we write C code
pin as output (1) or input (0). unsigned char temp; // temporary variable
temp = PINA; // read input
Example: To set Port A pins 0 to 3 for input, pins 4 to 7 for
output, we write C code Where do the C names PINA, PORTA, DDRA come from?
DDRA = 0b11110000; // configure pins // extract for header file <avr/iom16>
bit 7 6 5 4 3 2 1 0 #define PINA _SFR_IO8(0x19)
DDRA 1 1 1 1 0 0 0 0 #define DDRA _SFR_IO8(0x1A)
Example while(1){
// Read input from PORTA.
For ATmega16, the specific header file is ‘avr/iom16.h’. // This port will be connected to the 8 switches
i = PINA;
This header file is printed in Appendix A of the lab notes.
The header file lists the C names for all registers in ATmega16, // Send output to PORTB.
// This port will be connected to the 8 LEDs
and their memory locations. PORTB = i;
} Demo in
We always use the C names in our code. return 1; slide 15.
}
Lam Phung © University of Wollongong, 2010. 55 Lam Phung © University of Wollongong, 2010. 56
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.