AGH University of Science and Technology Cracow Department of Electronics

AGH University of Science and Technology Cracow Department of Electronics Microprocessors laboratory Tutorial 8 AVR programming in C Author: Paweł ...
Author: Phebe Nicholson
6 downloads 3 Views 285KB Size
AGH University of Science and Technology Cracow Department of Electronics

Microprocessors laboratory

Tutorial 8 AVR programming in C

Author: Paweł Russek http://www.fpga.agh.edu.pl/upt ver. 18/06/14

1/10

1. Introduction 1.1. Objective The main goal of this tutorial is to introduce the essential ability of AVR tools, which is programming of microcontrolers in C language. We will use AVR GCC compiler that is integrated with Atmel Studio. Basic techniques and methods will be presented. We will start with the explain of C data types for 8-bit AVRs. Next, some time delay functions will be proposed. GPIO ports manipulation will be presented. Logic and bit manipulation will be exercised later. We will conclude with timer and interrupt programming in C.

1.2. Prerequisites •

Knowledge of AVR ATMega32 hardware architecture.



Fundamental AVR assembly programming skills.



Basic knowledge and understanding of C language constructs.

2. C programming facts The size of the binary code that is downloaded to the microcontroller memory is one of the biggest concerns of programmers. Assembly language leads to the binary file that is as small as possible, while C language usually results in bigger program size. On the other hand assembly programming is tedious, cumbersome, and also time consuming. C programming is much less time consuming and much easier to write. C is also easier to modify and update. Also , C code is portable to other microcontrollers with little modifications. Several companies develop C compilers for the AVR.

2.1. C data types for AVR C We will focus on the specific C data types that are most common and widely used in AVR C compiler. Types and sizes for AVR GCC are shown in the table below, but be aware that these may vary from one compiler to another. Data type

Size in Bits

Data Range

unsigned char

8-bit

0 to 255 (0x0 to 0xff)

char

8-bit

-128 to +127

unsigned int

16-bit

0 to 65,535

int

16-bit

-32,768 to +32,767

unsigned long

32-bit

O to 4,294,967,295

long

32-bit

-2,147,483,648 to -2,147,483,647

float

32-bit

±1.175*10-38 to ±3.402*1038

Because the ATMega is an 8-bit microcontroller, the character (char and unsigned char) types are the most natural choice for many applications. Usually we want to use unsigned data instead of signed data, but remember that C compilers use signed values as the default value 2/10

unless we put the unsigned keyword. In declaring variables, you should pay attention to the size of the data and try to use 8-bit data instead of 16-bit (int and unsigned int) data, bacause the AVR microconroller has limited number if registers and data RAM locations. Using int in place of char can lead to the need for more memory space. We can also use the unsigned char data type for ASCII characters.

3. Setting up a C project in AVR Studio Exercise 8.1 Run Atmel Studio 6.0 and crate new AVR assembler project. 1. Click “Atmel Studio 6.0” in Windows' start menu 2. Choose “New project” in “Start Page – AtmelStudio” window 3. Select “C/C++” and “GCC C Executable project” 4. Rename your project in “Name” field. The best if project name include your name/nickname and laboratory name in the project name. For example 'shrek_upt_C_proj`. You can also change project “Location” field.

5. Click 'OK' 6. Select device: ATMega32

3/10

7. Click 'OK' 8. Your workspace is ready now.

4/10

4. Quick start with C code To access a PORT register as a byte , we use the PORTx label where x indicates the name of the port. We access the data direction registers DDRx in the same way. To access PIN register, we use the PINx label. Example #include int main() { unsigned char val; DDRA=0xFF; DDRB=0x00; PORTB=0xff; while(1) { val=PINB; PORTA=val; } return 0; }

//Standard AVR header

//Set port A as output //Set port B as input //Set pull-ups on port B //Stay here forever //Read port B //Send value to port A

Exercise 8.2 Connect port A to LEDs, and port B to the keypad on ZL3AVR development board. Configure the keypad in small keypad mode. Write the program in AVR Studio, which tests the four buttons of the small keypad, and sends four different ASCII values to the LEDs. Value 'A' for the button one, value 'B' for button two, etc. Build the code. Find Debug subdirectory in your project location. Copy AVRDude software to Debug folder. Program the board with the code, and test its functionality.

5. Implementing delays in C Example #include /* *Implement wait function */ void wait(unsigned int delay) { volatile unsigned int i; for(i=0; i

Suggest Documents