0% found this document useful (0 votes)
15 views25 pages

ECTE333 Tutorial 01 Solution

The document outlines the tutorial schedule and content for ECTE333, focusing on C programming for AVR microcontrollers at the University of Wollongong. It includes instructions for using Atmel Studio, details on AVR specifics, C language elements, bit-wise operators, and digital IO ports, along with practical lab tasks. Additionally, it provides examples of C code for various operations and encourages practice with programming tasks related to microcontroller applications.
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)
15 views25 pages

ECTE333 Tutorial 01 Solution

The document outlines the tutorial schedule and content for ECTE333, focusing on C programming for AVR microcontrollers at the University of Wollongong. It includes instructions for using Atmel Studio, details on AVR specifics, C language elements, bit-wise operators, and digital IO ports, along with practical lab tasks. Additionally, it provides examples of C code for various operations and encourages practice with programming tasks related to microcontroller applications.
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/ 25

ECTE333 – Tutorial 1

C Programming for AVR Microcontrollers

School of Electrical, Computer and Telecommunications Engineering


University of Wollongong
Australia
ECTE333’s schedule
Lecture (2h) Tutorial (1h) Lab (2h)
L1:Introduction to AVR Microcontrollers
L2: C Programming, Digital IO Tutorial 1 Lab 1
L3: Serial Communication
Tutorial 2 Lab 2
L4: Interrupts, Timers
Tutorial 3 Lab 3
L5: Pulse Width Modulators
Tutorial 4 Lab 4
L6: Analogue-to-Digital Converters
Tutorial 5 Lab 5
L7: Microcontroller Applications
Lab 6
Lam Phung ECTE333 Tutorial 1 2/26
Study Advice

ECTE333 Appendix is available on


e-learning:
ECTE333-Exam-Appendix.pdf

It is included in the exam paper.

You should learn to use this


appendix when solving the
tutorial problems.

6 pages of tables, formulae, descriptions

Lam Phung ECTE333 Tutorial 1 3/26


Lab 1 – Tasks 4 and 5

Write C program for ATmega16 to read 4×3 keypad and show on 7-segment display
Lam Phung ECTE333 Tutorial 1 4/26
Tutorial 1’s overview

1. C Development Environment

2. Atmel AVR Specifics

3. C Language Elements

4. Bit-wise Operators

5. Digital IO Ports

6. C Functions and Flow-Control Constructs

Lam Phung ECTE333 Tutorial 1 5/26


Q1 - C Development Environment
State the steps and the tool to develop a C program for the Atmel AVR
microcontroller.

Tool: Atmel Studio 7

Step 1: Create Atmel 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 HEX file on Atmel AVR microcontroller.

Lam Phung ECTE333 Tutorial 1 6/26


Q2 - Atmel AVR - Specifics

a) What is the role of the header file <avr/io.h> in a C program for the
Atmel AVR?

<avr/io.h> contains the C definitions for all registers and flags of the AVR
microcontroller.

Depending on the device selected for the project, <avr/io.h> will redirect
to a specific header file.

For ATmega16, the specific header file is <iom16.h>

(see Lab Notes Appendix D).

Lam Phung ECTE333 Tutorial 1 7/26


Q2 - Atmel AVR - Specifics

b) Write C code that uses inline assembly coding to create a delay of one CPU
clock cycle.

To insert assembly code into a C program, use the asm directive.

To create a delay of one clock cycle, use the assembly instruction nop:
asm volatile("nop");

Keyword volatile forces the compiler to keep the order of C instructions.


It also signals that a variable may be changed outside its current function.

Lam Phung ECTE333 Tutorial 1 8/26


Q3 - C Language Elements

a) Name the C keyword to quit a function immediately.

return;

b) Name the C keyword to terminate a loop immediately.

break;

c) Name the C keyword for forcing the next iteration of a loop to start
immediately.

continue;

Lam Phung ECTE333 Tutorial 1 9/26


Q3 - C Language Elements
d) State the roles of the keywords ‘break’ and ‘default’ in the C ‘switch-case’
construct.

Keyword ‘break’ terminates the ‘switch-case’ instruction after each case.

Keyword ‘default’ indicates the instructions to run when no case is matched.

switch (a){
case ‘0’:
PORTA = 0b00111111;
break;
case ‘1’ : Lab 1 example

case ‘9’: 7-segment display
PORTA = 0b01101111; (video)
break;
default:
PORTA = 0b00000000;
}
Lam Phung ECTE333 Tutorial 1 10/26
Q3 - C Language Elements
e) Differentiate the two C operators & and &&.

& is the bit-wise AND, used on integer data types: x & 0b00000010.
&& is the logical AND, used with logical variables: if ((x>0) && (x<10)){…}

f) Differentiate the two C operators ~ and !=.

~ is the bit-wise inversion, used on any integer data type.


!= is the comparison operator for NOT EQUAL, e.g. if (a!=b){…}

g) Differentiate the two C operators = and ==.

= is the assignment operator: PORTA = 0b00111111;


== is the comparison operator for EQUAL: if (a == b){…}

Lam Phung ECTE333 Tutorial 1 11/26


Q4 – Bit-wise Operators

a) Determine the number of bits and the value ranges for C variables.

char is 8-bit, int is 16-bit, long int is 32-bit (for Atmel AVR 8-bit).
Modifier ‘unsigned’ has a range of 0, 1, …, 2n-1, where n is the number of bits.
The size of char, int, long int may depend on the CPU and compiler.

Notes: To improve code portability, we can also use fixed-width data types.
#include <inttypes.h> // header file for integer data types
int8_t a; // 8-bit signed
int16_t b; // 16-bit signed
int32_t c; // 32-bit signed
uint8_t d; // 8-bit unsigned
uint16_t e; // 16-bit unsigned

Lam Phung ECTE333 Tutorial 1 12/26


Q4 – Bit-wise Operators

b to f) Summary of bit-wise operators

Operator Bit-wise OR Bit-wise AND Bit-wise XOR

C symbol | & ^

x OP 0 = … x 0 x

x OP 1 = … 1 x x

. set a bit position to 1 . clear a bit position to 0 . invert a bit position


Usage . keep other bits unchanged . keep other bits unchanged . keep other bits unchanged

Lam Phung ECTE333 Tutorial 1 13/26


Q4 – Bit-wise Operators

b) Write C code to set bits 0, 2, 4 and 6 of variable a to binary ONE, while


keeping all other bits unchanged.

7 6 5 4 3 2 1 0 bit position
a x x x x x x x x

mask 0 1 0 1 0 1 0 1

{a} bit-wise OR {mask} x 1 x 1 x 1 x 1 output

a = a | 0b01010101;
Both methods of writing are acceptable.
a |= 0b01010101;

Lam Phung ECTE333 Tutorial 1 14/26


Q4 – Bit-wise Operators
c) Write C code to clear bits 1, 3, 5 and 7 of variable a to binary ZERO, while
keeping all other bits unchanged.

7 6 5 4 3 2 1 0 bit position
a x x x x x x x x

mask 0 1 0 1 0 1 0 1

{a} bit-wise AND {mask} 0 x 0 x 0 x 0 x output

a = a & 0b01010101;
Both methods of writing are acceptable.
a &= 0b01010101;

Lam Phung ECTE333 Tutorial 1 15/26


Q4 – Bit-wise Operators

d) Write C code to check if bit 2 of variable a is equal to binary ZERO.


• If TRUE, assign to variable d the ASCII code of character ‘9’.
• If FALSE, assign to variable d a value of zero.

7 6 5 4 3 2 1 0
Code:
a x x x x x x x x
if ((a & 0b00000100) == 0)
mask 0 0 0 0 0 1 0 0 d = ‘9’;
else
a & mask 0 0 0 0 0 x 0 0
d = 0;

Shorter version:
d = ((a & 0b00000100) ==0)?’9’:0;

Lam Phung ECTE333 Tutorial 1 16/26


Q4 – Bit-wise Operators

e) Write C code to check if bit 3 of variable a is equal to binary ONE.


• If so, assign to variable e the hexadecimal value of C3.
• If not, assign to variable d the binary value of 0111 0110.

Code:
if ((a & 0b00001000) != 0)
e = 0xC3;
else
d = 0b0111 0110;

Lam Phung ECTE333 Tutorial 1 17/26


Q4 – Bit-wise Operators
f) Write C code to invert bit 0 of variable a while keeping all other bits
unchanged.

7 6 5 4 3 2 1 0 bit position
a x x x x x x x x

mask 0 0 0 0 0 0 0 1

{a} bit-wise XOR {mask} x x x x x x x x output

a = a ^ 0b00000001;
Both methods of writing are acceptable.
a ^= 0b00000001;

Lam Phung ECTE333 Tutorial 1 18/26


Q5 – Digital IO Ports

a) Describe 3 registers associated with each digital IO port of the ATmega16.

There are 4 ports, so x can be A, B, C or D.

Data Direction Register (DDRx): data


output pin
external
To set specific pins of an digital IO port for input pin devices
- output (1) or micro-
- input (0). controller

Input Pins Address (PINx): to read data from input port.

Data Register (PORTx): to write data to output port.

Lam Phung ECTE333 Tutorial 1 19/26


Q5 – Digital IO Ports
b) Write a C code example to read/write to a digital IO port (use 4×3 keypad).

send 0 check if this bit is 0

Key ‘8’ is connected to Column 2 and


Row 3.

To check if key ‘8’ is pressed:

o send a binary 0 to Column 2 (bit 5),


o wait a few clock cycles, and
o check the binary value of Row 3 (bit 1).
o If Row 3 value is 0, key ‘8’ is pressed;
otherwise key ‘8’ is not pressed.

Bit number 7 6 5 4 3 2 1 0
Assigned to na Column 1 Column 2 Column 3 Row 1 Row 2 Row 3 Row 4

Lam Phung ECTE333 Tutorial 1 20/26


Q5 – Digital IO Ports
Reading the keypad (simple, long version)

unsigned char x; // temporary variable


unsigned char key; // ASCII code of the key pressed
DDRB = 0b11110000; // configure port B: pins 4-7 for output, 0-3 for input

// To check if key ‘8’ is pressed


PORTB = 0b11011111; // write 0 to bit 5 of port B – Column 2

// Create a short delay (see Tutorial 1 – Q2) …


asm volatile(“nop”);

x = PINB; // read from port B


if ((x & 0b00000010) = = 0){ // check bit 1 – Row 3
key = ‘8’;
}
// … check other keys similarly …

Lam Phung ECTE333 Tutorial 1 21/26


Q6 – C Functions and Flow Control Constructs
Write and test a C function for the Atmel AVR to check if an 8-bit
integer input n is a prime number.
unsigned char is_prime(unsigned char n){
unsigned char result, i; // result = 1 if n is a prime, 0 otherwise
switch (n){
case 0:
case 1:
result = 0; // when n = 0, 1
break;
case 2:
result = 1; // when n = 2: it is a prime
break;
default: // when n > 2
result = 1; // initial return value
for (i = 2; i < n; i++){
if (n % i == 0){
result = 0;
break;
}
}
}
return result; // return result
}
Lam Phung ECTE333 Tutorial 1 23/26
Q6 – C Functions and Flow Control Constructs

// prime.c : main program to test the is_prime function

#include <avr/io.h> // AVR header file

unsigned char is_prime(unsigned char n){



}

int main(void){
unsigned char n, result;
n = 13; // any integer input
result = is_prime(n); // call function

DDRB = 0xFF; // set PORTB for output


PORTB = (result)?0x00:0xFF; // turn on LEDs if prime, off otherwise

while (1) {}; // infinite loop


return 1;
}

Lam Phung ECTE333 Tutorial 1 24/26


Lectures 1&2 / Lab 1 / Tutorial 1: Extra practice

Write and test a C program for STK500 board that repeatedly


 lets the user enter a digit by pressing a button on the 4x3 keyboard.
 turns on the corresponding LED.

For example, if user presses key ‘2’ then LED2 will be on.

Lam Phung ECTE333 Tutorial 1 25/26


Lectures 1&2 / Lab 1 / Tutorial 1: Extra practice

Consider a C variable defined as follows:


unsigned char a;

Write C code to invert bits 0, 1, and 2 of a, while keeping all other bits unchanged.

Lam Phung ECTE333 Tutorial 1 26/26

You might also like