You are on page 1of 32

CONCEPTS OF EMBEDDED ‘C’

C
PROGRAMMING

Gaurav Verma
Assistant Professor
Department of Electronics and Communication Engineering
Jaypee Institute
i off Information
f i and d Technology
h l
Sector-62, Noida, Uttar Pradesh, India

1 Copyright to gaurav verma


What is Embedded ‘C’
Whenever the conventional ‘C’ language
g g and
its extensions are used for programming
embedded systems,
systems it is referred to as
“Embedded C” programming.
Advantages
va tages
Š It is a ‘mid-level’, with ‘high-level’ features (such as support for
functions and modules),
modules) and ‘lo low-level
le el’ features (such as good access
to hardware via pointers).
Š C is the most common Embedded language 85%, of embedded
applications are coded in C. C
Š C , when used correctly is as safe and robust as any other high level
language.
Š It directly
di tl manipulates
i l t the th hardware
h d andd memory addresses.
dd
Š It is very efficient, It is popular and well understood.
Š Good, well proven compilers are available for every embedded
processor(8-bitb to 32-bit).
b
‘C’ Versus Embedded ‘C’
y ‘C’ is a well structured, well y Embedded ‘C’ can be
defined and standardized considered
id d as a subset b off
general purpose conventional ‘C’ language.
programming language.
y A software p program
g called
y A platform
l f specific
f
‘Cross compiler’ is used for
application , known as ,
compiler is used for the the conversion of programs
conversion off programs ritten in Embedded ‘C’
written C to
written in C to the target target processor/controller
processor specific binary specific instructions(machine
files. language).
‘Compiler’
p Versus ‘Cross Compiler’
p

y Compiler
C il isi a software
ft t l that
tool th t converts
t a source code
d written
itt
in a high level language on top of a particular operating system
runningg on a specific
p target
g pprocessor architecture.

y Cross compilers are software tools used in cross platform


development applications. (In cross platform development , the
compiler running on a particular target processor/OS converts
the source code to machine code for a target processor whose
architecture and instruction set is different from the current
development environment OS).
How to program a µC?

I write my program on an IDE

I compile my program

I upload
p it to myy µ
µC
My IDE,
IDE AVR Studio

The code is written in C language and ATmega doesn’t understand


C, so it needs to be converted to a language that ATmega
understands.

H ll
Hello.c H ll h
Hello.hex
Basics of C language
if-else block

If(condition)
( )
{
… If condition is found to be true, this ppart will be executed

}
else
{
… If condition is found to be false, this part will be executed

}
Basics of C language….
• While
while(condition)
{

}
• While(1) --Infinite loop
• For(;;)
(;;) --Infinite loop
p

• For
f (i i i li i ; condition;
for(initialization di i iincrement))
{

}
Data Types

Note: The storage size may vary for data type depending on the cross compiler in
use for embedded applications.
Unsigned Char
y 8 bits range :0 to 255
y Character data type is preferred data type for many
applications, like setting a counter value and ASCII
characters
h i
instead
d off signed
i d char.
h

y It is important to specify the keyword unsigned in front of


the char else compiler will use the signed char by default.

y As
A AVR is
i 8-bit
8 bi microcontroller
i ll and
d it
i has
h a limited
li i d number
b
of registers and data RAM locations, using the int in place
of char can lead to a larger size hex file.
y Example : Write an ATmega16 C program to send hex
values for ASCII characters of 0,
0 1,
1 2,
2 3,
3 4,
4 5,
5 A,
A B,
B C,
C and D
to port PB.

y #include
#i l d <avr/io.h>
< /i h> // d d AVR header
//standard h d
int main(void )
{
unsigned char num[ ]=“012345ABCD”;
unsigned char z;
DDRB= 0xFF;
DDRB // PORTB is output
for (z = 0 ; z <10; z++) // repeat 10 times and increment z
PORTB=num[z]; //send the character to PORTB
while
hil (1);
(1) // stay here
h forever
f
return 0;
}
y Write an ATmega16 C program to toggle all the bits of Port
C continuously.
i l

#include <avr/io.h>
int main(void)
{
DDRC=0xFF; //PORTC is output
PORTC=0xAA; //PORTC is 10101010
f (;;)
for //repeat forever,
f same as while(1).
hl
{
PORTC =~ PORTC; //toggle
gg PORTC
}
return 0;
}
Signed Char
y 8-bit
8 bit data type MSB D7 to represent – or +value.
+value Range –128
128
to +127.

y Use unsigned char unless the data needs to be represented as


signed numbers.
y Write an ATmega16 C program to send temperature
range off –44 to +4 to port B.
B

#include <avr/io.h>
int main(void)
{
char mynum[]={-4,-3,-2,-1,0,+1,+2,+3,+4};
mynum[]={ 4 3 2 1 0 +1 +2 +3 +4};
unsigned char z;
DDR B= 0xFF;
ffor (z = 0; z<8 ; z++)
PORTB=mynum[z];
return 0;
}
Note: The negative values will be displayed in the 2’s complement
form as FCH
FCH, FDH
FDH,FEH,FFH,
FEH FFH 00H00H, 01H
01H, 02H
02H,
03H,04H(hex values of -4,-3,-2,-1,0,1,2,3,4)
Unsigned
g int:

y 16-bit data type range : 0 to 65535 (0000 – FFFFH).


y Used to define 16-bit
16 bit variables such as memory addresses,
addresses set counter
values of more than 256.
y Registers and memory accesses are in 8-bit chunks, the misuse of int
variables will result in a larger hex file.
file
y NOTE : C compiler uses signed int as default if unsigned keyword is
not used.

Signed int:

y Signed int is a 16-bit data type that uses the MSB D15 to represent –
or +value. We have 15 bits for the magnitude of the number from –
32768 to +32767
Other data types
y The unsigned int is limited to values 0-65,535 (0000-
FFFFH). The AVR C compiler supports long data types, if
we want values greater than 16-bits. Also, to deal with
fractional numbers,
numbers most AVR C compilers support float
and double data types.

17 Copyright to gaurav verma


y Write an ATmega16 C program to toggle all bits of
P t B 50,000
Port 50 000 ti
times.
#include<avr/io.h>
int main(void)
( )
{
unsigned int z;
DDRB = 0xFF;
for(z=0;z<50000;z++)
{
PORTB= 0x55;
PORTB= 0xAA;
}
return 0;
}

18 Copyright to gaurav verma


y Write an ATmega16 C program to toggle all bits of Port B 100,000
times.
#include<avr/io.h>
int main(void)
( )
{
unsigned long z;
DDRB = 0xFF;
for(z=0;z<100000;z++)
{
PORTB= 0x55;
PORTB= 0xAA;
}
return 0;
}

19 Copyright to gaurav verma


Time Delays
y
y There are three ways to create a time delay in AVR C.
y Using the AVR timers
y Using a simple for loop
y Using predefined C functions

y Delays
l can be b observed
b d either
h on theh oscilloscope
ll or using a simulator.
l
y In creating a time delay using for loop factors need to be considered

1. Number of machine cycles and number of clock periods per machine


cycle.
2. Crystal
y frequency
q y connected between XTAL1 and XTAL2. Duration
of the clock period for the machine cycle is the function of crystal
frequency.
3. Compiler
p selected. Accuracyy of the time delayy is mainlyy due to the
compiler used .
y Write an ATmega16 C program to toggle bits of PortB continuously with 100ms
d l Assume
delay. A XTAL 8MH
XTAL=8MHz.

#include <avr/io.h>
void delay100ms(void) // for loop delay function
{
unsigned int i;
for(i=0;i<42150;i++) //different compiler
p have different value of 100ms.
}
int main(void)
{
DDRB= 0xFF;
while (1)
{
PORTB=0xAA;
delay100ms(); //delay function
PORTB=0x55;
delay100ms();
y ();
}
return 0;
}
y Write an ATmega16 C program to toggle bits of Port B continuously
with a 10ms delay. Use a predefined delay function.

#include <avr/io.h>
#include<util/delay.h> // predefined delay function
int main(void)
{
DDRB = 0xFF;
while(1) //repeat forever
{
PORTB=0x55;
_delay_ms(10);
delay ms(10); // or we can use _delay_us(10)
delay us(10)
PORTB=0xAA;
_delay_ms(10);
}
return 0;
}
I/O Programming in C
y All port registers of the AVR are both byte accessible and bit
accessible
accessible.
y But for Bit-accessible, some AVR C compilers do not support
this feature.
y PORTB.0= 1; line of code can be used in Code Vision to set
the first pin of PORT B to one, but cannot be used in other
compilers like AVR studio.
y For that case, we use AND and OR bit-wise operators to
access a single
i l bit off given
i register.
it
y Masking of a bit hold true for both bit and byte-accessible.

23 Copyright to gaurav verma


y Write an ATmega16 C program to get a byte of data form Port C. If it
is less than 100, send it to Port B; otherwise, send it to Port D.

#include <avr/io.h>
int main(void)
{
unsigned char temp;
DDRC=0x00; //make C input port
DDRB 0 FF
DDRB=0xFF; //
//make
k B output port
DDRD=0xFF; //make D output port
while (1)
{
temp=PINC; //read from PINC
if (temp<100)
PORTB t
PORTB=temp;
else
PORTD=temp;
}
return 0;
}
Logic
g Operation
p in AVR C
y The most important feature of the C language is its ability to
perform bit manipulation.
manipulation

y The bitwise operators


p used in C are
y AND (&),
y OR ( | ),
y EX-OR
EX OR ( ^ ),
)
y inverter (~),
y SShiftt right
g t ((>>))
y Shift left (<<)
y Example
l : The
h following
f ll i program will
ill explain
l i the
h different
diff
logical operations that are run on simulator.

#include <avr/io.h>
int main(void)
{
DDRB 0 FF
DDRB=0xFF; //
//make
k portt B output
t t
DDRC=0xFF; // make port C output
DDRD= 0xFF; // make port D output
PORTB = 0x35 & 0x0F; //ANDing
PORTC= 0x04 | 0x68; //ORing
PORTD= 0x54 ^ 0x78; //XORing
PORTB= ~ 0x55; //inverting
return 0;
}
y Example : Write an ATmega16
g C program
g to read the pins 1 and 0 of Port
B and issue an ASCII character to Port D according to following table
PIN1 PIN0
0 1 Send ‘0’
0 to port D
0 1 Send ‘1’ to port D
1 0 Send ‘2’ to port D
1 1 Send ‘3’ to port D

#include <avr/io.h>
int main(void)
{
unsigned char z;
DDR 0 00
DDRB=0x00;; //make
// k Port B as input
DDRD=0xFF; //make Port D an input
( )
while(1)
{
z= PINB; //read Port B
z= z & 0b00000011; // mask the unused bits
switch (z) //make decision
{
case(0):
{
PORTD=‘0’; break; //send ASCII 0
}
case(1):
{
PORTD=‘1’;; break;; //send ASCII 1
}
case(2):
{
PORTD ‘2’ bbreak;
PORTD=‘2’; k // d ASCII 2
//send
}
case(3):
{
PORTD=‘3’; break; //send ASCII 3
}
}
}
return 0;
}
Compound assignment and Bit-wise shift operation in C

Operation Abbreviated Expression Equal C Expression

AND assignment a &= b a = a& b

OR assignment a|= b a= a|b

Operation Symbol Format of shift Operation

Shift right >> Data>>number of bits to be shifted right

Shift left << Data <<number of bits to be shifted left

29 Copyright to gaurav verma


y Write an ATmega16 C program to monitor bit 5 of PortB. If it is 1, make bit 4
of Port B input; otherwise, change pin 4 of PortB to output.

#include<avr/io.h>
int main(void)
( )
{
DDRB = DDRB & 0b11011111; // bit 5 of Port B is input
while(1)
hl 1
{
if(PINB & 0b00100000)
DDRB &= 0b11101111; // bit 4 of Port B is input
else
DDRB |= 0b00010000;
0b00010000 //bit 4 of Port B is output
}
return 0;
}
30 Copyright to gaurav verma
y Write an ATmega16 C program to monitor bit 5 of PortB. If it is 1, make bit
4 of Port B input; otherwise, change pin 4 of PortB to output.

#include<avr/io.h>
int main(void)
( )
{
DDRB = DDRB & ~(1<<5); // bit 5 of Port B is input
while(1)
hl 1
{
if(PINB & (1<<7))
DDRB = DDRB & ~(1<<4); // bit 4 of Port B is input
else
DDRB = DDRB | (1<<4);
(1<<4) //bit 4 of Port B is output
}
return 0;
}
31 Copyright to gaurav verma
TRY THE FOLLOWING CODES IN C:
1. Led’s are connected to ppins of Port B. write an AVR C pprogram
g
that shows the count from 0 to FFH(0000 0000 to 1111 1111 in
binary) on the LED’s.
2. Write an AVR C program to monitor bit 5 of Port C. If it is high,
send 55H to port B; otherwise, send AAH to Port B.
3
3. Th ddata
The t pins
i off an LCD are connected
t d to
t Port
P tB B. th
the information
i f ti
is latched into the LCD whenever its Enable pin goes from HIGH
to LOW. The enable ppin is connected to pin
p 5 of Port C(6 ( th ppin).
)
Write a C to send “JIIT-62” to this LCD.
4. Write an AVR C program to toggle all the pins of Port B
continuously using inverting operator and EX-OR operator.
5. Write an AVR C program to convert packed BCD 0x29 to ASCII
andd display
di l th the bytes
b t on PORTB andd PORTC
PORTC.
32 Copyright to gaurav verma

You might also like