C Programming of Atmega328P
(Lecture-15)
R S Ananda Murthy
Associate Professor
Department of Electrical & Electronics Engineering,
Sri Jayachamarajendra College of Engineering,
Mysore 570 006
R S Ananda Murthy C Programming of Atmega328P
Specific Learning Outcomes
After completing this lecture, the student should be able to –
List the merits and demerits of programming MCU in C.
List some prominent C compilers for AVR MCUs.
Explain C-data types supported by AVR-GCC.
Explain how to choose correct data type for AVR C
programs.
Write C programs for Atmega328P, to do simple I/O
operations.
Realize time delays in C programs for Atmega328P.
Perform logical and bit-wise operations in AVR C programs.
Implement multiple branching in C Programs.
Use Interrupt Service Routines (ISR) in C Programs.
R S Ananda Murthy C Programming of Atmega328P
Merits and Demerits of C Programming AVR MCU
Merits
C program development is quicker 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.
Different C compilers may produce hex files of different
sizes from the same source file, causing variations in the
execution time.
Improper use of data types by the programmer can lead to
larger hex file size.
In C programs the programmer cannot directly access
GPRs of the MCU.
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
−2147483648 to
long 32-bit
+2147483648
float 32-bit ±1.175e − 38 to ±3.402e38
double 32-bit ±1.175e − 38 to ±3.402e38
Number of GPRs allocated by the C compiler to store a
variable depends upon the data type.
Using improper data type results in larger hex file.
R S Ananda Murthy C Programming of Atmega328P
Which Data Type for AVR C Programming?
C compilers use the signed char as the default unless
we put the keyword unsigned before char.
We can use unsigned char data type for a string of
ASCII characters.
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-1
for loop shown above is an endless loop because after
255, z is reset to 0 since it is an unsigned char data
type.
All AVR C programs will be endless loops as shown above.
The above program outputs $00 to $FF to continuously.
return 0; is written only for the sake of compatibility with
syntax of C. This statement is never executed.
R S Ananda Murthy C Programming of Atmega328P
AVR C Program-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
AVR C Program-3
This program toggles all the bits of Port B 200 times.
R S Ananda Murthy C Programming of Atmega328P
AVR C Program-4
The above program outputs 0xFC, 0xFD, 0xFE, 0xFF,
0x00, 0x01, 0x02, 0x03, and 0x04 to Port B. These are the
hex values for -4, -3, -2, -1, 0, 1, 2, 3, 4 respectively.
Verify the output at Port B by running this program in
single-step mode on the simulator.
R S Ananda Murthy C Programming of Atmega328P
AVR C Program-5
Run this program on the simulator to see how Port B
toggles continuously.
Notice that the maximum value for unsigned int data
type is 65,535.
R S Ananda Murthy C Programming of Atmega328P
AVR C Program-6
In this program z is defined as unsigned long data type
since its value becomes more than 65,535.
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 available in the MCU.
R S Ananda Murthy C Programming of Atmega328P
Realizing Delay using 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.
The maximum possible delay using _delay_ms() is
262.14 ms / F_CPU in MHz.
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.
The maximum possible delay is 768 µs / F_CPU in MHz.
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
Interrupt Programming in C
To use ISRs in C programs, we have to write
#include<avr/interrupt.h> at the beginning.
To enable interrupts we have to execute sei() function.
To disable interrupts we have to execute cli() function.
We should write ISR as
ISR(interrupt vector name)
{
..........//Instructions to serve the
..........//interrupt are written here.
sei();
}
Variables common to main() and ISR have to be globally
declared as volatile.
R S Ananda Murthy C Programming of Atmega328P
Interrupt Vector Names in avr/interrupt.h
Name Description
ADC_vect ADC Conversion Complete
ANALOG_COMP_vect Analog Comparator
EE_READY_vect EEPROM Ready
INT0_vect INT0 Interrupt (External Interrupt 0)
INT1_vect INT1 Interrupt (External Interrupt 1)
PCINT0_vect Pin Change Interrupt Request 0
PCINT1_vect Pin Change Interrupt Request 1
PCINT2_vect Pin Change Interrupt Request 2
SPI_STC_vect Serial Transfer Complete
SPM_READY_vect Store Program Memory Read
TIMER0_COMPB_vect Timer Counter 0 Compare Match B
These vector names are applicable to Atmega328P.
R S Ananda Murthy C Programming of Atmega328P
Interrupt Vector Names in avr/interrupt.h
Name Description
TIMER0_OVF_vect Timer/Counter0 Overflow
TIMER1_CAPT_vect Timer/Counter Capture Event
TIMER1_COMPA_vect Timer/Counter1 Compare Match A
TIMER1_COMPB_vect Timer/Counter1 Compare Match B
TIMER1_OVF_vect Timer/Counter1 Overflow
TIMER2_OVF_vect Timer/Counter2 Overflow
TWI_vect 2-wire Serial Interface
USART_RX_vect USART, Rx Complete
USART_TX_vect USART, Tx Complete
USART_UDRE_vect USART Data Register Empty
WDT_vect Watchdog Timeout Interrupt
These vector names are applicable to Atmega328P.
R S Ananda Murthy C Programming of Atmega328P
C-Code for Using INT0
In the program given above it is assumed that INT0 is
caused by pressing push button connected to INT0/PD2
pin.
R S Ananda Murthy C Programming of Atmega328P
Problem Statement to use INT0/INT1
Assume that a push button is connected to the INT0/PD2 pin of
Atmega328P MCU with internal pull-up resistor enabled and
that a 7-segment common cathode display is connected to
PORTB lines of the MCU through 330 Ω current limiting
resistors. Let a high-to-low logic change on this pin cause an
INT0 interrupt. Sketch a diagram showing the hardware
connections. Write a C program to display on the LED display
any integer in the range 1 to 6, both inclusive, when ever the
push button is pressed, thus simulating throwing of a dice.
Sketch a flow chart to show the logic.
Modify the program written above to use INT1/PD3.
R S Ananda Murthy C Programming of Atmega328P
Hardware for Simulation of Dice
Atmega328P Common Cathode Type
h
10 PB7 g
9 PB6
19 PB5 f
INT0/PD2 18 PB4 e
4 d
17 PB3
16 PB2 c
15 PB1 b
14 PB0 a
Pin numbers correspond to DIP package of MCU.
Program for simulation of dice is given in the next slide.
R S Ananda Murthy C Programming of Atmega328P
C Program for Simulation of Dice
Variables common to main() and ISR should be declared
as volatile globally.
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