0% found this document useful (1 vote)
80 views

Chapter 7

AVR Microcontroller

Uploaded by

Sreeni Vasulu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (1 vote)
80 views

Chapter 7

AVR Microcontroller

Uploaded by

Sreeni Vasulu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 34
CHAPTER 7 AVR PROGRAMMING INC OBJECTIVES Upon completion of this chapter, you will be able to: >> >> >> >> >> Examine C data types for the AVR Code C programs for time delay and I/O operations Code C programs for I/O bit manipulation Code C programs for logic and arithmetic operations Code C programs for ASCII and BCD data conversion Code C programs for binary (hex) to decimal conversion Code C programs for data serialization Code C programs for EEPROM access 255 Why program the AVR in C? Compilers produce hex files that we download into the Flash of the micro- controller. The size of the hex file produced by the compiler is one of the main concerns of microcontroller programmers because microcontrollers have limited on-chip Flash. For example, the Flash space for the ATmegal6 is 16K bytes. How does the choice of programming language affect the compiled pro- gram size? While Assembly language produces a hex file that is much smaller than C, programming in Assembly language is often tedious and time consuming, On the other hand, C programming is less time consuming and much easier to write, but the hex file size produced is much larger than if we used Assembly language. The following are some of the major reasons for writing programs in C instead of Assembly: 1. Itis easier and less time consuming to write in C than in Assembly. 2. Cis easier to modify and update. 3. You can use code available in function libraries. 4, C code is portable to other microcontrollers with little or no modification. Several third-party companies develop C compilers for the AVR microcon- troller. Our goal is not to recommend one over another, but to provide you with the fundamentals of C programming for the AVR. You can use the compiler of your choice for the chapter examples and programs. For this book we have chosen AVR GCC compiler to integrate with AVR Studio. At the time of the writing of this book AYR GCC and AVR Studio are available as a free download from the Web. See http://www.MicroDigitalEd.com for tutorials on AVR Studio and the AVR GCC compiler. C programming for the AVR is the main topic of this chapter, In Section 7.1, we discuss data types, and time delays, I/O programming is shown in Section 7.2. The logic operations AND, OR, XOR, inverter, and shift are discussed in Section 7.3. Section 7.4 describes ASCII and BCD conversions and checksums. In Section 7.5, data serialization for the AVR is shown. In Section 7.6, memory allo- cation in C is discussed. SECTION 7.1: DATA TYPES AND TIME DELAYS IN C In this section we first discuss C data types for the AVR and then provide code for time delay functions. C data types for the AVR C One of the goals of AVR programmers is to create smaller hex files, so it is worthwhile to re-examine C data types. In other words, a good understanding of C data types for the AVR can help programmers to create smaller hex files. In this section we focus on the specific C data types that are most common and widely used in AVR C compilers. Table 7-1 shows data types and sizes, but these may vary from one compiler to another. 256 Table 7-1; Some Data Types Widely Used by C Compilers Data ye Size in Bits Data Ran; unsigned char it 0 to 255 char =128 to +127 unsigned int 0 to 65,535 int ~32,768 to +32,767 unsigned long 32-bit 0 to 4,294,967,295. Jong 32-bit ~2,147,483,648 to +2,147,483,648 float 32-bit +1.175e-38 to +3.402¢38 double 32-bit +1.175e-38 to +3.402€38 Unsigned char Because the AVR is an 8-bit microcontroller, the character data type is the most natural choice for many applications. The unsigned char is an 8-bit data type that takes a value in the range of 0-255 (00-FFH). It is one of the most widely used data types for the AVR. In many situations, such as setting a counter value, where there is no need for signed data, we should use the unsigned char instead of the signed char. In declaring variables, we must pay careful attention to the size of the data and try to use unsigned char instead of int if possible. Because the AVR microcon- troller has a limited number of registers and data RAM locations, using int in place of char can lead to the need for more memory space. Such misuse of data types in compilers such as Microsoft Visual C++ for x86 IBM PCs is not a significant issue. Remember that C compilers use the signed char as the default unless we put the keyword unsigned in front of the char (sce Example 7-1). We can also use the unsigned char data type for a string of ASCII characters, including extended ASCII characters. Example 7-2 shows a string of ASCII characters. See Example 7-3 for toggling a port 200 times. Write an AVR C program to send values 00-FF to Port B. Solution: #include //standard AVR header int main (void) { unsigned char z+ DDRB = OxFF; //PORTB is output for(z = 0; 2 <= 2557 PORTB = a7 return 07 , //Notice that the program never exits the for loop because if you //inorement an unsigned char variable when it is OxFF, it will //become zero, ooo CHAPTER 7: AVR PROGRAMMING IN C 257

You might also like