SlideShare a Scribd company logo
7
Most read
8
Most read
9
Most read
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
Merits and Demerits of C Programming AVR MCU
Merits
Lesser time consuming and easier than ALP.
C programs can be easily ported to some other
microcontroller with minor modifications.
C programs are less error prone when compared to ALP.
Readiliy available C function libraries can be used to reduce
development time.
Demerits
C compilers do not produce optimized hex files.
Improper use of data types by the programmer can lead to
larger hex file size.
The programmer does not have direct access to general
purpose 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 Range
unsigned char 8-bit 0 to 255
char 8-bit −128 to +127
unsigned int 16-bit 0 to 65,535
int 16-bit −32768 to +32767
unsigned long 32-bit 0 to 4294967295
long 32-bit
−2147483648 to
+2147483648
float 32-bit ±1.175e −38 to ±3.402e38
double 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 the
data type having the smallest range to accommodate the
data.
As far as possible, unsigned char data type should be used
since most of the data handled by the AVR MCU is in the
range 0 to 255 or $00 to $FF.
Using int data type instead of char type can result in larger
hex 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 D
to 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 because
the actual delay depends upon the length of the hex file
generated by the compiler which varies from one compiler
to another and also on the clock frequency of MCU. For
these reasons, we have to measure the actual delay using
an oscilloscope.
By using predefined C functions.
_delay_ms() function can be used to cause several ms
delay and _delay_us() can be used to cause several µs
delay. Both of these functions are defined in avr-libc in
delay.h header file. See avr-libc documentation available
at 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, it
will 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 after
each 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 a
Creative Commons Attribution 4.0 International License.
R S Ananda Murthy C Programming of Atmega328P

More Related Content

PPTX
Visibility control in java
PPTX
Unit 4 sp macro
PPTX
Module 1 - ARM 32 Bit Microcontroller
PPT
ARM Micro-controller
PPTX
Design challenges in embedded systems
PPTX
ARM- Programmer's Model
PPTX
Embedded System Programming on ARM Cortex M3 and M4 Course
Visibility control in java
Unit 4 sp macro
Module 1 - ARM 32 Bit Microcontroller
ARM Micro-controller
Design challenges in embedded systems
ARM- Programmer's Model
Embedded System Programming on ARM Cortex M3 and M4 Course

What's hot (20)

PDF
Introduction to arm architecture
PDF
L15 timers-counters-in-atmega328 p
PPTX
Microprocessor architecture-I
PPTX
Counters & time delay
PPTX
INTRODUCTION TO MICROCONTROLLER
PPT
Memory organization of 8051
PPT
8085 interrupts
PPTX
Addressing modes
PPTX
data representation
PPTX
Sum of subset problem.pptx
PDF
Embedded Firmware Design and Development, and EDLC
PPTX
Interrupts in 8051
PPTX
Interrupts of 8086
DOCX
8085 interfacing with memory chips
PPTX
Micro programmed control
PPTX
ARM Exception and interrupts
PPTX
Deadlock Slides
PPTX
Addressing Modes of 8085 Microprocessor
PPSX
Matlab basic and image
Introduction to arm architecture
L15 timers-counters-in-atmega328 p
Microprocessor architecture-I
Counters & time delay
INTRODUCTION TO MICROCONTROLLER
Memory organization of 8051
8085 interrupts
Addressing modes
data representation
Sum of subset problem.pptx
Embedded Firmware Design and Development, and EDLC
Interrupts in 8051
Interrupts of 8086
8085 interfacing with memory chips
Micro programmed control
ARM Exception and interrupts
Deadlock Slides
Addressing Modes of 8085 Microprocessor
Matlab basic and image
Ad

Viewers also liked (20)

PDF
L14 kb-lcd-interfacing-with-atmega328 p
PDF
L3 instruction-execution-steps
PDF
L9 understanding-atmega328 p-2
PDF
L16 usart-atmega328 p
PDF
L7 starting-to-use-mcu
PDF
Introduction to-Tex-and-LaTeX
PDF
L8 understanding-atmega328 p-1
PDF
L11 assembly-language-programming-of-atmega328 p
PDF
L10 assembly-language-programming-of-atmega328 p
PDF
Lecture-1 : Introduction to Power Electronics
PDF
Trends in-power-electronics
PDF
L6 primary-memory
PDF
L5 data-parallel-computers
PDF
L1 intro-to-mpu-mcu
PDF
Transformers
PDF
L4 speeding-up-execution
PDF
Lecture-5 : Semiconductor Power Switching Devices-2
PDF
L13 interrupts-in-atmega328 p
PDF
Lecture-3 : More Applications of Power Electronics
PDF
Lecture-7 : Semiconductor Power Switching Devices-4
L14 kb-lcd-interfacing-with-atmega328 p
L3 instruction-execution-steps
L9 understanding-atmega328 p-2
L16 usart-atmega328 p
L7 starting-to-use-mcu
Introduction to-Tex-and-LaTeX
L8 understanding-atmega328 p-1
L11 assembly-language-programming-of-atmega328 p
L10 assembly-language-programming-of-atmega328 p
Lecture-1 : Introduction to Power Electronics
Trends in-power-electronics
L6 primary-memory
L5 data-parallel-computers
L1 intro-to-mpu-mcu
Transformers
L4 speeding-up-execution
Lecture-5 : Semiconductor Power Switching Devices-2
L13 interrupts-in-atmega328 p
Lecture-3 : More Applications of Power Electronics
Lecture-7 : Semiconductor Power Switching Devices-4
Ad

Similar to L12 c-language-programming-of-atmega328 p (20)

DOCX
Assignment
DOCX
Embedded System Practical manual (1)
PPTX
Tutorial on avr atmega8 microcontroller, architecture and its applications
DOCX
DSP_Assign_1
PPT
Introduction to Blackfin BF532 DSP
PDF
Sivanantham resume
PDF
C programming session9 -
PDF
Mobile robotic platform to gathering real time sensory data in wireless perso...
PDF
A STUDY OF AN ENTRENCHED SYSTEM USING INTERNET OF THINGS
PPT
Developing an avr microcontroller system
PDF
IRJET- Design & Implementation of Black Box in Automobiles System
PPT
Picmico
PDF
Bascom avr-course
PPT
EEE226a.ppt
PDF
Embedded concepts
PDF
Paper id 24201428
PDF
Doc8453
PDF
Effisiensi prog atmel
PDF
I010315760
PPT
Target updated track f
Assignment
Embedded System Practical manual (1)
Tutorial on avr atmega8 microcontroller, architecture and its applications
DSP_Assign_1
Introduction to Blackfin BF532 DSP
Sivanantham resume
C programming session9 -
Mobile robotic platform to gathering real time sensory data in wireless perso...
A STUDY OF AN ENTRENCHED SYSTEM USING INTERNET OF THINGS
Developing an avr microcontroller system
IRJET- Design & Implementation of Black Box in Automobiles System
Picmico
Bascom avr-course
EEE226a.ppt
Embedded concepts
Paper id 24201428
Doc8453
Effisiensi prog atmel
I010315760
Target updated track f

More from rsamurti (6)

PDF
EE110-elementary-circuit-and-network-theory-(a)
PDF
Synchronous generators
PDF
Three phase-circuits
PDF
L2 types-of-computers
PDF
Lecture-4 : Semiconductor Power Switching Devices-1
PDF
Lecture-2 : Applications of Power Electronics
EE110-elementary-circuit-and-network-theory-(a)
Synchronous generators
Three phase-circuits
L2 types-of-computers
Lecture-4 : Semiconductor Power Switching Devices-1
Lecture-2 : Applications of Power Electronics

Recently uploaded (20)

DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Geodesy 1.pptx...............................................
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
composite construction of structures.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
web development for engineering and engineering
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
UNIT 4 Total Quality Management .pptx
Digital Logic Computer Design lecture notes
OOP with Java - Java Introduction (Basics)
CYBER-CRIMES AND SECURITY A guide to understanding
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
bas. eng. economics group 4 presentation 1.pptx
R24 SURVEYING LAB MANUAL for civil enggi
Geodesy 1.pptx...............................................
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
composite construction of structures.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CH1 Production IntroductoryConcepts.pptx
Sustainable Sites - Green Building Construction
web development for engineering and engineering
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...

L12 c-language-programming-of-atmega328 p

  • 1. 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
  • 2. Merits and Demerits of C Programming AVR MCU Merits Lesser time consuming and easier than ALP. C programs can be easily ported to some other microcontroller with minor modifications. C programs are less error prone when compared to ALP. Readiliy available C function libraries can be used to reduce development time. Demerits C compilers do not produce optimized hex files. Improper use of data types by the programmer can lead to larger hex file size. The programmer does not have direct access to general purpose registers in C programs. R S Ananda Murthy C Programming of Atmega328P
  • 3. 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
  • 4. C Data Types in AVR-GCC Data Type Size in Bits Data Range unsigned char 8-bit 0 to 255 char 8-bit −128 to +127 unsigned int 16-bit 0 to 65,535 int 16-bit −32768 to +32767 unsigned long 32-bit 0 to 4294967295 long 32-bit −2147483648 to +2147483648 float 32-bit ±1.175e −38 to ±3.402e38 double 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
  • 5. Data Type for AVR C Programming Based on the knowledge of range of data, always use the data type having the smallest range to accommodate the data. As far as possible, unsigned char data type should be used since most of the data handled by the AVR MCU is in the range 0 to 255 or $00 to $FF. Using int data type instead of char type can result in larger hex files. R S Ananda Murthy C Programming of Atmega328P
  • 6. 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
  • 7. 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 D to PORTB. R S Ananda Murthy C Programming of Atmega328P
  • 8. Methods of Realizing Time Delay in C By using simple for loop. This method does not give accurate time delays because the actual delay depends upon the length of the hex file generated by the compiler which varies from one compiler to another and also on the clock frequency of MCU. For these reasons, we have to measure the actual delay using an oscilloscope. By using predefined C functions. _delay_ms() function can be used to cause several ms delay and _delay_us() can be used to cause several µs delay. Both of these functions are defined in avr-libc in delay.h header file. See avr-libc documentation available at 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
  • 9. Delay by FOR Loop This program toggles bits of PORTB continuously with a delay. R S Ananda Murthy C Programming of Atmega328P
  • 10. 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
  • 11. 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
  • 12. Input and Output Operations in C This program reads a byte from Port C. If it is less than 100, it will send it to Port B; otherwise it will send it to Port D. R S Ananda Murthy C Programming of Atmega328P
  • 13. Logic Operations in C In the program given above find the output at each port after each logical operation. R S Ananda Murthy C Programming of Atmega328P
  • 14. Bit-wise Shift Operations in C R S Ananda Murthy C Programming of Atmega328P
  • 15. Bit-wise Operations in C R S Ananda Murthy C Programming of Atmega328P
  • 16. Multiple Branching using Switch R S Ananda Murthy C Programming of Atmega328P
  • 17. License This work is licensed under a Creative Commons Attribution 4.0 International License. R S Ananda Murthy C Programming of Atmega328P