SlideShare a Scribd company logo
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
Most read
9
Delay by FOR Loop
This program toggles bits of PORTB continuously with a delay.
R S Ananda Murthy C Programming of Atmega328P
Most read
15
Bit-wise Operations in C
R S Ananda Murthy C Programming of Atmega328P
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

What's hot (20)

PIC MICROCONTROLLERS -CLASS NOTES
PIC MICROCONTROLLERS -CLASS NOTESPIC MICROCONTROLLERS -CLASS NOTES
PIC MICROCONTROLLERS -CLASS NOTES
Dr.YNM
 
Timer And Counter in 8051 Microcontroller
Timer And Counter in 8051 MicrocontrollerTimer And Counter in 8051 Microcontroller
Timer And Counter in 8051 Microcontroller
Jay Makwana
 
8 interrupt 8051
8 interrupt 80518 interrupt 8051
8 interrupt 8051
daniemol
 
2 bit comparator (Digital Electronics)
2 bit comparator (Digital Electronics)2 bit comparator (Digital Electronics)
2 bit comparator (Digital Electronics)
Shail Nakum
 
Microcontroller 8051 1
Microcontroller 8051  1Microcontroller 8051  1
Microcontroller 8051 1
khan yaseen
 
Field-Effect Transistors
Field-Effect TransistorsField-Effect Transistors
Field-Effect Transistors
guest3b5d8a
 
Abcd parameters
Abcd parametersAbcd parameters
Abcd parameters
vidhyavatisuryawansh
 
Semiconductor Devices
Semiconductor DevicesSemiconductor Devices
Semiconductor Devices
Rajesh Kumar
 
Orcad pspice intro and basics
Orcad pspice intro and basicsOrcad pspice intro and basics
Orcad pspice intro and basics
Praveen Kumar
 
Lesson sample introduction to arduino
Lesson sample   introduction to arduinoLesson sample   introduction to arduino
Lesson sample introduction to arduino
Betsy Eng
 
microprocessor-and-microcontroller
microprocessor-and-microcontrollermicroprocessor-and-microcontroller
microprocessor-and-microcontroller
jhcid
 
UP AND DOWN FADING OF LED
UP AND DOWN FADING OF LEDUP AND DOWN FADING OF LED
UP AND DOWN FADING OF LED
sanjay kumar pediredla
 
8085 interfacing with memory chips
8085 interfacing with memory chips8085 interfacing with memory chips
8085 interfacing with memory chips
Srikrishna Thota
 
The 8051 microcontroler based embedded systems
The 8051 microcontroler based embedded systemsThe 8051 microcontroler based embedded systems
The 8051 microcontroler based embedded systems
manishpatel_79
 
Getting started with Keil uVision 2020
Getting started with Keil uVision 2020Getting started with Keil uVision 2020
Getting started with Keil uVision 2020
SaravananVijayakumar4
 
8051 Microcontroller
8051 Microcontroller8051 Microcontroller
8051 Microcontroller
thokalpv
 
Ic 555
Ic 555Ic 555
Ic 555
Abhijit Jadhav
 
Chapter 4 Microprocessor CPU
Chapter 4 Microprocessor CPUChapter 4 Microprocessor CPU
Chapter 4 Microprocessor CPU
askme
 
Different semiconductor diodes.ppt
Different semiconductor diodes.pptDifferent semiconductor diodes.ppt
Different semiconductor diodes.ppt
SHASHIKUMARDASI
 
Mosfet
MosfetMosfet
Mosfet
Sirat Mahmood
 
PIC MICROCONTROLLERS -CLASS NOTES
PIC MICROCONTROLLERS -CLASS NOTESPIC MICROCONTROLLERS -CLASS NOTES
PIC MICROCONTROLLERS -CLASS NOTES
Dr.YNM
 
Timer And Counter in 8051 Microcontroller
Timer And Counter in 8051 MicrocontrollerTimer And Counter in 8051 Microcontroller
Timer And Counter in 8051 Microcontroller
Jay Makwana
 
8 interrupt 8051
8 interrupt 80518 interrupt 8051
8 interrupt 8051
daniemol
 
2 bit comparator (Digital Electronics)
2 bit comparator (Digital Electronics)2 bit comparator (Digital Electronics)
2 bit comparator (Digital Electronics)
Shail Nakum
 
Microcontroller 8051 1
Microcontroller 8051  1Microcontroller 8051  1
Microcontroller 8051 1
khan yaseen
 
Field-Effect Transistors
Field-Effect TransistorsField-Effect Transistors
Field-Effect Transistors
guest3b5d8a
 
Semiconductor Devices
Semiconductor DevicesSemiconductor Devices
Semiconductor Devices
Rajesh Kumar
 
Orcad pspice intro and basics
Orcad pspice intro and basicsOrcad pspice intro and basics
Orcad pspice intro and basics
Praveen Kumar
 
Lesson sample introduction to arduino
Lesson sample   introduction to arduinoLesson sample   introduction to arduino
Lesson sample introduction to arduino
Betsy Eng
 
microprocessor-and-microcontroller
microprocessor-and-microcontrollermicroprocessor-and-microcontroller
microprocessor-and-microcontroller
jhcid
 
8085 interfacing with memory chips
8085 interfacing with memory chips8085 interfacing with memory chips
8085 interfacing with memory chips
Srikrishna Thota
 
The 8051 microcontroler based embedded systems
The 8051 microcontroler based embedded systemsThe 8051 microcontroler based embedded systems
The 8051 microcontroler based embedded systems
manishpatel_79
 
Getting started with Keil uVision 2020
Getting started with Keil uVision 2020Getting started with Keil uVision 2020
Getting started with Keil uVision 2020
SaravananVijayakumar4
 
8051 Microcontroller
8051 Microcontroller8051 Microcontroller
8051 Microcontroller
thokalpv
 
Chapter 4 Microprocessor CPU
Chapter 4 Microprocessor CPUChapter 4 Microprocessor CPU
Chapter 4 Microprocessor CPU
askme
 
Different semiconductor diodes.ppt
Different semiconductor diodes.pptDifferent semiconductor diodes.ppt
Different semiconductor diodes.ppt
SHASHIKUMARDASI
 

Viewers also liked (20)

L14 kb-lcd-interfacing-with-atmega328 p
L14 kb-lcd-interfacing-with-atmega328 pL14 kb-lcd-interfacing-with-atmega328 p
L14 kb-lcd-interfacing-with-atmega328 p
rsamurti
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 p
rsamurti
 
L3 instruction-execution-steps
L3 instruction-execution-stepsL3 instruction-execution-steps
L3 instruction-execution-steps
rsamurti
 
L9 understanding-atmega328 p-2
L9 understanding-atmega328 p-2L9 understanding-atmega328 p-2
L9 understanding-atmega328 p-2
rsamurti
 
L7 starting-to-use-mcu
L7 starting-to-use-mcuL7 starting-to-use-mcu
L7 starting-to-use-mcu
rsamurti
 
Introduction to-Tex-and-LaTeX
Introduction to-Tex-and-LaTeXIntroduction to-Tex-and-LaTeX
Introduction to-Tex-and-LaTeX
rsamurti
 
L8 understanding-atmega328 p-1
L8 understanding-atmega328 p-1L8 understanding-atmega328 p-1
L8 understanding-atmega328 p-1
rsamurti
 
L11 assembly-language-programming-of-atmega328 p
L11 assembly-language-programming-of-atmega328 pL11 assembly-language-programming-of-atmega328 p
L11 assembly-language-programming-of-atmega328 p
rsamurti
 
L10 assembly-language-programming-of-atmega328 p
L10 assembly-language-programming-of-atmega328 pL10 assembly-language-programming-of-atmega328 p
L10 assembly-language-programming-of-atmega328 p
rsamurti
 
Lecture-1 : Introduction to Power Electronics
Lecture-1 : Introduction to Power ElectronicsLecture-1 : Introduction to Power Electronics
Lecture-1 : Introduction to Power Electronics
rsamurti
 
Trends in-power-electronics
Trends in-power-electronicsTrends in-power-electronics
Trends in-power-electronics
rsamurti
 
L6 primary-memory
L6 primary-memoryL6 primary-memory
L6 primary-memory
rsamurti
 
L5 data-parallel-computers
L5 data-parallel-computersL5 data-parallel-computers
L5 data-parallel-computers
rsamurti
 
L1 intro-to-mpu-mcu
L1 intro-to-mpu-mcuL1 intro-to-mpu-mcu
L1 intro-to-mpu-mcu
rsamurti
 
Transformers
TransformersTransformers
Transformers
rsamurti
 
L4 speeding-up-execution
L4 speeding-up-executionL4 speeding-up-execution
L4 speeding-up-execution
rsamurti
 
Lecture-5 : Semiconductor Power Switching Devices-2
Lecture-5 : Semiconductor Power Switching Devices-2Lecture-5 : Semiconductor Power Switching Devices-2
Lecture-5 : Semiconductor Power Switching Devices-2
rsamurti
 
L13 interrupts-in-atmega328 p
L13 interrupts-in-atmega328 pL13 interrupts-in-atmega328 p
L13 interrupts-in-atmega328 p
rsamurti
 
Lecture-3 : More Applications of Power Electronics
Lecture-3 : More Applications of Power ElectronicsLecture-3 : More Applications of Power Electronics
Lecture-3 : More Applications of Power Electronics
rsamurti
 
Lecture-7 : Semiconductor Power Switching Devices-4
Lecture-7 : Semiconductor Power Switching Devices-4Lecture-7 : Semiconductor Power Switching Devices-4
Lecture-7 : Semiconductor Power Switching Devices-4
rsamurti
 
L14 kb-lcd-interfacing-with-atmega328 p
L14 kb-lcd-interfacing-with-atmega328 pL14 kb-lcd-interfacing-with-atmega328 p
L14 kb-lcd-interfacing-with-atmega328 p
rsamurti
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 p
rsamurti
 
L3 instruction-execution-steps
L3 instruction-execution-stepsL3 instruction-execution-steps
L3 instruction-execution-steps
rsamurti
 
L9 understanding-atmega328 p-2
L9 understanding-atmega328 p-2L9 understanding-atmega328 p-2
L9 understanding-atmega328 p-2
rsamurti
 
L7 starting-to-use-mcu
L7 starting-to-use-mcuL7 starting-to-use-mcu
L7 starting-to-use-mcu
rsamurti
 
Introduction to-Tex-and-LaTeX
Introduction to-Tex-and-LaTeXIntroduction to-Tex-and-LaTeX
Introduction to-Tex-and-LaTeX
rsamurti
 
L8 understanding-atmega328 p-1
L8 understanding-atmega328 p-1L8 understanding-atmega328 p-1
L8 understanding-atmega328 p-1
rsamurti
 
L11 assembly-language-programming-of-atmega328 p
L11 assembly-language-programming-of-atmega328 pL11 assembly-language-programming-of-atmega328 p
L11 assembly-language-programming-of-atmega328 p
rsamurti
 
L10 assembly-language-programming-of-atmega328 p
L10 assembly-language-programming-of-atmega328 pL10 assembly-language-programming-of-atmega328 p
L10 assembly-language-programming-of-atmega328 p
rsamurti
 
Lecture-1 : Introduction to Power Electronics
Lecture-1 : Introduction to Power ElectronicsLecture-1 : Introduction to Power Electronics
Lecture-1 : Introduction to Power Electronics
rsamurti
 
Trends in-power-electronics
Trends in-power-electronicsTrends in-power-electronics
Trends in-power-electronics
rsamurti
 
L6 primary-memory
L6 primary-memoryL6 primary-memory
L6 primary-memory
rsamurti
 
L5 data-parallel-computers
L5 data-parallel-computersL5 data-parallel-computers
L5 data-parallel-computers
rsamurti
 
L1 intro-to-mpu-mcu
L1 intro-to-mpu-mcuL1 intro-to-mpu-mcu
L1 intro-to-mpu-mcu
rsamurti
 
Transformers
TransformersTransformers
Transformers
rsamurti
 
L4 speeding-up-execution
L4 speeding-up-executionL4 speeding-up-execution
L4 speeding-up-execution
rsamurti
 
Lecture-5 : Semiconductor Power Switching Devices-2
Lecture-5 : Semiconductor Power Switching Devices-2Lecture-5 : Semiconductor Power Switching Devices-2
Lecture-5 : Semiconductor Power Switching Devices-2
rsamurti
 
L13 interrupts-in-atmega328 p
L13 interrupts-in-atmega328 pL13 interrupts-in-atmega328 p
L13 interrupts-in-atmega328 p
rsamurti
 
Lecture-3 : More Applications of Power Electronics
Lecture-3 : More Applications of Power ElectronicsLecture-3 : More Applications of Power Electronics
Lecture-3 : More Applications of Power Electronics
rsamurti
 
Lecture-7 : Semiconductor Power Switching Devices-4
Lecture-7 : Semiconductor Power Switching Devices-4Lecture-7 : Semiconductor Power Switching Devices-4
Lecture-7 : Semiconductor Power Switching Devices-4
rsamurti
 
Ad

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

Assignment
AssignmentAssignment
Assignment
Abu Md Choudhury
 
Embedded System Practical manual (1)
Embedded System Practical manual (1)Embedded System Practical manual (1)
Embedded System Practical manual (1)
Niraj Bharambe
 
Tutorial on avr atmega8 microcontroller, architecture and its applications
Tutorial on avr atmega8 microcontroller, architecture and its applicationsTutorial on avr atmega8 microcontroller, architecture and its applications
Tutorial on avr atmega8 microcontroller, architecture and its applications
Edgefxkits & Solutions
 
DSP_Assign_1
DSP_Assign_1DSP_Assign_1
DSP_Assign_1
Joseph Chandler
 
Introduction to Blackfin BF532 DSP
Introduction to Blackfin BF532 DSPIntroduction to Blackfin BF532 DSP
Introduction to Blackfin BF532 DSP
Pantech ProLabs India Pvt Ltd
 
Sivanantham resume
Sivanantham resumeSivanantham resume
Sivanantham resume
sivananthamw3
 
C programming session9 -
C programming  session9 -C programming  session9 -
C programming session9 -
Keroles karam khalil
 
Mobile robotic platform to gathering real time sensory data in wireless perso...
Mobile robotic platform to gathering real time sensory data in wireless perso...Mobile robotic platform to gathering real time sensory data in wireless perso...
Mobile robotic platform to gathering real time sensory data in wireless perso...
Alexander Decker
 
A STUDY OF AN ENTRENCHED SYSTEM USING INTERNET OF THINGS
A STUDY OF AN ENTRENCHED SYSTEM USING INTERNET OF THINGSA STUDY OF AN ENTRENCHED SYSTEM USING INTERNET OF THINGS
A STUDY OF AN ENTRENCHED SYSTEM USING INTERNET OF THINGS
International Journal of Technical Research & Application
 
Developing an avr microcontroller system
Developing an avr microcontroller systemDeveloping an avr microcontroller system
Developing an avr microcontroller system
nugnugmacmac
 
IRJET- Design & Implementation of Black Box in Automobiles System
IRJET-  	  Design & Implementation of Black Box in Automobiles SystemIRJET-  	  Design & Implementation of Black Box in Automobiles System
IRJET- Design & Implementation of Black Box in Automobiles System
IRJET Journal
 
Picmico
PicmicoPicmico
Picmico
loges91
 
Bascom avr-course
Bascom avr-courseBascom avr-course
Bascom avr-course
handson28
 
EEE226a.ppt
EEE226a.pptEEE226a.ppt
EEE226a.ppt
SaifulAhmad27
 
Embedded concepts
Embedded conceptsEmbedded concepts
Embedded concepts
sartaj ahmed
 
Paper id 24201428
Paper id 24201428Paper id 24201428
Paper id 24201428
IJRAT
 
Doc8453
Doc8453Doc8453
Doc8453
khoaicuto
 
Effisiensi prog atmel
Effisiensi prog atmelEffisiensi prog atmel
Effisiensi prog atmel
rm_dhozooo
 
I010315760
I010315760I010315760
I010315760
IOSR Journals
 
Target updated track f
Target updated   track fTarget updated   track f
Target updated track f
Alona Gradman
 
Embedded System Practical manual (1)
Embedded System Practical manual (1)Embedded System Practical manual (1)
Embedded System Practical manual (1)
Niraj Bharambe
 
Tutorial on avr atmega8 microcontroller, architecture and its applications
Tutorial on avr atmega8 microcontroller, architecture and its applicationsTutorial on avr atmega8 microcontroller, architecture and its applications
Tutorial on avr atmega8 microcontroller, architecture and its applications
Edgefxkits & Solutions
 
Mobile robotic platform to gathering real time sensory data in wireless perso...
Mobile robotic platform to gathering real time sensory data in wireless perso...Mobile robotic platform to gathering real time sensory data in wireless perso...
Mobile robotic platform to gathering real time sensory data in wireless perso...
Alexander Decker
 
Developing an avr microcontroller system
Developing an avr microcontroller systemDeveloping an avr microcontroller system
Developing an avr microcontroller system
nugnugmacmac
 
IRJET- Design & Implementation of Black Box in Automobiles System
IRJET-  	  Design & Implementation of Black Box in Automobiles SystemIRJET-  	  Design & Implementation of Black Box in Automobiles System
IRJET- Design & Implementation of Black Box in Automobiles System
IRJET Journal
 
Bascom avr-course
Bascom avr-courseBascom avr-course
Bascom avr-course
handson28
 
Paper id 24201428
Paper id 24201428Paper id 24201428
Paper id 24201428
IJRAT
 
Effisiensi prog atmel
Effisiensi prog atmelEffisiensi prog atmel
Effisiensi prog atmel
rm_dhozooo
 
Target updated track f
Target updated   track fTarget updated   track f
Target updated track f
Alona Gradman
 
Ad

More from rsamurti (6)

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

Recently uploaded (20)

Artificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowyArtificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowy
dominikamizerska1
 
Class-Symbols for vessels ships shipyards.pdf
Class-Symbols for vessels ships shipyards.pdfClass-Symbols for vessels ships shipyards.pdf
Class-Symbols for vessels ships shipyards.pdf
takisvlastos
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)
pelumiadigun2006
 
Introduction of Structural Audit and Health Montoring.pptx
Introduction of Structural Audit and Health Montoring.pptxIntroduction of Structural Audit and Health Montoring.pptx
Introduction of Structural Audit and Health Montoring.pptx
gunjalsachin
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
Advanced Automation and Technology in Coal Handling Plants
Advanced Automation and Technology in Coal Handling PlantsAdvanced Automation and Technology in Coal Handling Plants
Advanced Automation and Technology in Coal Handling Plants
Infopitaara
 
cloud Lecture_2025 cloud architecture.ppt
cloud Lecture_2025 cloud architecture.pptcloud Lecture_2025 cloud architecture.ppt
cloud Lecture_2025 cloud architecture.ppt
viratkohli82222
 
Cloud Computing storage saas iaas paas.pptx
Cloud Computing storage saas iaas paas.pptxCloud Computing storage saas iaas paas.pptx
Cloud Computing storage saas iaas paas.pptx
viratkohli82222
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Environmental Engineering Wastewater.pptx
Environmental Engineering Wastewater.pptxEnvironmental Engineering Wastewater.pptx
Environmental Engineering Wastewater.pptx
SheerazAhmed77
 
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
ijccmsjournal
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
Axial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Axial Capacity Estimation of FRP-strengthened Corroded Concrete ColumnsAxial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Axial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Journal of Soft Computing in Civil Engineering
 
Influence line diagram in a robust model
Influence line diagram in a robust modelInfluence line diagram in a robust model
Influence line diagram in a robust model
ParthaSengupta26
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
Influence line diagram for truss in a robust
Influence line diagram for truss in a robustInfluence line diagram for truss in a robust
Influence line diagram for truss in a robust
ParthaSengupta26
 
introduction to Digital Signature basics
introduction to Digital Signature basicsintroduction to Digital Signature basics
introduction to Digital Signature basics
DhavalPatel171802
 
Artificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowyArtificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowy
dominikamizerska1
 
Class-Symbols for vessels ships shipyards.pdf
Class-Symbols for vessels ships shipyards.pdfClass-Symbols for vessels ships shipyards.pdf
Class-Symbols for vessels ships shipyards.pdf
takisvlastos
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)Strength of materials (Thermal stress and strain relationships)
Strength of materials (Thermal stress and strain relationships)
pelumiadigun2006
 
Introduction of Structural Audit and Health Montoring.pptx
Introduction of Structural Audit and Health Montoring.pptxIntroduction of Structural Audit and Health Montoring.pptx
Introduction of Structural Audit and Health Montoring.pptx
gunjalsachin
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
Advanced Automation and Technology in Coal Handling Plants
Advanced Automation and Technology in Coal Handling PlantsAdvanced Automation and Technology in Coal Handling Plants
Advanced Automation and Technology in Coal Handling Plants
Infopitaara
 
cloud Lecture_2025 cloud architecture.ppt
cloud Lecture_2025 cloud architecture.pptcloud Lecture_2025 cloud architecture.ppt
cloud Lecture_2025 cloud architecture.ppt
viratkohli82222
 
Cloud Computing storage saas iaas paas.pptx
Cloud Computing storage saas iaas paas.pptxCloud Computing storage saas iaas paas.pptx
Cloud Computing storage saas iaas paas.pptx
viratkohli82222
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Environmental Engineering Wastewater.pptx
Environmental Engineering Wastewater.pptxEnvironmental Engineering Wastewater.pptx
Environmental Engineering Wastewater.pptx
SheerazAhmed77
 
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
ijccmsjournal
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
Influence line diagram in a robust model
Influence line diagram in a robust modelInfluence line diagram in a robust model
Influence line diagram in a robust model
ParthaSengupta26
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
Influence line diagram for truss in a robust
Influence line diagram for truss in a robustInfluence line diagram for truss in a robust
Influence line diagram for truss in a robust
ParthaSengupta26
 
introduction to Digital Signature basics
introduction to Digital Signature basicsintroduction to Digital Signature basics
introduction to Digital Signature basics
DhavalPatel171802
 

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