L12 c-language-programming-of-atmega328 p

17
C Programming of Atmega328P (Lecture-12) R S Ananda Murthy Associate Professor and Head Department of Electrical & Electronics Engineering, Sri Jayachamarajendra College of Engineering, Mysore 570 006 R S Ananda Murthy C Programming of Atmega328P

Transcript of L12 c-language-programming-of-atmega328 p

C Programming of Atmega328P(Lecture-12)

R S Ananda Murthy

Associate Professor and HeadDepartment of Electrical & Electronics Engineering,

Sri Jayachamarajendra College of Engineering,Mysore 570 006

R S Ananda Murthy C Programming of Atmega328P

Merits and Demerits of C Programming AVR MCU

Merits

Lesser time consuming and easier than ALP.C programs can be easily ported to some othermicrocontroller with minor modifications.C programs are less error prone when compared to ALP.Readiliy available C function libraries can be used to reducedevelopment time.

Demerits

C compilers do not produce optimized hex files.Improper use of data types by the programmer can lead tolarger hex file size.The programmer does not have direct access to generalpurpose registers in C programs.

R S Ananda Murthy C Programming of Atmega328P

Some Prominent AVR C Compilers

AVR-GCC in Atmel Studio on Windows platforms.AVR-GCC in GNU Linux environment. URL:http://www.gnu.org/software/gcc/

microC PRO for AVR. URL:http://www.mikroe.com/mikroc/avr/

CrossWorks for AVR. URL:http://www.rowley.co.uk/avr/

JumpStart C Tools for AVR. https://www.imagecraft.com/devtools_AVR.html

AVR-GCC is the most popular compiler in industry.

R S Ananda Murthy C Programming of Atmega328P

C Data Types in AVR-GCC

Data Type Size in Bits Data Rangeunsigned char 8-bit 0 to 255

char 8-bit −128 to +127unsigned int 16-bit 0 to 65,535

int 16-bit −32768 to +32767unsigned long 32-bit 0 to 4294967295

long 32-bit−2147483648 to+2147483648

float 32-bit ±1.175e−38 to ±3.402e38double 32-bit ±1.175e−38 to ±3.402e38

Using improper data type results in larger hex file.

R S Ananda Murthy C Programming of Atmega328P

Data Type for AVR C Programming

Based on the knowledge of range of data, always use thedata type having the smallest range to accommodate thedata.As far as possible, unsigned char data type should be usedsince most of the data handled by the AVR MCU is in therange 0 to 255 or $00 to $FF.Using int data type instead of char type can result in largerhex files.

R S Ananda Murthy C Programming of Atmega328P

AVR C Program Sample-1

All AVR C programs will be infinite loops as shown above.The above program outputs $00 to $FF to continuously.

R S Ananda Murthy C Programming of Atmega328P

AVR C Program Sample-2

Infinite loop is caused by while(1) statement.The above program outputs 0, 1, 2, 3, 4, 5, A, B, C, and Dto PORTB.

R S Ananda Murthy C Programming of Atmega328P

Methods of Realizing Time Delay in C

By using simple for loop.

This method does not give accurate time delays becausethe actual delay depends upon the length of the hex filegenerated by the compiler which varies from one compilerto another and also on the clock frequency of MCU. Forthese reasons, we have to measure the actual delay usingan oscilloscope.

By using predefined C functions.

_delay_ms() function can be used to cause several msdelay and _delay_us() can be used to cause several µsdelay. Both of these functions are defined in avr-libc indelay.h header file. See avr-libc documentation availableat http://avr-libc.nongnu.org/user-manual/group__util__delay.html

By using timers in the MCU.

R S Ananda Murthy C Programming of Atmega328P

Delay by FOR Loop

This program toggles bits of PORTB continuously with a delay.

R S Ananda Murthy C Programming of Atmega328P

Delay by using _delay_ms() Function

This program blinks LED at PB2 with a delay of 100 ms.

R S Ananda Murthy C Programming of Atmega328P

Delay by using _delay_us() Function

This program blinks LED at PB2 with a delay of 100 µs.

R S Ananda Murthy C Programming of Atmega328P

Input and Output Operations in C

This program reads a byte from Port C. If it is less than 100, itwill send it to Port B; otherwise it will send it to Port D.

R S Ananda Murthy C Programming of Atmega328P

Logic Operations in C

In the program given above find the output at each port aftereach logical operation.

R S Ananda Murthy C Programming of Atmega328P

Bit-wise Shift Operations in C

R S Ananda Murthy C Programming of Atmega328P

Bit-wise Operations in C

R S Ananda Murthy C Programming of Atmega328P

Multiple Branching using Switch

R S Ananda Murthy C Programming of Atmega328P

License

This work is licensed under aCreative Commons Attribution 4.0 International License.

R S Ananda Murthy C Programming of Atmega328P