07 C Prgramming
07 C Prgramming
Chapter 7
AVR Programming in C
Topics
Basic C program
Data Types
Accessing I/O registers
Time Delays
IO Programming in C
Logic Operation in C
Data serialization in C
2
Languages
High Level Languages
Easy to develop and
update
C Language
Acceptable
performance High Level Languages
(like VB)
Easy to develop and
C Language
update
Portable Machine Language
Low Level Languages
High performance
Not portable
3
A simple C program
Write a program that calculates the sum of
{1,3,…,13,15}
int main ()
{
unsigned int sum = 0;
while (1);
return 0;
}
4
Basic C program structure
5
Preprocessor
#include and #define are the preprocessor
directives
#include: preprocessor inserts the file specified
Example #include <avr/io.h> is inserted in the place of the
include line (it defines memory addresses and symbolic
labels for CPU and peripheral function register addresses)
Include files normally store function prototypes and constant
definition
#define: simple macro substitution or to form
function-like macros
Example #define MAX 10
# define INC (x) (x+1) or #define SQR (a) ((a) * (a))
6
Program variables
7
Data Types
Use unsigned whenever you can
unsigned char instead of unsigned int if you can
8
Data types (cont.)
char lng;
long c; c
lng
int a1;
unsigned int a;
a1 a
Q1.
Q1.What
Whatisisthe
thedifference
differencebetween
betweenint
intand
andunsigned
unsignedint?
int?
9
Variables values
10
10
Data types
Precautions must be taken:
Floating point operations minimized whenever
possible.
Expensive in terms of memory usage and
processing power
Appropriate data types must be used
Example count from 1 to 100, 8 bit long data
type is enough
11
Choosing optimized data type
unsigned int sum; unsigned char sum;
for (int i = 1; i <= 15; i+=2) for (char i = 1; i <= 15; i+=2)
sum += i; sum += i;
12
Choosing optimized data type
13
Accessing I/O registers
Example 1: Write an AVR C program to
send value 0xAA to PORTD.
#include <avr/io.h>
int main ()
{
DDRD = 0xFF;
PORTD = 0xAA;
while (1);
return 0;
}
14
Accessing I/O registers
Example 2: Write an AVR C program to
calculate PINB + PINC and send the result
to PORTD.
#include <avr/io.h>
int main ()
{
DDRB = 0x00;
DDRC = 0x00;
DDRD = 0xFF;
while (1)
PORTD = PINB + PINC;
return 0;
}
15
Accessing I/O registers
Example 3: Write an AVR C program that
outputs 0 to 9 continuously on PORTD.
#include <avr/io.h>
int main ()
{
DDRD = 0xFF;// Port D is output
while(1)
for (unsigned char i = 0; i <= 9; i++)
PORTD = i;
return 0;
}
16
Accessing I/O registers Examples
17
Accessing I/O registers Examples
18
Accessing I/O registers Examples
19
Accessing I/O registers Examples
20
Accessing I/O registers Examples
21
Time Delays in C
You can use for loop to introduce time delay
void delay(void)
{
unsigned int i;
for(i = 0; i < 42150; i++)
{
}
}
duration !
The compiler has direct effect on delay duration!
22
Time Delays in C
You can use predefined functions of compilers to make
time delays
In Atmel Studio:
It is compiler dependent
23
I/O programming in C
Byte size I/O programming in C
DDRB = 0xFF;
while (1) {
PORTB = 0xFF;
_delay_ms(500);
PORTB = 0x55;
_delay_ms(500);
}
1110 1110
1111 1111 ~ 1110
& 0000 | 0000 1011
0001 0001 --------------
-------------- -------------- 0001
0000 1110 0100
0001 1111 25
Shift operations in C
data >> number of bits to be shifted right
data << number of bits to be shifted left
26
Setting a bit in a Byte to 1
We can use | operator to set a bit of a byte
to 1
xxxx xxxx
xxxx OR xxxx
| 0001 | 1 << 4
0000 -------------
-------------
xxx1 xxxx
PORTB |= (1<<4); //set bit 4 (5th bit) of PORTB
xxx1 xxxx
PORTB |= 0x10; //set bit 4 (5th bit) of PORTB
27
Clearing a bit in a Byte to 0
We can use | operator to set a bit of a byte
to 1
xxxx xxxx
xxxx OR xxxx
& 1110 & ~(1 <<
1111 4)
------------- -------------
PORTB &= ~(1<<4); //clear bit 4 (5th bit) of PORTB
xxx0 xxxx xxx0 xxxx
PORTB &= 0xEF; //clear bit 4 (5th bit) of PORTB
28
Inverting a bit in a Byte to 0
We can use ^ operator to toggle a bit of a
byte
xxxx xxxx
xxxx OR xxxx
^ 0001 ^ (1 << 4)
0000 -------------
-------------
xxxx’ xxxx
PORTB ^= (1<<4); //Toggle bit 4 (5th bit) of PORTB
xxxx’ xxxx
PORTB ^= 0x10; //Toggle bit 4 (5th bit) of PORTB
29
Checking a bit in a Byte
We can use & operator to see if a bit in a byte is 1
or 0
xxxx xxxx
xxxx OR xxxx
& 0001 & (1 << 4)
0000 -------------
-------------
if( (PINC & (1<<4)) != 0) 000x
//check 0000
bit 4 (5th bit)
000x 0000
if((PINC & 0x10) != 0) //check bit 4 (5th bit)
31
Examples
32
Examples
33
Examples
34
Examples
35
Examples
36
Data serialization in C
37
Data serialization in C
38
RECOMMENDED PROBLEMS
PROBLEMS HIGHLIGHTED IN RED HAVE HIGHER PRIORITY
Problems:
S7.1 (1, 2, 3) S7.2 (7, 8, 9, 10,11) S7.3 (13, 14, 15, 16,
17) S7.5 (20).
39